rstan icon indicating copy to clipboard operation
rstan copied to clipboard

error with already compiled stored models

Open bnicenboim opened this issue 3 years ago • 6 comments

Summary:

I'm getting an error quite often when the rds of a model is saved.

Description:

This:

N <- 10000
mu <- .002
sigma <- .0004
rt <- 1/rnorm(N, mu, sigma) 
fit_rec <- stan(normal_recrt.stan, data = list(N = N, recRT = 1/rt))

If the model was already ran (and stored), sometimes produces this error.

Error in if (!file.exists(file.rds) || (mtime.rds <- file.info(file.rds)$mtime) <  : 
  missing value where TRUE/FALSE needed

Here is the model and the stored model in rds: normal_recrt.zip

Reproducible Steps:

I cannot really reproduce it, it just happens and then I remove the rds file and continues working. I'm mostly running the models as part of a bookdown, and I get that error a lot!. But once I get it, the stan() command will give me errors until I delete the rds file.

RStan Version:

‘2.26.3’

R Version:

R version 4.1.1 (2021-08-10)

Operating System:

Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 20.04.3 LTS

bnicenboim avatar Sep 16 '21 06:09 bnicenboim

I'm getting the same issue on Windows 10 with rstan 2.26.4.

ShaunMcDonald1021 avatar Dec 08 '21 17:12 ShaunMcDonald1021

I am getting the same issue on Windows 10, rstan 2.26.6.

The underlying problem seems to be that at https://github.com/stan-dev/rstan/blob/da2fc9c079534a82d3d26adda51ad17bf22f5e2b/rstan/rstan/R/rstan.R#L91 the call to packageDescription("rstan")$Date returns NULL, which then makes the subexpression

(mtime.rds <- file.info(file.rds)$mtime) <
       as.POSIXct(packageDescription("rstan")$Date)

evaluate to logical(0). Since the file exists and is valid, the next subexpression (!is(obj <- readRDS(file.rds), "stanmodel")) evaluates to FALSE and logical(0) || FALSE evaluates to NA.

martinmodrak avatar Jan 22 '22 16:01 martinmodrak

The problem was probably introduced with https://github.com/stan-dev/rstan/commit/b6360c07af56f2cffa65aed40a2c7de0b3e4d24c - whether or not CRAN sets the Date field on release, the version at https://mc-stan.org/r-packages/ doesn't have it. Indeed building a version of the package with Date added to DESCRIPTION resolves the issue.

martinmodrak avatar Jan 22 '22 17:01 martinmodrak

So there is a couple possible solutions roughly in order of complexity:

  1. Put Date back in DESCRIPTION and keep it manually up-to-date
  2. When date is not found in DESCRIPTION, try to use a different source of package date, e.g. file.info(system.file("DESCRIPTION", package = "rstan"))$mtime
  3. Modify the deployment process to https://mc-stan.org/r-packages/ so that it automatically fills the Date field in.

Are there opinions on preferred one?

martinmodrak avatar Jan 22 '22 17:01 martinmodrak

@martinmodrak

Thank you for pointing the source of the problem! I solved the same issue I had in my local environment, by following your (first? and) second approach: I ran file.info(system.file("DESCRIPTION", package = "rstan"))$mtime and put that output into the Date field of DESCRIPTION file of the rstan package installed on my computer.

However, I think that restoring the Date field (by, for example, reverting https://github.com/stan-dev/rstan/commit/b6360c07af56f2cffa65aed40a2c7de0b3e4d24c ?) would be the best. The reason is that it might be a long time before CRAN accepts the latest version of rstan (due to the difficulty stated here) and automatically sets the release date in the Date field of DESCRIPTION file.

CLRafaelR avatar Feb 05 '22 13:02 CLRafaelR

Note: I had to do something similar to @CLRafaelR, but I needed to modify Meta/package.rds instead of DESCRIPTION.

tomshafer avatar Mar 04 '22 17:03 tomshafer