stan-dev / rstanarm

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

stan_surv in rstanarm 2.21.3 #566

Open pavilaalo opened 2 years ago

pavilaalo commented 2 years ago

Summary:

Unable to use stan_surv function.

Description:

After updating the last release of rstanarm (2.21.3) I am not able to find the stan_surv function for Bayesian survival models. I was using them in the previous version 2.21.2 without any problem.

I tried the instructions in https://github.com/stan-dev/rstanarm#survival-analysis-version but it didn't work. It worked using remotes::install_github('stan-dev/rstanarm@feature/survival')

Reproducible Steps:

If applicable, the steps required to reproduce the issue. If you have a reproducible example, please include it.

RStanARM Version:

2.21.3

R Version:

R 4.1.2

Operating System:

macOS Big Sur 11.4

canuckafar commented 2 years ago

I am having the same issue using R 4.2.1 for Windows 10 and rstanarm 2.21.3; no stan_surv function is present in the installation. I uninstalled rstanarm 2.21.3, updated my packages, then reinstalled rstanarm 2.21.2 using the method noted above and got no resolution. Here is the code to reproduce the issue:

> remove.packages('rstanarm')
> remotes::install_github('stan-dev/rstanarm@feature/survival', build_vignettes = FALSE)
> library(rstanarm)

Loading required package: Rcpp
This is rstanarm version 2.21.2
- See https://mc-stan.org/rstanarm/articles/priors for changes to default priors!
- Default priors may change, so it's safest to specify priors, even if equivalent to the defaults.
- For execution on a local, multicore CPU with excess RAM we recommend calling
  options(mc.cores = parallel::detectCores())

> ?stan_surv
No documentation for ‘stan_surv’ in specified packages and libraries:
you could try ‘??stan_surv’

This suggest no stan_surv function is present. But then I tried this:

> library(survival)
> data(mgus)
> surv <- stan_surv(Surv(futime, death) ~ mspike, data = mgus, 
                  basehaz = "weibull", chains = 4, seed = 1234)

SAMPLING FOR MODEL 'surv' NOW (CHAIN 1).
Chain 1: 
Chain 1: Gradient evaluation took 0 seconds
Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 1: Adjust your expectations accordingly!
Chain 1: 
Chain 1: 
Chain 1: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 1: Iteration:  200 / 2000 [ 10%]  (Warmup)
Chain 1: Iteration:  400 / 2000 [ 20%]  (Warmup)
Chain 1: Iteration:  600 / 2000 [ 30%]  (Warmup)
Chain 1: Iteration:  800 / 2000 [ 40%]  (Warmup)
Chain 1: Iteration: 1000 / 2000 [ 50%]  (Warmup)
Chain 1: Iteration: 1001 / 2000 [ 50%]  (Sampling)
Chain 1: Iteration: 1200 / 2000 [ 60%]  (Sampling)
Chain 1: Iteration: 1400 / 2000 [ 70%]  (Sampling)
Chain 1: Iteration: 1600 / 2000 [ 80%]  (Sampling)
Chain 1: Iteration: 1800 / 2000 [ 90%]  (Sampling)
Chain 1: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 1: 
Chain 1:  Elapsed Time: 2.387 seconds (Warm-up)
Chain 1:                2.232 seconds (Sampling)
Chain 1:                4.619 seconds (Total)
Chain 1: 

SAMPLING FOR MODEL 'surv' NOW (CHAIN 2).
Chain 2: 
Chain 2: Gradient evaluation took 0 seconds
Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 2: Adjust your expectations accordingly!
Chain 2: 
Chain 2: 
Chain 2: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 2: Iteration:  200 / 2000 [ 10%]  (Warmup)
Chain 2: Iteration:  400 / 2000 [ 20%]  (Warmup)
Chain 2: Iteration:  600 / 2000 [ 30%]  (Warmup)
Chain 2: Iteration:  800 / 2000 [ 40%]  (Warmup)
Chain 2: Iteration: 1000 / 2000 [ 50%]  (Warmup)
Chain 2: Iteration: 1001 / 2000 [ 50%]  (Sampling)
Chain 2: Iteration: 1200 / 2000 [ 60%]  (Sampling)
Chain 2: Iteration: 1400 / 2000 [ 70%]  (Sampling)
Chain 2: Iteration: 1600 / 2000 [ 80%]  (Sampling)
Chain 2: Iteration: 1800 / 2000 [ 90%]  (Sampling)
Chain 2: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 2: 
Chain 2:  Elapsed Time: 2.497 seconds (Warm-up)
Chain 2:                2.24 seconds (Sampling)
Chain 2:                4.737 seconds (Total)
Chain 2: 

SAMPLING FOR MODEL 'surv' NOW (CHAIN 3).
Chain 3: 
Chain 3: Gradient evaluation took 0 seconds
Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 3: Adjust your expectations accordingly!
Chain 3: 
Chain 3: 
Chain 3: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 3: Iteration:  200 / 2000 [ 10%]  (Warmup)
Chain 3: Iteration:  400 / 2000 [ 20%]  (Warmup)
Chain 3: Iteration:  600 / 2000 [ 30%]  (Warmup)
Chain 3: Iteration:  800 / 2000 [ 40%]  (Warmup)
Chain 3: Iteration: 1000 / 2000 [ 50%]  (Warmup)
Chain 3: Iteration: 1001 / 2000 [ 50%]  (Sampling)
Chain 3: Iteration: 1200 / 2000 [ 60%]  (Sampling)
Chain 3: Iteration: 1400 / 2000 [ 70%]  (Sampling)
Chain 3: Iteration: 1600 / 2000 [ 80%]  (Sampling)
Chain 3: Iteration: 1800 / 2000 [ 90%]  (Sampling)
Chain 3: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 3: 
Chain 3:  Elapsed Time: 2.439 seconds (Warm-up)
Chain 3:                2.132 seconds (Sampling)
Chain 3:                4.571 seconds (Total)
Chain 3: 

SAMPLING FOR MODEL 'surv' NOW (CHAIN 4).
Chain 4: 
Chain 4: Gradient evaluation took 0 seconds
Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 4: Adjust your expectations accordingly!
Chain 4: 
Chain 4: 
Chain 4: Iteration:    1 / 2000 [  0%]  (Warmup)
Chain 4: Iteration:  200 / 2000 [ 10%]  (Warmup)
Chain 4: Iteration:  400 / 2000 [ 20%]  (Warmup)
Chain 4: Iteration:  600 / 2000 [ 30%]  (Warmup)
Chain 4: Iteration:  800 / 2000 [ 40%]  (Warmup)
Chain 4: Iteration: 1000 / 2000 [ 50%]  (Warmup)
Chain 4: Iteration: 1001 / 2000 [ 50%]  (Sampling)
Chain 4: Iteration: 1200 / 2000 [ 60%]  (Sampling)
Chain 4: Iteration: 1400 / 2000 [ 70%]  (Sampling)
Chain 4: Iteration: 1600 / 2000 [ 80%]  (Sampling)
Chain 4: Iteration: 1800 / 2000 [ 90%]  (Sampling)
Chain 4: Iteration: 2000 / 2000 [100%]  (Sampling)
Chain 4: 
Chain 4:  Elapsed Time: 2.609 seconds (Warm-up)
Chain 4:                2.349 seconds (Sampling)
Chain 4:                4.958 seconds (Total)
Chain 4: 
Warning messages:
1: In status < 0 || status > 3 :
  'length(x) = 241 > 1' in coercion to 'logical(1)'
2: In status < 0 || status > 3 :
  'length(x) = 241 > 1' in coercion to 'logical(1)'
3: In status < 0 || status > 3 :
  'length(x) = 241 > 1' in coercion to 'logical(1)'
4: In status < 0 || status > 3 :
  'length(x) = 241 > 1' in coercion to 'logical(1)'

> summary(surv)

Model Info:

 function:        stan_surv
 baseline hazard: weibull
 formula:         Surv(futime, death) ~ mspike
 algorithm:       sampling
 sample:          4000 (posterior sample size)
 priors:          see help('prior_summary')
 observations:    241
 events:          225 (93.4%)
 right censored:  16 (6.6%)
 delayed entry:   no

Estimates:
                mean   sd    10%   50%   90%
(Intercept)   -10.2    0.6 -11.0 -10.2  -9.3
mspike         -0.1    0.2  -0.3  -0.1   0.1
weibull-shape   1.2    0.1   1.1   1.2   1.3

MCMC diagnostics
              mcse Rhat n_eff
(Intercept)   0.0  1.0  1265 
mspike        0.0  1.0  1571 
weibull-shape 0.0  1.0  1144 
log-posterior 0.0  1.0  1185 

For each parameter, mcse is Monte Carlo standard error, n_eff is a crude measure of effective sample size, and Rhat is the potential scale reduction factor on split chains (at convergence Rhat=1).

Which suggests the model works. Have not tested more complex models yet.

timhowes commented 2 years ago

The git repo doesn't have the *.Rd files committed, so you'll need to run roxygen2 to build the documentation files.

git clone -b feature/survival https://github.com/stan-dev/rstanarm.git
R -e 'roxygen2::roxygenize("rstanarm"); devtools::install("rstanarm", build_vignettes = FALSE)'
Hazel2544 commented 4 months ago

Hi all, I got same problem and would like to figure out the solution. I used an example to test if the stan_surv works and found the following comment


```  stan_surv_exponential <- stan_surv(formula = Surv(time, status) ~ x,
+                                    data = leukemia,
+                                    basehaz = "exp")
Error in stan_surv(formula = Surv(time, status) ~ x, data = leukemia,  : 
  could not find function "stan_surv"

I downloaded the rstanarm-feature-survival from Github branch and installed it to the library locally. The thing is rstanarm-feature-survival is installed in the library, but when I call the rstanarm pacakge, it didn't work because of the name (stan_surv.R is in the rstanarm-feature-survival, but not in the rstanarm). So I copied the stan_surv.R from rstanarm-feature-survival to rstanarm in the library, it didn't work as well. I've also already tried the methods discussed above, but none of them works, I would much appreciate it if anyone could share the solution that works. Thank you!