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

May 10, 2016

Chapter 6 of Allison's (2009) book on Fixed-Effects Regression Models using Stata

use http://statisticalhorizons.com/wp-content/uploads/nlsy.dta, clear
 
// Random effects model
sem (Falpha -> anti90@1 anti92@1 anti94@1) /// 
    (anti90 <- pov90@a self90@b black@c hispanic@d childage@e married@f gender@g momage@h momwork@i) /// 
    (anti92 <- pov92@a self92@b black@c hispanic@d childage@e married@f gender@g momage@h momwork@i) ///  
    (anti94 <- pov94@a self94@b black@c hispanic@d childage@e married@f gender@g momage@h momwork@i) /// 
   , cov(Falpha*pov90@0   Falpha*pov92@0    Falpha*pov94@0    /// 
         Falpha*self90@0  Falpha*self92@0   Falpha*self94@0   /// 
         Falpha*black@0   Falpha*hispanic@0 Falpha*childage@0 /// 
         Falpha*married@0 Falpha*gender@0   Falpha*momage@0   /// 
         Falpha*momwork@0)                                    /// 
     var(e.anti90@j       e.anti92@j        e.anti94@j)
estimates store random
  
// Fixed effects model
sem (Falpha -> anti90@1 anti92@1 anti94@1) /// 
    (anti90 <- pov90@a self90@b black@c hispanic@d childage@e married@f gender@g momage@h momwork@i) /// 
    (anti92 <- pov92@a self92@b black@c hispanic@d childage@e married@f gender@g momage@h momwork@i) ///  
    (anti94 <- pov94@a self94@b black@c hispanic@d childage@e married@f gender@g momage@h momwork@i) /// 
   , cov(Falpha*black@0   Falpha*hispanic@0 Falpha*childage@0 /// 
         Falpha*married@0 Falpha*gender@0   Falpha*momage@0   /// 
         Falpha*momwork@0)                                    /// 
     var(e.anti90@j       e.anti92@j        e.anti94@j)
estimates store fixed
  
// Compromise model
sem (Falpha -> anti90@1 anti92@1 anti94@1) /// 
    (anti90 <- pov90@a self90@b black@c hispanic@d childage@e married@f gender@g momage@h momwork@i) /// 
    (anti92 <- pov92@a self92@b black@c hispanic@d childage@e married@f gender@g momage@h momwork@i) ///  
    (anti94 <- pov94@a self94@b black@c hispanic@d childage@e married@f gender@g momage@h momwork@i) /// 
   , cov(Falpha*self90@0  Falpha*self92@0   Falpha*self94@0   /// 
         Falpha*black@0   Falpha*hispanic@0 Falpha*childage@0 /// 
         Falpha*married@0 Falpha*gender@0   Falpha*momage@0   /// 
         Falpha*momwork@0)                                    /// 
     var(e.anti90@j       e.anti92@j        e.anti94@j)
estimates store compromise

// Table 6.1
estimates table random fixed compromise, ///
          b(%9.3f) se(%9.3f) ///
          keep(anti90:self90   anti90:pov90    anti90:black ///
               anti90:hispanic anti90:childage anti90:married ///
               anti90:gender   anti90:momage   anti90:momwork)

// Table 6.2
// Shows covariances instead of correlations
estimates table fixed, b t keep(cov(pov90,Falpha):_cons cov(pov92,Falpha):_cons cov(pov94,Falpha):_cons ///
                            cov(self90,Falpha):_cons cov(self92,Falpha):_cons cov(self94,Falpha):_cons)

// Table 6.3
use http://statisticalhorizons.com/wp-content/uploads/occ.dta, clear  
// Thanks to http://www.stata.com/statalist/archive/2013-10/msg00019.html

sem (Falpha -> pf2@1 pf3@1 pf4@1) ///
    (pf4 <- pf3@a mdwgf3@b) /// 
    (pf3 <- pf2@a mdwgf2@b) /// 
    (pf2 <- pf1@a mdwgf1@b ERR1@1), ///
    cov(e.pf2@0) cov(ERR1*_oexogenous@0 ERR1*Falpha@0 ERR1*mdwgf3) method(mlmv)
estimates store medianwage

sem (Falpha -> mdwgf2@1 mdwgf3@1 mdwgf4@1) /// 
    (mdwgf4 <- pf3@a mdwgf3@b) /// 
    (mdwgf3 <- pf2@a mdwgf2@b) /// 
    (mdwgf2 <- pf1@a mdwgf1@b ERR1@1), ///
    cov(e.mdwgf2@0) cov(ERR1*_oexogenous@0 ERR1*Falpha@0 ERR1*pf3) method(mlmv)
estimates store proportionfemale

esttab medianwage proportionfemale, ///
       b(%9.3f) se(%9.3f) ///
       keep(main:pf1 main:mdwgf1)

Reference

Allison, Paul D. 2009. Fixed Effects Regression Models. Sage. doi: 10.4135/9781412993869

Feb 4, 2016

Interactions with and without main effects


set scheme s1mono
clear

set obs 10000
set seed 1

gen a = rnormal()
gen b = rnormal()
gen e = 2*rnormal()

gen y = 2 + 3*a + 4*b + 5*a*b + e

summarize y a b

estimates clear
regress y c.a##c.b
estimates store M1
regress y c.a  c.b
estimates store M2
regress y      c.b c.a#c.b
estimates store M3
regress y c.a      c.a#c.b
estimates store M4
regress y          c.a#c.b
estimates store M5

estimates restore M1
margins, at(a=(-1 0 1) b=(-1 0 1))
marginsplot, name(M1, replace) noci ///
             title("M1: Main terms and interaction") legend(col(1))

estimates restore M2
margins, at(a=(-1 0 1) b=(-1 0 1))
marginsplot, name(M2, replace) noci ///
             title("M2: Main terms, no interaction") legend(off)

estimates restore M3
margins, at(a=(-1 0 1) b=(-1 0 1))
marginsplot, name(M3, replace) noci ///
             title("M3: Interaction, one main term missing") legend(off)

estimates restore M4
margins, at(a=(-1 0 1) b=(-1 0 1))
marginsplot, name(M4, replace) noci ///
             title("M4: Interaction, other main term missing") legend(off)

estimates restore M5
margins, at(a=(-1 0 1) b=(-1 0 1))
marginsplot, name(M5, replace) noci ///
             title("M5: Interaction term, both main terms missing") legend(off)

grc1leg M1 M2 M3 M4 M5, col(2) ysize(11) xsize(8) ring(0) pos(5) ycommon
estimates table M1 M2 M3 M4 M5, b(%9.2f) stat(F r2_a)

Jul 9, 2013

Morgan and Winship's (2007) example for bias due to conditioning on a collider in Stata

Morgan and Winship (2007: p. 66) illustrate Pearl's (2009) concern about conditioning on a collider variable using a simple example.

The general problem of conditioning on a collider is as follows. Consider three variables A, B, and C, with both A and B being causes of C: A → C ← B. (Formally, any variable C that has two arrows pointing to it along a given path is a collider.) Unlike a confounder (an uncontrolled common cause of A and B), a collider does not induce a zero-order correlation between A and B. However, when handled inappropriately, a collider can induce a conditional correlation between A and B.

Morgan and Winship's example shows just that: A college admits applicants based on their SAT scores and ratings of their motivation based on an interview. Those in the top 15 per cent of the sum of SAT and motivation ratings are being admitted. SAT scores and motivation ratings are largely uncorrelated.

// Generate and label two variables
drawnorm sat motivation, ///
         n(250) ///
         means(.007, -.053) ///
         sds(1.01, 1.02) ///
         corr(1, .035, 1) cstorage(lower) ///
         clear seed(1)
label var sat        "SAT"
label var motivation "Motivation"

// Only the 15 per cent at the top are admitted
gen admission_sc = sat + motivation
_pctile admission_sc, percentiles(85)

gen admission = (admission_sc > r(r1))
label var admission "Admission status"
label define admission 1 "Admitted applicant" ///
                       0 "Rejected applicant"
label val admission admission
drop admission_sc

// Plot as in Morgan and Winship, p. 67:
twoway (scatter motivation sat if admission == 1) ///
       (scatter motivation sat if admission == 0) ///
       , ///
       legend(label(1 "Admitted applicants") ///
              label(2 "Rejected applicants") ///
              pos(5) ring(0)) ///
       ylabel(none) xlabel(none) ///
       name(collider1, replace)
// Enhanced plot with fitted lines and correlations
quietly cor motivation sat
local r_overall = round(r(rho), .01)
quietly cor motivation sat if admission == 1
local r_admitted = round(r(rho), .01)
quietly cor motivation sat if admission == 0
local r_rejected = round(r(rho), .01)
    
twoway (scatter motivation sat if admission == 1) ///
       (scatter motivation sat if admission == 0) ///
       (lfit motivation sat) ///
       (lfit motivation sat if admission == 1) ///
       (lfit motivation sat if admission == 0) ///
       , ///
       legend(label(1 "Admitted applicants") ///
              label(2 "Rejected applicants") ///
              label(3 "Overall fit, {it:r} = `r_overall'") ///
              label(4 "Fit for admitted, {it:r} = `r_admitted'") ///
              label(5 "Fit for rejected, {it:r} = `r_rejected'") ///
              pos(5) ring(0)) ///
       ylabel(none) xlabel(none) ytitle("Motivation")

What the Figures show is that the very small correlation between motivation and SAT score for the overall group turns out to be much larger when conditioning for admission status.

This also shows in an OLS regression:

regress motivation sat, beta
estimates store m1
regress motivation sat admission, beta
estimates store m2

estimates table m1 m2, b(%7.2f) se(%7.2f) stats(N) label

----------------------------------------------
                Variable |   m1        m2     
-------------------------+--------------------
                     SAT |    0.10     -0.21  
                         |    0.06      0.06  
        Admission status |              1.60  
                         |              0.17  
                Constant |   -0.12     -0.36  
                         |    0.06      0.06  
-------------------------+--------------------
                       N |     250       250  
----------------------------------------------
                                  legend: b/se

Cole et al. (2010) present additional illustrations for this problem.

References


Cole, Stephen R., Robert W. Platt, Enrique F. Schisterman, Haitao Chu, Daniel Westreich, David Richardson, and Charles Poole. 2010. "Illustrating Bias Due to Conditioning on a Collider." International Journal of Epidemiology 39(2):417-420. doi: 10.1093/ije/dyp334

Morgan, Stephen L., and Christopher Winship. 2007. Counterfactuals and Causal Inference. Methods and Principles for Social Research. Cambridge University Press.

Pearl, Judea. 2009. Causality. Models, Reasoning, and Inference, 2nd ed. Cambridge University Press.