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

Methods for survdiff objects #147

Merged
merged 5 commits into from Nov 19, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Expand Up @@ -91,4 +91,4 @@ URL: http://github.com/tidyverse/broom
BugReports: http://github.com/tidyverse/broom/issues
VignetteBuilder: knitr
License: MIT + file LICENSE
RoxygenNote: 5.0.1
RoxygenNote: 6.0.0
2 changes: 2 additions & 0 deletions NAMESPACE
Expand Up @@ -73,6 +73,7 @@ S3method(glance,smooth.spline)
S3method(glance,stanreg)
S3method(glance,summary.lm)
S3method(glance,summaryDefault)
S3method(glance,survdiff)
S3method(glance,survexp)
S3method(glance,survfit)
S3method(glance,survreg)
Expand Down Expand Up @@ -167,6 +168,7 @@ S3method(tidy,stanreg)
S3method(tidy,summary.glht)
S3method(tidy,summary.lm)
S3method(tidy,summaryDefault)
S3method(tidy,survdiff)
S3method(tidy,survexp)
S3method(tidy,survfit)
S3method(tidy,survreg)
Expand Down
106 changes: 106 additions & 0 deletions R/survival_tidiers.R
Expand Up @@ -8,6 +8,7 @@
# - survexp
# - survfit
# - survreg
# - survdiff


#' Tidiers for aareg objects
Expand Down Expand Up @@ -681,3 +682,108 @@ glance.survreg <- function(x, conf.level = .95, ...) {

finish_glance(ret, x)
}








#' Tidiers for Tests of Differences between Survival Curves
#'
#' @param x a "survdiff" object
#' @param strata logical, whether to include strata in the output
#' @param ... other arguments passed to/from other methods, currently ignored
#'
#' @seealso \code{\link[survival]{survdiff}}
#'
#' @template boilerplate
#'
#' @name survdiff_tidiers
#'
#' @examples
#' if( require("survival") ) {
#' s <- survdiff( Surv(time, status) ~ pat.karno + strata(inst), data=lung)
#' tidy(s)
#' glance(s)
#' }
NULL


#' @rdname survdiff_tidiers
#'
#' @return
#' \code{tidy} on "survdiff" objects returns a data frame with the following columns:
#' \item{...}{initial column(s) correspond to grouping factors (right-hand side of the formula)}
#' \item{obs}{weighted observed number of events in each group}
#' \item{exp}{weighted expected number of events in each group}
#' \item{N}{number of subjects in each group}
#'
#' @export
tidy.survdiff <- function(x, strata=FALSE, ...) {
# if one-sample test
if( length(x$obs) == 1 ) {
return(
data.frame(
N = x$n,
obs = x$obs,
exp = x$exp
)
)
}
# grouping variables (unless one-sample test)
l <- lapply(strsplit(rownames(x$n), ", "), strsplit, "=")
row_list <- lapply(l, function(x)
structure(
as.data.frame(lapply(x, "[", 2), stringsAsFactors = FALSE),
names = sapply(x, "[", 1) )
)
gvars <- do.call("rbind", row_list)
has_strata <- "strata" %in% names(x)
if(strata && has_strata) {
.NotYetUsed(strata)
d_obs <- cbind(gvars, as.data.frame(x$obs)) %>%
tidyr::gather(strata, obs, dplyr::matches("V[0-9]+") ) %>%
tidyr::extract(strata, "strata", "([0-9]+)")
d_exp <- cbind(gvars, as.data.frame(x$exp)) %>%
tidyr::gather(strata, exp, dplyr::matches("V[0-9]+") ) %>%
tidyr::extract(strata, "strata", "([0-9]+)")
z <- d_obs %>% dplyr::left_join(d_exp)
} else {
rval <- data.frame(
N = as.numeric(x$n),
obs = if(has_strata) apply(x$obs, 1, sum) else x$obs,
exp = if(has_strata) apply(x$exp, 1, sum) else x$exp
)
}
cbind( gvars, rval )
}




#' @rdname survdiff_tidiers
#'
#' @return
#' \code{glance} on "survdiff" objects returns a data frame with the following columns:
#' \item{statistic}{value of the test statistic}
#' \item{df}{degrees of freedom}
#' \item{p.value}{p-value}
#'
#'
#' @export
glance.survdiff <- function(x, ...) {
e <- x$exp
if(is.matrix(e)) {
tmp <- apply(e, 1, sum)
} else {
tmp <- e
}
rval <- data.frame(
statistic = x$chisq,
df = (sum(1 * (tmp > 0))) - 1
)
rval$p.value <- 1 - stats::pchisq(rval$statistic, rval$df)
rval
}
3 changes: 1 addition & 2 deletions man/Arima_tidiers.Rd

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

4 changes: 2 additions & 2 deletions man/aareg_tidiers.Rd

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

1 change: 0 additions & 1 deletion man/acf_tidiers.Rd

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

1 change: 0 additions & 1 deletion man/anova_tidiers.Rd

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

1 change: 0 additions & 1 deletion man/auc_tidiers.Rd

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

1 change: 0 additions & 1 deletion man/augment.Rd

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

1 change: 0 additions & 1 deletion man/augment_columns.Rd

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

5 changes: 2 additions & 3 deletions man/betareg_tidiers.Rd

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

3 changes: 1 addition & 2 deletions man/biglm_tidiers.Rd

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

3 changes: 1 addition & 2 deletions man/binDesign_tidiers.Rd

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

1 change: 0 additions & 1 deletion man/binWidth_tidiers.Rd

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

1 change: 0 additions & 1 deletion man/boot_tidiers.Rd

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

1 change: 0 additions & 1 deletion man/bootstrap.Rd

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

1 change: 0 additions & 1 deletion man/brms_tidiers.Rd

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

2 changes: 1 addition & 1 deletion man/broom.Rd

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

1 change: 0 additions & 1 deletion man/btergm_tidiers.Rd

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

3 changes: 1 addition & 2 deletions man/cch_tidiers.Rd

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

1 change: 0 additions & 1 deletion man/compact.Rd

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

1 change: 0 additions & 1 deletion man/confint.geeglm.Rd

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

1 change: 0 additions & 1 deletion man/confint_tidy.Rd

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

5 changes: 2 additions & 3 deletions man/coxph_tidiers.Rd

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

3 changes: 1 addition & 2 deletions man/cv.glmnet_tidiers.Rd

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

5 changes: 2 additions & 3 deletions man/data.frame_tidiers.Rd

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

3 changes: 1 addition & 2 deletions man/ergm_tidiers.Rd

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

5 changes: 2 additions & 3 deletions man/felm_tidiers.Rd

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

1 change: 0 additions & 1 deletion man/finish_glance.Rd

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