Skip to content

Commit

Permalink
Update NEWS, adds make_newdata function + cleans up
Browse files Browse the repository at this point in the history
  • Loading branch information
giabaio committed Oct 3, 2024
1 parent 36c47d9 commit c4dd15f
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 75 deletions.
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: survHE
Title: Survival Analysis in Health Economic Evaluation
Version: 2.0.1
Date: 2023-05-23
Version: 2.0.2
Date: 2024-10-03
Authors@R: c(
person(given = "Gianluca",family = "Baio",role = c("aut", "cre"),email = "[email protected]"),
person("Andrea","Berardi",role="ctb",email="[email protected]"),
Expand All @@ -27,7 +27,7 @@ License: GPL (>=3)
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Biarch: true
Depends:
methods,
Expand All @@ -39,7 +39,8 @@ Imports:
rms,
xlsx,
tools,
tibble
tibble,
tidyr
Suggests:
survHEinla,
survHEhmc,
Expand Down
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ importFrom("utils", "read.table","write.table","head","tail","modifyList")
importFrom("stats", "terms","as.formula","model.frame","model.matrix","dexp",
"pexp","dweibull","pweibull","dgamma","pgamma","median","var","time",
"update","dt","profile","dlnorm","plnorm","dlogis","plogis","sd",
"quantile","qnorm")
"quantile","qnorm","model.matrix.lm")
importFrom("grDevices", "colors","adjustcolor","colorRampPalette")
importFrom("tools", "file_ext")
importFrom("rms", "npsurv","survplot")
importFrom("tibble", "rownames_to_column")
importFrom("tidyr", "unite")
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ geom_step(data = datakm, aes(x = time, y = S, group=as.factor(strata:object_name
aes(x = time, y = S, ymin=lower, ymax=upper, group=as.factor(strata:object_name)),
alpha = 0.2)
```
This means that when plotting two or more `survHE` objects, the KM is added and displayed correctly
This means that when plotting two or more `survHE` objects, the KM is added and displayed correctly

* Adds a utility function `make_newdata` that can be used to generate profiles of covariates, to then plot specific groups of individuals' survival or hazard curves.
40 changes: 5 additions & 35 deletions R/survHE-package.R
Original file line number Diff line number Diff line change
@@ -1,36 +1,6 @@
#' Survival Analysis in Health Economic Evaluation
#'
#' Contains a suite of functions to perform survival analysis with the aim of
#' aiding in health economic modelling (extrapolation, model checking and PSA)
#'
#' \tabular{ll}{ Package: \tab survHE\cr Type: \tab Package\cr Version: \tab
#' 2.0.1\cr Date: \tab 2023-03-10\cr License: \tab GPL2 \cr LazyLoad: \tab
#' yes\cr } Contains a suite of functions to perform survival analysis with the
#' aim of aiding in health economic modelling (extrapolation, model checking
#' and PSA)
#'
#' @name survHE-package
#' @aliases survHE-package
#' @aliases survHE
#' @docType package
#' @author Gianluca Baio
#'
#' Maintainer: Gianluca Baio
#' @template refs
#' @keywords Survival Modelling Health Economic Evaluation
#' @examples
#' \dontrun{
#' # Loads some survival data
#' data(bc)
#' # Fits a parametric model
#' m <- fit.models(formula=Surv(recyrs,censrec)~group,data=bc,
#' distr="exp",method="mle")
#' # Print output in tabular format
#' print(m)
#' # Visualise output in terms of survival curves
#' plot(m)
#' }
NULL


#' @keywords internal
"_PACKAGE"

## usethis namespace: start
## usethis namespace: end
NULL
68 changes: 68 additions & 0 deletions R/utils_plot_survHE.R
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,74 @@ plot_ggplot_survHE <- function(exArgs) {
surv.curv
}

#' Creates a 'newdata' list to modify the plots for specific individual
#' profiles (with respect to the covariates)
#'
#' @param data The original dataset that has been used as input to the call
#' to 'fit.models'
#' @param vars A vector of strings, including the names of the variables that
#' are to be used to construct specific profiles of individual covariates
#' @param conts A subset of 'vars', which include the named covariates that
#' are continuous. These will be averaged over, while for the remaining
#' covariates (assumed to be factors), the specific profiles will be listed.
#' Defaults to NULL
#' @return \item{newdata}{The list 'newdata' to be passed as optional argument
#' to a call to the 'plot' method}
#' @return \item{labs}{A vector of labels (say to use in the plot, for each
#' profile)}
#' @note Something will go here
#' @author Gianluca Baio
#' @keywords Parametric survival models
#' @examples
#' \dontrun{
#' data(bc)
#'
#' # Fits a model using the 'bc' data
#' mle = fit.models(formula=Surv(recyrs,censrec)~group,data=bc,
#' distr="exp",method="mle")
#' # Now makes the default plot
#' plot(mle)
#' # Now creates a 'newdata' list to modify the plot for selected profiles
#' newdata=make_newdata(data=bc,vars="group")
#' # And can plot, say, only two of the three treatment arms
#' plot(mle,newdata=newdata$newdata[c(1,3)],lab.profile=newdata$labs[c(1,3)])
#' }
#'
#' @export make_newdata

make_newdata=function(data,vars,conts=NULL) {
df=
# Takes the original data
data |>
# Selects the relevant variables to construct the profiles
select(all_of(vars)) |>
# Takes the mean for the continuous variables
mutate(across(all_of(conts),~mean(.x))) |>
# Takes only the unique combinations
unique()

# Creates a vector of labels (say to use in the plot, for each profile)
labs=df |>
# Don't need to keep the continuous variables as they've been averaged over
select(-conts) |>
# Uses 'unite' to turn into vectors
tidyr::unite(labs,sep=", ") |>
# NB: needs to remove names in the vector or else won't work in the plot(...)
unlist(use.names=F)

# Creates the list 'newdata' to pass to the plot function
newdata = df |>
# And then transforms into a list of values
(function(.) split(.,f=seq(nrow(.))))()

# Returns the output
list(
newdata=newdata,
labs=labs
)
}



#' Make the dataset to be used by \code{ggplot2} to plot the survival curves
#'
Expand Down
57 changes: 57 additions & 0 deletions man/make_newdata.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 20 additions & 34 deletions man/survHE-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c4dd15f

Please sign in to comment.