*********************************** ************************************ ***SOCI 420: ADVANCED METHODS OF SOCIAL RESEARCH ***MEASURES OF CENTRAL TENDENCY (chapter 3) ************************************ ************************************ ************************************ ***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\Stata03.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 ************************************ ***MEAN AND MEDIAN OF INCOME ***Respondent income in constant dollars ***Inflation-adjusted personal income ************************************ ***Income ***Histogram hist conrinc, freq normal hist conrinc, percent normal ***No weight sum conrinc, d mean conrinc estat sd ***Weight ***It corrects the mean sum conrinc [aweight=wtssnrps], d mean conrinc [aweight=wtssnrps] ***Survey design ***It corrects the mean and standard error svy: mean conrinc estat sd ************************************ ***INCOME BY CATEGORIES OF ONE VARIABLE ************************************ ***Income tabstat conrinc [aweight=wtssnrps], stat(mean p50) ***Income by sex tabstat conrinc [aweight=wtssnrps], by(sex) stat(mean p50) ***Income by race/ethnicity tabstat conrinc [aweight=wtssnrps], by(raceeth) stat(mean p50) ***Income by age group tabstat conrinc [aweight=wtssnrps], by(agegr) stat(mean p50) ************************************ ***INCOME BY COMBINATIONS OF MORE THAN ONE VARIABLE ************************************ ***Income by sex and race/ethnicity table raceeth sex [aweight=wtssnrps], stat(mean conrinc) stat(p50 conrinc) ***Income by sex and age group table agegr sex [aweight=wtssnrps], stat(mean conrinc) stat(p50 conrinc) ************************************ ***LINE GRAPH - MEAN INCOME BY AGE ************************************ ***Generate variable with mean income by age sort age by age: egen mincage=mean(conrinc) sum mincage, d ***Line graph of income by age twoway line mincage age [aweight=wtssnrps], ytitle("Mean income") ***Save graph graph export "$output\age-income_line.png", replace ************************************ ***LINE GRAPH - MEAN INCOME BY AGE AND SEX ************************************ ***Generate variable with mean income by age and sex sort sex age by sex age: egen mincagesex=mean(conrinc) sum mincagesex, d ***Line graph of income by age and sex twoway line mincagesex age if female==0 [aweight=wtssnrps] || /// line mincagesex age if female==1 [aweight=wtssnrps], /// legend(label(1 Males) label(2 Females)) /// ytitle("Mean income") /// xtitle("Age") ***Save graph graph export "$output\age-sex-income_line.png", replace ************************************ ***CLOSING COMMANDS ************************************ ***Save data save "$data\Stata03.dta", replace ***Save log log close