Loading Packages

if (!require("pacman")) install.packages("pacman")
## Loading required package: pacman
pacman::p_load(tidyverse, hdm, stargazer, broom, knitr)

Linear Model

data("GrowthData")
y <- GrowthData$Outcome
d <- GrowthData$gdpsh465
x <- as.matrix(GrowthData %>% select(-c("Outcome", "gdpsh465", "intercept")))

varnames <- colnames(GrowthData)
xnames <-  varnames[-c(1, 2, 3)] # names of X variables
dandxnames <- varnames[-c(1, 2)] # names of D and X variables
fmla <- as.formula(paste("Outcome ~ ", paste(dandxnames, collapse = "+")))


ols <- lm(fmla, GrowthData)
partial_out <- rlassoEffect(x, y, d, method = "partialling out")
double_selection <- rlassoEffect(x, y, d, method = "double selection")


bind_rows(
  tidy(ols) %>% mutate(method = "full reg via ols") %>% filter(term == "gdpsh465") %>%
    select(method, estimate, std.error),
  tibble(
    method = "via post-lasso",
    estimate = summary(partial_out)[[1]][1,1],
    std.error = summary(partial_out)[[1]][1,2]
  ),
  tibble(
    method = "partial reg via double selection",
    estimate = summary(double_selection)[[1]][1,1],
    std.error = summary(double_selection)[[1]][1,2]
  )
) %>%
  kable(digits = 3, format = "html")
method estimate std.error
full reg via ols -0.009 0.030
via post-lasso -0.050 0.014
partial reg via double selection -0.050 0.016

IV Model

data("AJR")
y <- AJR$GDP
d <- AJR$Exprop
z <- AJR$logMort
x <- model.matrix(~-1 + (Latitude + Latitude2 + Africa + Asia + Namer + Samer)^2, data = AJR)

lasso_IV <- rlassoIV(x, d, y, z, select.Z = FALSE, select.X = TRUE)
summary(lasso_IV)
## [1] "Estimation and significance testing of the effect of target variables in the IV regression model"
##    coeff.    se. t-value p-value   
## d1 0.8450 0.2699   3.131 0.00174 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
confint(lasso_IV)
##        2.5 %   97.5 %
## d1 0.3159812 1.374072
rY <- lm(GDP ~ (Latitude + Latitude2 + Africa + Asia + Namer + Samer)^2, data = AJR)$res
rD <- lm(Exprop ~ (Latitude + Latitude2 + Africa + Asia + Namer + Samer)^2, data = AJR)$res
rZ <- lm(logMort ~ (Latitude + Latitude2 + Africa + Asia + Namer + Samer)^2, data = AJR)$res

summary(tsls(rY ~ rD | rZ, intercept = FALSE))
## [1] "Estimates and Significance Testing from from tsls"
##    Estimate Std. Error t value p value
## rD    1.267      1.731   0.732   0.464
rY <- rlasso(GDP ~ (Latitude + Latitude2 + Africa + Asia + Namer + Samer)^2, data = AJR)$res
rD <- rlasso(Exprop ~ (Latitude + Latitude2 + Africa + Asia + Namer + Samer)^2, data = AJR)$res
rZ <- rlasso(logMort ~ (Latitude + Latitude2 + Africa + Asia + Namer + Samer)^2, data = AJR)$res

summary(tsls(rY ~ rD | rZ, intercept = FALSE))
## [1] "Estimates and Significance Testing from from tsls"
##    Estimate Std. Error t value p value   
## rD   0.8450     0.2699   3.131 0.00174 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1