clear
set obs 10000
set seed 1
// Simulate data
generate x = rnormal(0,1) // Exogenous variable
generate w = rnormal(0,1) // Instrumental variable
generate u = rnormal(0,1) // Omitted variable
generate e1 = rnormal(0,1) // Outcome eqation error
generate e2 = rnormal(0,1) // Endogenous regressor equation error
generate y2 = x + .2 * w + .5 * u + e2 // Endogenous regressor equation
generate y1 = .5 * y2 + .5 * x + .5 * u + e1 // Outcome equation
// Fit models
eststo clear
eststo: regress y1 y2 x u
eststo: regress y1 y2 x
eststo: ivregress 2sls y1 x (y2 = w)
coefplot est1 est2 est3, xscale(alt) xtitle(Coefficient) ///
xline(.5) ///
coeflabel(_cons = "Intercept" ///
u = "Omitted variable" ///
x = "Exogenous predictor" ///
y2 = "Endogenous predictor") ///
legend(order(2 "Without omitted variable bias" ///
4 "With omitted variable bias" ///
6 "2SLS estimate") pos(11) ring(0) col(1))
Showing posts with label plotting regression coefficients. Show all posts
Showing posts with label plotting regression coefficients. Show all posts
Jan 8, 2018
Omitted variable bias
Feb 17, 2016
Random graphs (59): Plotting regression coefficients
reg health i.income i.race i.race i.female age
coefplot, drop(_cons) baselevels xline(0) xlabel(0 (.25) 1.5, format(%6.2f)) ///
headings(0.income = {bf:Income} 0.race ={bf:Race} 0.female = {bf:Sex}) ///
coeflabels(age = "{bf:Age}") xscale(alt) xtitle("Regression coefficient")
Feb 11, 2016
Random graphs (57): Odds ratio plots
clear
// ART mothers
input str20 var or lb ub
agele30 . . .
age3039 4.25 3.872 4.668
agege40 14.23 12.746 15.893
foreign . . .
italian 1.69 1.534 1.865
unmarried . . .
married 2.41 2.239 2.597
marriedother 1.78 1.515 2.083
notemployed . . .
employed 1.22 1.132 1.318
loed . . .
mied 1.15 1.069 1.242
hied 1.17 1.077 1.266
prevlivebirth . . .
noprevlivebirth 7.63 7.055 8.248
nomiscarriage . . .
miscarriage 1.14 1.06 1.217
end
encode var, gen(var1)
recode var1 ( 3 = 1 "{bf:Age} ({it:Ref.} <= 30 y)") ///
( 1 = 2 "30{c 0150}39 y") ///
( 2 = 3 "40+ y") ///
( 5 = 4 "{bf:Citizenship} ({it:Ref.} foreign)") ///
( 7 = 5 "Italian") ///
(17 = 6 "{bf:Marital status} ({it:Ref.} unmarried)") ///
( 9 = 7 "Married") ///
(10 = 8 "Other marital status") ///
(15 = 9 "{bf:Occ. status} ({it:Ref.} not employed)") ///
( 4 = 10 "Employed") ///
( 8 = 11 "{bf:Education} ({it:Ref.} low)") ///
(11 = 12 "Medium") ///
( 6 = 13 "High") ///
(16 = 14 "{bf:Previous live birth} ({it:Ref.} yes)") ///
(14 = 15 "No") ///
(13 = 16 "{bf:Previous miscarriage} ({it:Ref.} no)") ///
(12 = 17 "Yes") ///
, gen(var2)
eclplot or lb ub var2, horiz ///
ylabel(1(1)17, val) ///
xline(1) xlabel(1 (1) 10 15) xtitle("Odds ratio") xscale(log) ///
ytitle("Predictors") name(art_users, replace) //ysize(6) xsize(5.5)
clear
input str20 var or lb ub
noart . . .
others 0.853 0.566 1.285
icsi 1.071 0.906 1.267
ivf 1.3 1.109 1.524
iui 0.977 0.723 1.318
ph 1.256 0.857 1.84
nomiscarriage . . .
miscarriage 1.591 1.542 1.641
previouslivebirth . . .
nopreviouslivebirth 1.031 1.003 1.061
loed . . .
mied 1.257 1.219 1.297
hied 1.997 1.916 2.081
notemployed . . .
employed 1.066 1.032 1.102
unmarried . . .
married 1.218 1.182 1.255
foreign . . .
italian 0.949 0.917 0.983
agege40 . . .
age3039 2.888 2.777 3.003
agele30 4.183 3.993 4.383
end
encode var, gen(var1)
recode var1 ( 2 = 1 "{bf:Age} ({it:Ref.} 40+ y)") ///
( 1 = 2 "30{c 0150}39 y") ///
( 3 = 3 "<= 30 y") ///
( 5 = 4 "{bf:Citizenship} ({it:Ref.} foreign)") ///
( 8 = 5 "Italian") ///
(22 = 6 "{bf:Marital status} ({it:Ref.} unmarried)") ///
(12 = 7 "Married") ///
(18 = 8 "{bf:Occ. status} ({it:Ref.} not employed)") ///
( 4 = 9 "Employed") ///
(11 = 10 "{bf:Education} ({it:Ref.} low)") ///
(13 = 11 "Medium") ///
( 6 = 12 "High") ///
(21 = 13 "{bf:Previous live birth} ({it:Ref.} yes)") ///
(17 = 14 "No") ///
(16 = 15 "{bf:Previous miscarriage} ({it:Ref.} no)") ///
(14 = 16 "Yes") ///
(15 = 17 "{bf:Conception method} ({it:Ref.} natural)") ///
(20 = 18 "Pharmacological") ///
( 9 = 19 "IUI") ///
(10 = 20 "IVF") ///
( 7 = 21 "ICSI") ///
(19 = 22 "Others") ///
, gen(var2)
eclplot or lb ub var2, horiz ///
ylabel(1(1)22, val) ///
xline(1) xlabel(0.6 (1) 5) xtitle("Odds ratio") xscale(log) ///
ytitle("Predictors") name(livebirth, replace)
Sep 30, 2015
Random graphs (53): Visualizing multilevel models
use cntry happy mnactic agea gndr using ESS1e06_4.dta, clear
// Prepare variables
recode agea (999 = .), gen(age)
recode gndr (1 = 0 "Male") (2 = 1 "Female") (9 = .), gen(female)
recode mnactic (77 88 99 = .), gen(active)
label var female "Sex (ref. Male)"
label var age "Age in decades"
label var happy "Happiness"
label var active "Labor market status"
label val active mnactic
center happy age // Center outcome to make intercept smaller for plot
replace c_age = c_age / 10
// Listwise deletion
drop if missing(female, c_age, c_happy, active, cntry)
drop happy gndr agea mnactic // Unecessary variables can go
// Fit model
mixed c_happy c_age i.female i.active || cntry: c_age, ml cov(uns)
// Save random part
local var_age = round(exp(_b[lns1_1_1:_cons])^2, .0001)
local var_int = round(exp(_b[lns1_1_2:_cons])^2, .0001)
local covaria = round(tanh(_b[atr1_1_1_2:_cons]) * ///
exp(_b[lns1_1_1:_cons]) * ///
exp(_b[lns1_1_2:_cons]), .0001)
// Plot fixed part
coefplot, xline(0) ///
xtitle(" " "Estimates and 95% CI's") ///
scheme(s1mono) ///
ciopts(recast(rcap)) ///
coeflabels(_cons = "{bf:Intercept}" ///
c_age = "{bf:Age} in decades" ///
8.active = "Housework") /// // Shorten label
headings(c_age = " " ///
1.female = "{bf:Sex} ({it:ref.} Male)" ///
2.active = `""{bf:Labor market status}" "({it:ref.} Paid work)""' ///
_cons = " ") ///
mlabel format(%9.2f) mlabposition(12) mlabgap(*1.5) ///
xscale(alt) msymbol(x) ///
ysize(8) xsize(4) ///
title("Fixed part", span) ///
name(fixed_part, replace) nodraw
/* The default sizes of the available area are -ysize(4)- and -xsize(5.5)-,
by the way. Letter size is -ysize(11)- and -xsize(8.5)-*/
// Plot random part
// Estimate residuals
capture drop u1* u0*
predict u1 u0, reffects
predict u1se u0se, reses
// Plot intercept variation
preserve
egen pickone = tag(cntry)
keep if pickone
egen order_ = rank(-u0), unique
labmask order_, value(cntry)
gen high = u0 + (1.96 * u0se)
gen low = u0 - (1.96 * u0se)
twoway (rcap u0 u0 order_, dsymbol(x)) ///
(rspike high low order_) , ///
yline(0) xlabel(1/22, val ang(v)) ///
title("Intercept variance = `var_int'") ///
xtitle("") ///
ytitle("Random intercept residuals") ///
legend(off) ///
name(rand_int, replace) nodraw
restore
// Plot slope variation
preserve
egen pickone = tag(cntry)
keep if pickone
egen order_ = rank(-u1), unique
labmask order_, value(cntry)
gen high = u1 + (1.96 * u1se)
gen low = u1 - (1.96 * u1se)
twoway (rcap u1 u1 order_, dsymbol(x)) ///
(rspike high low order_) , ///
yline(0) xlabel(1/22, val ang(v)) ///
xtitle("") ///
title("Slope variance = `var_age'") ///
ytitle("Random slope residuals") ///
ylabel(-1 (.5) 1) /// // Make y-axis identical to other plot,
legend(off) /// // otherwise x-axis looks different, too
name(rand_slope, replace) nodraw
restore
// Plot intercept-slope covariance
preserve
gen predRandomSlope= (_b[_cons] + u0) + ((_b[c_age] + u1) * c_age)
qui sum c_age
local hi1 = 1*r(sd)
local hi2 = 2*r(sd)
local hi3 = 3*r(sd)
local lo1 = -1*r(sd)
local lo2 = -2*r(sd)
sort cntry c_age
twoway (line predRandomSlope c_age, connect(ascending)), ///
ytitle("Predicted Happiness") ///
xtitle("") ///
title("Intercept{c 150}slope covariance = `covaria'") ///
xlabel(`lo1' "-1 SD" 0 "Average age" `hi1' "+1 SD" `hi2' "+2 SD" `hi3' "+3 SD") ///
ylabel(,format(%6.1f)) ///
name(covariance, replace) nodraw
restore
// Combine Figures
graph combine rand_int rand_slope covariance, col(1) ysize(8) title("Random part") name(random_part, replace) nodraw
graph combine fixed_part random_part, col(2) ysize(8) xsize(8) altshrink title("Random coefficient model")
Mar 18, 2013
Random graphs (8): Coefficient plot by hand
While the result certainly looks cool, such a graph can be extremely tedious to make.
// Requires parmest, ingap, and eclplot
use eulfsahm2010, clear
// Create and label dummy variables for regression model
fre age
quietly tab age, gen(age)
label var age1 "15-19 years"
label var age2 "20-24 years"
label var age3 "25-29 years"
label var age4 "30-34 years"
label var age5 "35-39 years"
label var age6 "40-44 years"
label var age7 "45-49 years"
label var age8 "50-54 years"
label var age9 "55-59 years"
label var age10 "60-64 years"
fre marstat
quietly tab marstat, gen(marstat)
label var marstat1 "Widowed, divorced, or separated"
label var marstat2 "Single"
label var marstat3 "Married"
fre education
quietly tab education, gen(education)
label var education1 "Low education"
label var education2 "Medium education"
label var education3 "Tertiary education"
fre parttime
quietly tab parttime, gen(parttime)
label var parttime1 "Marginal part-time (< 20 hrs.)"
label var parttime2 "Substantial part-time (20-34 hrs.)"
label var parttime3 "Full-time (35-80 hrs.)"
fre occgroups
quietly tab occgroups, gen(occgroups)
label var occgroups1 "Unskilled job"
label var occgroups2 "Skilled manual job"
label var occgroups3 "Skilled routine services job"
label var occgroups4 "High-skilled services"
fre industry
quietly tab industry , gen(industry)
label var industry1 "Agriculture"
label var industry2 "Industry and construction"
label var industry3 "Market services"
label var industry4 "Non-market services"
// Re-label some more variables here.
// Variable labels HAVE to be identical to those specified below.
// Formatting is possible here.
label var female "{bf:Female sex}"
label var smallfirm "{bf:Small firm} (< 10 employees)"
label var fixedterm "{bf:Fixed-term contract}"
label var jobtenure "{bf:Job tenure} (in years)"
label var supervisor "{bf:Supervisor status}"
// Estimate regression model
ologit posstend female age1-age3 age5-age10 marstat1 marstat3 ///
education1 education3 parttime1 parttime2 smallfirm fixedterm ///
jobtenure supervisor occgroups1 occgroups3 occgroups4 ///
industry1 industry3 industry4, cluster(cntry)
// Save regression parameters in data set
parmest, norestore eform label
// Add gaps to add some structure to the regression plots
ingap 2 11 13 15 21 24, rowlabel(label) ///
growlabel("Age ({it:Ref.} 30-34 years)" ///
"Marital status ({it:Ref.} Single)" ///
"Education ({it:Ref.} Medium)" ///
"Working hours ({it:Ref.} Full-time (35-80 hrs)" ///
"Occup. group ({it:Ref.} Skilled manual job)" ///
"Industry ({it:Ref.} Industry and construction)")
// Define label by hand to be able to determine order of predictors on axis
// Labels must be exactly the same as above
//
// Formatting axis label text seems
// to be working here as well
//
// An easier solution might be the one here:
// http://www.survey-design.com.au/Stata%20Graphs.html
// ("Odds ratio graph - selecting order of bars")
#delimit ;
label define label1 1 "{bf:Female sex}"
2 "{bf:Age} ({it:Ref.} 30-34 years)"
3 "15-19 years"
4 "20-24 years"
5 "25-29 years"
6 "35-39 years"
7 "40-44 years"
8 "45-49 years"
9 "50-54 years"
10 "55-59 years"
11 "60-64 years"
12 "{bf:Marital status} ({it:Ref.} Single)"
13 "Widowed, divorced, or separated"
14 "Married"
15 "{bf:Education} ({it:Ref.} Medium)"
16 "Low education"
17 "Tertiary education"
18 "{bf:Working hours} ({it:Ref.} Full-time (35-80 hrs)"
19 "Marginal part-time (< 20 hrs.)"
20 "Substantial part-time (20-34 hrs.)"
21 "{bf:Small firm} (< 10 employees)"
22 "{bf:Fixed-term contract}"
23 "{bf:Job tenure} (in years)"
24 "{bf:Supervisor status}"
25 "{bf:Occupational group} ({it:Ref.} Skilled manual job)"
26 "Unskilled job"
27 "Skilled routine services job"
28 "High-skilled services"
29 "{bf:Industry} ({it:Ref.} Industry and construction)"
30 "Agriculture"
31 "Market services"
32 "Non-market services"
33 "Constant"
34 "Constant" ;
#delimit cr
encode label, gen(label1) label(label1)
// Specifying a label in the -encode- command makes sure that -encode-
// does not sort labels in alphabetical order
eclplot estimate min95 max95 label1 ///
if label1 <= 32 /// Don't plot the cut values
, horizontal ylabel(1(1)32, labsize(vsmall)) /// Reduce font size on axis
xline(1) /// Add vertical line
xtitle("Odds ratio") ///
ytitle("Predictors") ///
title("Possibility to vary start and/or end of working day", span) ///
caption("Source: EU-LFS AHM 2010, authors' calculations." /*
*/ "Ordered logit regression, /*
*/ 95% CI's adjusted for clustering in countries", span)
Oct 5, 2012
Random graphs (4): Plotting intercept and slope variation
use V3 V51 ISCO88 using "C:\Users\User\work\data sets\issp - work orientations\work orientations iii (2005)\ZA4350_F1.dta", clear // Generate variables gen jobsatisf = 7 - V51 iskoisei isei, isko(ISCO88) drop V51 ISCO88 preserve // Calculating intercept and slope variation using OLS statsby inter = _b[_cons] /// slope = _b[isei] /// , by(V3) /// saving(ols, replace): /// regress jobsatisf isei merge m:1 V3 using ols drop _merge // Visualizing intercept and slope variation gen yhat_ols = inter + slope*isei separate jobsatisf, by(V3) separate yhat_ols, by(V3) twoway (line yhat_ols1-yhat_ols43 isei, sort(V3 isei)) /// (lfit jobsatisf isei, clwidth(vvthick) clcolor(black)) /// , legend(off) ytitle("Job satisfaction") xtitle("ISEI") /// xlabel(16 25 50 75 90) ylabel(,format(%6.1f)) /// caption("{it:Source:} ISSP 2005 (Work Orientations III), own calculations", span) /// name(one, replace) restore // Calculating intercept and slope variation using a random effects model center isei, inplace mixed jobsatisf isei || V3: isei, var predict u1 u0, reffects // Visualizing intercept and slope variation gen predRandomSlope = (_b[_cons] + u0) + ((_b[isei] + u1) * isei) twoway (line predRandomSlope isei, connect(ascending) sort(V3 isei)), /// ytitle("Job satisfaction") /// xtitle("ISEI (centered)") /// xlabel(-25 0 25 50) /// ylabel(,format(%6.1f)) /// caption("{it:Source:} ISSP 2005 (Work Orientations III), own calculations", span) /// name(two, replace)
Subscribe to:
Posts (Atom)







