************************************
************************************
***ELABORATING BIVARIATE TABLES (chapter 14)
************************************
************************************

************************************
***Clear memory
************************************
clear all

************************************
***Windows
************************************
***Start saving results window
log using "C:\course\progs\Stata14.log", replace text

***Shortcut for data folders
global data = "C:\course\data"

***Shortcut for output folders
global output = "C:\course\output"

************************************
***Macintosh
************************************
***Start saving results window
log using "/course/progs/Stata14.log", replace text

***Shortcut for data folders
global data = "/course/data"

***Shortcut for output folders
global output = "/course/output"

************************************
***Opening commands
************************************
***Tell Stata to not pause for "more" messages
set more off

***Change directory
cd "$data"

************************************
***Append different years
************************************
***Open 2016 GSS
use "GSS2016.dta", clear

***Append 2010 GSS
append using "GSS2010.dta"

***Append 2004 GSS
append using "GSS2004.dta"

***Verify year
tab year, missing
tab year, m

************************************
***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

************************************
***Complex survey design
************************************
svyset [weight=wtssall], strata(vstrat) psu(vpsu) singleunit(scaled)

************************************
***Gamma for opinion about immigration and sex
************************************
tab letin1 sex if year==2016, col nofreq gamma

*Test statistic: Z = gamma / ASE
*ASE: Asymptotic Standard Error
di 0.0321/0.035

*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)

*There is NOT a statistically significant association
di 1-normal(0.91714286)

************************************
***Gamma only for republicans
************************************
tab letin1 sex if year==2016 & democrat==0, col nofreq gamma

*Test statistic: Z = gamma / ASE
*ASE: Asymptotic Standard Error
di -0.0081/0.062

*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)

*There is NOT a statistically significant association
di normal(-0.13064516)

************************************
***Gamma only for democrats
************************************
tab letin1 sex if year==2016 & democrat==1, col nofreq gamma

*Test statistic: Z = gamma / ASE
*ASE: Asymptotic Standard Error
di 0.1430/0.053

*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)

*There is a statistically significant association
di 1-normal(2.6981132)

************************************
***CLOSING COMMANDS
************************************
***Save data
save "Stata14.dta", replace

***Save log
log close