************************************ ************************************ ***SOCI 420: ADVANCED METHODS OF SOCIAL RESEARCH ***BIVARIATE ASSOCIATION FOR NOMINAL- AND ORDINAL-LEVEL VARIABLES (chapter 12) ************************************ ************************************ ************************************ ***CLEAR MEMORY ************************************ clear all ************************************ ***CREATE SHORTCUTS AND LOG FILE ************************************ ***Shortcut for folders global codes = "H:\course\codes" global data = "H:\course\data" global output = "H:\course\output" ***Start saving results window log using "$codes\Stata12.log", replace text ************************************ ***OPENING COMMANDS ************************************ ***Tell Stata to not pause for "more" messages set more off ***Open 2021 GSS use "$data\GSS2021.dta", clear ***Complex survey design svyset [weight=wtssnrps], strata(vstrat) psu(vpsu) singleunit(scaled) ************************************ ***GENERATE VARIABLES ************************************ ************************************ ***Sex ***Original variable tab sex tab sex, m tab sex, m nolabel ***Generate variable generate female=. replace female=0 if sex==1 replace female=1 if sex==2 ***Verify variable tab female, m tab sex female, m ***Labels label variable female "Sex" // Label for variable label define female 0 "Male" 1 "Female" // Labels for categories label values female female // Assign labels for categories tab female, m ************************************ ***Hispanic ***Original variable tab hispanic tab hispanic, m tab hispanic, m nolabel ***Generate variable gen hisp=. replace hisp=0 if hispanic==1 replace hisp=1 if hispanic>=2 & hispanic<=50 ***Verify variable tab hisp, m tab hispanic hisp, m ***Labels label variable hisp "Hispanic" // Label for variable label define hisp 0 "Non-Hispanic" 1 "Hispanic" // Labels for categories label values hisp hisp // Assign labels for categories tab hisp, m ************************************ ***Race/ethnicity ***Original variable tab race tab race, m tab race, m nolabel ***Generate variable gen raceeth=. replace raceeth=1 if race==1 & hisp==0 //non-hispanic white replace raceeth=2 if race==2 & hisp==0 //non-hispanic black replace raceeth=3 if hisp==1 //hispanic replace raceeth=4 if race==3 & hisp==0 //other ***Verify variable tab raceeth, m tab raceeth race, m tab raceeth hisp, m ***Labels label variable raceeth "Race/Ethnicity" // Label for variable label define racecode 1 "Non-hispanic white" 2 "Non-hispanic black" 3 "Hispanic" 4 "Other" // Labels for categories label values raceeth racecode // Assign labels for categories tab raceeth, m ************************************ ***Age group ***Original variable tab age tab age, m tab age, m nolabel ***Generate variable egen agegr = cut(age), at(18,25,45,65,90) ***Verify variable tab agegr, m table agegr, stat(min age) stat(max age) stat(count age) ***Labels label variable agegr "Age group" // Label for variable label define agecode 18 "18-24" 25 "25-44" 45 "45-64" 65 "65-89" // Labels for categories label values agegr agecode // Assign labels for categories tab agegr, m ************************************ ***Education group ***Original variable tab educ, m tab educ, m nolabel ***Generate variable gen educgr=. replace educgr=1 if educ>=0 & educ<=11 // Less than high school replace educgr=2 if educ==12 // High school replace educgr=3 if educ>=13 & educ<=15 // Some college replace educgr=4 if educ==16 // College replace educgr=5 if educ>=17 & educ<=20 // 5+ years of college, graduate school ***Verify variable tab educgr, m tab educ educgr, m ***Labels label variable educgr "Education group" // Label for variable label define educgr 1 "Less than high school" 2 "High school" 3 "Some college" 4 "College" 5 "Some graduate school" // Labels for categories label values educgr educgr // Assign labels for categories tab educgr, m ************************************ ***Religion ***Original variable tab relig tab relig, m tab relig, m nolabel ***Generate variable gen religion=. replace religion=1 if relig==1 //protestant replace religion=2 if relig==2 //catholic replace religion=3 if relig==3 //jewish replace religion=4 if relig>=5 & relig<=13 //other replace religion=5 if relig==4 //none ***Verify variable tab religion, m tab relig religion, m ***Labels label variable religion "Religion" // Label for variable label define relcode 1 "Protestant" 2 "Catholic" 3 "Jewish" 4 "Other" 5 "None" // Labels for categories label values religion relcode // Assign labels for categories tab religion, m ************************************ ***Veterans ***Original variable tab vetyears tab vetyears, m tab vetyears, m nolabel ***Generate variable gen veteran=. replace veteran=1 if vetyears>=1 & vetyears<=4 // Some years of active duty replace veteran=0 if vetyears==0 // No active duty ***Verify variable tab veteran, m tab vetyears veteran, m ***Labels label variable veteran "Veteran" // Label for variable label define veteran 0 "Non-Veteran" 1 "Veteran" // Labels for categories label values veteran veteran // Assign labels for categories tab veteran, m ************************************ ***Immigration attitude ***Original variable tab letin1, m tab letin1, m nolabel ***Generate variable ***"remain the same as it is" will be missing gen proimmig=. replace proimmig=1 if letin1==1 | letin1==2 replace proimmig=0 if letin1==4 | letin1==5 ***Verify variable tab proimmig, m tab letin1 proimmig, m ***Labels label variable proimmig "Immigration attitude" // Label for variable label define proimmig 0 "Anti-immigration" 1 "Pro-immigration" // Labels for categories label values proimmig proimmig // Assign labels for categories tab proimmig, m ************************************ ***Political party ***Original variable tab partyid, m tab partyid, m nolabel ***Generate variable ***"Independents" will be missing gen democrat=. replace democrat=1 if partyid>=0 & partyid<=2 replace democrat=0 if partyid>=4 & partyid<=6 ***Verify variable tab democrat, m tab partyid democrat, m ***Labels label variable democrat "Political party" // Label for variable label define party 1 "Democrats" 0 "Republicans" // Label for categories label values democrat party // Assign labels for categories tab democrat, m ************************************ ***ASSOCIATIONS BETWEEN NOMINAL-LEVEL VARIABLES ************************************ ************************************ ***PHI - Political party by ethnicity ************************************ ***Remember to report column percentages ***taking into account survey weights tab democrat hisp [aweight=wtssnrps], col nofreq // column percentages tab democrat hisp // sample size tab democrat hisp, m // missing cases ***Phi correlation coefficient ***Phi is designed to measure the degree ***of relation for two binary variables ***(i.e., dichotomous variables, dummy variables) ***To compute Phi, first convert the binary variables into 1's and 0's, ***and estimate the Pearson'r correlation corr democrat hisp // in this case, Pearson's r correlation same as Phi pwcorr democrat hisp // same as above pwcorr democrat hisp, sig // Phi with test of significance ************************************ ***CHI SQUARE, LAMBDA, CRAMER'S V - Political party by race/ethnicity ************************************ ***Remember to report column percentages ***taking into account survey weights tab partyid raceeth [aweight=wtssnrps], col nofreq // column percentages tab partyid raceeth // sample size tab partyid raceeth, m // missing cases ***Chi square tab partyid raceeth, chi // weights not allowed svy: tab partyid raceeth // chi square test with complex survey design (correct form) ***Cramer's V tab partyid raceeth, V // weights not allowed ***Chi square, Cramer's V tab educgr raceeth, chi V // weights not allowed ***Lambda ***If your Stata doesn't have the lambda command, ***type "ssc install lambda" to install it. ***ssc install lambda ***Note: When row totals are very unequal, ***Lambda can be zero even when there is an association between the variables. ***For very unequal row marginals, it's better to use ***a Chi Square based measure of association. lambda partyid raceeth [aweight=wtssnrps] ************************************ ***ASSOCIATIONS BETWEEN ORDINAL-LEVEL VARIABLES ************************************ ************************************ ***GAMMA - Politcal party by education group ************************************ ***Remember to report column percentages ***taking into account survey weights tab partyid educgr [aweight=wtssnrps], col nofreq // column percentages tab partyid educgr // sample size tab partyid educgr, m // missing cases ***Gamma measures the strength and pattern/direction of the association tab partyid educgr, gamma // weights not allowed ***Test statistic: Z = gamma / ASE ***ASE: Asymptotic Standard Error di -0.1259/0.015 // test statistic ***p-value ***"normal" command calculates area under the curve below the Z-score ***If Z is positive, p-value (one-tailed test): di 1-normal(Z) ***If Z is negative, p-value (one-tailed test): di normal(Z) di normal(-8.3933333) // p-value ************************************ ***SPEARMAN'S RHO - Political party by years of schooling ************************************ tab partyid, m tab educ, m ***Remember to report column percentages ***taking into account survey weights ***These commands might not work, because of too many values for education tab partyid educ [aweight=wtssnrps], col nofreq // column percentages tab partyid educ // sample size tab partyid educ, m // missing cases ***Spearman's rho (rank correlation coefficient) spearman partyid educ // weights not allowed ***Spearman's rho squared di (-0.1320) * (-0.1320) di -0.1320^2 ************************************ ***CLOSING COMMANDS ************************************ ***Save data save "$data\Stata12.dta", replace ***Save log log close