************************************ ************************************ ***SOCI 420: ADVANCED METHODS OF SOCIAL RESEARCH ***MEASURES OF DISPERSION (chapter 4) ************************************ ************************************ ************************************ ***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\Stata04.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) ************************************ ***SEX ************************************ tab sex tab sex, m tab sex [aweight=wtssnrps], m svy: tab sex ***Generate dummy variable for female tab sex, nolabel generate female=. replace female=0 if sex==1 replace female=1 if sex==2 tab sex female, m tab female tab female, m svy: tab female ***Create label for variable label variable female "Sex" ***Create labels for categories label define female 0 "Male" 1 "Female" ***Assign labels for categories label values female female ************************************ ***RACE/ETHNICITY ************************************ ***Race tab race [aweight=wtssnrps] tab race [aweight=wtssnrps], m svy: tab race ***Hispanic tab hispanic, m tab hispanic, m nolabel svy: tab hispanic ***Generate dummy variable for hispanic gen hisp=. replace hisp=0 if hispanic==1 replace hisp=1 if hispanic>=2 & hispanic<=50 tab hispanic hisp, m ***Generate race/ethnicity 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 tab raceeth race, m tab raceeth hisp, m ***Create label for variable label variable raceeth "Race/Ethnicity" ***Create labels for categories label define racecode 1 "Non-hispanic white" /// 2 "Non-hispanic black" /// 3 "Hispanic" /// 4 "Other" ***Assign labels for categories of a specific variable label values raceeth racecode ***New race/ethnicity variable tab raceeth, m tab raceeth [aweight=wtssnrps], m svy: tab raceeth ************************************ ***AGE ************************************ ***Age distribution tab age, m tab age [aweight=wtssnrps], m tab age, m nolabel tab age [aweight=wtssnrps], m nolabel svy: tab age ***Generate age group variable - manually gen agegr=. replace agegr=18 if age>=18 & age<=24 replace agegr=25 if age>=25 & age<=44 replace agegr=45 if age>=45 & age<=64 replace agegr=65 if age>=65 & age<=89 tab agegr, m ***Create label for variables label variable agegr "Age group" ***Create labels for categories label define agecode 18 "18-24" /// 25 "25-44" /// 45 "45-64" /// 65 "65-89" ***Assign labels for categories of specific variables label values agegr agecode ***New age group variables tab agegr, m tab agegr [aweight=wtssnrps], m svy: tab agegr ************************************ ***INCOME BY CATEGORIES OF ONE VARIABLE ************************************ ***Income tabstat conrinc [aweight=wtssnrps], stat(min p25 p50 p75 max iqr mean sd) ***Income by sex tabstat conrinc [aweight=wtssnrps], by(female) stat(min p25 p50 p75 max iqr mean sd) ***Income by race/ethnicity tabstat conrinc [aweight=wtssnrps], by(raceeth) stat(min p25 p50 p75 max iqr mean sd) ***Income by age group tabstat conrinc [aweight=wtssnrps], by(agegr) stat(min p25 p50 p75 max iqr mean sd) ************************************ ***INCOME BY COMBINATIONS OF MORE THAN ONE VARIABLE ************************************ ***Income by sex and race/ethnicity table raceeth female [aweight=wtssnrps], stat(min conrinc) stat(p25 conrinc) /// stat(p50 conrinc) stat(p75 conrinc) /// stat(max conrinc) stat(iqr conrinc) /// stat(mean conrinc) stat(sd conrinc) ***Income by sex and age group table agegr female [aweight=wtssnrps], stat(min conrinc) stat(p25 conrinc) /// stat(p50 conrinc) stat(p75 conrinc) /// stat(max conrinc) stat(iqr conrinc) /// stat(mean conrinc) stat(sd conrinc) ************************************ ***INCOME WITH COMPLEX SAMPLE DESIGN ************************************ ***No weight mean conrinc if conrinc!=. estat sd ***Weight ***It corrects the mean mean conrinc if conrinc!=. [aweight=wtssnrps] estat sd ***Complex survey design ***It correct the mean, standard error, and standard deviation svy, subpop(if conrinc!=.): mean conrinc estat sd ***Income by sex svy, subpop(if conrinc!=.): mean conrinc, over(female) estat sd ***Income by race/ethnicity svy, subpop(if conrinc!=.): mean conrinc, over(raceeth) estat sd ***Income by age group svy, subpop(if conrinc!=.): mean conrinc, over(agegr) estat sd ***Income by sex and race/ethnicity svy, subpop(if conrinc!=.): mean conrinc, over(female raceeth) estat sd svy, subpop(if conrinc!=.): mean conrinc, over(raceeth female) estat sd ***Income by sex and age group svy, subpop(if conrinc!=.): mean conrinc, over(female agegr) estat sd svy, subpop(if conrinc!=.): mean conrinc, over(agegr female) estat sd ************************************ ***BOXPLOT ************************************ ***Income graph box conrinc [aweight=wtssnrps], ytitle(Respondents' income) graph hbox conrinc [aweight=wtssnrps], ytitle(Respondents' income) ***Income by sex graph hbox conrinc [aweight=wtssnrps], over(female) ytitle(Respondents' income) ***Income by race/ethnicity graph hbox conrinc [aweight=wtssnrps], over(raceeth) ytitle(Respondents' income) ***Income by age group graph hbox conrinc [aweight=wtssnrps], over(agegr) ytitle(Respondents' income) ***Income by sex and race/ethnicity graph hbox conrinc [aweight=wtssnrps], over(female) over(raceeth) ytitle(Respondents' income) graph hbox conrinc [aweight=wtssnrps], over(raceeth) over(female) ytitle(Respondents' income) ***Income by sex and age group graph hbox conrinc [aweight=wtssnrps], over(female) over(agegr) ytitle(Respondents' income) graph hbox conrinc [aweight=wtssnrps], over(agegr) over(female) ytitle(Respondents' income) ************************************ ***CLOSING COMMANDS ************************************ ***Save data save "$data\Stata04.dta", replace ***Save log log close