Files
AIclinicalresearch/r-statistics-service/utils/result_formatter.R

45 lines
1.0 KiB
R
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# utils/result_formatter.R
# 统计结果格式化,确保 p 值显示规范
# 格式化 p 值(符合 APA 规范)
format_p_value <- function(p) {
if (is.na(p)) return(NA)
if (p < 0.001) {
return("< 0.001")
} else {
return(sprintf("%.3f", p))
}
}
# 构建标准化结果(包含 p_value_fmt
make_result <- function(p_value, statistic, method, ...) {
list(
p_value = p_value,
p_value_fmt = format_p_value(p_value),
statistic = statistic,
method = method,
...
)
}
# 格式化置信区间
format_ci <- function(lower, upper, digits = 2) {
sprintf("[%.2f, %.2f]", lower, upper)
}
# 格式化效应量
format_effect_size <- function(value, type = "d") {
interpretation <- ""
if (type == "d") { # Cohen's d
if (abs(value) < 0.2) interpretation <- "微小"
else if (abs(value) < 0.5) interpretation <- "小"
else if (abs(value) < 0.8) interpretation <- "中等"
else interpretation <- "大"
}
list(
value = round(value, 3),
interpretation = interpretation
)
}