Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
cyk0315 authored Feb 28, 2024
2 parents ad027da + ed24d3f commit 5504b77
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
39 changes: 17 additions & 22 deletions R/forestcox.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#' @importFrom survey svycoxph regTermTest
#' @importFrom stats confint coefficients
#' @importFrom utils tail


TableSubgroupCox <- function(formula, var_subgroup = NULL, var_cov = NULL, data, time_eventrate = 3 * 365, decimal.hr = 2, decimal.percent = 1, decimal.pvalue = 3) {
. <- NULL
Expand Down Expand Up @@ -85,38 +85,35 @@ TableSubgroupCox <- function(formula, var_subgroup = NULL, var_cov = NULL, data,
xlabel <- setdiff(as.character(formula)[[3]], "+")[1]

ncoef <- ifelse(any(class(data) == "survey.design"), ifelse(length(levels(data$variables[[xlabel]])) <= 2, 1, length(levels(data$variables[[xlabel]])) - 1),
ifelse(length(levels(data[[xlabel]])) <= 2||is.numeric(data[[xlabel]]), 1, length(levels(data[[xlabel]])) - 1)
ifelse(length(levels(data[[xlabel]])) <= 2 || is.numeric(data[[xlabel]]), 1, length(levels(data[[xlabel]])) - 1)
)

if (is.null(var_subgroup)) {

if (!is.null(var_cov)) {
formula <- as.formula(paste0(deparse(formula), " + ", paste(var_cov, collapse = "+")))
}
if (any(class(data) == "survey.design")) {
model <- survey::svycoxph(formula, design = data, x = T)
# if (!is.null(model$xlevels) & length(model$xlevels[[1]]) != 2) stop("Categorical independent variable must have 2 levels.")
if(is.numeric(data[[xlabel]])){
prop<-NULL
}
else{
res.kap <- survey::svykm(formula.km, design = data)
prop <- round(100 * sapply(res.kap, function(x) {
1 - x[["surv"]][which.min(abs(x[["time"]] - time_eventrate))]
}), decimal.percent)
names(prop) <- model$xlevels[[1]]
if (is.numeric(data[[xlabel]])) {
prop <- NULL
} else {
res.kap <- survey::svykm(formula.km, design = data)
prop <- round(100 * sapply(res.kap, function(x) {
1 - x[["surv"]][which.min(abs(x[["time"]] - time_eventrate))]
}), decimal.percent)
names(prop) <- model$xlevels[[1]]
}
} else {
model <- survival::coxph(formula, data = data, x = TRUE)
# if (!is.null(model$xlevels) & length(model$xlevels[[1]]) != 2) stop("Categorical independent variable must have 2 levels.")
if(is.numeric(data[[xlabel]])){
prop<-NULL
}
else{
res.kap <- survival::survfit(formula.km, data = data)
res.kap.times <- summary(res.kap, times = time_eventrate, extend = T)
prop <- round(100 * (1 - res.kap.times[["surv"]]), decimal.percent)
names(prop) <- model$xlevels[[1]]
if (is.numeric(data[[xlabel]])) {
prop <- NULL
} else {
res.kap <- survival::survfit(formula.km, data = data)
res.kap.times <- summary(res.kap, times = time_eventrate, extend = T)
prop <- round(100 * (1 - res.kap.times[["surv"]]), decimal.percent)
names(prop) <- model$xlevels[[1]]
}
# out.kap <- paste(res.kap.times[["n.event"]], " (", round(100 * (1 - res.kap.times[["surv"]]), decimal.percent), ")", sep = "")
}
Expand Down Expand Up @@ -223,13 +220,11 @@ TableSubgroupCox <- function(formula, var_subgroup = NULL, var_cov = NULL, data,
coefficients()
pv_int <- round(pvs_int[nrow(pvs_int), ncol(pvs_int)], decimal.pvalue)
# if (!is.null(xlev) & length(xlev[[1]]) != 2) stop("Categorical independent variable must have 2 levels.")

} else {
model.int$call$formula <- as.formula(gsub(xlabel, paste0(xlabel, "*", var_subgroup), deparse(formula)))
model.int$call$data <- as.name("data")
pv_anova <- anova(model.int)
pv_int <- round(pv_anova[nrow(pv_anova), 4], decimal.pvalue)

}
}

Expand Down
4 changes: 2 additions & 2 deletions R/forestglm.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ TableSubgroupGLM <- function(formula, var_subgroup = NULL, var_cov = NULL, data,
coefficients()
pv_int <- round(pvs_int[nrow(pvs_int), ncol(pvs_int)], decimal.pvalue)
# if (!is.null(xlev) & length(xlev[[1]]) != 2) stop("Categorical independent variable must have 2 levels.")
data.design<-data
if(family=='binomial'){
data.design <- data
if (family == "binomial") {
model.int <- survey::svyglm(as.formula(gsub(xlabel, paste(xlabel, "*", var_subgroup, sep = ""), deparse(formula))), design = data.design, family = quasibinomial())
}else if(family=='gaussian'){
model.int <- survey::svyglm(as.formula(gsub(xlabel, paste(xlabel, "*", var_subgroup, sep = ""), deparse(formula))), design = data.design, family = gaussian())
Expand Down
11 changes: 10 additions & 1 deletion cla/cla.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{
"signedContributors": []
"signedContributors": [
{
"name": "cyk0315",
"id": 91959343,
"comment_id": 1922935084,
"created_at": "2024-02-02T06:13:52Z",
"repoId": 135014959,
"pullRequestNo": 16
}
]
}

0 comments on commit 5504b77

Please sign in to comment.