############################################ ## Homogeneity of variance test - 空壳版(照抄 single_mean) ############################################ # Homogeneity of variance tests for radiant.basics #' @export homo_variance_test <- function(dataset, var, group, method = "levene", conf_lev = .95, data_filter = "", envir = parent.frame()) { df_name <- if (is_string(dataset)) dataset else deparse(substitute(dataset)) dataset <- get_data(dataset, var, group, filt = data_filter, na.rm = TRUE, envir = envir) x <- dataset[[var]] g <- dataset[[group]] if (!is.numeric(x)) stop(i18n$t("Variable must be numeric")) if (length(unique(g)) < 2) stop(i18n$t("Grouping variable must have at least 2 levels")) ## ---- 空壳结果 ---- res <- tibble::tribble( ~Test, ~Statistic, ~p.value, "Levene", 0.42, 0.52, "Bartlett", 0.38, 0.54, "Fligner", 0.45, 0.50 ) dat_summary <- dataset %>% group_by(!!sym(group)) %>% summarise( n = n(), mean = mean(!!sym(var), na.rm = TRUE), sd = sd(!!sym(var), na.rm = TRUE), .groups = "drop" ) ## 绘图数据 plot_obj <- list(hist = list(type = "hist", data = dataset, var = var, group = group), density = list(type = "density", data = dataset, var = var, group = group), boxplot = list(type = "boxplot", data = dataset, var = var, group = group)) as.list(environment()) %>% add_class("homo_variance_test") } # Summary method #' @export summary.homo_variance_test <- function(object, dec = 3, ...) { cat("Homogeneity of variance tests\n") cat("Data :", object$df_name, "\n") if (!is.empty(object$data_filter)) { cat("Filter :", gsub("\\n", "", object$data_filter), "\n") } cat("Variable :", object$var, "\n") cat("Group :", object$group, "\n\n") ## 打印统计量表 object$res %>% as.data.frame(stringsAsFactors = FALSE) %>% format_df(dec = dec) %>% print(row.names = FALSE) cat("\n") } # Plot method #' @export plot.homo_variance_test <- function(x, plots = c("boxplot", "density"), shiny = FALSE, custom = FALSE, ...) { plot_list <- list() if ("boxplot" %in% plots) { plot_list[[which("boxplot" == plots)]] <- ggplot(x$dat_summary, aes(x = .data[[x$group]], y = .data[[x$var]])) + geom_boxplot(fill = "lightblue", alpha = 0.7) } if ("density" %in% plots) { plot_list[[which("density" == plots)]] <- ggplot(x$dat_summary, aes(x = .data[[x$var]], fill = .data[[x$group]])) + geom_density(alpha = 0.5) } if ("hist" %in% plots) { plot_list[[which("hist" == plots)]] <- ggplot(x$dat_summary, aes(x = .data[[x$var]], fill = .data[[x$group]])) + geom_histogram(alpha = 0.5, position = "identity", bins = 30) } if (length(plot_list) == 0) return(invisible()) patchwork::wrap_plots(plot_list, ncol = 1) %>% { if (shiny) print(.) else print(.) } invisible(x) }