Showing posts with label regress. Show all posts
Showing posts with label regress. Show all posts

Jun 10, 2018

Direct and indirect effects in OLS regression

sysuse auto, clear

eststo clear

// Bivariate relationship:
eststo: regress price mpg
// Accounting for confounding variable:
eststo: regress price mpg displacement

// Accounting for confounding variable in three steps:
  // 1
quietly regress mpg displacement
predict u, residuals
  // 2
quietly regress price displacement
predict v, residuals
  // 3
eststo: regress v u

// Table:
esttab, rename(u mpg) se nomtitle label varwidth(30)

Dec 27, 2017

Random graphs (122): Specification checks of OLS regression

// Simulate data set
clear
set obs 1000
set seed 1
generate x = runiform()
generate z = runiform()
generate u = rnormal()
generate y1 =      0 +      x   + 0 * z + u
generate y2 =      0 + 10 * x^2 + 0 * z + u
generate y3 = exp(-1 +      x   + 0 * z + u)

// Correctly specified model
regress y1 x z
rvfplot,   name(rvf1, replace)  title(Fitted values vs. residuals) nodraw
rvpplot x, name(rvpx1, replace) title(Predictor variable x vs. residuals) nodraw
rvpplot z, name(rvpz1, replace) title(Predictor variable z vs. residuals) nodraw
graph combine rvf1 rvpx1 rvpz1, col(3) xsize(7) ysize(2) /// 
                                title(Residual plots of correctly specified model) ///
                                name(correct, replace) nodraw

// Missing out on quadratic relationship
regress y2 x z
rvfplot,   name(rvf2, replace)  title(Fitted values vs. residuals) nodraw
rvpplot x, name(rvpx2, replace) title(Predictor variable x vs. residuals) nodraw
rvpplot z, name(rvpz2, replace) title(Predictor variable z vs. residuals) nodraw
graph combine rvf2 rvpx2 rvpz2, col(3) xsize(7) ysize(2) ///
                                title(Residual plots of model missing quadratic term) ///
                                name(quadractic, replace) nodraw

// Missing out on exponential relationship
regress y3 x z
rvfplot,   name(rvf3, replace)  title(Fitted values vs. residuals) nodraw
rvpplot x, name(rvpx3, replace) title(Predictor variable x vs. residuals) nodraw
rvpplot z, name(rvpz3, replace) title(Predictor variable z vs. residuals) nodraw
graph combine rvf3 rvpx3 rvpz3, col(3) xsize(7) ysize(2) ///
                                title(Residual plots of model missing exponential shape) ///
                                name(exponential, replace) nodraw

graph combine correct quadractic exponential, col(1) xsize(7) ysize(6) altshrink