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

Error interacting covariates with uSplines #222

Closed johnnybonney closed 2 years ago

johnnybonney commented 2 years ago

This might be related to the fix for #211.

If I interact an as-is function of a covariate with a uSpline, I get a parsing error. Here is a simple example:

library(ivmte)

mtr <- ~black + I(black == 0):uSplines(degree = 3, knots = 0.5) +
  I(black == 1):uSplines(degree = 3, knots = 0.5)

args <- list(data = AE,
             outcome = "worked",
             m1 = mtr,
             m0 = mtr,
             propensity = morekids ~ factor(samesex)*factor(black),
             target = 'ate',
             solver = "gurobi",
             noisy = T)  

do.call(ivmte, args)

returns the error

Error in str2lang(x) : <text>:1:17: unexpected '=='
1: ~ black==0+black==
                    ^

If I remove the interactions, the error doesn't appear. Or, if I replace the uSplines with just u, the error disappears.

If I replace the MTR specification with

mtr2 <- ~black + uSplines(degree = 3, knots = 0.5) + I(black == 1):uSplines(degree = 3, knots = 0.5)
args$m1 <- mtr2
args$m0 <- mtr2

do.call(ivmte, args)

then I get a different error:

Obtaining propensity scores...

Generating target moments...
    Integrating terms for control group...
    Integrating terms for treated group...
Error in `[.data.frame`(nonSplinesDmat, , inters[[j]]) : 
  undefined columns selected

If I replace I(black == 1) with black, then this error disappears and everything runs as expected.

jkcshea commented 2 years ago

Sorry for the hold up on all the other issues. This one was tricky to resolve because I had to revisit the code for parsing. But this is done!

We now also allow for as-is interactions like, I(black * samesex == 1):uSplines(...). The other approach---and probably more robust since it avoids all the parsing code---is just to create a new variable. e.g.,

AE[, black.samesex := black * samesex]

then use black.samesex:uSplines(...) in the MTR expressions.