Task 4: Testing a Potential Moderator
In this post I’m generating the anova test for two variables of my dataset with a potential moderator.
I’m using the dataset nesarc to response my research question:
are dystymia symtons associated with the gender?
According with my research question, my variables are:
AGE
SEX
RACE
S1Q1C (Hispanic or latin origin)
S1Q1D1 (American indian or Alaska native)
S1Q1D2 (Asian)
S1Q1D3 (Black or African American)
S1Q1D4 (Native Hawaiian or other pacific islander)
S1Q1D5 (White)
S4CQ5 (Age at onset of first episode)
S4CQ6A (Number of episodes)
sub1 = data[['AGE', 'SEX', 'S1Q1C', 'S1Q1D1','S1Q1D2','S1Q1D3', 'S1Q1D4', 'S1Q1D5', 'S4CQ5', 'S4CQ6A']]
After checking my Code Book, I need to replace the values 69 and 99 in my variables “S4CQ5″ and “S4CQ6A” by NaN, but after to apply the filters that response my research question I realized that I didn’t have this values into my dataset.
# setting missing data
sub1["S4CQ5"] = sub1["S4CQ5"].replace(69, numpy.nan)
sub1["S4CQ5"] = sub1["S4CQ5"].replace(99, numpy.nan)
sub1["S4CQ6A"] = sub1["S4CQ6A"].replace(99, numpy.nan)
I made a new column into my dataset, this column represent the sex of the person but represented with as letters F and M:
recode1 = {1: 'M', 2:'F'}
sub1["SEX"] = sub1["SEX"].map(recode1)
After, I create a new column to unified the ethnicity of each person in a single column and eliminate missing data.
sub1["RACE"] = sub1.apply (lambda row: setRace(row),axis=1)
sub1["RACE"] = sub1["RACE"].replace(7, numpy.nan)
Then, create the sub2 with only the columns that it will be used in the analysis
sub2 = sub1[['AGE', 'SEX', 'RACE', 'S4CQ5', 'S4CQ6A']].dropna()
The Anova Test was performed without a moderator and these were the results.
I used the variable “RACE” as a moderator to analyze the relationship between “SEX” and “S4CQ6A, in order to answer my reseach question:
Lating people
# using ols function for calculating the F-statistic and associated p value sub3 = sub2[sub2['RACE'] == 1].dropna() model2 = smf.ols(formula='S4CQ6A ~ C(SEX)', data=sub3) results2 = model2.fit() print (results2.summary())
No latin people
# using ols function for calculating the F-statistic and associated p value sub4 = sub2[sub2['RACE'] != 1].dropna() model3 = smf.ols(formula='S4CQ6A ~ C(SEX)', data=sub4) results3 = model3.fit() print (results3.summary())print ('means for sex') m3 = sub4.groupby('SEX').mean() print(m3)
Summary
The execution of the Anova test show us that there isn’t a signitifcant relation between the selected variables because the p-value is high.
Using the variable RACE, the result show the same. In both cases, when RACE is equal to 1, which means latin people, and is not. The p-value high, so besides there is not exist significant relationship between my variables, RACE is not a varliable moderator for this relationship.
Code
""" Created on Sun Oct 25 15:40:25 2015
@author: Jorge Rodriguez E. <[email protected]> """ import numpy import pandas import statsmodels.formula.api as smf import statsmodels.stats.multicomp as multi import scipy.stats import matplotlib.pyplot as plt import seaborn
def frequencyTable(colName, data): count = data[colName].value_counts(sort=False) percent = data[colName].value_counts(sort=False, normalize=True) print("counts for ", colName) # age at onset of first episode print(count)
print("percent for ", colName) print(percent)
#creating 7 level race variable def setRace (row): if row['S1Q1C'] == 1 : return 1 # Hispanic or latin origin elif row['S1Q1D1'] == 1 : return 2 # AMERICAN INDIAN OR ALASKA NATIVE elif row['S1Q1D2'] == 1 : return 3 # ASIAN elif row['S1Q1D3'] == 1 : return 4 # BLACK OR AFRICAN AMERICAN elif row['S1Q1D4'] == 1 : return 5 # NATIVE HAWAIIAN OR OTHER PACIFIC ISLANDER elif row['S1Q1D5'] == 1 : return 6 # WHITE else : return 7
""" analysis of variance. Analysis of variance assesses whether the means of two or more groups are statistically different from each other. """
# Import the entire dataset to memory data = pandas.read_csv("nesarc.csv", low_memory=False) #Set PANDAS to show all columns in DataFrame pandas.set_option('display.max_columns', None) #Set PANDAS to show all rows in DataFrame pandas.set_option('display.max_rows', None) # Upper case all DataFrame column names data.columns = map(str.upper, data.columns) # bug fix for display formats to avoid run time errors pandas.set_option('display.float_format', lambda x:'%f'%x)
#setting variables I will be working with to numeric data["AGE"] = data["AGE"].convert_objects(convert_numeric=True) # AGE data["SEX"] = data["SEX"].convert_objects(convert_numeric=True) # SEX # Hispanic or latin origin data["S1Q1C"] = data["S1Q1C"].convert_objects(convert_numeric=True) # AMERICAN INDIAN OR ALASKA NATIVE data["S1Q1D1"] = data["S1Q1D1"].convert_objects(convert_numeric=True) # ASIAN data["S1Q1D2"] = data["S1Q1D2"].convert_objects(convert_numeric=True) # BLACK OR AFRICAN AMERICAN data["S1Q1D3"] = data["S1Q1D3"].convert_objects(convert_numeric=True) # NATIVE HAWAIIAN OR OTHER PACIFIC ISLANDER data["S1Q1D4"] = data["S1Q1D4"].convert_objects(convert_numeric=True) # WHITE data["S1Q1D5"] = data["S1Q1D5"].convert_objects(convert_numeric=True) # age at onset of first episode data["S4CQ5"] = data["S4CQ5" ].convert_objects(convert_numeric=True) # number of episodes data["S4CQ6A"] = data["S4CQ6A"].convert_objects(convert_numeric=True)
# Reserch question: # the association between the age when someone has dystymia symtons and the gender
# Refined question: # Are dystymia symtons and the gender associated with young adults whose # suffer the first episode in their adolescence
# subdata to young adults with age between 19 and 35 who suffer the first # episodo between 12 and 18 years
sub1 = data[['AGE', 'SEX', 'S1Q1C', 'S1Q1D1','S1Q1D2','S1Q1D3', 'S1Q1D4', 'S1Q1D5', 'S4CQ5', 'S4CQ6A']]
# setting missing data sub1["S4CQ5"] = sub1["S4CQ5"].replace(69, numpy.nan) sub1["S4CQ5"] = sub1["S4CQ5"].replace(99, numpy.nan) sub1["S4CQ6A"] = sub1["S4CQ6A"].replace(99, numpy.nan)
recode1 = {1: 'M', 2:'F'} sub1["SEX"] = sub1["SEX"].map(recode1)
sub1["RACE"] = sub1.apply (lambda row: setRace(row),axis=1) sub1["RACE"] = sub1["RACE"].replace(7, numpy.nan)
sub2 = sub1[['AGE', 'SEX', 'RACE', 'S4CQ5', 'S4CQ6A']].dropna()
# en los latinos # SEX --> S4CQ6A Sexo determina el número de episodios # using ols function for calculating the F-statistic and associated p value model1 = smf.ols(formula='S4CQ6A ~ C(SEX)', data=sub2) results1 = model1.fit() print (results1.summary())
# En los latinos # SEX --> S4CQ6A Sexo determina el número de episodios # using ols function for calculating the F-statistic and associated p value sub3 = sub2[sub2['RACE'] == 1].dropna() model2 = smf.ols(formula='S4CQ6A ~ C(SEX)', data=sub3) results2 = model2.fit() print (results2.summary())
print ('means for sex') m2 = sub3.groupby('SEX').mean() print(m2)
# En los no latinos # SEX --> S4CQ6A Sexo determina el número de episodios # using ols function for calculating the F-statistic and associated p value sub4 = sub2[sub2['RACE'] != 1].dropna() model3 = smf.ols(formula='S4CQ6A ~ C(SEX)', data=sub4) results3 = model3.fit() print (results3.summary())
print ('means for sex') m3 = sub4.groupby('SEX').mean() print(m3)







