joshuaschwab / ltmle

Longitudinal Targeted Maximum Likelihood Estimation package
http://joshuaschwab.github.io/ltmle/
23 stars 16 forks source link

Support for tibbles in data argument #16

Closed malcolmbarrett closed 6 years ago

malcolmbarrett commented 6 years ago

I was in a workshop recently that used ltmle (which is super cool, by the way!), and I read in the data with readr::read_csv(), which returns a tbl rather than a pure data.frame. I found that ltmle fails when the data object is a tbl:

library(tibble)
library(ltmle)
library(SuperLearner)

# fake data
test_data <- tibble(w1 = rnorm(100), w2 = rnorm(100), a = sample(0:1, 100, replace = TRUE), 
  y = rnorm(100))
sl_libs <- c("SL.lm", "SL.gam")

# submit a tibble
model <- ltmle(test_data, Anodes = "a", Ynodes = "y", abar = list(1, 0), SL.library = sl_libs)
#> Error in matrix(unlist(value, recursive = FALSE, use.names = FALSE), nrow = nr, : length of 'dimnames' [2] not equal to array extent

Whereas converting it to a data.frame works:

model <- ltmle(as.data.frame(test_data), Anodes = "a", Ynodes = "y", abar = list(1, 
  0), SL.library = sl_libs)
 # ...output

Considering how common tibbles are these days, I thought it might be useful to add a quick line to check and convert tibbles. I'm sending a PR that adds that, if you feel it would be helpful.

Thanks! Malcolm

joshuaschwab commented 6 years ago

Hi Malcolm, Sorry this took me a while, but this works now (v1.1-0, here or on CRAN). Thank you for the suggestion. Josh

malcolmbarrett commented 6 years ago

I saw that on the CRAN logs! I see the repo itself hasn't changed, though. Is there a different location with the current source code?

malcolmbarrett commented 6 years ago

Cancel that, I see it's been updated now