Tuesday, December 20, 2016

Data Regression Tools - Week 2

Running Linear Regression with mean of 0

Summary

The results of the linear regression model show that identifying as a born-again Christian is significantly P<0.00014) and negatively associated (Beta =-4.9709) with the number of times marijuana is smoked in the lifetime of the adolescent respondents.

Prep work

The data I had originally chosen was not suitable for this activity so I chose the categorical explanatory variable "Are you a born-again Christian" (H1RE5) and the quantitative response variable "What is the total number of times you have smoked marijuana in your life?" (H1TO31).

Respondents who identified as a religion other than Christian or those who had not identified themselves as religious were removed from the cleaned dataset. Additionally, respondents who had reported never smoking marijuana (response 997) were changed to 0 and refused and don't know responses (996 and 998) were changed to NA.

The P value (.00014) was very low indicating a significant probability that a relationship exists. The R-square value (.004), however, was also very low which means the explanatory variable is not a good overall predictor of the response variable. The original mean was subtracted from the values in the explanatory variable column to set the mean of the explanatory variable to 0.

The output

Statistics for H1TO31 - Times smoke marijuana (Lifetime)

count    3603.000000
mean        6.764918
std        39.185240
min         0.000000
25%         0.000000
50%         0.000000
75%         0.000000
max       900.000000
Name: H1TO31, dtype: float64
Statistics for religious people who claim to be born again 
(Adjusted Mean)
count    3.603000e+03
mean    -2.434294e-17
std      4.998146e-01
min     -4.840411e-01
25%     -4.840411e-01
50%     -4.840411e-01
75%      5.159589e-01
max      5.159589e-01
Name: H1RE5, dtype: float64


The mean of the explanatory variable is 0.


Count of Christians who identify as 'Born Again'
0- Does not identify, 1-Identifies
-0.484041    1859
 0.515959    1744
Name: H1RE5, dtype: int64
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                 H1TO31   R-squared:                       0.004
Model:                            OLS   Adj. R-squared:                  0.004
Method:                 Least Squares   F-statistic:                     14.54
Date:                Tue, 20 Dec 2016   Prob (F-statistic):           0.000140
Time:                        08:29:54   Log-Likelihood:                -18322.
No. Observations:                3603   AIC:                         3.665e+04
Df Residuals:                    3601   BIC:                         3.666e+04
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
========================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept      6.7649      0.652     10.382      0.000         5.487     8.042
H1RE5         -4.9709      1.304     -3.813      0.000        -7.527    -2.415
========================================================================
Omnibus:                     6202.059   Durbin-Watson:                   2.016
Prob(Omnibus):                  0.000   Jarque-Bera (JB):          5264759.670
Skew:                          11.928   Prob(JB):                         0.00
Kurtosis:                     188.742   Cond. No.                         2.00
=======================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

The code

Prepping the data

  1. import pandas as pd  
  2. import numpy as np  
  3. import matplotlib.pyplot as plt  
  4. import statsmodels.formula.api as smf  
  5.   
  6. filename = ('addhealth_pds.csv')  
  7. data = pd.read_csv(filename, low_memory=False)  
  8. pd.set_option('display.max_rows', None, 'display.max_columns', None)  
  9. #%%  
  10. #This section cleans up the data  
  11.   
  12. data['H1RE5']=data['H1RE5'].replace(8, np.nan)  
  13. data['H1RE5']=data['H1RE5'].replace(7, np.nan)  
  14. data['H1RE5']=data['H1RE5'].replace(6, np.nan)   
  15. data['H1RE5']=data['H1RE5'].replace(9, np.nan)  
  16. data['H1TO31']=data['H1TO31'].replace(996, np.nan) #change "refused" to NA  
  17. data['H1TO31']=data['H1TO31'].replace(998, np.nan)  
  18. data['H1TO31']=data['H1TO31'].replace(997, 0)  
  19. data['H1TO31']=data['H1TO31'].replace(999, np.nan)  
  20.   
  21. clean = data[['H1RE5', 'H1TO31']].dropna()  

Generating statistics

  1. clean['H1RE5']=clean['H1RE5']-(clean['H1RE5'].mean())  
  2. bornagain = clean['H1RE5'].value_counts()  
  3. mjstats =clean['H1TO31'].describe()  
  4. print("Statistics for H1TO31 - Times smoke marijuana (Lifetime)\n")  
  5. print(mjstats)  
  6.   
  7. bornstats=clean['H1RE5'].describe()  
  8. print("Statistics for religious people who claim to be born again \  
  9. \n(Adjusted Mean)")  
  10. print(bornstats)  
  11. print("\n\nThe mean of the explanatory variable is "\  
  12.       + str(int(clean['H1RE5'].mean())) + ".\n\n")  
  13.   
  14.   
  15. print("Count of Christians who identify as 'Born Again'")  
  16. print("0- Does not identify, 1-Identifies")  
  17. print(bornagain)  
  18.   
  19. model = smf.ols('H1TO31 ~ H1RE5', data=clean).fit()  
  20. print(model.summary())  

No comments:

Post a Comment