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

Bug when specifying intercept = FALSE in spline #192

Closed a-torgovitsky closed 3 years ago

a-torgovitsky commented 3 years ago

data_example.csv.zip

This works

library(ivmte)

data <- fread("data_example.csv")

args <- list(data = data,
             target = "ate",
             ivlike = YD ~ Z1,
             propensity = D ~ Z1,
             m1 = ~ uSplines(degree = 0, knots = c(.5, .75), intercept = FALSE),
             m0 = ~ 1,
             m0.lb = 0,
             m0.ub = 0)
do.call(ivmte, args)

but if I only have a single knot, I get an error

args[["m1"]] <- ~ uSplines(degree = 0, knots = c(.5), intercept = FALSE)
do.call(ivmte, args)

yields

Error in colMeans(splinesGamma) : 
  'x' must be an array of at least two dimensions
jkcshea commented 3 years ago

This was happening because R likes to convert matrices with one row or one column into a vector. In the second specification, since there is only one knot and no intercept, the spline component consists of just 1 variable. That means a matrix with only one column, and at some point R converts that into a vector. This has now been corrected.

a-torgovitsky commented 3 years ago

Thanks

This was happening because R likes to convert matrices with one row or one column into a vector.

I've certainly run into this before. It's a bit annoying.