sambrilleman / rstanarm

rstanarm R package for Bayesian applied regression modeling
http://mc-stan.org/interfaces/rstanarm.html
GNU General Public License v3.0
0 stars 1 forks source link

Formulas not evaluated correctly in develop2 branch #48

Closed jburos closed 7 years ago

jburos commented 7 years ago

Summary:

When running loo on a fitted stan_jm model, I see the following error.

Error in eval(expr, envir, enclos) : object '[omitted]' not found

where the omitted portion contains the name of a field used in my formulaLong.

Description:

In this particular example, I had called stan_jm in a manner similar to the following:

library(rstanarm)
library(dplyr)
fit1 <- stan_jm(formulaLong = log1p(platelet) ~ year + (1|id),
                         dataLong = pbcLong %>% dplyr::arrange(id, year),
                        ....) 

Interestingly enough, now when I go to create a reproducible example, I come across the following:

Input:

fit <- stan_jm(formulaLong = log1p(platelet) ~ year + (year | id),
               dataLong = pbcLong,
               formulaEvent = Surv(futimeYears, death) ~ sex + trt, 
               dataEvent = pbcSurv,
               assoc = c("etavalue", "etaslope"),
               time_var = "year",
               )

Output:

Error in eval(expr, envir, enclos) : object 'platelet' not found

Previously I had been able to fit models using this convention. However, it doesn't appear that the latest version of code is handling formulas correctly.

Reproducible Steps:

See above.

RStanARM Version:

I installed a local fork of develop2 branch, located here:

devtools::install_github('jburos/rstanarm', ref = 'fix-posterior-predict-newdata', args = '--preclean', local = TRUE)

I will do some more tests to see if this issue is specific to that fork or is evident on develop2 as well.

jburos commented 7 years ago

Not sure if this is related, but when re-installing rstanarm I get the following message:

Reloading installed rstanarm
Error in get(Info[i, 1], envir = env) : 
  lazy-load database '/home/jacquelineburos/projects/xx-jointmodel-develop2/Rlib/lme4/R/lme4.rdb' is corrupt
jburos commented 7 years ago

Now that I have cleaned up my install environment (fun!) I am still seeing this issue, particularly when estimating loo.

At the moment, when debugging, I run into the problem on line 284 of log_lik.R:

    nms <- collect_nms(colnames(stanmat), get_M(object))
    y <- eval(formula(object, m = m)[[2L]], data$ndL[[m]]) ## <- here
    x <- data$yX[[m]]

At this point in the code, the formula & data look like:

Browse[6]> formula(object, m = m)[[2L]]
log1p(sld)
Browse[6]> names(data$ndL[[m]])
[1] "log1p(sld)"               "cat"                      "poly(months, degree = 2)"
[4] "usubjid"                  "months"

Which yields the object 'sld' not found error on evaluation of log1p(sld).

This is a minor issue, but posting here for the record.

jburos commented 7 years ago

Just FYI that I'm still seeing this issue on a fresh install from develop2 branch.

E.g.:

> f0 <- stan_jm(formulaLong = sqrt(CD4) ~ obstime*drug + (1 + obstime | patient), 
+               dataLong = aids,
+               time_var = "obstime",
+               formulaEvent = Surv(Time, death) ~ gender + drug + AZT + prevOI, 
+               dataEvent = aids.id
+               )
Error in eval(expr, envir, enclos) : object 'CD4' not found
> packageDescription('rstanarm', fields = c('GithubRepo', 'GithubUsername', 'GithubRef', 'GithubSHA1'))
GithubRepo: rstanarm
GithubUsername: sambrilleman
GithubRef: develop2
GithubSHA1: 0176bc3591d24e4442a7a4d1f44adcf1b57418d8
sambrilleman commented 7 years ago

Hopefully the changes here help to solve this issue. But, let me know if you are still encountering them.

I'll merge these changes into the develop4 branch too, which is the branch I am going to now start using as the "most up to date". I plan to tidy and possibly change a few things in the develop4 branch, so I want to retain the develop2 branch pretty much as it is, so as we have something reasonably stable.

sambrilleman commented 7 years ago

I'm hoping that this issue is now resolved, given the changes related to evaluation environments in stan_jm (shown in the commit above), and the new approach constructing design matrices from a data frame instead of model frame, related to the commits surrounding this one.