stan-dev / rstanarm

rstanarm R package for Bayesian applied regression modeling
https://mc-stan.org/rstanarm
GNU General Public License v3.0
385 stars 132 forks source link

stan_surv: Error when including frailty terms #536

Closed jburos closed 2 years ago

jburos commented 3 years ago

Address #535 by reversing the order of checks for type in both model.frame.stanreg and formula.stanreg.

Note: this breaks behavior of ranef(), when applied to a stan_surv model with frailty.

jburos commented 3 years ago

Here is the output of running check on this branch: Status: 2 ERRORs, 2 WARNINGs, 4 NOTEs

==> roxygen2::roxygenize('.', roclets = c('rd', 'collate', 'namespace'))

ℹ Loading rstanarm
Warning: [/Users/jburos/projects/rstanarm/R/posterior_survfit.R:299] @examples mismatched braces or quotes
Warning: [/Users/jburos/projects/rstanarm/R/posterior_survfit.R:1131] @examples mismatched braces or quotes
Warning: [/Users/jburos/projects/rstanarm/R/posterior_traj.R:183] @examples mismatched braces or quotes
Warning: [/Users/jburos/projects/rstanarm/R/posterior_traj.R:545] @examples mismatched braces or quotes
Warning: [/Users/jburos/projects/rstanarm/R/ps_check.R:71] @examples mismatched braces or quotes
Writing NAMESPACE
Writing NAMESPACE
Documentation completed

==> R CMD build rstanarm

* checking for file ‘rstanarm/DESCRIPTION’ ... OK
* preparing ‘rstanarm’:
* checking DESCRIPTION meta-information ... OK
* cleaning src
* running ‘cleanup’
* installing the package to build vignettes
* creating vignettes ... OK
* cleaning src
* running ‘cleanup’
* checking for LF line-endings in source and make files and shell scripts
* checking for empty or unneeded directories
* building ‘rstanarm_2.21.2.tar.gz’

==> R CMD check --run-dontrun --run-donttest rstanarm_2.21.2.tar.gz

* using log directory ‘/Users/jburos/projects/rstanarm.Rcheck’
* using R version 4.0.3 (2020-10-10)
* using platform: x86_64-apple-darwin17.0 (64-bit)
* using session charset: UTF-8
* using options ‘--run-dontrun --run-donttest’
* checking for file ‘rstanarm/DESCRIPTION’ ... OK
* checking extension type ... Package
* this is package ‘rstanarm’ version ‘2.21.2’
* package encoding: UTF-8
* checking package namespace information ... OK
* checking package dependencies ... OK
* checking if this is a source package ... OK
* checking if there is a namespace ... OK
* checking for executable files ... OK
* checking for hidden files and directories ... OK
* checking for portable file names ... OK
* checking for sufficient/correct file permissions ... OK
* checking whether package ‘rstanarm’ can be installed ... OK
* checking installed package size ... NOTE
  installed size is 28.7Mb
  sub-directories of 1Mb or more:
    R      2.0Mb
    doc    7.8Mb
    libs  18.1Mb
* checking package directory ... OK
* checking ‘build’ directory ... OK
* checking DESCRIPTION meta-information ... OK
* checking top-level files ... OK
* checking for left-over files ... OK
* checking index information ... OK
* checking package subdirectories ... OK
* checking R files for non-ASCII characters ... OK
* checking R files for syntax errors ... OK
* checking whether the package can be loaded ... OK
* checking whether the package can be loaded with stated dependencies ... OK
* checking whether the package can be unloaded cleanly ... OK
* checking whether the namespace can be loaded with stated dependencies ... OK
* checking whether the namespace can be unloaded cleanly ... OK
* checking dependencies in R code ... NOTE
Namespace in Imports field not imported from: ‘RcppParallel’
  All declared Imports should be used.
* checking S3 generic/method consistency ... OK
* checking replacement functions ... OK
* checking foreign function calls ... OK
* checking R code for possible problems ... NOTE
.pp_data_surv: no visible binding for global variable ‘xlevs’
stan_surv: no visible binding for global variable ‘xlevs’
Undefined global functions or variables:
  xlevs
* checking Rd files ... OK
* checking Rd metadata ... OK
* checking Rd cross-references ... WARNING
Package unavailable to check Rd xrefs: ‘biglm’
Missing link or links in documentation object 'tve.Rd':
  ‘stanreg’

See section 'Cross-references' in the 'Writing R Extensions' manual.

* checking for missing documentation entries ... OK
* checking for code/documentation mismatches ... OK
* checking Rd \usage sections ... WARNING
Undocumented arguments in documentation object 'stan_surv'
  ‘prior_covariance’

Functions with \usage entries need to have the appropriate \alias
entries, and all their arguments documented.
The \usage entries must correspond to syntactically valid R code.
See chapter ‘Writing R documentation files’ in the ‘Writing R
Extensions’ manual.
* checking Rd contents ... OK
* checking for unstated dependencies in examples ... OK
* checking contents of ‘data’ directory ... OK
* checking data for non-ASCII characters ... OK
* checking data for ASCII and uncompressed saves ... OK
* checking line endings in shell scripts ... OK
* checking line endings in C/C++/Fortran sources/headers ... OK
* checking line endings in Makefiles ... OK
* checking compilation flags in Makevars ... OK
* checking for GNU extensions in Makefiles ... NOTE
GNU make is a SystemRequirements.
* checking for portable use of $(BLAS_LIBS) and $(LAPACK_LIBS) ... OK
* checking use of PKG_*FLAGS in Makefiles ... OK
* checking compiled code ... OK
* checking installed files from ‘inst/doc’ ... OK
* checking files in ‘vignettes’ ... OK
* checking examples ... ERROR
Running examples in ‘rstanarm-Ex.R’ failed
sh: line 1: 67023 Segmentation fault: 11  LANGUAGE=en _R_CHECK_INTERNALS2_=1 '/Library/Frameworks/R.framework/Resources/bin/R' --vanilla --encoding=UTF-8 > 'rstanarm-Ex.Rout' 2>&1 < 'rstanarm-Ex.R'
The error most likely occurred in:

> ### Name: pp_validate
> ### Title: Model validation via simulation
> ### Aliases: pp_validate
> 
> ### ** Examples
> 
> if (.Platform$OS.type != "windows" || .Platform$r_arch != "i386") {
+ 
+ if (!exists("example_model")) example(example_model)
+ try(pp_validate(example_model)) # fails with default seed / priors
+ 
+ }

exmpl_> if (.Platform$OS.type != "windows" || .Platform$r_arch != "i386") {
exmpl_+ example_model <- 
exmpl_+   stan_glmer(cbind(incidence, size - incidence) ~ size + period + (1|herd),
exmpl_+              data = lme4::cbpp, family = binomial, QR = TRUE,
exmpl_+              # this next line is only to keep the example small in size!
exmpl_+              chains = 2, cores = 1, seed = 12345, iter = 1000, refresh = 0)
exmpl_+ example_model
exmpl_+ }
stan_glmer
 family:       binomial [logit]
 formula:      cbind(incidence, size - incidence) ~ size + period + (1 | herd)
 observations: 56
------
            Median MAD_SD
(Intercept) -1.5    0.6  
size         0.0    0.0  
period2     -1.0    0.3  
period3     -1.1    0.4  
period4     -1.6    0.5  

Error terms:
 Groups Name        Std.Dev.
 herd   (Intercept) 0.79    
Num. levels: herd 15 

------
* For help interpreting the printed output see ?print.stanreg
* For info on the priors used see ?prior_summary.stanreg
Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
Running the chains for more iterations may help. See
http://mc-stan.org/misc/warnings.html#bulk-ess

 *** caught segfault ***
address 0x0, cause 'unknown'

Traceback:
 1: new_CppObject_xp(fields$.module, fields$.pointer, ...)
 2: Rcpp::cpp_object_initializer(.self, .refClassDef, ...)
 3: .Object$initialize(...)
 4: initialize(value, ...)
 5: initialize(value, ...)
 6: new(stan_fit_cpp_module, data, as.integer(seed), cxxfun)
 7: doTryCatch(return(expr), name, parentenv, handler)
 8: tryCatchOne(expr, names, parentenv, handlers[[1L]])
 9: tryCatchList(expr, classes, parentenv, handlers)
10: tryCatch(expr, error = function(e) {    call <- conditionCall(e)    if (!is.null(call)) {        if (identical(call[[1L]], quote(doTryCatch)))             call <- sys.call(-4L)        dcall <- deparse(call)[1L]        prefix <- paste("Error in", dcall, ": ")        LONG <- 75L        sm <- strsplit(conditionMessage(e), "\n")[[1L]]        w <- 14L + nchar(dcall, type = "w") + nchar(sm[1L], type = "w")        if (is.na(w))             w <- 14L + nchar(dcall, type = "b") + nchar(sm[1L],                 type = "b")        if (w > LONG)             prefix <- paste0(prefix, "\n  ")    }    else prefix <- "Error : "    msg <- paste0(prefix, conditionMessage(e), "\n")    .Internal(seterrmessage(msg[1L]))    if (!silent && isTRUE(getOption("show.error.messages"))) {        cat(msg, file = outFile)        .Internal(printDeferredWarnings())    }    invisible(structure(msg, class = "try-error", condition = e))})
11: try(new(stan_fit_cpp_module, data, as.integer(seed), cxxfun))
12: .local(object, ...)
13: (new("nonstandardGenericFunction", 
  [...] # truncated
27: as.matrix(do.call("update", update_args))
28: pp_validate(example_model)
29: doTryCatch(return(expr), name, parentenv, handler)
30: tryCatchOne(expr, names, parentenv, handlers[[1L]])
31: tryCatchList(expr, classes, parentenv, handlers)
32: tryCatch(expr, error = function(e) {    call <- conditionCall(e)    if (!is.null(call)) {        if (identical(call[[1L]], quote(doTryCatch)))             call <- sys.call(-4L)        dcall <- deparse(call)[1L]        prefix <- paste("Error in", dcall, ": ")        LONG <- 75L        sm <- strsplit(conditionMessage(e), "\n")[[1L]]        w <- 14L + nchar(dcall, type = "w") + nchar(sm[1L], type = "w")        if (is.na(w))             w <- 14L + nchar(dcall, type = "b") + nchar(sm[1L],                 type = "b")        if (w > LONG)             prefix <- paste0(prefix, "\n  ")    }    else prefix <- "Error : "    msg <- paste0(prefix, conditionMessage(e), "\n")    .Internal(seterrmessage(msg[1L]))    if (!silent && isTRUE(getOption("show.error.messages"))) {        cat(msg, file = outFile)        .Internal(printDeferredWarnings())    }    invisible(structure(msg, class = "try-error", condition = e))})
33: try(pp_validate(example_model))
An irrecoverable exception occurred. R is aborting now ...
* checking for unstated dependencies in ‘tests’ ... OK
* checking tests ...
  Running ‘testthat.R’/Library/Frameworks/R.framework/Resources/bin/BATCH: line 60: 67334 Segmentation fault: 11  ${R_HOME}/bin/R -f ${in} ${opts} ${R_BATCH_OPTIONS} > ${out} 2>&1

 ERROR
Running the tests in ‘tests/testthat.R’ failed.
Last 13 lines of output:
  45: tryCatch(withCallingHandlers({    eval(code, test_env)    if (!handled && !is.null(test)) {        skip_empty()    }}, expectation = handle_expectation, skip = handle_skip, warning = handle_warning,     message = handle_message, error = handle_error), error = handle_fatal,     skip = function(e) {    })
  46: test_code(NULL, exprs, env)
  47: source_file(path, child_env(env), wrap = wrap)
  48: FUN(X[[i]], ...)
  49: lapply(test_paths, test_one_file, env = env, wrap = wrap)
  50: force(code)
  51: doWithOneRestart(return(expr), restart)
  52: withOneRestart(expr, restarts[[1L]])
  53: withRestarts(testthat_abort_reporter = function() NULL, force(code))
  54: with_reporter(reporters$multi, lapply(test_paths, test_one_file,     env = env, wrap = wrap))
  55: test_files(test_dir = test_dir, test_package = test_package,     test_paths = test_paths, load_helpers = load_helpers, reporter = reporter,     env = env, stop_on_failure = stop_on_failure, stop_on_warning = stop_on_warning,     wrap = wrap, load_package = load_package)
  56: test_files(test_dir = path, test_paths = test_paths, test_package = package,     reporter = reporter, load_helpers = load_helpers, env = env,     stop_on_failure = stop_on_failure, stop_on_warning = stop_on_warning,     wrap = wrap, load_package = load_package, parallel = parallel)
  57: test_dir("testthat", package = package, reporter = reporter,     ..., load_package = "installed")
  58: test_check("rstanarm", invert = FALSE, filter = if (Sys.getenv("NOT_CRAN") !=     "true") "stan_functions")
  An irrecoverable exception occurred. R is aborting now ...
* checking for unstated dependencies in vignettes ... OK
* checking package vignettes in ‘inst/doc’ ... OK
* checking running R code from vignettes ...
  ‘aov.Rmd’ using ‘UTF-8’... OK
  ‘betareg.Rmd’ using ‘UTF-8’... OK
  ‘binomial.Rmd’ using ‘UTF-8’... OK
  ‘continuous.Rmd’ using ‘UTF-8’... OK
  ‘count.Rmd’ using ‘UTF-8’... OK
  ‘glmer.Rmd’ using ‘UTF-8’... OK
  ‘jm.Rmd’ using ‘UTF-8’... OK
  ‘lm.Rmd’ using ‘UTF-8’... OK
  ‘mrp.Rmd’ using ‘UTF-8’... OK
  ‘polr.Rmd’ using ‘UTF-8’... OK
  ‘pooling.Rmd’ using ‘UTF-8’... OK
  ‘priors.Rmd’ using ‘UTF-8’... OK
  ‘rstanarm.Rmd’ using ‘UTF-8’... OK
  ‘surv.Rmd’ using ‘UTF-8’... OK
 NONE
* checking re-building of vignette outputs ... OK
* checking PDF version of manual ... OK
* DONE
Status: 2 ERRORs, 2 WARNINGs, 4 NOTEs

See
  ‘/Users/jburos/projects/rstanarm.Rcheck/00check.log’
for details.
sambrilleman commented 3 years ago

Thanks @jburos! Am I reading this right, in that it didn't actually run the testthat tests because something failed when loading the tests?

jburos commented 3 years ago

Thanks @sambrilleman. That failure to load the package (betareg I think) was in an earlier edit of that comment.

I then updated the comment with output after finishing the tests. This ended with Status: 2 ERRORs, 2 WARNINGs, 4 NOTEs.

The two errors are:

  1. Running examples in ‘rstanarm-Ex.R’ failed
    • segfault: (address 0x0, cause 'unknown') in stan_glmer, when running pp_validate(example_model).
  2. Running the tests in ‘tests/testthat.R’ failed.
    • Also looks like a segfault: An irrecoverable exception occurred. R is aborting now ...

I don't think these are related to the changes I made; they may be due to some peculiarity of my system. I'm going to try on a clean setup to confirm