************************************ ************************************ ***MEASURES OF DISPERSION (chapter 4) ************************************ ************************************ ************************************ ***OPENING COMMANDS ************************************ ***Clear memory clear all ***Start saving results window log using "C:\course\progs\Stata04.log", replace text // Windows log using "/course/progs/Stata04.log", replace text // Macintosh ***Open data use "C:\course\data\GSS2016.dta", clear // Windows use "/course/data/GSS2016.dta", clear // Macintosh ***Complex survey design svyset [weight=wtssall], strata(vstrat) psu(vpsu) singleunit(scaled) ************************************ ***GENERATE VARIABLES ************************************ ************************************ ***AGE ************************************ ***Age distribution tab age, m tab age [aweight=wtssall], m tab age, m nolabel tab age [aweight=wtssall], m nolabel *Easy way to know minimum and maximum age values sum age, d ***Generate age group variable - manually gen agegr1=. replace agegr1=18 if age>=18 & age<=24 replace agegr1=25 if age>=25 & age<=44 replace agegr1=45 if age>=45 & age<=64 replace agegr1=65 if age>=65 & age<=89 tab agegr1, m table agegr1, contents(min age max age count age) ***Create label for variables label variable agegr1 "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 agegr1 agecode ***New age group variables tab agegr1, m tab agegr1 [aweight=wtssall], m ************************************ ***RACE/ETHNICITY ************************************ ***Race tab race [aweight=wtssall] tab race [aweight=wtssall], m ***Hispanic tab hispanic, m tab hispanic, m nolabel ***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=wtssall], m ************************************ ***TABLES AND GRAPHS ************************************ ***Income ***No weight sum conrinc, d mean conrinc estat sd ***Weight ***It corrects the mean sum conrinc [aweight=wtssall], d mean conrinc [aweight=wtssall] ***Survey design ***It corrects the mean and standard error svy: mean conrinc estat sd ***Income by sex table sex [aweight=wtssall], c(min conrinc p25 conrinc p50 conrinc p75 conrinc max conrinc) table sex [aweight=wtssall], c(iqr conrinc sd conrinc mean conrinc) graph box conrinc [aweight=wtssall], over(sex) ytitle(Individual income in constant dollars) ***Income by age group table agegr1 [aweight=wtssall], c(min conrinc p25 conrinc p50 conrinc p75 conrinc max conrinc) table agegr1 [aweight=wtssall], c(iqr conrinc sd conrinc mean conrinc) graph box conrinc [aweight=wtssall], over(agegr1) ytitle(Individual income in constant dollars) ***Income by race/ethnicity table raceeth [aweight=wtssall], c(min conrinc p25 conrinc p50 conrinc p75 conrinc max conrinc) table raceeth [aweight=wtssall], c(iqr conrinc sd conrinc mean conrinc) graph box conrinc [aweight=wtssall], over(raceeth) ytitle(Individual income in constant dollars) ***Income by sex and age group graph box conrinc [aweight=wtssall], over(sex) over(agegr1) ytitle(Individual income in constant dollars) graph box conrinc [aweight=wtssall], over(agegr1) over(sex) ytitle(Individual income in constant dollars) ***Income by sex and race/ethnicity graph hbox conrinc [aweight=wtssall], over(sex) over(raceeth) ytitle(Individual income in constant dollars) graph hbox conrinc [aweight=wtssall], over(raceeth) over(sex) ytitle(Individual income in constant dollars) ***Income by age group and race/ethnicity graph hbox conrinc [aweight=wtssall], over(agegr1) over(raceeth) ytitle(Individual income in constant dollars) graph hbox conrinc [aweight=wtssall], over(raceeth) over(agegr1) ytitle(Individual income in constant dollars) ***Income by sex, age group, and race/ethnicity graph hbox conrinc [aweight=wtssall], over(sex) over(agegr1) over(raceeth) ytitle(Individual income in constant dollars) ************************************ ***CLOSING COMMANDS ************************************ ***Save data save "Stata04.dta", replace ***Save log log close