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
- import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- import statsmodels.formula.api as smf
- filename = ('addhealth_pds.csv')
- data = pd.read_csv(filename, low_memory=False)
- pd.set_option('display.max_rows', None, 'display.max_columns', None)
- #%%
- #This section cleans up the data
- data['H1RE5']=data['H1RE5'].replace(8, np.nan)
- data['H1RE5']=data['H1RE5'].replace(7, np.nan)
- data['H1RE5']=data['H1RE5'].replace(6, np.nan)
- data['H1RE5']=data['H1RE5'].replace(9, np.nan)
- data['H1TO31']=data['H1TO31'].replace(996, np.nan) #change "refused" to NA
- data['H1TO31']=data['H1TO31'].replace(998, np.nan)
- data['H1TO31']=data['H1TO31'].replace(997, 0)
- data['H1TO31']=data['H1TO31'].replace(999, np.nan)
- clean = data[['H1RE5', 'H1TO31']].dropna()
Generating statistics
- clean['H1RE5']=clean['H1RE5']-(clean['H1RE5'].mean())
- bornagain = clean['H1RE5'].value_counts()
- mjstats =clean['H1TO31'].describe()
- print("Statistics for H1TO31 - Times smoke marijuana (Lifetime)\n")
- print(mjstats)
- bornstats=clean['H1RE5'].describe()
- print("Statistics for religious people who claim to be born again \
- \n(Adjusted Mean)")
- print(bornstats)
- print("\n\nThe mean of the explanatory variable is "\
- + str(int(clean['H1RE5'].mean())) + ".\n\n")
- print("Count of Christians who identify as 'Born Again'")
- print("0- Does not identify, 1-Identifies")
- print(bornagain)
- model = smf.ols('H1TO31 ~ H1RE5', data=clean).fit()
- print(model.summary())
No comments:
Post a Comment