Showing posts with label expand. Show all posts
Showing posts with label expand. Show all posts

Nov 24, 2021

Decomposing the difference between two means

// GSS 1990-2004
use year race sibs reg16 educ maeduc if inrange(year, 1990, 2004) ///
  & year != 2002 ///  // 2002 measures race in a non-standard way
    using "gss7221_r1.dta", clear

// In Table 7.8 Treiman states that he's got 17,090 cases (14,985 non-black + 
// 2,105 black). There seems to be no way I can get there with the data
// because maeduc, mother's education, only has 15,996 valid observations.

// The reason for this seems to be that in his do-file, he starts with
// the 1990 GSS file and then appends all other GSS year files -- including
// the 1990 file, thus including the 1990 file twice. Running the do-file with

// expand 2 if year == 1990

// uncommented (almost) replicates the numbers reported in the book.

drop year // Not needed

// Race variables
generate black    = (race == 2)
generate nonblack = !black
drop race

// Truncate number of siblings at 15
replace sibs = 15 if sibs > 15  & !missing(sibs)
label var sibs "Sibsize"

// South
gen south = (inrange(reg16, 5, 7))
label var south "Southern origin"
drop reg16

// Education
label var educ "Education"
label var maeduc "Mother's education"

// Listwise deletion
mark touse if !missing(educ, maeduc, sibs)
keep if touse

// Table 7.8a eststo clear local vlist educ maeduc sibs south local upper local lower `vlist' foreach v of local vlist { estpost correlate `v' `lower' if !black local nnonblack = e(N) foreach m in b rho p count { matrix `m' = e(`m') } if "`upper'"!="" { estpost correlate `v' `upper' if black local nblack = e(N) foreach m in b rho p count { matrix `m' = e(`m'), `m' } } ereturn post b foreach m in rho p count { quietly estadd matrix `m' = `m' } eststo `v' local lower: list lower - v local upper `upper' `v' } // Table 7.8b esttab using table2.tex, replace nonumbers noobs mtitles not booktabs label nostar /// title(Correlations between study variables: Blacks (\emph{N} = `nblack') above, non-blacks (\emph{N} = `nnonblack') below the diagonal) /// mtitle("Education" "Mother's education" "Sibsize" "Southern origin") eststo clear eststo: estpost sum educ maeduc sibs south if black eststo: estpost sum educ maeduc sibs south if !black esttab using table1.tex, booktabs replace /// cells("mean(label(Mean) fmt(2))" "sd(label(SD) fmt(2) par)") /// label mtitle("Blacks" "Non-Blacks") /// title("Means and standard deviations of study variables")
// Table 7.9 eststo clear eststo: regress educ maeduc sibs south if touse & black eststo: regress educ maeduc sibs south if touse & !black esttab using table3.tex, booktabs replace /// b(2) se(2) r2(2) /// label mtitle("Blacks" "Non-Blacks") /// title("Coefficients of a model of years of schooling, for blacks and non-blacks, US adults, 1990--2004") // Table 7.10 eststo clear eststo: oaxaca educ maeduc sibs south if touse, by(black) detail noisily eststo: oaxaca educ maeduc sibs south if touse, by(nonblack) detail noisily esttab using table4.tex, booktabs replace /// b(2) nose not label mtitle("Blacks as reference" "Non-Blacks as reference") /// eqlabel("\emph{Overall}" /// "\emph{Differences in assets}" /// "\emph{Differences in returns to assets}" /// "\emph{Interactions}") /// coeflabels(overall:difference "Difference in years of schooling" /// overall:endowments "Total due to difference in assets" /// overall:coefficients "Total due to difference in returns" /// overall:interaction "Total due to interactions") /// drop(overall:group*) /// varwidth(30) alignment(D{.}{.}{-1}) /// title("Decomposition of the difference in the mean years of schooling by blacks and non-blacks, US adults, 1990--2004")

Feb 19, 2018

Random graphs (126): Turning tables into figures

sdmxuse data ESTAT, dataset(lfsa_epgar) start(2010) end(2010) clear

keep if inlist(geo, "AT", "BE", "BG", "CY", "CZ", "DK", "EE", "FI") ///
      | inlist(geo, "FR", "DE", "EL", "HU", "IE", "LT", "NL", "NO") ///
      | inlist(geo, "PL", "PT", "ES", "SE", "CH", "UK") 
keep if age == "Y_GE15"

replace geo = "GR" if geo == "EL"
kountry geo, from(iso2c)
rename NAMES_STD country

drop if sex == "T"
replace sex = "Men" if sex == "M"
replace sex = "Women" if sex == "F"
encode reason, gen(reasonno)
label var reasonno "Reasons for part-time work"

label define reasonno 1 "Care activities" ///
                      2 "Other personal reasons" ///
                      3 "Own illness, disability" 4 "Education, training" ///
                      5 "Could not find full-time job" 6 "Other", modify

replace value = value * 10
expand value
drop if missing(value)

tabplot reasonno country, by(sex, ///
                          note("{it:Note:} Bars and numbers indicate percentage of part-time workers per country" ///
                               "{it:Source:} Eurostat, lfsa_egpar, data refer to 2010.")) ///
                          percent(sex country) ///
                          showval(mlabsize(tiny) format(%6.0f)) xtitle("") ///
                          xlabel(, angle(vertical) labsize(small)) name(figure2, replace)

Jul 4, 2017

Chapters 9 and 10 of Singer and Willett's (2003) book on longitudinal data analysis

// Figure 9.1
use "C:\singer willett (2003)\teachers.dta", clear label define censor 0 "Not censored" 1 "Censored" label val censor censor histogram t, by(censor, legend(off) note("")) freq xlabel(1 (1) 12) ylabel(0 (100) 500) /// addlabels discrete xtitle("Years of teaching") name(figure91, replace) // Table 10.1
generate event = !censor qui ltable t event, noadjust saving(lifetable, replace) preserve use lifetable, clear list t0 t1 start deaths lost hazard survival, sep(0) noobs erase lifetable.dta restore // Figure 10.1
stset t, failure(event) sts generate h = h twoway scatter h t, msymbol(i) connect(l) ylabel(0 (.05) .15) xlabel(0 (1) 13) /// sort xtitle("Years in teaching") /// ytitle("Estimated hazard probability") /// name(figure101a, replace) nodraw ltable t event, noadjust notab graph noconf xlabel(0 (1) 13) ylabel(0 (.5) 1) /// yline(.5) xline(7.6) /// xtitle("Years in teaching") /// ytitle("Proportion surviving") /// name(figure101b, replace) nodraw graph combine figure101a figure101b, col(1) name(figure101, replace) ysize(8) // Figure 10.2
use "C:\singer willett (2003)\relapse_days.dta", clear generate weeks = int(days / 7) + 1 generate event = !censor stset weeks, failure(event) sts generate h = h twoway (scatter h weeks, connect(l) msymbol(i)), xlabel(0 (1) 12) ylabel(, format(%6.2f)) /// ytitle("Estimated hazard probability") /// xtitle("Weeks after release") /// name(figure102a1, replace) nodraw ltable weeks event, notable graph noconf ylabel(0 (.25) 1, format(%6.2f)) /// xlabel(0(1)12) yline(0.5) /// ytitle("Estimated survival probability") /// xtitle("Weeks after release") /// name(figure102a2, replace) nodraw graph combine figure102a1 figure102a2, col(2) /// title("{bf:A} Time to cocaine relapse", pos(11)) name(figure102a, replace) nodraw use "C:\singer willett (2003)\firstsex.dta", clear generate event = !censor stset time, failure(event) sts generate h = h twoway (scatter h time, connect(l) msymbol(i) sort), xlabel(6 (1) 12) ylabel(, format(%6.2f)) /// ytitle("Estimated hazard probability") /// xtitle("Grade") /// name(figure102b1, replace) nodraw ltable time event, notable graph noconf ylabel(0 (.25) 1, format(%6.2f)) /// xlabel(6 (1) 12) yline(0.5) /// ytitle("Estimated survival probability") /// xtitle("Grade") /// name(figure102b2, replace) nodraw graph combine figure102b1 figure102b2, col(2) /// title("{bf:B} Age at first intercourse for males", pos(11)) name(figure102b, replace) nodraw use "C:\singer willett (2003)\suicide_orig.dta", clear generate event = !censor stset time, failure(event) sts generate h = h twoway (scatter h time, connect(l) msymbol(i) sort), xlabel(5 (2) 21) ylabel(, format(%6.2f)) /// ytitle("Estimated hazard probability") /// xtitle("Age") /// name(figure102c1, replace) nodraw ltable time event, notable graph noconf ylabel(0 (.25) 1, format(%6.2f)) /// xlabel(5 (2) 21) yline(0.5) /// ytitle("Estimated survival probability") /// xtitle("Age") /// name(figure102c2, replace) nodraw graph combine figure102c1 figure102c2, col(2) /// title("{bf:C} Age at first suicide ideation", pos(11)) name(figure102c, replace) nodraw use "C:\singer willett (2003)\congress_orig.dta", clear generate event = !censor stset time, failure(event) sts generate h = h twoway (scatter h time, connect(l) msymbol(i) sort), xlabel(0 (1) 8) ylabel(0 (.1) .3, format(%6.2f)) /// ytitle("Estimated hazard probability") /// xtitle("Terms in office") /// name(figure102d1, replace) nodraw ltable time event, notable graph noconf ylabel(0 (.25) 1, format(%6.2f)) /// xlabel(0 (1) 8) yline(0.5) /// ytitle("Estimated survival probability") /// xtitle("Terms in office") /// name(figure102d2, replace) nodraw graph combine figure102d1 figure102d2, col(2) /// title("{bf:D} Duration of congressional careers for females", pos(11)) name(figure102d, replace) nodraw graph combine figure102a figure102b figure102c figure102d, row(4) ysize(11) name(figure102, replace) // Table 10.2 use "C:\singer willett (2003)\teachers.dta", clear generate event = !censor qui ltable t event, noadjust saving(lifetable, replace) preserve use lifetable, clear list t0 hazard sehazard survival sesurvival, noobs sep(0) erase lifetable.dta restore // Figure 10.4 use "C:\singer willett (2003)\teachers.dta", clear list id t censor if inlist(id, 20, 126, 129), noobs sep(0) expand t bysort id: generate period = _n // Period identifier bysort id: generate event = !censor & _n == _N // Calculate outcome for // discrete-time analysis list id period event if inlist(id, 20, 126, 129), noobs sepby(id) // Table 10.3 table period, c(sum event n event mean event)

Reference

Singer, Judith D., and John B. Willett. 2003. Applied Longitudinal Data Analysis. Modeling Change and Event Occurrence. Oxford University Press. doi: 10.1093/acprof:oso/9780195152968.001.0001

Mar 18, 2013

Merging quarterly time-series data with monthly time series data

version 12
set more off
clear

// DATASET: GDP and main components - Current prices [namq_gdp_c]
// LAST UPDATE: 15.03.13 20:50:29
// EXTRACTION DATE: 18.03.13 18:04:00
// SOURCE OF DATA: Eurostat

insheet using "namq_gdp_c_1_Data.csv", comma clear names
 
// Generate country variable
kountryadd "Germany (until 1990 former territory of the FRG)" to "Germany" add
kountry geo, from(other) stuck
ren _ISO3N_ country
kountry  country, from(iso3n) to(iso2c)
ren  _ISO2C_ country_str
replace country_str = "UK" if country_str == "GB"
list country_str geo
 
// Fix GDP variable
replace value = "" if value == ":"          // Fix missing data indicator 
replace value = subinstr(value, " ", "",.)  // Remove blank (thousands separator)
destring value, replace                     // Convert to numeric

// Drop unecessary variables and clean up data set
drop s_adj unit indic_na flagandfootnotes country
order country_str geo time value
sort geo time

// Generate monthly time variable
    
   // Generate quarterly time variable first
gen current_time_point_q = quarterly(time, "YQ")
    
   // Convert to months
gen current_time_point = mofd(dofq(current_time_point_q))
format current_time_point %tm
    
   // Carry forward first monthly observation
expand 3                                      // Create 2 duplicates of each case
by geo time, s: gen counter = _n - 1          // Count duplicates,
                                              // original    = 1 - 1 = 0
                                              // duplicate 1 = 2 - 1 = 1        
                                              // duplicate 2 = 3 - 1 = 2
replace current_time_point = current_time_point + counter
drop current_time_point_q counter time
 
// Cleaning up 
ren value gdp
label var gdp "GDP per capita (quarterly)"
keep country_str current_time_point gdp
save gdp.dta, replace
 
// Open master data set and merge
use unmepl.dta, clear 
merge 1:1 country_str current_time_point using gdp.dta 
keep if _merge == 3  
 
// Create semi-ugly plot for illustration 
keep if country_str == "NL" 
label var total_unempl_nsa "Unemployment rate (monthly)" 
 
tsset  current_time_point 
twoway (tsline gdp, yaxis(1)) /// 
       (tsline total_unempl_nsa, yaxis(2)) /// 
    , legend(ring(0)) /// 
      ytitle("Euros per inhabitant", axis(1)) /// 
   ytitle("Percentage points", axis(2)) /// 
   tlabel(2004m1 (6) 2009m12, angle(45)) /// 
   ttitle("Months")