Introduction to the R package survstan

library(survstan)
library(dplyr)

ovarian <- ovarian %>%
  mutate(
    rx = as.factor(rx)
  )

# fitting the model:
fit <- aftreg(Surv(futime, fustat) ~ rx + age, baseline = "weibull", data = ovarian, init = 0)

# investigating the fitted model:
estimates(fit)
#>           rx2           age         alpha         gamma 
#>  5.674487e-01 -7.909102e-02  1.816064e+00  6.166990e+04
coef(fit)
#>         rx2         age 
#>  0.56744871 -0.07909102
confint(fit)
#>                2.5%         97.5%
#> rx2   -9.942215e-02  1.234320e+00
#> age   -1.180869e-01 -4.009511e-02
#> alpha  1.351074e+00  2.281055e+00
#> gamma  5.855536e+04  6.478443e+04
summary(fit)
#> Call:
#> aftreg(formula = Surv(futime, fustat) ~ rx + age, data = ovarian, 
#>     baseline = "weibull", init = 0)
#> 
#> Accelerated failure time model fit with weibull baseline distribution 
#> 
#> Regression coefficients:
#>      Estimate    StdErr z.value   p.value    
#> rx2  0.567449  0.340246  1.6678   0.09536 .  
#> age -0.079091  0.019896 -3.9752 7.033e-05 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Baseline parameters:
#>         estimate         se       2.5%      97.5%
#> alpha 1.8161e+00 2.3724e-01 1.3511e+00     2.2811
#> gamma 6.1670e+04 1.5891e+03 5.8555e+04 64784.4280
#> --- 
#> loglik = -88.76171   AIC = 185.5234
tidy(fit)
#> # A tibble: 4 × 6
#>   type        parameter   estimate        se     `2.5%`    `97.5%`
#>   <chr>       <chr>          <dbl>     <dbl>      <dbl>      <dbl>
#> 1 coefficient rx2           0.567     0.340     -0.0994     1.23  
#> 2 coefficient age          -0.0791    0.0199    -0.118     -0.0401
#> 3 baseline    alpha         1.82      0.237      1.35       2.28  
#> 4 baseline    gamma     61670.     1589.     58555.     64784.
vcov(fit)
#>             rx2          age
#> rx2 0.115767673 0.0017578433
#> age 0.001757843 0.0003958601


# residual plots:
ggresiduals(fit, type = "coxsnell")

ggresiduals(fit, type = "martingale")
#> `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

ggresiduals(fit, type = "deviance")



# Deviance analysis:
fit1 <- aftreg(Surv(futime, fustat) ~ 1, baseline = "weibull", data = ovarian, init = 0)
fit2 <- aftreg(Surv(futime, fustat) ~ rx, baseline = "weibull", data = ovarian, init = 0)
fit3 <- aftreg(Surv(futime, fustat) ~ rx + ecog.ps, baseline = "weibull", data = ovarian, init = 0)

anova(fit1, fit2, fit3)
#> 
#> weibull(aft) 1: Surv(futime, fustat) ~ 1 
#> weibull(aft) 2: Surv(futime, fustat) ~ rx 
#> weibull(aft) 3: Surv(futime, fustat) ~ rx + ecog.ps 
#> --- 
#>                    loglik        LR df Pr(>Chi)
#> weibull(aft) 1: -97.95390   1.73882  2   0.4192
#> weibull(aft) 2: -97.36415   0.55932  1   0.4545
#> weibull(aft) 3: -97.08449         -  -        -

AIC(fit1, fit2, fit3)
#>    fit      aic npars
#> 1 fit1 199.9078     2
#> 2 fit2 200.7283     3
#> 3 fit3 202.1690     4