[R] Extracting values from Surv function in survival package

2024-05-15 Thread Dennis Fisher
OS X
R 4.3.3

Colleagues

I have created objects using the Surv function in the survival package:
> FIT.1
Call: survfit(formula = FORMULA1)

   n events median 0.95LCL 0.95UCL
SUBDATA$ARM=1, SUBDATA[, EXP.STRAT]=0 18 13345 156  NA
SUBDATA$ARM=2, SUBDATA[, EXP.STRAT]=1 13  5 NA 186  NA
SUBDATA$ARM=2, SUBDATA[, EXP.STRAT]=2  5  5168  81  NA
SUBDATA$ARM=2, SUBDATA[, EXP.STRAT]=3  1  1 22  NA  NA

I am interested in extracting the “n” and “events” values.
“n” is easy:
> FIT.1[[1]]
[1] 18 13  5  1

or
> FIT.1$n
[1] 18 13  5  1

But I can’t figure out how to access “events”.

str(FIT.1) provides no insights:
List of 17
 $ n: int [1:4] 18 13 5 1
 $ time : num [1:37] 45 106 107 124 152 156 170 176 319 371 ...
 $ n.risk   : num [1:37] 18 17 16 15 14 13 12 11 10 9 ...
 $ n.event  : num [1:37] 1 1 1 1 1 1 1 1 1 1 ...
 $ n.censor : num [1:37] 0 0 0 0 0 0 0 0 0 0 ...
 $ surv : num [1:37] 0.944 0.889 0.833 0.778 0.722 ...
 $ std.err  : num [1:37] 0.0572 0.0833 0.1054 0.126 0.1462 ...
 $ cumhaz   : num [1:37] 0.0556 0.1144 0.1769 0.2435 0.315 ...
 $ std.chaz : num [1:37] 0.0556 0.0809 0.1022 0.1221 0.1414 ...
 $ strata   : Named int [1:4] 18 13 5 1
  ..- attr(*, "names")= chr [1:4] "SUBDATA$ARM=1, SUBDATA[, EXP.STRAT]=0" 
"SUBDATA$ARM=2, SUBDATA[, EXP.STRAT]=1" "SUBDATA$ARM=2, SUBDATA[, EXP.STRAT]=2" 
"SUBDATA$ARM=2, SUBDATA[, EXP.STRAT]=3"
 $ type : chr "right"
 $ logse: logi TRUE
 $ conf.int : num 0.95
 $ conf.type: chr "log"
 $ lower: num [1:37] 0.844 0.755 0.678 0.608 0.542 ...
 $ upper: num [1:37] 1 1 1 0.996 0.962 ...
 $ call : language survfit(formula = FORMULA1)
 - attr(*, "class")= chr "survfit"

If I could access:
   n events median 0.95LCL 0.95UCL
SUBDATA$ARM=1, SUBDATA[, EXP.STRAT]=0 18 13345 156  NA
SUBDATA$ARM=2, SUBDATA[, EXP.STRAT]=1 13  5 NA 186  NA
SUBDATA$ARM=2, SUBDATA[, EXP.STRAT]=2  5  5168  81  NA
SUBDATA$ARM=2, SUBDATA[, EXP.STRAT]=3  1  1 22  NA  NA
it should be easy to get “events”.

Any thoughts?

Dennis

Dennis Fisher MD
P < (The "P Less Than" Company)
Phone / Fax: 1-866-PLessThan (1-866-753-7784)
www.PLessThan.com

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] flexsurvspline with offset

2024-05-15 Thread Bonnett, Laura
Dear all,

I am using R 4.4.0 via RStudio (2024.04.0) on a Windows PC.  The code below 
worked on the previous version of the flexsurv but is not working since my 
recent update (version 2.3).

The code that was working is:

library(survival)

rfs  <- pmax(rotterdam$recur, rotterdam$death)
rfstime <- with(rotterdam, ifelse(recur==1, rtime, dtime))
fit1 <- flexsurvspline(Surv(rfstime, rfs) ~ age + meno + size + er,  k=2, data 
= rotterdam)

# obtain the linear predictor for each patient
predf <- model.matrix(fit1) %*% fit1$res[-(1:4),"est"]

# Predict baseline survival at 5 years
y5_Flex_1 <- summary(fit1,type="survival",t=5)
y5_Flex <- y5_Flex_1[[1]]$est
y5_Flex

# Shrinkage factor
vanH <- 0.9589255
heuristic_lp_flex = vanH*predf  

# Now calculate the shrunken model baseline survival prob at 5 years by fixing 
the shrunken lp as an offset and predicting the baseline survival
fit1_shrunk <- 
flexsurvspline(Surv(rfstime,rfs)~offset(heuristic_lp_flex),k=2,data=rotterdam)

The error message is
Error in model.matrix.default(forms[[i]], m) :
  model frame and formula mismatch in model.matrix()

Any suggestions gratefully received!!!

Kind regards,
Laura

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.