diff --git a/R/forestcox.R b/R/forestcox.R index 3e9e7b1..a60e3f5 100644 --- a/R/forestcox.R +++ b/R/forestcox.R @@ -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 @@ -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 = "") } @@ -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) - } } diff --git a/R/forestglm.R b/R/forestglm.R index e2e62d8..e16c013 100644 --- a/R/forestglm.R +++ b/R/forestglm.R @@ -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()) diff --git a/cla/cla.json b/cla/cla.json index 18d5487..a7f4287 100644 --- a/cla/cla.json +++ b/cla/cla.json @@ -1,3 +1,12 @@ { - "signedContributors": [] + "signedContributors": [ + { + "name": "cyk0315", + "id": 91959343, + "comment_id": 1922935084, + "created_at": "2024-02-02T06:13:52Z", + "repoId": 135014959, + "pullRequestNo": 16 + } + ] } \ No newline at end of file