* replication code for
* krls: A Stata Package for Kernel-Based Regularized Least Squares
ssc install krls, all replace
use growthdata.dta, clear
/* ********** */
/* basic fit */
* ols and krls regression of growth on schooling
reg growth yearsschool , r
predict Yhat_OLS
krls growth yearsschool
predict Yhat_KRLS , fit
* krls explains more of the variation
* regression misspecified and misses non-linearity in schooling
* fit 3rd order polynomial to confirm
reg growth c.yearsschool##c.yearsschool##c.yearsschool , r
test c.yearsschool#c.yearsschool#c.yearsschool c.yearsschool c.yearsschool#c.yearsschool
predict Yhat_OLS2
* Figure 1: Fitted Values from KRLS and OLS Models
twoway (scatter growth yearsschool, sort) ///
(line Yhat_KRLS yearsschool, sort) ///
(line Yhat_OLS yearsschool, sort) ///
(line Yhat_OLS2 yearsschool , sort lpattern(dash)), ///
ytitle("GDP growth rate (%)") ///
legend(order(2 "KRLS fitted values" 3 "OLS fitted values" ///
4 "OLS polynomial fitted values"))
graph display Graph, xsize(6) ysize(4)
*graph export fig1.pdf , replace
/* ******************* */
/* partial derivatives */
* regression of growth on schooling and assasinations
reg growth yearsschool assassinations , r
* find a marginal effect of .23 for schooling
* run CR plot and find that regression is mispecified because
* the conditional relationship between growth and schooling is nonlinear
* Figure 2: Conditional Relationship between Growth and Schooling (Controlling for Assassinations).
cprplot yearsschool , lowess
graph display Graph, xsize(6) ysize(4)
*graph export fig2.pdf , replace
krls growth yearsschool assassinations, deriv(d)
* krls model fits better
* find average effect of schooling is .35
* but this is only a summary
* in fact, a hist suggests effects
* are positive mostly, but highly heterogeneous
sum d_yearsschool,detail
* Figure 3: Distribution of Pointwise Marginal Effect of Schooling on Growth.
hist d_yearsschool
graph display Graph, xsize(6) ysize(4)
*graph export fig3.pdf , replace
* plot deriv against schooling
* marginal effects track the nonlinearity
* effect is big at low levels and zero at high levels
* Figure 4: Pointwise Marginal Effect of Schooling and Level of Schooling.
lowess d_yearsschool yearsschool
graph display Graph, xsize(6) ysize(4)
* graph export fig4.pdf , replace
drop d_*
/* *************** */
/* full model fits */
reg growth rgdp60 tradeshare yearsschool assassinations , r
predict ols_simple_fitted
krls growth rgdp60 tradeshare yearsschool assassinations , deriv(d)
predict krls_fitted
* many differences:
* krls fits better
* effect of tradeshare is big in reg and zero in krls
* effect of assassinations is almosyt sig in krls but zero in ols
* first we fix the schooling
* more flexible ols
reg growth rgdp60 tradeshare c.yearsschool##c.yearsschool##c.yearsschool ///
assassinations , r
* now we look at tradeshare.
* the effect is heterogenenous
* negative at low levels of tradeshare and high at high levels of tradeshare
* effect of trade is related to assassinations
reg d_tradeshare rgdp60 tradeshare yearsschool assassinations
* Figure 5: Pointwise Marginal Effect of Trade Share and Level of Trade Share.
lowess d_tradeshare tradeshare
graph display Graph, xsize(6) ysize(4)
*graph export fig5.pdf , replace
* Figure 6: Pointwise Marginal Effect of Trade Share and Number of Assassinations.
lowess d_tradeshare assassinations
graph display Graph, xsize(6) ysize(4)
*graph export fig6.pdf , replace
* augmented ols fits almost as well as krls.
reg growth rgdp60 c.tradeshare##c.tradeshare##c.tradeshare##c.assassinations ///
c.yearsschool##c.yearsschool##c.yearsschool , r
predict ols_complex_fitted
corr krls_fitted ols_complex_fitted ols_simple_fitted
/* ************************ */
/* out of sample prediction */
*Get LOO predictions for OLS
gen yhat_ols_loo=.
gen yhat_all=.
nois _dots 0, title(Loop running)
qui {
forv i = 1/`=_N' {
drop yhat_all
reg growth rgdp60 tradeshare yearsschool assassinations if _n != `i'
predict yhat_all
scalar yhatmissing = yhat_all[`i']
replace yhat_ols_loo = yhatmissing in `i'
nois _dots `i' 0
}
}
*Get LOO predictions for KRLS
gen yhat_krls_loo=.
replace yhat_all=.
nois _dots 0, title(Loop running)
qui {
forv i = 1/`=_N' {
drop yhat_all
krls growth rgdp60 tradeshare yearsschool assassinations if _n != `i'
predict yhat_all, fit
scalar yhatmissing = yhat_all[`i']
replace yhat_krls_loo = yhatmissing in `i'
nois _dots `i' 0
}
}
*Get LOO predictions for augmented OLS
gen yhat_ols2_loo=.
replace yhat_all=.
nois _dots 0, title(Loop running)
qui {
forv i = 1/`=_N' {
drop yhat_all
reg growth rgdp60 c.tradeshare##c.tradeshare##c.tradeshare##c.assassinations ///
c.yearsschool##c.yearsschool##c.yearsschool if _n != `i'
predict yhat_all
scalar yhatmissing = yhat_all[`i']
replace yhat_ols2_loo = yhatmissing in `i'
nois _dots `i' 0
}
}
gen OLS_resid=growth-yhat_ols_loo
gen KRLS_resid=growth-yhat_krls_loo
gen krlswins = KRLS_resid <= OLS_resid
sum krlswins
egen OLS2_MSE = mean((growth-yhat_ols2_loo)^2)
egen OLS_MSE = mean(OLS_resid^2)
egen KRLS_MSE = mean(KRLS_resid^2)
sum OLS_MSE KRLS_MSE OLS2_MSE
/* ************** */
/* binary example */
gen yearsschool3 = (yearsschool>3)
krls growth rgdp60 tradeshare yearsschool3 assassinations