diff --git a/R/iv.R b/R/iv.R index 7635d77..6bfdc11 100644 --- a/R/iv.R +++ b/R/iv.R @@ -83,7 +83,8 @@ ivreg_ss <- function(formula, X, data, W, subset, weights, method, beta0=0, #' process. If not \code{NULL}, weighted least squares is used with weights #' \code{w}, i.e., \code{sum(w * residuals^2)} is minimized. #' @export -ivreg_ss.fit <- function(y1, y2, X, W, Z, w=NULL, method=c("akm", "akm0"), +ivreg_ss.fit <- function(y1, y2, X, W, Z, w=NULL, #nolint + method=c("akm", "akm0"), beta0=0, alpha=0.05, region_cvar=NULL, sector_cvar=NULL) { diff --git a/R/ols.R b/R/ols.R index aedbe9b..210c29b 100644 --- a/R/ols.R +++ b/R/ols.R @@ -72,7 +72,8 @@ reg_ss <- function(formula, X, data, W, subset, weights, method, beta0=0, #' process. If not \code{NULL}, weighted least squares is used with weights #' \code{w}, i.e., \code{sum(w * residuals^2)} is minimized. #' @export -reg_ss.fit <- function(y, X, W, Z, w=NULL, method=c("akm", "akm0"), beta0=0, +reg_ss.fit <- function(y, X, W, Z, w=NULL, # nolint + method=c("akm", "akm0"), beta0=0, alpha=0.05, region_cvar=NULL, sector_cvar=NULL) { mm <- cbind(X, Z) @@ -86,7 +87,6 @@ reg_ss.fit <- function(y, X, W, Z, w=NULL, method=c("akm", "akm0"), beta0=0, hX <- stats::lm.fit(y=ddX, x=W)$coefficients # \hat{\Xs} wgt <- 1 r <- stats::lm.fit(mm, y) - } else { ddX <- stats::lm.wfit(y=X, x=Z, w=w)$residuals ddY <- stats::lm.wfit(y=y, x=Z, w=w)$residuals @@ -121,7 +121,8 @@ reg_ss.fit <- function(y, X, W, Z, w=NULL, method=c("akm", "akm0"), beta0=0, } else if ("region_cluster" %in% method) { nc <- length(unique(region_cvar)) # # of clusters se.s <- sqrt((nc / (nc-1)) * (n-1) / (n-p) * - drop(crossprod(tapply(u, factor(region_cvar), sum)))) / RX + drop(crossprod(tapply(u, factor(region_cvar), + sum)))) / RX } if ("akm" %in% method || "akm0" %in% method) { diff --git a/doc/ShiftShareSE.R b/doc/ShiftShareSE.R index 0a97d30..7ad5117 100644 --- a/doc/ShiftShareSE.R +++ b/doc/ShiftShareSE.R @@ -1,4 +1,4 @@ -## ---- include=FALSE, cache=FALSE---------------------------------------------- +## ----include=FALSE, cache=FALSE----------------------------------------------- library("knitr") knitr::opts_knit$set(self.contained = FALSE) knitr::opts_chunk$set(tidy = TRUE, collapse=TRUE, comment = "#>", @@ -6,19 +6,21 @@ knitr::opts_chunk$set(tidy = TRUE, collapse=TRUE, comment = "#>", ## ----------------------------------------------------------------------------- library("ShiftShareSE") -ctrls <- paste("t2 + l_shind_manuf_cbp + l_sh_popedu_c +", "l_sh_popfborn + l_sh_empl_f + l_sh_routine33 + l_task_outsource", "+ division") +ctrls <- paste("t2 + l_shind_manuf_cbp + l_sh_popedu_c +", + "l_sh_popfborn + l_sh_empl_f + l_sh_routine33", + " + l_task_outsource + division") sic <- floor(ADH$sic/10) ## ----------------------------------------------------------------------------- reg_ss(as.formula(paste("shock ~ ", ctrls)), W=ADH$W, X=IV, - data=ADH$reg, weights=weights, region_cvar=statefip, - sector_cvar=sic, method="all") + data=ADH$reg, weights=weights, region_cvar=statefip, + sector_cvar=sic, method="all") ## ----------------------------------------------------------------------------- reg_ss(as.formula(paste("d_sh_empl ~", ctrls)), W=ADH$W, X=IV, - data=ADH$reg, region_cvar=statefip, weights=weights, - sector_cvar=sic, method="all") + data=ADH$reg, region_cvar=statefip, weights=weights, + sector_cvar=sic, method="all") ivreg_ss(as.formula(paste("d_sh_empl ~", ctrls, "| shock")), W=ADH$W, - X=IV, data=ADH$reg, region_cvar=statefip, - weights=weights, sector_cvar=sic, method="all") + X=IV, data=ADH$reg, region_cvar=statefip, + weights=weights, sector_cvar=sic, method="all") diff --git a/doc/ShiftShareSE.Rmd b/doc/ShiftShareSE.Rmd index 024eddc..aece698 100644 --- a/doc/ShiftShareSE.Rmd +++ b/doc/ShiftShareSE.Rmd @@ -54,7 +54,9 @@ We now replicate column (1) of Table V in @akm18. First we load the package, define the vector of controls, and define a vector of 3-digit SIC codes: ```{r} library("ShiftShareSE") -ctrls <- paste("t2 + l_shind_manuf_cbp + l_sh_popedu_c +", "l_sh_popfborn + l_sh_empl_f + l_sh_routine33 + l_task_outsource", "+ division") +ctrls <- paste("t2 + l_shind_manuf_cbp + l_sh_popedu_c +", + "l_sh_popfborn + l_sh_empl_f + l_sh_routine33", + " + l_task_outsource + division") sic <- floor(ADH$sic/10) ``` @@ -66,8 +68,8 @@ options. The first-stage regression: ```{r} reg_ss(as.formula(paste("shock ~ ", ctrls)), W=ADH$W, X=IV, - data=ADH$reg, weights=weights, region_cvar=statefip, - sector_cvar=sic, method="all") + data=ADH$reg, weights=weights, region_cvar=statefip, + sector_cvar=sic, method="all") ``` Note that for `"AKM0"`, `"Std. Error"` corresponds to the normalized standard @@ -78,11 +80,11 @@ $2z_{1-\alpha/2}$. The reduced-form and IV regressions: ```{r} reg_ss(as.formula(paste("d_sh_empl ~", ctrls)), W=ADH$W, X=IV, - data=ADH$reg, region_cvar=statefip, weights=weights, - sector_cvar=sic, method="all") + data=ADH$reg, region_cvar=statefip, weights=weights, + sector_cvar=sic, method="all") ivreg_ss(as.formula(paste("d_sh_empl ~", ctrls, "| shock")), W=ADH$W, - X=IV, data=ADH$reg, region_cvar=statefip, - weights=weights, sector_cvar=sic, method="all") + X=IV, data=ADH$reg, region_cvar=statefip, + weights=weights, sector_cvar=sic, method="all") ``` # Collinear share matrix diff --git a/doc/ShiftShareSE.pdf b/doc/ShiftShareSE.pdf index 24e4c96..9f4685b 100644 Binary files a/doc/ShiftShareSE.pdf and b/doc/ShiftShareSE.pdf differ