Showing posts with label estimates store. Show all posts
Showing posts with label estimates store. Show all posts

Dec 3, 2017

Random graphs (121): Overlaid prediction plots

// Open cumulated Allbus 2004-2014
use if year >= 2004 using "ZA4584_v1-0-0.dta", clear

// Select data
keep if inrange(v729, 25, 65)  // Only those between 25 and 65
drop if v495 == 0 // Health split

// Poor self-rated health
generate poorhealth = inlist(v495, 4, 5) if !inlist(v495, 0, 9)

// Top-bottom
recode v152 (0 99 = .), gen(sss)
label define sss 1 `""1" "Bottom""' 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" 8 "8" 9 "9" 10 `""10" "Top""'
label val sss sss
label var sss "Subjective SES"

// Education
recode v767 (94 99 = .), gen(educ)
label define educ 1 "Basic" 2 "Lower secondary" 3 "Upper secondary" 4 "Post-secondary" 5 "Tertiary"
label val educ educ
label var educ "Education"

// ISEI 1988
generate isei = v789 if !inlist(v789, 0, 99)
replace  isei = v825 if missing(isei) & !inlist(v825, 0, 99)
label var isei "Occupational status (ISEI)"

// EGP
recode v784 (0 = .a) (10004 = .b) (10009 = .c) (1 2 = .d) // ISCO-88
recode v820 (0 = .a) (10004 = .b) (10009 = .c) (1 2 = .d) // Last job ISCO-88
clonevar isco88 = v784
replace  isco88 = v820 if missing(isco88) & !missing(v820)
recode v804 (0 = .a "Not available") (9999 = .b "No answer"), gen(supervisor) // Number of people supervised
recode v773 (0 = .a "Not available") (99 = .b "No answer") ///
            (10/24 = 1 "Self-employed") (40/65 = 0 "Employed") ///
            (30 70/74 = .c "Not employed"), gen(selfemployed)  // Self-employed
iskoegp egp, isko(isco88) supvis(supervisor) sempl(selfemployed)

// Household income
generate income = v924+1 if !inlist(v924, 99996, 99997, 99999)
generate lninc  = log(income)
label var lninc "Household income (logged, equivalized)"

// Controls
generate state = v1374
generate female = (v731 == 2)
generate age = v729 if v729 != 999

// Macro for controls
local control i.female c.age##c.age i.state##i.year

// Fit some models
eststo clear
qui eststo: logit poorhealt sss                     `control', robust cluster(year) 
qui margins, over(sss) post
est store model1
eststo: logit poorhealt sss i.educ                  `control', robust cluster(year) 
qui margins, over(sss) post
est store model2
eststo: logit poorhealt sss i.educ isei             `control', robust cluster(year) 
qui margins, over(sss) post
est store model3
eststo: logit poorhealt sss i.educ isei i.egp       `control', robust cluster(year) 
qui margins, over(sss) post
est store model4
eststo: logit poorhealt sss i.educ isei i.egp lninc `control', robust cluster(year) 
qui margins, over(sss) post
est store model5

// Plot findings
coefplot model1 model2 model3 model4 model5, vertical ///
         legend(order(2 "Age, federal state, survey year" 4 "plus Education" ///
                      6 "plus occupational status (ISEI)" ///
                      8 "plus social class (EGP)" ///
                      10 "plus household income") pos(2) ring(0) col(1) ///
                title(Model accounting for)) ///
         scheme(plotplainblind) ///
         xtitle(Subjective socioeconomic status) ///
         ytitle(Predicted probability of poor health)

Aug 27, 2015

Preacher et al. (2008): Latent Growth Curve Modeling in Stata

The website accompanying Preacher et al.'s (2008) book on latent growth curve modeling provide syntax files for Lisrel, Mplus, and Mx, but not for Stata. These are the Stata commands for replicating the first four models presented in the book.

version 13

// Chapter 2

clear
// Read in data from clsn_cov.dat
ssd init clsn1 clsn3 clsn4 clsn5 clsn6 sex
ssd set observations 851
ssd set means 37.9542 37.2785 37.0463 36.5696 36.1363 0.49
#delimit ;
ssd set cov 
 6.3944 \
 3.2716 7.5282 \
 4.1435 6.0804 10.7290 \
 3.7058 5.1597 6.5672 10.2920 \
 4.1286 5.7608 7.2365 7.6463 12.9085 \
-0.0940 -0.0390 -0.1521 -0.1104 -0.1469 0.2502
;
#delimit cr

// Table 2.2
ssd list

// Model 0: The null model
// Only mean intercept and residual variance are estimated


sem (Intercept@1 -> clsn1, ) ///  // Set loadings to 1 
    (Intercept@1 -> clsn3, ) ///  // for estimating constant
    (Intercept@1 -> clsn4, ) ///
    (Intercept@1 -> clsn5, ) ///
    (Intercept@1 -> clsn6, ) ///
    (clsn1 <- _cons@a, ) ///      // Constrain means to be
    (clsn3 <- _cons@a, ) ///      // the same
    (clsn4 <- _cons@a, ) ///
    (clsn5 <- _cons@a, ) ///
    (clsn6 <- _cons@a, ) ///
  , latent(Intercept ) ///
    cov(Intercept@0 ///  Constrain intercept variance to 0
        e.clsn1@b e.clsn3@b ///  // Constrain residual variances
        e.clsn4@b e.clsn5@b ///  // to be the same
        e.clsn6@b) ///
    nocapslatent
estimates store m0
estat gof, stats(chi2 rmsea indices residuals)
// Non-normed fit index (NNFI) is called 
// Tucker-Lewis index (TLI) in Stata


// Model 1: Random intercept model (Table 2.3)
// Only mean intercept, intercept variance, and residual variance are 
// estimated

sem (Intercept@1 -> clsn1, ) /// // Set loadings to 1 (Intercept@1 -> clsn3, ) /// // for estimating constant (Intercept@1 -> clsn4, ) /// (Intercept@1 -> clsn5, ) /// (Intercept@1 -> clsn6, ) /// (clsn1 <- _cons@a, ) /// // Constrain means to be (clsn3 <- _cons@a, ) /// // the same (clsn4 <- _cons@a, ) /// (clsn5 <- _cons@a, ) /// (clsn6 <- _cons@a, ) /// , latent(Intercept ) /// cov(e.clsn1@b e.clsn3@b /// // Constrain residual variances e.clsn4@b e.clsn5@b /// // to be the same e.clsn6@b) /// nocapslatent estimates store m1 di 5.27 / (5.27 + 4.68) // Random intercept model allows calculating an ICC estat gof, stats(chi2 rmsea indices residuals) // Poor model fit according to all tests // Likelihood ratio test: lrtest m1 m0 // Massive improvement in fit for Model 1, though // Model 2: Fixed intercept, fixed slope model (Table 2.4) // Only mean intercept, intercept variance, and residual variance are // estimated
sem (Intercept@1 -> clsn1, ) /// // Constrain paths to be 1 (Intercept@1 -> clsn3, ) /// (Intercept@1 -> clsn4, ) /// (Intercept@1 -> clsn5, ) /// (Intercept@1 -> clsn6, ) /// (clsn1 <- _cons@a, ) /// // Constrain intercepts to be the same (clsn3 <- _cons@a, ) /// (clsn4 <- _cons@a, ) /// (clsn5 <- _cons@a, ) /// (clsn6 <- _cons@a, ) /// (Slope@0 -> clsn1, ) /// // Determine temporal structure (Slope@2 -> clsn3, ) /// (Slope@3 -> clsn4, ) /// (Slope@4 -> clsn5, ) /// (Slope@5 -> clsn6, ) /// , covstruct(_lexogenous, diagonal) /// latent(Intercept Slope ) /// cov(Intercept@0 /// // Set intercept variance to 0 Slope@0 /// // Set slope variance to 0 e.clsn1@b /// e.clsn3@b /// e.clsn4@b /// e.clsn5@b /// e.clsn6@b) /// means(Slope) /// // Estimate slope nocapslatent estimates store m2 estat gof, stats(chi2 rmsea indices residuals) // Poor fit, even worse than Model 1 // Model 3: Random intercept, fixed slope (Table 2.5)
sem (Intercept@1 -> clsn1, ) /// // Constrain paths to be 1 (Intercept@1 -> clsn3, ) /// (Intercept@1 -> clsn4, ) /// (Intercept@1 -> clsn5, ) /// (Intercept@1 -> clsn6, ) /// (clsn1 <- _cons@a, ) /// // Constrain intercepts to be the same (clsn3 <- _cons@a, ) /// (clsn4 <- _cons@a, ) /// (clsn5 <- _cons@a, ) /// (clsn6 <- _cons@a, ) /// (Slope@0 -> clsn1, ) /// // Determine temporal structure (Slope@2 -> clsn3, ) /// (Slope@3 -> clsn4, ) /// (Slope@4 -> clsn5, ) /// (Slope@5 -> clsn6, ) /// , covstruct(_lexogenous, diagonal) /// latent(Intercept Slope ) /// cov(Slope@0 /// // Set slope variance to 0 e.clsn1@b /// e.clsn3@b /// e.clsn4@b /// e.clsn5@b /// e.clsn6@b) /// means(Slope) /// // Estimate slope nocapslatent estimates store m3 estat gof, stats(chi2 rmsea indices residuals) // Likelihood ratio test: lrtest m3 m2 // Improvement in fit for Model 3 // Model 4: Random intercept, random slope (Table 2.6)
sem (Intercept@1 -> clsn1, ) /// // Constrain paths to be 1 (Intercept@1 -> clsn3, ) /// (Intercept@1 -> clsn4, ) /// (Intercept@1 -> clsn5, ) /// (Intercept@1 -> clsn6, ) /// (clsn1 <- _cons@a, ) /// // Constrain intercepts to be the same (clsn3 <- _cons@a, ) /// (clsn4 <- _cons@a, ) /// (clsn5 <- _cons@a, ) /// (clsn6 <- _cons@a, ) /// (Slope@0 -> clsn1, ) /// // Determine temporal structure (Slope@2 -> clsn3, ) /// (Slope@3 -> clsn4, ) /// (Slope@4 -> clsn5, ) /// (Slope@5 -> clsn6, ) /// , covstruct(_lexogenous, diagonal) /// latent(Intercept Slope ) /// cov(Intercept*Slope /// // Include intercept-slope covariance e.clsn1@b /// e.clsn3@b /// e.clsn4@b /// e.clsn5@b /// e.clsn6@b) /// means(Slope) /// // Estimate slope nocapslatent estimates store m4 estat gof, stats(chi2 rmsea indices residuals) // Likelihood ratio test: lrtest m4 m3 // Improvement in fit for Model 4 // Model 4 estimated as a multilevel model (Table 4.1): // Read in data from clsn_cov.dat, this time using -corr2data- #delimit ; matrix input C = ( 6.3944, 3.2716, 7.5282, 4.1435, 6.0804, 10.7290, 3.7058, 5.1597, 6.5672, 10.2920, 4.1286, 5.7608, 7.2365, 7.6463, 12.9085, -0.0940, -0.0390, -0.1521, -0.1104, -0.1469, 0.2502 ) ; #delimit cr corr2data clsn1 clsn3 clsn4 clsn5 clsn6 sex, n(851) /// means(37.9542 37.2785 37.0463 36.5696 36.1363 0.49) /// cov(C) cstorage(lower) clear correlate, cov // Seems to work gen id = _n // Create individual identifier reshape long clsn, i(id) j(grade) // Convert to long format mixed clsn grade || id: grade, var cov(uns)

Reference

Preacher, Kristopher J., Aaron L. Wichman, Robert C. MacCallum, and Nancy E. Briggs. 2008. Latent Growth Curve Modeling. Sage. doi: 10.4135/9781412984737