Title: | Simulate and Plot Estimates from Cox Proportional Hazards Models |
---|---|
Description: | Simulates and plots quantities of interest (relative hazards, first differences, and hazard ratios) for linear coefficients, multiplicative interactions, polynomials, penalised splines, and non-proportional hazards, as well as stratified survival curves from Cox Proportional Hazard models. It also simulates and plots marginal effects for multiplicative interactions. Methods described in Gandrud (2015) <doi:10.18637/jss.v065.i03>. |
Authors: | Christopher Gandrud [aut, cre] |
Maintainer: | Christopher Gandrud <[email protected]> |
License: | GPL-3 |
Version: | 1.3.13 |
Built: | 2024-11-02 03:12:36 UTC |
Source: | https://github.com/christophergandrud/simph |
Convert a coxsim class object into a data frame
## S3 method for class 'coxsim' as.data.frame(x, ...)
## S3 method for class 'coxsim' as.data.frame(x, ...)
x |
a |
... |
arguments to pass to |
A data set from Carpenter (2002).
A data set with 408 observations and 32 variables.
Carpenter, Daniel P. 2002. ”Groups, the Media, Agency Waiting Costs, and FDA Drug Approval.” American Journal of Political Science 46(3): 490-505.
Luke Keele, "Replication data for: Proportionally Difficult: Testing for Nonproportional Hazards In Cox Models". doi:10.7910/DVN/VJAHRG. V1 [Version].
coxsimInteract
simulates quantities of interest for linear
multiplicative interactions using multivariate normal distributions.
These can be plotted with simGG
.
coxsimInteract( obj, b1, b2, qi = "Marginal Effect", X1 = NULL, X2 = NULL, means = FALSE, expMarg = TRUE, nsim = 1000, ci = 0.95, spin = FALSE, extremesDrop = TRUE )
coxsimInteract( obj, b1, b2, qi = "Marginal Effect", X1 = NULL, X2 = NULL, means = FALSE, expMarg = TRUE, nsim = 1000, ci = 0.95, spin = FALSE, extremesDrop = TRUE )
obj |
a |
b1 |
character string of the first constitutive variable's name.
Note |
b2 |
character string of the second constitutive variable's name. |
qi |
quantity of interest to simulate. Values can be
|
X1 |
numeric vector of fitted values of |
X2 |
numeric vector of fitted values of |
means |
logical, whether or not to use the mean values to fit the
hazard rate for covaraiates other than |
expMarg |
logical. Whether or not to exponentiate the marginal effect. |
nsim |
the number of simulations to run per value of X. Default is
|
ci |
the proportion of middle simulations to keep. The default is
|
spin |
logical, whether or not to keep only the shortest probability interval rather than the middle simulations. Currently not supported for hazard rates. |
extremesDrop |
logical whether or not to drop simulated quantity of
interest values that are |
Simulates marginal effects, first differences, hazard ratios, and
hazard rates for linear multiplicative interactions.
Marginal effects are calculated as in Brambor et al. (2006) with the
addition that we take the exponent, so that it resembles a hazard ratio.
You can choose not to take the exponent by setting the argument
expMarg = FALSE
. For an interaction between variables and
the marginal effect for
is:
Note that for First Differences the comparison is not between two values of the same variable but two values of the constitute variable and 0 for the two variables.
a siminteract
class object
Gandrud, Christopher. 2015. simPH: An R Package for Illustrating Estimates from Cox Proportional Hazard Models Including for Interactive and Nonlinear Effects. Journal of Statistical Software. 65(3)1-20.
Brambor, Thomas, William Roberts Clark, and Matt Golder. 2006. ”Understanding Interaction Models: Improving Empirical Analyses.” Political Analysis 14(1): 63-82.
King, Gary, Michael Tomz, and Jason Wittenberg. 2000. ”Making the Most of Statistical Analyses: Improving Interpretation and Presentation.” American Journal of Political Science 44(2): 347-61.
Liu, Ying, Andrew Gelman, and Tian Zheng. 2013. ”Simulation-Efficient Shortest Probability Intervals.” Arvix. https://arxiv.org/pdf/1302.2142v1.pdf.
simGG
, survival
, strata
,
and coxph
,
# Load Carpenter (2002) data data("CarpenterFdaData") # Load survival package library(survival) # Run basic model M1 <- coxph(Surv(acttime, censor) ~ lethal*prevgenx, data = CarpenterFdaData) # Simulate Marginal Effect of lethal for multiple # values of prevgenx Sim1 <- coxsimInteract(M1, b1 = "lethal", b2 = "prevgenx", X2 = seq(2, 115, by = 5), spin = TRUE) ## Not run: # Change the order of the covariates to make a more easily # interpretable relative hazard graph. M2 <- coxph(Surv(acttime, censor) ~ prevgenx*lethal + orphdum, data = CarpenterFdaData) # Simulate Hazard Ratio of lethal for multiple # values of prevgenx Sim2 <- coxsimInteract(M2, b1 = "prevgenx", b2 = "lethal", X1 = seq(2, 115, by = 2), X2 = c(0, 1), qi = "Hazard Ratio", ci = 0.9) # Simulate First Difference Sim3 <- coxsimInteract(M2, b1 = "prevgenx", b2 = "lethal", X1 = seq(2, 115, by = 2), X2 = c(0, 1), qi = "First Difference", spin = TRUE) # Simulate Hazard Rate Sim4 <- coxsimInteract(M2, b1 = "prevgenx", b2 = "lethal", X1 = 90, X2 = 1, qi = "Hazard Rate", means = TRUE) ## End(Not run) # Example with a categorical variable # Download data data(hmohiv) # Create category lables hmohiv$drug <- factor(hmohiv$drug, labels = c('not treated', 'treated')) M3 <- coxph(Surv(time,censor) ~ drug*age, data = hmohiv) # Note: Use relevant coefficient name as shown in model summary, e.g. # 'drugtreated'. Sim5 <- coxsimInteract(M3, b1 = "drugtreated", b2 = 'age', X2 = 20:54)
# Load Carpenter (2002) data data("CarpenterFdaData") # Load survival package library(survival) # Run basic model M1 <- coxph(Surv(acttime, censor) ~ lethal*prevgenx, data = CarpenterFdaData) # Simulate Marginal Effect of lethal for multiple # values of prevgenx Sim1 <- coxsimInteract(M1, b1 = "lethal", b2 = "prevgenx", X2 = seq(2, 115, by = 5), spin = TRUE) ## Not run: # Change the order of the covariates to make a more easily # interpretable relative hazard graph. M2 <- coxph(Surv(acttime, censor) ~ prevgenx*lethal + orphdum, data = CarpenterFdaData) # Simulate Hazard Ratio of lethal for multiple # values of prevgenx Sim2 <- coxsimInteract(M2, b1 = "prevgenx", b2 = "lethal", X1 = seq(2, 115, by = 2), X2 = c(0, 1), qi = "Hazard Ratio", ci = 0.9) # Simulate First Difference Sim3 <- coxsimInteract(M2, b1 = "prevgenx", b2 = "lethal", X1 = seq(2, 115, by = 2), X2 = c(0, 1), qi = "First Difference", spin = TRUE) # Simulate Hazard Rate Sim4 <- coxsimInteract(M2, b1 = "prevgenx", b2 = "lethal", X1 = 90, X2 = 1, qi = "Hazard Rate", means = TRUE) ## End(Not run) # Example with a categorical variable # Download data data(hmohiv) # Create category lables hmohiv$drug <- factor(hmohiv$drug, labels = c('not treated', 'treated')) M3 <- coxph(Surv(time,censor) ~ drug*age, data = hmohiv) # Note: Use relevant coefficient name as shown in model summary, e.g. # 'drugtreated'. Sim5 <- coxsimInteract(M3, b1 = "drugtreated", b2 = 'age', X2 = 20:54)
Simulates relative hazards, first differences, hazard ratios,
and hazard rates for linear, non-time interacted covariates from Cox
Proportional Hazard models. These can be plotted with simGG
.
coxsimLinear( obj, b, qi = "Relative Hazard", Xj = NULL, Xl = NULL, means = FALSE, nsim = 1000, ci = 0.95, spin = FALSE, extremesDrop = TRUE )
coxsimLinear( obj, b, qi = "Relative Hazard", Xj = NULL, Xl = NULL, means = FALSE, nsim = 1000, ci = 0.95, spin = FALSE, extremesDrop = TRUE )
obj |
a |
b |
character string name of the coefficient you would like to simulate. |
qi |
quantity of interest to simulate. Values can be
|
Xj |
numeric vector of fitted values for |
Xl |
numeric vector of values to compare |
means |
logical, whether or not to use the mean values to fit the
hazard rate for covaraiates other than |
nsim |
the number of simulations to run per value of X. Default is
|
ci |
the proportion of simulations to keep. The default is
|
spin |
logical, whether or not to keep only the shortest probability interval rather than the middle simulations. Currently not supported for Hazard Rates. |
extremesDrop |
logical whether or not to drop simulated quantity of
interest values that are |
coxsimLinear
simulates relative hazards, first differences, and
hazard ratios for linear covariates that are not interacted with time or
nonlinearly transformed from models estimated with coxph
using
the multivariate normal distribution. These can be plotted with
simGG
.
a simlinear
, coxsim
object
Gandrud, Christopher. 2015. simPH: An R Package for Illustrating Estimates from Cox Proportional Hazard Models Including for Interactive and Nonlinear Effects. Journal of Statistical Software. 65(3)1-20.
Licht, Amanda A. 2011. ”Change Comes with Time: Substantive Interpretation of Nonproportional Hazards in Event History Analysis.” Political Analysis 19: 227-43.
King, Gary, Michael Tomz, and Jason Wittenberg. 2000. ”Making the Most of Statistical Analyses: Improving Interpretation and Presentation.” American Journal of Political Science 44(2): 347-61.
Liu, Ying, Andrew Gelman, and Tian Zheng. 2013. ”Simulation-Efficient Shortest Probability Intervals.” Arvix. https://arxiv.org/pdf/1302.2142v1.pdf.
simGG.simlinear
, survival
,
strata
, and coxph
# Load Carpenter (2002) data data("CarpenterFdaData") # Load survival package library(survival) # Run basic model M1 <- coxph(Surv(acttime, censor) ~ prevgenx + lethal + deathrt1 + acutediz + hosp01 + hhosleng + mandiz01 + femdiz01 + peddiz01 + orphdum + vandavg3 + wpnoavg3 + condavg3 + orderent + stafcder, data = CarpenterFdaData) # Simulate Hazard Ratios Sim1 <- coxsimLinear(M1, b = "stafcder", Xj = c(1237, 1600), Xl = c(1000, 1000), qi = "Hazard Ratio", spin = TRUE, ci = 0.99) ## Not run: # Simulate Hazard Rates Sim2 <- coxsimLinear(M1, b = "stafcder", Xj = 1237, ci = 0.99) ## End(Not run)
# Load Carpenter (2002) data data("CarpenterFdaData") # Load survival package library(survival) # Run basic model M1 <- coxph(Surv(acttime, censor) ~ prevgenx + lethal + deathrt1 + acutediz + hosp01 + hhosleng + mandiz01 + femdiz01 + peddiz01 + orphdum + vandavg3 + wpnoavg3 + condavg3 + orderent + stafcder, data = CarpenterFdaData) # Simulate Hazard Ratios Sim1 <- coxsimLinear(M1, b = "stafcder", Xj = c(1237, 1600), Xl = c(1000, 1000), qi = "Hazard Ratio", spin = TRUE, ci = 0.99) ## Not run: # Simulate Hazard Rates Sim2 <- coxsimLinear(M1, b = "stafcder", Xj = 1237, ci = 0.99) ## End(Not run)
coxsimPoly
simulates quantities of interest for polynomial covariate
effects estimated from Cox Proportional Hazards models. These can be plotted
with simGG
.
coxsimPoly( obj, b = NULL, qi = "Relative Hazard", pow = 2, Xj = NULL, Xl = NULL, nsim = 1000, ci = 0.95, spin = FALSE, extremesDrop = TRUE )
coxsimPoly( obj, b = NULL, qi = "Relative Hazard", pow = 2, Xj = NULL, Xl = NULL, nsim = 1000, ci = 0.95, spin = FALSE, extremesDrop = TRUE )
obj |
a |
b |
character string name of the coefficient you would like to simulate.
To find the quantity of interest using only the polynomial and not the
polynomial + the linear terms enter the polynomial created using
|
qi |
quantity of interest to simulate. Values can be
|
pow |
numeric polynomial used in |
Xj |
numeric vector of fitted values for |
Xl |
numeric vector of values to compare |
nsim |
the number of simulations to run per value of |
ci |
the proportion of simulations to keep. The default is
|
spin |
logical, whether or not to keep only the shortest probability interval rather than the middle simulations. Currently not supported for hazard rates. |
extremesDrop |
logical whether or not to drop simulated quantity of
interest values that are |
Simulates quantities of interest for polynomial covariate effects.
For example if a nonlinear effect is modeled with a second order
polynomial–i.e. –we can draw
simulations from the
multivariate normal distribution for both
and
. Then we simply calculate quantities of interest
for a range of values and plot the results as before. For example, we find
the first difference for a second order polynomial with:
where .
Note, you must use I
to create the polynomials.
a simpoly
, coxsim
object
Gandrud, Christopher. 2015. simPH: An R Package for Illustrating Estimates from Cox Proportional Hazard Models Including for Interactive and Nonlinear Effects. Journal of Statistical Software. 65(3)1-20.
Keele, Luke. 2010. ”Proportionally Difficult: Testing for Nonproportional Hazards in Cox Models.” Political Analysis 18(2): 189-205.
Carpenter, Daniel P. 2002. ”Groups, the Media, Agency Waiting Costs, and FDA Drug Approval.” American Journal of Political Science 46(3): 490-505.
King, Gary, Michael Tomz, and Jason Wittenberg. 2000. ”Making the Most of Statistical Analyses: Improving Interpretation and Presentation.” American Journal of Political Science 44(2): 347-61.
Liu, Ying, Andrew Gelman, and Tian Zheng. 2013. ”Simulation-Efficient Shortest Probability Intervals.” Arvix. https://arxiv.org/pdf/1302.2142v1.pdf.
simGG.simpoly
, survival
,
strata
, and coxph
# Load Carpenter (2002) data data("CarpenterFdaData") # Load survival package library(survival) # Run basic model M1 <- coxph(Surv(acttime, censor) ~ prevgenx + lethal + deathrt1 + acutediz + hosp01 + hhosleng + mandiz01 + femdiz01 + peddiz01 + orphdum + natreg + I(natreg^2) + I(natreg^3) + vandavg3 + wpnoavg3 + condavg3 + orderent + stafcder, data = CarpenterFdaData) # Simulate simpoly First Difference Sim1 <- coxsimPoly(M1, b = "natreg", qi = "First Difference", pow = 3, Xj = seq(1, 150, by = 5), nsim = 100) ## Not run: # Simulate simpoly Hazard Ratio with spin probibility interval Sim2 <- coxsimPoly(M1, b = "natreg", qi = "Hazard Ratio", pow = 3, Xj = seq(1, 150, by = 5), spin = TRUE) ## End(Not run)
# Load Carpenter (2002) data data("CarpenterFdaData") # Load survival package library(survival) # Run basic model M1 <- coxph(Surv(acttime, censor) ~ prevgenx + lethal + deathrt1 + acutediz + hosp01 + hhosleng + mandiz01 + femdiz01 + peddiz01 + orphdum + natreg + I(natreg^2) + I(natreg^3) + vandavg3 + wpnoavg3 + condavg3 + orderent + stafcder, data = CarpenterFdaData) # Simulate simpoly First Difference Sim1 <- coxsimPoly(M1, b = "natreg", qi = "First Difference", pow = 3, Xj = seq(1, 150, by = 5), nsim = 100) ## Not run: # Simulate simpoly Hazard Ratio with spin probibility interval Sim2 <- coxsimPoly(M1, b = "natreg", qi = "Hazard Ratio", pow = 3, Xj = seq(1, 150, by = 5), spin = TRUE) ## End(Not run)
coxsimSpline
simulates quantities of interest from penalized splines
using multivariate normal distributions.
coxsimSpline( obj, bspline, bdata, qi = "Relative Hazard", Xj = 1, Xl = 0, nsim = 1000, ci = 0.95, spin = FALSE, extremesDrop = TRUE )
coxsimSpline( obj, bspline, bdata, qi = "Relative Hazard", Xj = 1, Xl = 0, nsim = 1000, ci = 0.95, spin = FALSE, extremesDrop = TRUE )
obj |
a |
bspline |
a character string of the full |
bdata |
a numeric vector of the splined variable's values. |
qi |
quantity of interest to simulate. Values can be
|
Xj |
numeric vector of fitted values for |
Xl |
numeric vector of values to compare |
nsim |
the number of simulations to run per value of |
ci |
the proportion of simulations to keep. The default is
|
spin |
logical, whether or not to keep only the shortest probability interval rather than the middle simulations. Currently not supported for hazard rates. |
extremesDrop |
logical whether or not to drop simulated quantity of
interest values that are |
Simulates relative hazards, first differences, hazard ratios, and
hazard rates for penalized splines from Cox Proportional Hazards models.
These can be plotted with simGG
.
A Cox PH model with one penalized spline is given by:
where is the penalized spline function. For our post-estimation
purposes
is basically a series of linearly combined coefficients
such that:
where are the equally spaced spline knots with values inside of the
range of observed
and
is the number of knots.
We can again draw values of each
from the multivariate normal distribution
described above. We then use these simulated coefficients to estimates
quantities of interest for a range covariate values. For example, the first
difference between two values
and
is:
FD(h[i](t)) = (exp(g(x[j]) - g(x[l])) - 1) * 100
Relative hazards and hazard ratios can be calculated by extension.
Currently coxsimSpline
does not support simulating hazard rates form
multiple stratified models.
a simspline
object
Gandrud, Christopher. 2015. simPH: An R Package for Illustrating Estimates from Cox Proportional Hazard Models Including for Interactive and Nonlinear Effects. Journal of Statistical Software. 65(3)1-20.
Luke Keele, "Replication data for: Proportionally Difficult: Testing for Nonproportional Hazards In Cox Models", 2010, doi:10.7910/DVN/VJAHRG V1 [Version].
King, Gary, Michael Tomz, and Jason Wittenberg. 2000. ”Making the Most of Statistical Analyses: Improving Interpretation and Presentation.” American Journal of Political Science 44(2): 347-61.
Liu, Ying, Andrew Gelman, and Tian Zheng. 2013. ”Simulation-Efficient Shortest Probability Intervals.” Arvix. https://arxiv.org/pdf/1302.2142v1.pdf.
simGG
, survival
, strata
,
and coxph
# Load Carpenter (2002) data data("CarpenterFdaData") # Load survival package library(survival) # Run basic model # From Keele (2010) replication data M1 <- coxph(Surv(acttime, censor) ~ prevgenx + lethal + deathrt1 + acutediz + hosp01 + pspline(hospdisc, df = 4) + pspline(hhosleng, df = 4) + mandiz01 + femdiz01 + peddiz01 + orphdum + natreg + vandavg3 + wpnoavg3 + pspline(condavg3, df = 4) + pspline(orderent, df = 4) + pspline(stafcder, df = 4), data = CarpenterFdaData) ## Not run: # Simulate Relative Hazards for orderent Sim1 <- coxsimSpline(M1, bspline = "pspline(stafcder, df = 4)", bdata = CarpenterFdaData$stafcder, qi = "Hazard Ratio", Xj = seq(1100, 1700, by = 10), Xl = seq(1099, 1699, by = 10), spin = TRUE) ## End(Not run) # Simulate Hazard Rates for orderent Sim2 <- coxsimSpline(M1, bspline = "pspline(orderent, df = 4)", bdata = CarpenterFdaData$orderent, qi = "Hazard Rate", Xj = seq(2, 53, by = 3), nsim = 100)
# Load Carpenter (2002) data data("CarpenterFdaData") # Load survival package library(survival) # Run basic model # From Keele (2010) replication data M1 <- coxph(Surv(acttime, censor) ~ prevgenx + lethal + deathrt1 + acutediz + hosp01 + pspline(hospdisc, df = 4) + pspline(hhosleng, df = 4) + mandiz01 + femdiz01 + peddiz01 + orphdum + natreg + vandavg3 + wpnoavg3 + pspline(condavg3, df = 4) + pspline(orderent, df = 4) + pspline(stafcder, df = 4), data = CarpenterFdaData) ## Not run: # Simulate Relative Hazards for orderent Sim1 <- coxsimSpline(M1, bspline = "pspline(stafcder, df = 4)", bdata = CarpenterFdaData$stafcder, qi = "Hazard Ratio", Xj = seq(1100, 1700, by = 10), Xl = seq(1099, 1699, by = 10), spin = TRUE) ## End(Not run) # Simulate Hazard Rates for orderent Sim2 <- coxsimSpline(M1, bspline = "pspline(orderent, df = 4)", bdata = CarpenterFdaData$orderent, qi = "Hazard Rate", Xj = seq(2, 53, by = 3), nsim = 100)
coxsimtvc
simulates time-interactive relative hazards, first
differences, and hazard ratios from models estimated with coxph
using the multivariate normal distribution. These can be plotted with
simGG
.
coxsimtvc( obj, b, btvc, qi = "Relative Hazard", Xj = NULL, Xl = NULL, tfun = "linear", pow = NULL, nsim = 1000, from, to, by = 1, ci = 0.95, spin = FALSE, extremesDrop = TRUE )
coxsimtvc( obj, b, btvc, qi = "Relative Hazard", Xj = NULL, Xl = NULL, tfun = "linear", pow = NULL, nsim = 1000, from, to, by = 1, ci = 0.95, spin = FALSE, extremesDrop = TRUE )
obj |
a |
b |
the non-time interacted variable's name. |
btvc |
the time interacted variable's name. |
qi |
character string indicating what quantity of interest you would
like to calculate. Can be |
Xj |
numeric vector of fitted values for |
Xl |
numeric vector of fitted values for Xl. Must be the same length as
|
tfun |
function of time that btvc was multiplied by. Default is
"linear". It can also be "log" (natural log) and "power". If
|
pow |
if |
nsim |
the number of simulations to run per point in time. Default is
|
from |
point in time from when to begin simulating coefficient values |
to |
point in time to stop simulating coefficient values. |
by |
time intervals by which to simulate coefficient values. |
ci |
the proportion of simulations to keep. The default is
|
spin |
logical, whether or not to keep only the shortest probability interval rather than the middle simulations. Currently not supported for hazard rates. |
extremesDrop |
logical whether or not to drop simulated quantity of
interest values that are |
Simulates time-varying relative hazards, first differences, and
hazard ratios using parameter estimates from coxph
models. Can also
simulate hazard rates for multiple strata.
Relative hazards are found using:
where is the function of time.
First differences are found using:
where and
are some values of
to
contrast.
Hazard ratios are calculated using:
When simulating non-stratifed time-varying harzards coxsimtvc
uses
the point estimates for a given coefficient and its time interaction
along with the variance matrix (
)
estimated from a
coxph
model. These are used to draw values of
and
from the
multivariate normal distribution
.
When simulating stratified time-varying hazard rates for a given
strata
,
coxsimtvc
uses:
The resulting simulation values can be plotted using simGG
.
a simtvc
object
Gandrud, Christopher. 2015. simPH: An R Package for Illustrating Estimates from Cox Proportional Hazard Models Including for Interactive and Nonlinear Effects. Journal of Statistical Software. 65(3)1-20.
Golub, Jonathan, and Bernard Steunenberg. 2007. ”How Time Affects EU Decision-Making.” European Union Politics 8(4): 555-66.
Licht, Amanda A. 2011. ”Change Comes with Time: Substantive Interpretation of Nonproportional Hazards in Event History Analysis.” Political Analysis 19: 227-43.
King, Gary, Michael Tomz, and Jason Wittenberg. 2000. ”Making the Most of Statistical Analyses: Improving Interpretation and Presentation.” American Journal of Political Science 44(2): 347-61.
Liu, Ying, Andrew Gelman, and Tian Zheng. 2013. ”Simulation-Efficient Shortest Probability Intervals.” Arvix. https://arxiv.org/pdf/1302.2142v1.pdf.
simGG
, survival
, strata
,
and coxph
## Not run: # Load Golub & Steunenberg (2007) Data data("GolubEUPData") # Load survival package library(survival) # Expand data (not run to speed processing time, but should be run) GolubEUPData <- SurvExpand(GolubEUPData, GroupVar = 'caseno', Time = 'begin', Time2 = 'end', event = 'event') # Create time interactions BaseVars <- c('qmv', 'backlog', 'coop', 'codec', 'qmvpostsea', 'thatcher') GolubEUPData <- tvc(GolubEUPData, b = BaseVars, tvar = 'end', tfun = 'log') # Run Cox PH Model M1 <- coxph(Surv(begin, end, event) ~ qmv + qmvpostsea + qmvpostteu + coop + codec + eu9 + eu10 + eu12 + eu15 + thatcher + agenda + backlog + qmv_log + qmvpostsea_log + coop_log + codec_log + thatcher_log + backlog_log, data = GolubEUPData, ties = "efron") # Create simtvc object for Relative Hazard Sim1 <- coxsimtvc(obj = M1, b = "qmv", btvc = "qmv_log", tfun = "log", from = 80, to = 2000, Xj = 1, by = 15, ci = 0.99, nsim = 100) # Create simtvc object for First Difference Sim2 <- coxsimtvc(obj = M1, b = "qmv", btvc = "qmv_log", qi = "First Difference", Xj = 1, tfun = "log", from = 80, to = 2000, by = 15, ci = 0.95) # Create simtvc object for Hazard Ratio Sim3 <- coxsimtvc(obj = M1, b = "backlog", btvc = "backlog_log", qi = "Hazard Ratio", Xj = c(191, 229), Xl = c(0, 0), tfun = "log", from = 80, to = 2000, by = 15, ci = 0.5) ## End(Not run)
## Not run: # Load Golub & Steunenberg (2007) Data data("GolubEUPData") # Load survival package library(survival) # Expand data (not run to speed processing time, but should be run) GolubEUPData <- SurvExpand(GolubEUPData, GroupVar = 'caseno', Time = 'begin', Time2 = 'end', event = 'event') # Create time interactions BaseVars <- c('qmv', 'backlog', 'coop', 'codec', 'qmvpostsea', 'thatcher') GolubEUPData <- tvc(GolubEUPData, b = BaseVars, tvar = 'end', tfun = 'log') # Run Cox PH Model M1 <- coxph(Surv(begin, end, event) ~ qmv + qmvpostsea + qmvpostteu + coop + codec + eu9 + eu10 + eu12 + eu15 + thatcher + agenda + backlog + qmv_log + qmvpostsea_log + coop_log + codec_log + thatcher_log + backlog_log, data = GolubEUPData, ties = "efron") # Create simtvc object for Relative Hazard Sim1 <- coxsimtvc(obj = M1, b = "qmv", btvc = "qmv_log", tfun = "log", from = 80, to = 2000, Xj = 1, by = 15, ci = 0.99, nsim = 100) # Create simtvc object for First Difference Sim2 <- coxsimtvc(obj = M1, b = "qmv", btvc = "qmv_log", qi = "First Difference", Xj = 1, tfun = "log", from = 80, to = 2000, by = 15, ci = 0.95) # Create simtvc object for Hazard Ratio Sim3 <- coxsimtvc(obj = M1, b = "backlog", btvc = "backlog_log", qi = "Hazard Ratio", Xj = c(191, 229), Xl = c(0, 0), tfun = "log", from = 80, to = 2000, by = 15, ci = 0.5) ## End(Not run)
This function largely improves plot.survfit
. It
plots the curves using ggplot2 rather than base R graphics. One major
advantage is the ability to split the survival curves into multiple plots and
arrange them in a grid. This makes it easier to examine many strata at once.
Otherwise they can be very bunched up.
Note: the strata legend labels need to be changed manually (see
revalue
) in the survfit
object with the strata
component.
ggfitStrata( obj, byStrata = FALSE, xlab = "", ylab = "", title = "", lcolour = "#2C7FB8", rcolour = "#2C7FB8" )
ggfitStrata( obj, byStrata = FALSE, xlab = "", ylab = "", title = "", lcolour = "#2C7FB8", rcolour = "#2C7FB8" )
obj |
a |
byStrata |
logical, whether or not you want to include all of the stratified survival curves on one plot or separate them into a grid arranged plot. |
xlab |
a label for the plot's x-axis |
ylab |
a label of the plot's y-axis |
title |
plot title. |
lcolour |
line color. Currently only works if |
rcolour |
confidence bounds ribbon color. Either a single color or a
vector of colours. The default it |
ggfitStrata
graphs fitted survival curves created with
survfit
using ggplot2.
# Load packages library(survival) library(simPH) # Subset data bladder1 <- bladder[bladder$enum < 5, ] # Estimate coxph model (note that this model is for code illustration only) M1 <- coxph(Surv(stop, event) ~ (rx + size + number) + strata(enum) + cluster(id), bladder1) # Find predicted values M1Fit <- survfit(M1) # Plot strata in a grid ggfitStrata(M1Fit, byStrata = TRUE) # Plot all in one ggfitStrata(M1Fit, byStrata = FALSE)
# Load packages library(survival) library(simPH) # Subset data bladder1 <- bladder[bladder$enum < 5, ] # Estimate coxph model (note that this model is for code illustration only) M1 <- coxph(Surv(stop, event) ~ (rx + size + number) + strata(enum) + cluster(id), bladder1) # Find predicted values M1Fit <- survfit(M1) # Plot strata in a grid ggfitStrata(M1Fit, byStrata = TRUE) # Plot all in one ggfitStrata(M1Fit, byStrata = FALSE)
A data set from Golub & Steunenberg (2007)
A data set with 3001 observations and 17 variables.
Golub, Jonathan, and Bernard Steunenberg. 2007. ”How Time Affects EU Decision-Making.” European Union Politics 8(4): 555-66.
Amanda A. Licht, 2011, "Replication data for: Change Comes with Time". doi:10.7910/DVN/VJAHRG. IQSS Dataverse Network [Distributor] V3 [Version].
Simulated HIV patient data from UCLA IDRE
hmohiv
hmohiv
An object of class data.frame
with 100 rows and 7 columns.
UCLA IDRE https://stats.idre.ucla.edu/r/examples/asa/r-applied-survival-analysis-ch-1/
MinMaxLines
is an internal function to transform the simulation
object to include only the min and max of the intervals set by ci
in
the coxsim
command, as well as the lower and upper bounds of the
middle 50 percent of these intervals. It also returns the medians.
MinMaxLines(df, byVars = "Xj", hr = FALSE, strata = FALSE, clean = FALSE)
MinMaxLines(df, byVars = "Xj", hr = FALSE, strata = FALSE, clean = FALSE)
df |
a data frame or a simulation class object. |
byVars |
character vector of the variables to subset the data frame by.
The default is |
hr |
logical indicating whether or not |
strata |
logical indicating whether or not |
clean |
logical, whether or not to clean up the output data frame to
only include |
# Load Carpenter (2002) data data("CarpenterFdaData") # Load survival package library(survival) # Run basic model M1 <- coxph(Surv(acttime, censor) ~ prevgenx + lethal + deathrt1 + acutediz + hosp01 + hhosleng + mandiz01 + femdiz01 + peddiz01 + orphdum + vandavg3 + wpnoavg3 + condavg3 + orderent + stafcder, data = CarpenterFdaData) # Simulate Hazard Ratios Sim1 <- coxsimLinear(M1, b = "stafcder", Xj = c(1237, 1600), Xl = c(1000, 1000), qi = "Hazard Ratio", spin = TRUE, ci = 0.99) # Find summary statistics of the constricted interval Sum <- MinMaxLines(Sim1, clean = TRUE)
# Load Carpenter (2002) data data("CarpenterFdaData") # Load survival package library(survival) # Run basic model M1 <- coxph(Surv(acttime, censor) ~ prevgenx + lethal + deathrt1 + acutediz + hosp01 + hhosleng + mandiz01 + femdiz01 + peddiz01 + orphdum + vandavg3 + wpnoavg3 + condavg3 + orderent + stafcder, data = CarpenterFdaData) # Simulate Hazard Ratios Sim1 <- coxsimLinear(M1, b = "stafcder", Xj = c(1237, 1600), Xl = c(1000, 1000), qi = "Hazard Ratio", spin = TRUE, ci = 0.99) # Find summary statistics of the constricted interval Sum <- MinMaxLines(Sim1, clean = TRUE)
setXl
creates a sequence of Xl
values given a sequence of
Xj
values and a fixed difference.
setXl(Xj, diff = 1)
setXl(Xj, diff = 1)
Xj |
numeric vector of fitted values for the covariate of interest to simulate for. |
diff |
numeric vector of length 1. It specifies the difference between
|
a vector
# Set Xj setXj = seq(1100, 1700, by = 10) # Find Xl that are 1 less than Xj setXl(Xj = setXj, diff = 1)
# Set Xj setXj = seq(1100, 1700, by = 10) # Find Xl that are 1 less than Xj setXl(Xj = setXj, diff = 1)
simGG
a method for ploting simulation objects created by simPH.
simGG(obj, ...)
simGG(obj, ...)
obj |
an object created by one of simPH's simulation commands. |
... |
arguments to be passed to methods. |
Gandrud, Christopher. 2015. simPH: An R Package for Illustrating Estimates from Cox Proportional Hazard Models Including for Interactive and Nonlinear Effects. Journal of Statistical Software. 65(3)1-20.
simGG.siminteract
, simGG.simtvc
,
simGG.simlinear
, simGG.simpoly
,
simGG.simspline
## Not run: # Load Carpenter (2002) data data("CarpenterFdaData") # Load survival package library(survival) # Run basic model M1 <- coxph(Surv(acttime, censor) ~ lethal*prevgenx, data = CarpenterFdaData) # Simulate Marginal Effect of lethal for multiple # values of prevgenx Sim1 <- coxsimInteract(M1, b1 = "lethal", b2 = "prevgenx", X2 = seq(2, 115, by = 5), spin = TRUE) # Plot simulations simGG(Sim1) ## End(Not run)
## Not run: # Load Carpenter (2002) data data("CarpenterFdaData") # Load survival package library(survival) # Run basic model M1 <- coxph(Surv(acttime, censor) ~ lethal*prevgenx, data = CarpenterFdaData) # Simulate Marginal Effect of lethal for multiple # values of prevgenx Sim1 <- coxsimInteract(M1, b1 = "lethal", b2 = "prevgenx", X2 = seq(2, 115, by = 5), spin = TRUE) # Plot simulations simGG(Sim1) ## End(Not run)
simGG.siminteract
uses ggplot2 to plot the quantities of
interest from siminteract
objects, including marginal effects, first
differences, hazard ratios, and hazard rates.
## S3 method for class 'siminteract' simGG( obj, from = NULL, rug = TRUE, rug_position = "identity", to = NULL, xlab = NULL, ylab = NULL, title = NULL, method = "auto", spalette = "Set1", legend = "legend", leg.name = "", lcolour = "#2B8CBE", lsize = 1, pcolour = "#A6CEE3", psize = 1, alpha = 0.2, type = "ribbons", ... )
## S3 method for class 'siminteract' simGG( obj, from = NULL, rug = TRUE, rug_position = "identity", to = NULL, xlab = NULL, ylab = NULL, title = NULL, method = "auto", spalette = "Set1", legend = "legend", leg.name = "", lcolour = "#2B8CBE", lsize = 1, pcolour = "#A6CEE3", psize = 1, alpha = 0.2, type = "ribbons", ... )
obj |
a |
from |
numeric time to start the plot from. Only relevant if
|
rug |
logical indicating whether or not to include a rug plot showing
the distribution of values in the sample used to estimate the |
rug_position |
character string. The position adjustment to use for
overlapping points in the rug plot. Use |
to |
numeric time to plot to. Only relevant if
|
xlab |
a label for the plot's x-axis. |
ylab |
a label of the plot's y-axis. The default uses the value of
|
title |
the plot's main title. |
method |
what type of smoothing method to use to summarize the center of the simulation distribution. |
spalette |
colour palette for when there are multiple sets of
comparisons to plot. Not relevant for |
legend |
specifies what type of legend to include (if applicable). The
default is |
leg.name |
name of the legend (if applicable). |
lcolour |
character string colour of the smoothing line. The default is
hexadecimal colour |
lsize |
size of the smoothing line. Default is 1. See
|
pcolour |
character string colour of the simulated points or ribbons
(when there are not multiple sets of simulations). Default is hexadecimal
colour |
psize |
size of the plotted simulation points. Default is
|
alpha |
numeric. Alpha (e.g. transparency) for the points, lines, or
ribbons. Default is |
type |
character string. Specifies how to plot the simulations. Can be
|
... |
Additional arguments. (Currently ignored.) |
Uses ggplot2 to plot the quantities of interest from
siminteract
objects, including marginal effects, first differences,
hazard ratios, and hazard rates. If there are multiple strata, the quantities
of interest will be plotted in a grid by strata.
Note: A dotted line is created at y = 1 (0 for first difference), i.e. no effect, for time-varying hazard ratio graphs. No line is created for hazard rates.
Note: if qi = "Hazard Ratio"
or qi = "First Difference"
then
you need to have choosen more than one fitted value for X1
in
coxsimInteract
.
a gg
ggplot
class object
Gandrud, Christopher. 2015. simPH: An R Package for Illustrating Estimates from Cox Proportional Hazard Models Including for Interactive and Nonlinear Effects. Journal of Statistical Software. 65(3)1-20.
Brambor, Thomas, William Roberts Clark, and Matt Golder. 2006. ”Understanding Interaction Models: Improving Empirical Analyses.” Political Analysis 14(1): 63-82.
Keele, Luke. 2010. ”Proportionally Difficult: Testing for Nonproportional Hazards in Cox Models.” Political Analysis 18(2): 189-205.
Carpenter, Daniel P. 2002. ”Groups, the Media, Agency Waiting Costs, and FDA Drug Approval.” American Journal of Political Science 46(3): 490-505.
coxsimInteract
, simGG.simlinear
,
and ggplot2
# Load Carpenter (2002) data data("CarpenterFdaData") # Load survival package library(survival) # Run basic model M1 <- coxph(Surv(acttime, censor) ~ lethal*prevgenx, data = CarpenterFdaData) # Simulate Marginal Effect of lethal for multiple values of prevgenx Sim1 <- coxsimInteract(M1, b1 = "lethal", b2 = "prevgenx", X2 = seq(2, 115, by = 2), nsim = 100) # Plot quantities of interest simGG(Sim1) simGG(Sim1, rug_position = 'jitter') ## Not run: # Change the order of the covariates to make a more easily # interpretable hazard ratio graph. M2 <- coxph(Surv(acttime, censor) ~ prevgenx*lethal, data = CarpenterFdaData) # Simulate Hazard Ratio of lethal for multiple values of prevgenx Sim2 <- coxsimInteract(M2, b1 = "prevgenx", b2 = "lethal", X1 = seq(2, 115, by = 2), X2 = c(0, 1), qi = "Hazard Ratio", ci = 0.9) # Simulate First Difference Sim3 <- coxsimInteract(M2, b1 = "prevgenx", b2 = "lethal", X1 = seq(2, 115, by = 2), X2 = c(0, 1), qi = "First Difference", spin = TRUE) # Simulate Hazard Rate Sim4 <- coxsimInteract(M2, b1 = "prevgenx", b2 = "lethal", X1 = 100, X2 = 1, qi = "Hazard Rate") # Plot quantities of interest simGG(Sim1, xlab = "\nprevgenx", ylab = "Marginal Effect of lethal\n") simGG(Sim2, type = 'ribbons', rug_position = 'jitter') simGG(Sim3) simGG(Sim4, to = 150, type = 'lines', legend = FALSE) ## End(Not run)
# Load Carpenter (2002) data data("CarpenterFdaData") # Load survival package library(survival) # Run basic model M1 <- coxph(Surv(acttime, censor) ~ lethal*prevgenx, data = CarpenterFdaData) # Simulate Marginal Effect of lethal for multiple values of prevgenx Sim1 <- coxsimInteract(M1, b1 = "lethal", b2 = "prevgenx", X2 = seq(2, 115, by = 2), nsim = 100) # Plot quantities of interest simGG(Sim1) simGG(Sim1, rug_position = 'jitter') ## Not run: # Change the order of the covariates to make a more easily # interpretable hazard ratio graph. M2 <- coxph(Surv(acttime, censor) ~ prevgenx*lethal, data = CarpenterFdaData) # Simulate Hazard Ratio of lethal for multiple values of prevgenx Sim2 <- coxsimInteract(M2, b1 = "prevgenx", b2 = "lethal", X1 = seq(2, 115, by = 2), X2 = c(0, 1), qi = "Hazard Ratio", ci = 0.9) # Simulate First Difference Sim3 <- coxsimInteract(M2, b1 = "prevgenx", b2 = "lethal", X1 = seq(2, 115, by = 2), X2 = c(0, 1), qi = "First Difference", spin = TRUE) # Simulate Hazard Rate Sim4 <- coxsimInteract(M2, b1 = "prevgenx", b2 = "lethal", X1 = 100, X2 = 1, qi = "Hazard Rate") # Plot quantities of interest simGG(Sim1, xlab = "\nprevgenx", ylab = "Marginal Effect of lethal\n") simGG(Sim2, type = 'ribbons', rug_position = 'jitter') simGG(Sim3) simGG(Sim4, to = 150, type = 'lines', legend = FALSE) ## End(Not run)
simGG.simlinear
uses ggplot2 to plot the quantities of interest
from simlinear
objects, including relative hazards, first
differences, hazard ratios, and hazard rates.
## S3 method for class 'simlinear' simGG( obj, from = NULL, to = NULL, rug = TRUE, rug_position = "identity", xlab = NULL, ylab = NULL, title = NULL, method = "auto", spalette = "Set1", legend = "legend", leg.name = "", lcolour = "#2B8CBE", lsize = 1, pcolour = "#A6CEE3", psize = 1, alpha = 0.2, type = "ribbons", ... )
## S3 method for class 'simlinear' simGG( obj, from = NULL, to = NULL, rug = TRUE, rug_position = "identity", xlab = NULL, ylab = NULL, title = NULL, method = "auto", spalette = "Set1", legend = "legend", leg.name = "", lcolour = "#2B8CBE", lsize = 1, pcolour = "#A6CEE3", psize = 1, alpha = 0.2, type = "ribbons", ... )
obj |
a |
from |
numeric time to start the plot from. Only relevant if
|
to |
numeric time to plot to. Only relevant if
|
rug |
logical indicating whether or not to include a rug plot showing
the distribution of values in the sample used to estimate the |
rug_position |
character string. The position adjustment to use for
overlapping points in the rug plot. Use |
xlab |
a label for the plot's x-axis. |
ylab |
a label of the plot's y-axis. The default uses the value of
|
title |
the plot's main title. |
method |
what type of smoothing method to use to summarize the center of the simulation distribution. |
spalette |
colour palette for when there are multiple sets of
comparisons to plot. Default palette is |
legend |
specifies what type of legend to include (if applicable).
The default is |
leg.name |
name of the legend (if applicable). |
lcolour |
character string colour of the smoothing line. The default is
hexadecimal colour |
lsize |
size of the smoothing line. Default is 1. See
|
pcolour |
character string colour of the simulated points or ribbons
(when there are not multiple sets of simulations). Default is hexadecimal
colour |
psize |
size of the plotted simulation points. Default is
|
alpha |
numeric. Alpha (e.g. transparency) for the points, lines, or
ribbons. Default is |
type |
character string. Specifies how to plot the simulations. Can be
|
... |
Additional arguments. (Currently ignored.) |
Uses ggplot2 to plot the quantities of interest from
simlinear
objects, including relative hazards, first differences,
hazard ratios, and hazard rates. If there are multiple strata, the
quantities of interest will be plotted in a grid by strata.
Note: A dotted line is created at y = 1 (0 for first difference), i.e. no
effect, for time-varying hazard ratio graphs. No line is created for hazard
rates.
a gg
ggplot
class object
Gandrud, Christopher. 2015. simPH: An R Package for Illustrating Estimates from Cox Proportional Hazard Models Including for Interactive and Nonlinear Effects. Journal of Statistical Software. 65(3)1-20.
Licht, Amanda A. 2011. ”Change Comes with Time: Substantive Interpretation of Nonproportional Hazards in Event History Analysis.” Political Analysis 19: 227-43.
Keele, Luke. 2010. ”Proportionally Difficult: Testing for Nonproportional Hazards in Cox Models.” Political Analysis 18(2): 189-205.
Carpenter, Daniel P. 2002. ”Groups, the Media, Agency Waiting Costs, and FDA Drug Approval.” American Journal of Political Science 46(3): 490-505.
coxsimLinear
, simGG.simtvc
, and
ggplot2
# Load survival package library(survival) # Load Carpenter (2002) data data("CarpenterFdaData") # Estimate basic model M1 <- coxph(Surv(acttime, censor) ~ prevgenx + lethal + deathrt1 + acutediz + hosp01 + hhosleng + mandiz01 + femdiz01 + peddiz01 + orphdum + vandavg3 + wpnoavg3 + condavg3 + orderent + stafcder, data = CarpenterFdaData) # Simulate and plot Hazard Ratios for stafcder variable Sim1 <- coxsimLinear(M1, b = "stafcder", Xj = c(1237, 1600), Xl = c(1000, 1000), qi = "Hazard Ratio", spin = TRUE, ci = 0.99) simGG(Sim1, method = 'lm', rug_position = 'jitter') simGG(Sim1, rug_position = 'jitter') ## Not run: # Simulate and plot Hazard Rate for stafcder variable Sim2 <- coxsimLinear(M1, b = "stafcder", nsim = 100, qi = "Hazard Rate", Xj = c(1237, 1600)) simGG(Sim2, type = 'points') simGG(Sim2, type = 'lines') ## End(Not run)
# Load survival package library(survival) # Load Carpenter (2002) data data("CarpenterFdaData") # Estimate basic model M1 <- coxph(Surv(acttime, censor) ~ prevgenx + lethal + deathrt1 + acutediz + hosp01 + hhosleng + mandiz01 + femdiz01 + peddiz01 + orphdum + vandavg3 + wpnoavg3 + condavg3 + orderent + stafcder, data = CarpenterFdaData) # Simulate and plot Hazard Ratios for stafcder variable Sim1 <- coxsimLinear(M1, b = "stafcder", Xj = c(1237, 1600), Xl = c(1000, 1000), qi = "Hazard Ratio", spin = TRUE, ci = 0.99) simGG(Sim1, method = 'lm', rug_position = 'jitter') simGG(Sim1, rug_position = 'jitter') ## Not run: # Simulate and plot Hazard Rate for stafcder variable Sim2 <- coxsimLinear(M1, b = "stafcder", nsim = 100, qi = "Hazard Rate", Xj = c(1237, 1600)) simGG(Sim2, type = 'points') simGG(Sim2, type = 'lines') ## End(Not run)
simGG.simpoly
uses ggplot2 to plot simulated relative
quantities of interest from a simpoly
class object.
## S3 method for class 'simpoly' simGG( obj, from = NULL, to = NULL, rug = TRUE, rug_position = "identity", xlab = NULL, ylab = NULL, title = NULL, method = "auto", spalette = "Set1", legend = "legend", leg.name = "", lcolour = "#2B8CBE", lsize = 1, pcolour = "#A6CEE3", psize = 1, alpha = 0.2, type = "ribbons", ... )
## S3 method for class 'simpoly' simGG( obj, from = NULL, to = NULL, rug = TRUE, rug_position = "identity", xlab = NULL, ylab = NULL, title = NULL, method = "auto", spalette = "Set1", legend = "legend", leg.name = "", lcolour = "#2B8CBE", lsize = 1, pcolour = "#A6CEE3", psize = 1, alpha = 0.2, type = "ribbons", ... )
obj |
a |
from |
numeric time to start the plot from. Only relevant if
|
to |
numeric time to plot to. Only relevant if
|
rug |
logical indicating whether or not to include a rug plot showing
the distribution of values in the sample used to estimate the |
rug_position |
character string. The position adjustment to use for
overlapping points in the rug plot. Use |
xlab |
a label for the plot's x-axis. |
ylab |
a label of the plot's y-axis. The default uses the value of
|
title |
the plot's main title. |
method |
what type of smoothing method to use to summarize the center of the simulation distribution. |
spalette |
colour palette for when there are multiple sets of
comparisons to plot. Default palette is |
legend |
specifies what type of legend to include (if applicable). The
default is |
leg.name |
name of the legend (if applicable). |
lcolour |
character string colour of the smoothing line. The default is
hexadecimal colour |
lsize |
size of the smoothing line. Default is 1. See
|
pcolour |
character string colour of the simulated points or ribbons
(when there are not multiple sets of simulations). Default is hexadecimal
colour |
psize |
size of the plotted simulation points. Default is
|
alpha |
numeric. Alpha (e.g. transparency) for the points, lines, or
ribbons. Default is |
type |
character string. Specifies how to plot the simulations. Can be
|
... |
Additional arguments. (Currently ignored.) |
Uses ggplot2 to plot the quantities of interest from
simpoly
objects.
a gg
ggplot
class object
Gandrud, Christopher. 2015. simPH: An R Package for Illustrating Estimates from Cox Proportional Hazard Models Including for Interactive and Nonlinear Effects. Journal of Statistical Software. 65(3)1-20.
coxsimPoly
and ggplot2
# Load Carpenter (2002) data data("CarpenterFdaData") # Load survival package library(survival) # Run basic model M1 <- coxph(Surv(acttime, censor) ~ prevgenx + lethal + deathrt1 + acutediz + hosp01 + hhosleng + mandiz01 + femdiz01 + peddiz01 + orphdum + natreg + I(natreg^2) + I(natreg^3) + vandavg3 + wpnoavg3 + condavg3 + orderent + stafcder, data = CarpenterFdaData) # Simulate simpoly First Difference Sim1 <- coxsimPoly(M1, b = "natreg", qi = "First Difference", pow = 3, Xj = seq(1, 150, by = 5), nsim = 100) # Plot simulations simGG(Sim1, rug_position = 'jitter') ## Not run: # Simulate simpoly Hazard Ratio with spin probibility interval Sim2 <- coxsimPoly(M1, b = "natreg", qi = "Hazard Ratio", pow = 3, Xj = seq(1, 150, by = 5), spin = TRUE, nsim = 100) # Plot simulations simGG(Sim2, type = 'ribbons', rug_position = 'jitter') Sim3 <- coxsimPoly(M1, b = "natreg", qi = "Hazard Rate", pow = 3, Xj = c(1, 150), nsim = 100) # Plot simulations simGG(Sim3, type = 'lines') ## End(Not run)
# Load Carpenter (2002) data data("CarpenterFdaData") # Load survival package library(survival) # Run basic model M1 <- coxph(Surv(acttime, censor) ~ prevgenx + lethal + deathrt1 + acutediz + hosp01 + hhosleng + mandiz01 + femdiz01 + peddiz01 + orphdum + natreg + I(natreg^2) + I(natreg^3) + vandavg3 + wpnoavg3 + condavg3 + orderent + stafcder, data = CarpenterFdaData) # Simulate simpoly First Difference Sim1 <- coxsimPoly(M1, b = "natreg", qi = "First Difference", pow = 3, Xj = seq(1, 150, by = 5), nsim = 100) # Plot simulations simGG(Sim1, rug_position = 'jitter') ## Not run: # Simulate simpoly Hazard Ratio with spin probibility interval Sim2 <- coxsimPoly(M1, b = "natreg", qi = "Hazard Ratio", pow = 3, Xj = seq(1, 150, by = 5), spin = TRUE, nsim = 100) # Plot simulations simGG(Sim2, type = 'ribbons', rug_position = 'jitter') Sim3 <- coxsimPoly(M1, b = "natreg", qi = "Hazard Rate", pow = 3, Xj = c(1, 150), nsim = 100) # Plot simulations simGG(Sim3, type = 'lines') ## End(Not run)
simGG.simspline
uses ggplot2 to plot
quantities of interest from simspline
objects, including relative
hazards, first differences, hazard ratios, and hazard rates.
## S3 method for class 'simspline' simGG( obj, SmoothSpline = TRUE, FacetTime = NULL, from = NULL, to = NULL, rug = TRUE, rug_position = "identity", xlab = NULL, ylab = NULL, zlab = NULL, title = NULL, method = "auto", lcolour = "#2B8CBE", lsize = 1, pcolour = "#A6CEE3", psize = 1, alpha = 0.2, type = "ribbons", ... )
## S3 method for class 'simspline' simGG( obj, SmoothSpline = TRUE, FacetTime = NULL, from = NULL, to = NULL, rug = TRUE, rug_position = "identity", xlab = NULL, ylab = NULL, zlab = NULL, title = NULL, method = "auto", lcolour = "#2B8CBE", lsize = 1, pcolour = "#A6CEE3", psize = 1, alpha = 0.2, type = "ribbons", ... )
obj |
a |
SmoothSpline |
logical whether or not to fit the simulations with
smoothing splines. Creates a smoother plot. See |
FacetTime |
a numeric vector of points in time where you would like to
plot Hazard Rates in a facet grid. Only relevant if
|
from |
numeric time to start the plot from. Only relevant if
|
to |
numeric time to plot to. Only relevant if
|
rug |
logical indicating whether or not to include a rug plot showing
the distribution of values in the sample used to estimate the |
rug_position |
character string. The position adjustment to use for
overlapping points in the rug plot. Use |
xlab |
a label for the plot's x-axis. |
ylab |
a label of the plot's y-axis. The default uses the value of
|
zlab |
a label for the plot's z-axis. Only relevant if
|
title |
the plot's main title. |
method |
what type of smoothing method to use to summarize the center of the simulation distribution. |
lcolour |
character string colour of the smoothing line. The default is
hexadecimal colour |
lsize |
size of the smoothing line. Default is 1. See
|
pcolour |
character string colour of the simulated points or ribbons
(when there are not multiple sets of simulations). Default is hexadecimal
colour |
psize |
size of the plotted simulation points. Default is
|
alpha |
numeric. Alpha (e.g. transparency) for the points, lines, or
ribbons. Default is |
type |
character string. Specifies how to plot the simulations. Can be
|
... |
Additional arguments. (Currently ignored.) |
Uses ggplot2
to plot the quantities of
interest from simspline
objects, including relative hazards, first
differences, hazard ratios, and hazard rates. If currently does not support
hazard rates for multiple strata.
You can to plot hazard rates for a range of values of Xj
in two
dimensional plots at specific points in time. Each plot is arranged in a
facet grid.
Note: A dotted line is created at y = 1 (0 for first difference), i.e. no effect, for time-varying hazard ratio graphs. No line is created for hazard rates.
a gg
ggplot
class object.
Gandrud, Christopher. 2015. simPH: An R Package for Illustrating Estimates from Cox Proportional Hazard Models Including for Interactive and Nonlinear Effects. Journal of Statistical Software. 65(3)1-20.
coxsimLinear
, simGG.simtvc
,
ggplot2
# Load Carpenter (2002) data data("CarpenterFdaData") # Load survival package library(survival) # Run basic model # From Keele (2010) replication data M1 <- coxph(Surv(acttime, censor) ~ prevgenx + lethal + deathrt1 + acutediz + hosp01 + pspline(hospdisc, df = 4) + pspline(hhosleng, df = 4) + mandiz01 + femdiz01 + peddiz01 + orphdum + natreg + vandavg3 + wpnoavg3 + pspline(condavg3, df = 4) + pspline(orderent, df = 4) + pspline(stafcder, df = 4), data = CarpenterFdaData) # Simulate Relative Hazards for orderent Sim1 <- coxsimSpline(M1, bspline = "pspline(stafcder, df = 4)", bdata = CarpenterFdaData$stafcder, qi = "Hazard Ratio", Xj = seq(1100, 1700, by = 10), Xl = seq(1099, 1699, by = 10), spin = TRUE, nsim = 100) # Plot relative hazard simGG(Sim1, alpha = 0.5) ## Not run: # Simulate Hazard Rate for orderent Sim2 <- coxsimSpline(M1, bspline = "pspline(orderent, df = 4)", bdata = CarpenterFdaData$orderent, qi = "Hazard Rate", Xj = seq(1, 30, by = 2), ci = 0.9, nsim = 10) # Create a time grid plot # Find all points in time where baseline hazard was found unique(Sim2$sims$Time) # Round time values so they can be exactly matched with FacetTime Sim2$sims$Time <- round(Sim2$sims$Time, digits = 2) # Create plot simGG(Sim2, FacetTime = c(6.21, 25.68, 100.64, 202.36), type = 'ribbons', alpha = 0.5) # Simulated Fitted Values of stafcder Sim3 <- coxsimSpline(M1, bspline = "pspline(stafcder, df = 4)", bdata = CarpenterFdaData$stafcder, qi = "Hazard Ratio", Xj = seq(1100, 1700, by = 10), Xl = seq(1099, 1699, by = 10), ci = 0.90) # Plot simulated Hazard Ratios simGG(Sim3, xlab = "\nFDA Drug Review Staff", type = 'lines', alpha = 0.2) simGG(Sim3, xlab = "\nFDA Drug Review Staff", alpha = 0.2, SmoothSpline = TRUE, type = 'points') ## End(Not run)
# Load Carpenter (2002) data data("CarpenterFdaData") # Load survival package library(survival) # Run basic model # From Keele (2010) replication data M1 <- coxph(Surv(acttime, censor) ~ prevgenx + lethal + deathrt1 + acutediz + hosp01 + pspline(hospdisc, df = 4) + pspline(hhosleng, df = 4) + mandiz01 + femdiz01 + peddiz01 + orphdum + natreg + vandavg3 + wpnoavg3 + pspline(condavg3, df = 4) + pspline(orderent, df = 4) + pspline(stafcder, df = 4), data = CarpenterFdaData) # Simulate Relative Hazards for orderent Sim1 <- coxsimSpline(M1, bspline = "pspline(stafcder, df = 4)", bdata = CarpenterFdaData$stafcder, qi = "Hazard Ratio", Xj = seq(1100, 1700, by = 10), Xl = seq(1099, 1699, by = 10), spin = TRUE, nsim = 100) # Plot relative hazard simGG(Sim1, alpha = 0.5) ## Not run: # Simulate Hazard Rate for orderent Sim2 <- coxsimSpline(M1, bspline = "pspline(orderent, df = 4)", bdata = CarpenterFdaData$orderent, qi = "Hazard Rate", Xj = seq(1, 30, by = 2), ci = 0.9, nsim = 10) # Create a time grid plot # Find all points in time where baseline hazard was found unique(Sim2$sims$Time) # Round time values so they can be exactly matched with FacetTime Sim2$sims$Time <- round(Sim2$sims$Time, digits = 2) # Create plot simGG(Sim2, FacetTime = c(6.21, 25.68, 100.64, 202.36), type = 'ribbons', alpha = 0.5) # Simulated Fitted Values of stafcder Sim3 <- coxsimSpline(M1, bspline = "pspline(stafcder, df = 4)", bdata = CarpenterFdaData$stafcder, qi = "Hazard Ratio", Xj = seq(1100, 1700, by = 10), Xl = seq(1099, 1699, by = 10), ci = 0.90) # Plot simulated Hazard Ratios simGG(Sim3, xlab = "\nFDA Drug Review Staff", type = 'lines', alpha = 0.2) simGG(Sim3, xlab = "\nFDA Drug Review Staff", alpha = 0.2, SmoothSpline = TRUE, type = 'points') ## End(Not run)
simGG.simtvc
uses ggplot2 to plot the simulated hazards from a
simtvc
class object created by coxsimtvc
using
ggplot2.
## S3 method for class 'simtvc' simGG( obj, from = NULL, to = NULL, xlab = NULL, ylab = NULL, title = NULL, method = "auto", spalette = "Set1", legend = "legend", leg.name = "", lsize = 1, psize = 1, alpha = 0.2, type = "ribbons", ... )
## S3 method for class 'simtvc' simGG( obj, from = NULL, to = NULL, xlab = NULL, ylab = NULL, title = NULL, method = "auto", spalette = "Set1", legend = "legend", leg.name = "", lsize = 1, psize = 1, alpha = 0.2, type = "ribbons", ... )
obj |
a |
from |
numeric time to start the plot from. |
to |
numeric time to plot to. |
xlab |
a label for the plot's x-axis. |
ylab |
a label of the plot's y-axis. The default uses the value of
|
title |
the plot's main title. |
method |
what type of smoothing method to use to summarize the center of the simulation distribution. |
spalette |
colour palette for when there are multiple sets of
comparisons to plot. Default palette is |
legend |
specifies what type of legend to include (if applicable).
The default is |
leg.name |
name of the legend (if applicable). |
lsize |
size of the smoothing line. Default is 1. See
|
psize |
size of the plotted simulation points. Default is
|
alpha |
numeric. Alpha (e.g. transparency) for the points, lines, or
ribbons. Default is |
type |
character string. Specifies how to plot the simulations. Can be
|
... |
Additional arguments. (Currently ignored.) |
Plots either a time-interactive hazard ratios, first differences,
and relative hazards, or the hazard rates for multiple strata. Currently the
strata legend labels need to be changed manually (see revalue
in the plyr package) in the simtvc
object with the
strata
component. Also, currently the x-axis tick marks and break
labels must be adjusted manually for non-linear functions of time.
Note: A dotted line is created at y = 1 (0 for first difference), i.e. no
effect, for time-varying hazard ratio graphs. No line is created for hazard
rates.
a gg
ggplot
class object
Gandrud, Christopher. 2015. simPH: An R Package for Illustrating Estimates from Cox Proportional Hazard Models Including for Interactive and Nonlinear Effects. Journal of Statistical Software. 65(3)1-20.
Licht, Amanda A. 2011. ”Change Comes with Time: Substantive Interpretation of Nonproportional Hazards in Event History Analysis.” Political Analysis 19: 227-43.
## Not run: # Load Golub & Steunenberg (2007) Data data("GolubEUPData") # Load survival package library(survival) # Expand data GolubEUPData <- SurvExpand(GolubEUPData, GroupVar = 'caseno', Time = 'begin', Time2 = 'end', event = 'event') # Create time interactions BaseVars <- c('qmv', 'backlog', 'coop', 'codec', 'qmvpostsea', 'thatcher') GolubEUPData <- tvc(GolubEUPData, b = BaseVars, tvar = 'end', tfun = 'log') # Run Cox PH Model M1 <- coxph(Surv(begin, end, event) ~ qmv + qmvpostsea + qmvpostteu + coop + codec + eu9 + eu10 + eu12 + eu15 + thatcher + agenda + backlog + qmv_log + qmvpostsea_log + coop_log + codec_log + thatcher_log + backlog_log, data = GolubEUPData, ties = "efron") # Create simtvc object for Relative Hazard Sim1 <- coxsimtvc(obj = M1, b = "qmv", btvc = "qmv_log", tfun = "log", from = 80, to = 2000, Xj = 1, by = 15, ci = 0.99, nsim = 100) # Create plot simGG(Sim1, legend = FALSE) # Create simtvc object for First Difference Sim2 <- coxsimtvc(obj = M1, b = "qmv", btvc = "qmv_log", qi = "First Difference", Xj = 1, tfun = "log", from = 80, to = 2000, by = 15, ci = 0.95) # Create simtvc object for Hazard Ratio Sim3 <- coxsimtvc(obj = M1, b = "backlog", btvc = "backlog_log", qi = "Hazard Ratio", Xj = c(191, 229), Xl = c(0, 0), tfun = "log", from = 100, to = 2000, by = 15, ci = 0.99) # Create plots simGG(Sim2, type = 'points') simGG(Sim3, leg.name = "Comparision", from = 1200, type = 'lines') ## End(Not run)
## Not run: # Load Golub & Steunenberg (2007) Data data("GolubEUPData") # Load survival package library(survival) # Expand data GolubEUPData <- SurvExpand(GolubEUPData, GroupVar = 'caseno', Time = 'begin', Time2 = 'end', event = 'event') # Create time interactions BaseVars <- c('qmv', 'backlog', 'coop', 'codec', 'qmvpostsea', 'thatcher') GolubEUPData <- tvc(GolubEUPData, b = BaseVars, tvar = 'end', tfun = 'log') # Run Cox PH Model M1 <- coxph(Surv(begin, end, event) ~ qmv + qmvpostsea + qmvpostteu + coop + codec + eu9 + eu10 + eu12 + eu15 + thatcher + agenda + backlog + qmv_log + qmvpostsea_log + coop_log + codec_log + thatcher_log + backlog_log, data = GolubEUPData, ties = "efron") # Create simtvc object for Relative Hazard Sim1 <- coxsimtvc(obj = M1, b = "qmv", btvc = "qmv_log", tfun = "log", from = 80, to = 2000, Xj = 1, by = 15, ci = 0.99, nsim = 100) # Create plot simGG(Sim1, legend = FALSE) # Create simtvc object for First Difference Sim2 <- coxsimtvc(obj = M1, b = "qmv", btvc = "qmv_log", qi = "First Difference", Xj = 1, tfun = "log", from = 80, to = 2000, by = 15, ci = 0.95) # Create simtvc object for Hazard Ratio Sim3 <- coxsimtvc(obj = M1, b = "backlog", btvc = "backlog_log", qi = "Hazard Ratio", Xj = c(191, 229), Xl = c(0, 0), tfun = "log", from = 100, to = 2000, by = 15, ci = 0.99) # Create plots simGG(Sim2, type = 'points') simGG(Sim3, leg.name = "Comparision", from = 1200, type = 'lines') ## End(Not run)
An R package for simulating and plotting quantities of interest (relative hazards, first differences, and hazard ratios) for linear coefficients, multiplicative interactions, polynomials, penalised splines, and non-proportional hazards, as well as stratified survival curves from Cox Proportional Hazard models.
The package includes the following simulation functions:
coxsimLinear
: a function for simulating relative
hazards, first differences, hazard ratios, and hazard rates for linear,
non-time interacted covariates from Cox Proportional Hazard
(coxph
) models.
coxsimtvc
: a function for simulating time-interactive
hazards (relative hazards, first differences, and hazard ratios) for
covariates from Cox Proportional Hazard models. The function will calculate
time-interactive hazard ratios for multiple strata estimated from a
stratified Cox Proportional Hazard model.
coxsimSpline
: a function for simulating quantities
of interest from penalised splines using multivariate normal distributions.
Currently does not support simulating hazard rates from stratified models.
Note: be extremely careful about the number of simulations you ask the
function to find. It is very easy to ask for more than your computer can
handle.
coxsimPoly
: a function for simulating quantities of
interest for a range of values for a polynomial nonlinear effect from Cox
Proportional Hazard models.
coxsimInteract
: a function for simulating quantities
of interest for linear multiplicative interactions, including marginal
effects and hazard rates.
Results from these functions can be plotted using the simGG
method.
The package also includes two functions that make it easier to create time interactions:
SurvExpand
: a function to convert a data frame of
non-equal interval continuous observations into equal interval continuous
observations.
tvc
: a function to create time interaction variables
that can be used in a coxph
model (or any other model with time
interactions).
setXl: a function for setting valid Xl
values given a
sequence of fitted Xj
values. This makes it more intituitive to find
hazard ratios and first differences for comparisons between some
fitted values and
values other than 0.
Gandrud, Christopher. 2015. simPH: An R Package for Illustrating Estimates from Cox Proportional Hazard Models Including for Interactive and Nonlinear Effects. Journal of Statistical Software. 65(3)1-20.
SurvExpand
convert a data frame of non-equal interval continuous
observations into equal interval continuous observations. This is useful for
creating time-interactions with tvc
.
SurvExpand( data, GroupVar, Time, Time2, event, PartialData = TRUE, messages = TRUE )
SurvExpand( data, GroupVar, Time, Time2, event, PartialData = TRUE, messages = TRUE )
data |
a data frame. |
GroupVar |
a character string naming the unit grouping variable. |
Time |
a character string naming the variable with the interval start time. |
Time2 |
a character string naming the variable with the interval end time. |
event |
a character string naming the event variable. Note: must be numeric with 0 indicating no event. |
PartialData |
logical indicating whether or not to keep only the expanded data required to find the Cox partial likelihood. |
messages |
logical indicating if you want messages returned while the function is working. |
The function primarily prepares data from the creation of accurate
time-interactions with the tvc
command.
Note: the function will work best if your original time intervals are
recorded in whole numbers. It also currently does not support repeated
events data.
Returns a data frame where observations have been expanded into equally spaced time intervals.
Gandrud, Christopher. 2015. simPH: An R Package for Illustrating Estimates from Cox Proportional Hazard Models Including for Interactive and Nonlinear Effects. Journal of Statistical Software. 65(3)1-20.
# Load Golub & Steunenberg (2007) Data data("GolubEUPData") # Subset PURELY TO SPEED UP THE EXAMPLE GolubEUPData <- GolubEUPData[1:500, ] # Expand data GolubEUPDataExpand <- SurvExpand(GolubEUPData, GroupVar = 'caseno', Time = 'begin', Time2 = 'end', event = 'event')
# Load Golub & Steunenberg (2007) Data data("GolubEUPData") # Subset PURELY TO SPEED UP THE EXAMPLE GolubEUPData <- GolubEUPData[1:500, ] # Expand data GolubEUPDataExpand <- SurvExpand(GolubEUPData, GroupVar = 'caseno', Time = 'begin', Time2 = 'end', event = 'event')
tvc
creates a time interaction variable that can be used in a coxph
model (or any other model with time interactions)
tvc(data, b, tvar, tfun = "linear", pow = NULL, vector = FALSE)
tvc(data, b, tvar, tfun = "linear", pow = NULL, vector = FALSE)
data |
a data frame |
b |
the non-time interacted variable's name. Either a single value or a vector of variable names can be entered. |
tvar |
the time variable's name |
tfun |
function of time that btvc was multiplied by. Default is
|
pow |
if |
vector |
logical. Whether or not to return one vector a or a data frame.
Can only be used if only one |
Interacts a variable with a specified function of time. Possible
functions of time include 'linear'
, natural 'log'
, and
exponentiated ('power'
).
a data frame or vector. If a data frame is returned it will include
all of the original variables as well as the interactions denoted by a
variable name 'bn
_tfun
', where bn
is one variable name
from b
and tfun
as entered into the function.
SurvExpand
, simGG.simtvc
,
coxsimtvc
, survival
, and coxph
# Load Golub & Steunenberg (2007) Data data('GolubEUPData') # Subset PURELY TO SPEED UP THE EXAMPLE GolubEUPData <- GolubEUPData[1:500, ] # Expand data into equally spaced time intervals GolubEUPData <- SurvExpand(GolubEUPData, GroupVar = 'caseno', Time = 'begin', Time2 = 'end', event = 'event') # Create natural log time interaction with the qmv variable GolubEUPData$Lqmv <- tvc(GolubEUPData, b = 'qmv', tvar = 'end', tfun = 'log', vector = TRUE) # Create interactions for a vector of variables BaseVars <- c('qmv', 'backlog', 'coop', 'codec', 'qmvpostsea', 'thatcher') Test <- tvc(GolubEUPData, b = BaseVars, tvar = 'end', tfun = 'log')
# Load Golub & Steunenberg (2007) Data data('GolubEUPData') # Subset PURELY TO SPEED UP THE EXAMPLE GolubEUPData <- GolubEUPData[1:500, ] # Expand data into equally spaced time intervals GolubEUPData <- SurvExpand(GolubEUPData, GroupVar = 'caseno', Time = 'begin', Time2 = 'end', event = 'event') # Create natural log time interaction with the qmv variable GolubEUPData$Lqmv <- tvc(GolubEUPData, b = 'qmv', tvar = 'end', tfun = 'log', vector = TRUE) # Create interactions for a vector of variables BaseVars <- c('qmv', 'backlog', 'coop', 'codec', 'qmvpostsea', 'thatcher') Test <- tvc(GolubEUPData, b = BaseVars, tvar = 'end', tfun = 'log')