Belief in Divine Intervention vs Psychological Resilience

Published: 7 May 2024| Version 1 | DOI: 10.17632/9bj4hnjtw6.1
Contributor:
Sunil Maria Benedict

Description

import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt # Step 1: Simulate data np.random.seed(42) n_samples = 120 belief_in_divine = np.random.uniform(1, 10, n_samples) psychological_resilience = belief_in_divine + np.random.normal(0, 1, n_samples) # Adding noise # Create DataFrame experiment_df = pd.DataFrame({'Belief_in_Divine': belief_in_divine, 'Psychological_Resilience': psychological_resilience}) # Step 2: Visualize the relationship with a scatter plot plt.figure(figsize=(8, 6)) sns.scatterplot(x='Belief_in_Divine', y='Psychological_Resilience', data=experiment_df) plt.title('Belief in Divine Intervention vs. Psychological Resilience') plt.xlabel('Belief in Divine Intervention') plt.ylabel('Psychological Resilience') # Step 3: Add regression line equation to the plot plt.text(3, 15, r'$Y = MX + C$', fontsize=12) # Step 4: Generate individual plots for each regression line for i in range(5): # Create a new figure for each line plt.figure(figsize=(8, 6)) # Randomly select slope and intercept values slope = np.random.uniform(0.5, 1.5) intercept = np.random.uniform(-1, 2) # Plot regression line x_values = np.linspace(min(belief_in_divine), max(belief_in_divine), 100) y_values = slope * x_values + intercept plt.plot(x_values, y_values, label=f'Line {i+1}: Y = {slope:.2f}X + {intercept:.2f}') # Add scatter plot on top sns.scatterplot(x='Belief_in_Divine', y='Psychological_Resilience', data=experiment_df) plt.title(f'Regression Line {i+1}: Belief in Divine Intervention vs. Psychological Resilience') plt.xlabel('Belief in Divine Intervention') plt.ylabel('Psychological Resilience') plt.legend() plt.grid(True) plt.show() # Step 5: Correlation Analysis correlation_coefficient = np.corrcoef(experiment_df['Belief_in_Divine'], experiment_df['Psychological_Resilience'])[0, 1] print("Correlation Coefficient:", correlation_coefficient) # Step 6: Perform Linear Regression X = experiment_df['Belief_in_Divine'].values.reshape(-1, 1) y = experiment_df['Psychological_Resilience'].values # Calculate coefficients X = np.column_stack((np.ones_like(X), X)) # Add intercept term coefficients = np.linalg.lstsq(X, y, rcond=None)[0] slope = coefficients[1] intercept = coefficients[0] print("Slope (m):", slope) print("Intercept (c):", intercept) # Step 7: Plot regression line plt.figure(figsize=(8, 6)) plt.scatter(experiment_df['Belief_in_Divine'], experiment_df['Psychological_Resilience']) x_values = experiment_df['Belief_in_Divine'].values # Convert to numpy array plt.plot(x_values, slope * x_values + intercept, color='red', label='Regression Line') plt.title('Regression Analysis: Belief in Divine Intervention vs. Psychological Resilience') plt.xlabel('Belief in Divine Intervention') plt.ylabel('Psychological Resilience') plt.legend() plt.grid(True) plt.show()

Files

Steps to reproduce

import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt # Step 1: Simulate data np.random.seed(42) n_samples = 120 belief_in_divine = np.random.uniform(1, 10, n_samples) psychological_resilience = belief_in_divine + np.random.normal(0, 1, n_samples) # Adding noise # Create DataFrame experiment_df = pd.DataFrame({'Belief_in_Divine': belief_in_divine, 'Psychological_Resilience': psychological_resilience}) # Step 2: Visualize the relationship with a scatter plot plt.figure(figsize=(8, 6)) sns.scatterplot(x='Belief_in_Divine', y='Psychological_Resilience', data=experiment_df) plt.title('Belief in Divine Intervention vs. Psychological Resilience') plt.xlabel('Belief in Divine Intervention') plt.ylabel('Psychological Resilience') # Step 3: Add regression line equation to the plot plt.text(3, 15, r'$Y = MX + C$', fontsize=12) # Step 4: Generate individual plots for each regression line for i in range(5): # Create a new figure for each line plt.figure(figsize=(8, 6)) # Randomly select slope and intercept values slope = np.random.uniform(0.5, 1.5) intercept = np.random.uniform(-1, 2) # Plot regression line x_values = np.linspace(min(belief_in_divine), max(belief_in_divine), 100) y_values = slope * x_values + intercept plt.plot(x_values, y_values, label=f'Line {i+1}: Y = {slope:.2f}X + {intercept:.2f}') # Add scatter plot on top sns.scatterplot(x='Belief_in_Divine', y='Psychological_Resilience', data=experiment_df) plt.title(f'Regression Line {i+1}: Belief in Divine Intervention vs. Psychological Resilience') plt.xlabel('Belief in Divine Intervention') plt.ylabel('Psychological Resilience') plt.legend() plt.grid(True) plt.show() # Step 5: Correlation Analysis correlation_coefficient = np.corrcoef(experiment_df['Belief_in_Divine'], experiment_df['Psychological_Resilience'])[0, 1] print("Correlation Coefficient:", correlation_coefficient) # Step 6: Perform Linear Regression X = experiment_df['Belief_in_Divine'].values.reshape(-1, 1) y = experiment_df['Psychological_Resilience'].values # Calculate coefficients X = np.column_stack((np.ones_like(X), X)) # Add intercept term coefficients = np.linalg.lstsq(X, y, rcond=None)[0] slope = coefficients[1] intercept = coefficients[0] print("Slope (m):", slope) print("Intercept (c):", intercept) # Step 7: Plot regression line plt.figure(figsize=(8, 6)) plt.scatter(experiment_df['Belief_in_Divine'], experiment_df['Psychological_Resilience']) x_values = experiment_df['Belief_in_Divine'].values # Convert to numpy array plt.plot(x_values, slope * x_values + intercept, color='red', label='Regression Line') plt.title('Regression Analysis: Belief in Divine Intervention vs. Psychological Resilience') plt.xlabel('Belief in Divine Intervention') plt.ylabel('Psychological Resilience') plt.legend() plt.grid(True) plt.show()

Institutions

European International University

Categories

Psychology, Educational Psychology, Emotional Development, Emotional Intelligence, Resilience

Licence