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 constant splines with covariates #217

Closed johnnybonney closed 2 years ago

johnnybonney commented 2 years ago

I ran into an error coming from base R when experimenting with some simple MTR specifications. Here is an example that reproduces what I'm seeing:

library(ivmte)
set.seed(9)

ivmte(
  data = AE,
  outcome = "worked",
  target = "ate",
  m0 = ~factor(yob)*uSplines(degree = 0, knots = 0.5),
  m1 = ~factor(yob)*uSplines(degree = 0, knots = 0.5),
  propensity = morekids ~ samesex + factor(yob),
  solver = "gurobi",
  noisy = T
)

yields output

Obtaining propensity scores...

Generating target moments...
    Integrating terms for control group...
    Integrating terms for treated group...

Performing direct MTR regression...
    MTR is not point identified.

Performing audit procedure...
    Solver: Gurobi ('gurobi')
    Generating initial constraint grid...
Error in array(STATS, dims[perm]) : 'dims' cannot be of length 0
In addition: Warning message:
In sweep(x = bmatTmp[, 4:ncol(bmatTmp)], MARGIN = 1, STATS = bmatTmp[,  :
  STATS is longer than the extent of 'dim(x)[MARGIN]'

I can only generate this error under the following three conditions:

  1. splines are constant (with degree = 0)
  2. there is a single internal knot for the spline
  3. splines are interacted with covariates (in this example, yob)
a-torgovitsky commented 2 years ago

Possibly related to the fix in #214 ? What happens if you put intercept = TRUE for uSplines?

johnnybonney commented 2 years ago

Putting intercept = TRUE leads to no issue---suggesting it is almost surely related to the fix in #214.

jkcshea commented 2 years ago

Resolved!

It is indeed to do with #214. R was converting a matrix with a single column into a vector with dimension NULL. That is why we have that error regarding dimensions. The reason why we were getting matrices with a single column is because we no longer include the intercept term for the splines as the default. So what would've been a matrix with two columns has now reduced into one with a single column. I should've caught this error earlier by testing more thoroughly...