************************************ ************************************ ***BIVARIATE ASSOCIATION FOR NOMINAL- AND ORDINAL-LEVEL VARIABLES (chapter 12) ************************************ ************************************ ************************************ ***Opening commands ************************************ ***Clear memory clear all ***Start saving results window log using "C:\course\progs\Stata12.log", replace text // Windows log using "/course/progs/Stata12.log", replace text // Macintosh ***Open 2016 GSS use "C:\course\data\GSS2016.dta", clear // Windows use "/course/data/GSS2016.dta", clear // Macintosh ************************************ ***Generate variables ************************************ ***Generate dummy variable for Democrats vs. Republicans ***"Independents" will be missing tab partyid, m tab partyid, m nolabel gen democrat=. replace democrat=1 if partyid>=0 & partyid<=2 replace democrat=0 if partyid>=4 & partyid<=6 label variable democrat "Political party" label define party 1 "Democrats" 0 "Republicans" label values democrat party tab partyid democrat, m tab democrat, m ***Generate dummy variable for hispanic tab hispanic, m tab hispanic, m nolabel gen hisp=. replace hisp=0 if hispanic==1 replace hisp=1 if hispanic>=2 & hispanic<=50 tab hispanic hisp, m tab hisp, m ***Generate race/ethnicity variable tab race, m tab race, m nolabel 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 label variable raceeth "Race/Ethnicity" label define race 1 "White" 2 "Black" 3 "Hispanic" 4 "Other" label values raceeth race tab raceeth race, m tab raceeth hisp, m tab raceeth, m ************************************ ***Complex survey design ************************************ svyset [weight=wtssall], strata(vstrat) psu(vpsu) singleunit(scaled) ************************************ ***Political party and ethnicity ************************************ ***Remember to report column percentages ***taking into account GSS complex survey design svy: tab democrat hisp, col ***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 ************************************ ***Respondent's income and race/ethnicity ************************************ ***Remember to report column percentages ***taking into account GSS complex survey design svy: tab rincome raceeth, col ***Chi square tab rincome raceeth, chi ***Cramer's V tab rincome raceeth, V ***Lambda *If your Stata doesn't have the lambda command, *type "ssc install lambda" to install it. *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 rincome raceeth ***Gamma *Measure the strength and pattern/direction of the association tab rincome raceeth, gamma *Test statistic: Z = gamma / ASE *ASE: Asymptotic Standard Error di -0.2287/0.035 // test statistic *p-value *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(-6.5342857) // p-value ***Chi square, Cramer's V, Gamma tab rincome raceeth, chi V gamma ***Spearman's rho (rank correlation coefficient) spearman rincome raceeth ************************************ ***Opinion about immigration and sex ************************************ ***Remember to report column percentages ***taking into account GSS complex survey design svy: tab letin1 sex, col ***Lambda lambda letin1 sex ***Chi square, Cramer's V, Gamma tab letin1 sex, chi V gamma ***Test statistic for Gamma: Z = gamma / ASE di 0.0321/0.035 // test statistic di 1-normal(0.91714286) // p-value ***Spearman's rho (rank correlation coefficient) spearman letin1 sex ************************************ ***CLOSING COMMANDS ************************************ ***Save data save "Stata12.dta", replace ***Save log log close