jkcshea / ivmte

An R package for implementing the method in Mogstad, Santos, and Torgovitsky (2018, Econometrica).
GNU General Public License v3.0
18 stars 2 forks source link

Problems interacting factor variables with u-portion of MTRs #148

Closed johnnybonney closed 4 years ago

johnnybonney commented 4 years ago

I just updated the package for the first time in a week or so (I installed directly from CRAN). I am running into some new problems when interacting factor variables with the u-portion of m1.

Here is an example:

# setup
library(data.table)
library(ivmte)

set.seed(1001)
N <- 2000

dt <- data.table(
  id = 1:N,
  u = runif(N),
  x = sample(0:1, N, replace = T),
  z = sample(1:4, N, replace = T),
  epsilon = rnorm(N, sd = .1)
)

dt[, p := (z == 1)*0.12 + (z == 2)*0.29 + (z == 3)*0.48 + (z == 4)*0.78]
dt[, d := as.integer(u <= p)]
dt[, y0 := 0.9 - 1.1*u + 0.3*u^2]
dt[, y1 := 0.35 - 0.3*u - 0.05*u^2]
dt[, y := y1*d + y0*(1 - d) + epsilon]

args_test <- list(
  data = dt,
  propensity = d ~ factor(z),
  ivlike = c(y ~ d,
             y ~ d | factor(z)),
  m0 = ~u + I(u^2),
  target = "att",
  lpsolver = "gurobi"
)

# interacting a factor with a linear u
args_test$m1 <- ~factor(x):u
res.test <- do.call(ivmte, args_test)
Obtaining propensity scores...
Generating target moments...
Error in dmat[, oterms] : subscript out of bounds
In addition: Warning message:
No list of components provided. All covariates in each IV-like specification will be included when constructing each S-set. 

I get a distinct error when I model the MTRs similarly but specify the u-portion to be a spline:

# interacting a factor with a u-spline
args_test$m1 <- ~factor(x):uSplines(degree = 2, knots = c(0.3, 0.6))
res.test <- do.call(ivmte, args_test)
Obtaining propensity scores...
Generating target moments...
    Integrating terms for control group...
    Integrating terms for treated group...
Generating IV-like moments...
    Moment 1...
    Moment 2...
    Moment 3...
    Moment 4...
Performing audit procedure...
    Audit count: 1 
Full support of covariates now included as grid.
Error in get(paste0("A", d))[, c(uname, iName, ".grid.order")] : 
  subscript out of bounds
In addition: Warning message:
No list of components provided. All covariates in each IV-like specification will be included when constructing each S-set.
johnnybonney commented 4 years ago

I realize that the version on CRAN is quite outdated. I thought these issues looked familiar... it is because I have seen them before, submitted them, and @jkcshea has fixed them.

I will update directly from the repository. The errors should disappear.