#delimit ; clear; set more off; set memory 200000; set maxvar 32000; version 9.0; capture: log close; log using "c:\data\psid\eco 721\lab5.log", text replace; /*********************************************************************************/ /* Program: lab5_cont_haz.do -- Stata do file to read 1968-87 longitudinal data */ /* from the PSID, construct spell information for the first observed */ /* non-left-censored marriage spell for each household, construct */ /* spell records and variables, and estimate continuous-time hazard */ /* models */ /* */ /* Authors: D. Ribar April 2009 */ /*********************************************************************************/ /* Read/input data set */ use "c:\data\psid\eco 721\psid_cons_68_87.dta", clear; /* Sort data by household identifier (int_id) and the observation year */ sort int_id year; /*********************************************************************************/ /* Clean and construct marriage spell records */ /*********************************************************************************/ /* Drop records starting with any breaks in the longitudinal sequence */ by int_id: gen compyear = year[1] + _n - 1; by int_id: gen yearflag = year - compyear; by int_id: replace yearflag = year - compyear - 1 if year>=1974 & year[1]<1974; drop if yearflag>0; drop compyear yearflag; /* Find year of first observed transition from being unmarried to married */ gen umtranyr = .; by int_id: replace umtranyr = year if umtranyr[_n-1]>=. & tvcurmar[_n]==1 & tvcurmar[_n-1]==0 & _n>1; by int_id: replace umtranyr = umtranyr[_n-1] if umtranyr[_n-1]<. & _n>1; by int_id: replace umtranyr = umtranyr[_N]; label var umtranyr "Year of start of marriage spell"; /* Drop observations prior to first non-censored marriage spell and observations */ /* without transitions into marriage */ drop if year < umtranyr; /* Artificially right censor sequences that extend over the 1973 gap */ drop if year>=1974 & umtranyr<=1974; /* Find year of first observed transition from being married to unmarried */ gen mutranyr = .; by int_id: replace mutranyr = year if mutranyr[_n-1]>=. & tvcurmar[_n]==0 & tvcurmar[_n-1]==1 & _n>1; by int_id: replace mutranyr = mutranyr[_n-1] if mutranyr[_n-1]<. & _n>1; by int_id: replace mutranyr = mutranyr[_N]; label var mutranyr "Year of end of marriage spell"; /* Drop observations after transition out of marriage */ drop if year > mutranyr; /* Create spell variables (duration and exit indicator) */ by int_id: gen marotdur = year[_N] - umtranyr + 1; gen mar_exit = mutranyr<.; replace marotdur = marotdur - mar_exit; label var mar_exit "Was marriage observed to end?"; label var marotdur "Observed total duration of marriage"; /* Keep just one record per spell */ by int_id: keep if _n==1; tab marotdur mar_exit; /****************************************************************************/ /* Create Stata "survival" data set */ /****************************************************************************/ stset marotdur, failure(mar_exit); /* Estimate Kaplan-Meier survivor function */ sts list; /* Estimate continuous-time model with exponential distribution */ streg tvage year tvwhite tvblack tvhdeduc, distribution(exponential); /* Estimate continuous-time model with Gompertz distribution */ streg tvage year tvwhite tvblack tvhdeduc, distribution(gompertz); /* Estimate continuous-time model with Gompertz distribution with gamma het.*/ streg tvage year tvwhite tvblack tvhdeduc, distribution(gompertz) frailty(gamma); /* Estimate Cox PH model */ stcox tvage year tvwhite tvblack tvhdeduc; log close; clear; #delimit cr