Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for survival models? #1060

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Expand Up @@ -114,7 +163,7 @@

#' @export
standard_error.riskRegression <- function(model, ...) {
junk <- utils::capture.output(cs <- stats::coef(model))

Check warning on line 166 in R/methods_survival.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/methods_survival.R,line=166,col=33,[implicit_assignment_linter] Avoid implicit assignments in function calls. For example, instead of `if (x <- 1L) { ... }`, write `x <- 1L; if (x) { ... }`.
.data_frame(
Parameter = .remove_backticks_from_string(as.vector(cs[, 1])),
SE = as.numeric(cs[, "StandardError"])
Expand All @@ -124,7 +173,7 @@

#' @export
p_value.riskRegression <- function(model, ...) {
junk <- utils::capture.output(cs <- stats::coef(model))

Check warning on line 176 in R/methods_survival.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/methods_survival.R,line=176,col=33,[implicit_assignment_linter] Avoid implicit assignments in function calls. For example, instead of `if (x <- 1L) { ... }`, write `x <- 1L; if (x) { ... }`.
.data_frame(
Parameter = .remove_backticks_from_string(as.vector(cs[, 1])),
p = as.numeric(cs[, "Pvalue"])
Expand Down
Loading