Skip to content

Commit

Permalink
Support for survival models? (#1060)
Browse files Browse the repository at this point in the history
Fixes #651
  • Loading branch information
strengejacke authored Jan 7, 2025
1 parent 1fa8be6 commit a4f78d7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: parameters
Title: Processing of Model Parameters
Version: 0.24.0.7
Version: 0.24.0.8
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ S3method(model_parameters,stanfit)
S3method(model_parameters,stanmvreg)
S3method(model_parameters,stanreg)
S3method(model_parameters,summary_emm)
S3method(model_parameters,survfit)
S3method(model_parameters,svy2lme)
S3method(model_parameters,svyglm)
S3method(model_parameters,svytable)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Changes

* `model_parameters()` now supports objects of class `survfit`.

* `model_parameters()` now gives informative error messages for more model
classes than before when the function fails to extract model parameters.

Expand Down
51 changes: 50 additions & 1 deletion R/methods_survival.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,53 @@
# classes: .coxph, .aareg, .survreg, .riskRegression
# classes: .coxph, .aareg, .survreg, .riskRegression, .survfit

#################### .survfit ------

#' @export
model_parameters.survfit <- function(model,
keep = NULL,
drop = NULL,
verbose = TRUE,
...) {
s <- summary(model)
# extract all elements with same length, which occur most in that list
# that is the data we need
uniqv <- unique(lengths(s))
tab <- tabulate(match(lengths(s), uniqv))
idx <- which.max(tab)
most_len <- uniqv[idx]

# convert list into data frame, only for elements of same length
params <- as.data.frame(s[lengths(s) == most_len])

# keep specific columns
keep_columns <- intersect(
c("time", "n.risk", "n.event", "surv", "std.err", "strata", "lower", "upper"),
colnames(params)
)
params <- params[keep_columns]

# rename
params <- datawizard::data_rename(
params,
select = c(
Time = "time", `N Risk` = "n.risk", `N Event` = "n.event", Survival = "surv",
SE = "std.err", Group = "strata", CI_low = "lower", CI_high = "upper"
)
)

# fix labels
params$Group <- gsub("x=", "", params$Group, fixed = TRUE)

# These are integers, need to be character to display without decimals
params$Time <- as.character(params$Time)
params[["N Risk"]] <- as.character(params[["N Risk"]])
params[["N Event"]] <- as.character(params[["N Event"]])

attr(params, "ci") <- s$conf.int
class(params) <- c("parameters_model", "see_parameters_model", class(params))

params
}


#################### .coxph ------
Expand Down

0 comments on commit a4f78d7

Please sign in to comment.