Skip to content

Commit

Permalink
Don't worry about cyclomatic complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
kolesarm committed Aug 19, 2024
1 parent 4579fcc commit e3a2d81
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
3 changes: 2 additions & 1 deletion R/iv.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
7 changes: 4 additions & 3 deletions R/ols.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 10 additions & 8 deletions doc/ShiftShareSE.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
## ---- 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 = "#>",
tidy.opts=list(blank=FALSE, width.cutoff=55))

## -----------------------------------------------------------------------------
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")

16 changes: 9 additions & 7 deletions doc/ShiftShareSE.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

Expand All @@ -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
Expand All @@ -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
Expand Down
Binary file modified doc/ShiftShareSE.pdf
Binary file not shown.

0 comments on commit e3a2d81

Please sign in to comment.