#' Parametric tests for multiple comparisons with ggplot
#'
#' Unpaired ANOVA or paired t-test with post-hoc
#' multiple comparisons and boxplots with letters using ggplot2.
#'
#' Function to perform (unpaired) ANOVA or paired t-test with post-hoc
#' multiple comparisons and boxplots with letters (using agricolae::LSD.test).
#' ggplert() is based on an adaptation to ggplot2 of boxplert() by Chinese
#' students of Jiangshan Lai (http://blog.sciencenet.cn/blog-1835014-1151822.html).
#' This function requires ggplot2 and ggpubr libraries.
#' Mean values are added as big points.
#'
#' @author François Gillet, 2021-08-22
#' @param Y the numeric response variable
#' @param X the explanatory factor (groups, treatments...) or a qualitative
#' variable that can be converted to a factor
#' @param paired if TRUE and if 2 groups only, then paired t-test is performed
#' (objects are supposed to be sorted twice the same way in the data frame)
#' @param p.adj correction of p-values for multiple comparisons after
#' ANOVA test (default = "holm", else "none", "bonferroni",...)
#' @param varwidth if TRUE, adapt boxplot width to relative group size
#' @param notch if TRUE, add notches to help multiple comparisons
#' @param bcol fill colour(s) of boxplots; if bcol = "" then boxplots are
#' filled according to a gradient of heat colors based on means
#' @param xlab x-axis title (name of the explanatory factor X)
#' @param ylab y-axis title (name of the numeric response variable Y)
#' @return A summary table ($comparison) is printed and boxplots ($plot) are
#' drawn with result of the test ($p.value) and, if it is significant, of
#' post-hoc tests as letters (in decreasing order); the method applied to
#' adjustp-values is also returned ($p.adjust)
#' @export
#' @examples
#' data(sweetpotato)
#'
#' # Check ANOVA assumptions
#' shapiro.test(resid(aov(sweetpotato$yield ~ sweetpotato$virus)))
#' bartlett.test(sweetpotato$yield, sweetpotato$virus)
#'
#' # Since assumptions are met, perform the parametric tests:
#' ggplert(
#' sweetpotato$yield,
#' sweetpotato$virus,
#' xlab = "Virus",
#' ylab = "Yield",
#' bcol = "",
#' p.adj = "holm")
ggplert <- function(Y,
X,
paired = FALSE,
p.adj = "holm",
varwidth = TRUE,
notch = FALSE,
bcol = NULL,
xlab = "X factor",
ylab = "Y value") {
if (!is.factor(X)) {
X <- as.factor(X)
}
aa <- levels(X)
data <- data.frame(X, Y)
tt1 <- matrix(nrow = length(aa), ncol = 7)
for (i in 1:length(aa)) {
temp <- Y[X == aa[i]]
tt1[i, 1] <- mean(temp, na.rm = TRUE)
tt1[i, 2] <- sd(temp, na.rm = TRUE) / sqrt(length(temp))
tt1[i, 3] <- sd(temp, na.rm = TRUE)
tt1[i, 4] <- min(temp, na.rm = TRUE)
tt1[i, 5] <- max(temp, na.rm = TRUE)
tt1[i, 6] <- median(temp, na.rm = TRUE)
tt1[i, 7] <- length(temp)
}
tt1 <- as.data.frame(tt1)
row.names(tt1) <- aa
colnames(tt1) <- c("mean", "se", "sd", "min", "max", "median", "n")
# ANOVA test
if (paired == TRUE & length(aa) == 2) {
res <- t.test(Y ~ X, paired = TRUE)
pp <- res$p.value
tt1$group <- rep("a", 2)
if (pp <= 0.05)
tt1$group[which(tt1$mean == min(tt1$mean))] <- "b"
}
else {
model <- aov(Y ~ X)
pp <- anova(model)$Pr[1]
comp <- agricolae::LSD.test(model,
"X",
alpha = 0.05,
p.adj = p.adj,
group = TRUE)
gror <- comp$groups[aa,]
tt1$group <- gror$groups
}
if (is.null(bcol)) {
bcol <- heat.colors(nrow(tt1), rev = TRUE)[rank(tt1$mean)]
}
p <- ggplot(data = data, aes(X, Y, aes(X, Y, fill = aa))) +
geom_boxplot(fill = bcol,
varwidth = varwidth,
notch = notch,
outlier.colour = 1,
outlier.shape = 1,
outlier.alpha = 0.5,
na.rm = TRUE) +
labs(x = xlab, y = ylab) +
theme_pubr()
if (pp <= 0.1) {
p <- p + stat_summary(
geom = "text",
label = tt1$group,
fun = "max",
size = 5,
colour = "steelblue4",
vjust = -0.6)
}
if (pp < 0.001)
st <- expression(italic(P) < 0.001)
else {
pp2 <- round(pp, 4)
st <- bquote(italic(P) == .(pp2))
}
p <- p + labs(subtitle = st)
p <- p + stat_summary(
geom = "point",
fun = "mean",
pch = 21,
size = 4,
fill = "white",
alpha = 0.75)
list(comparison = tt1,
p.value = pp,
p.adjust = p.adj,
plot = p)
}
没有合适的资源?快使用搜索试试~ 我知道了~
克鲁斯卡尔-沃利斯检验 (Kruskal-Wallis test)代码
共5个文件
r:5个
需积分: 0 3 下载量 152 浏览量
2024-01-09
15:53:42
上传
评论 1
收藏 8KB ZIP 举报
温馨提示
克鲁斯卡尔-沃利斯检验 (Kruskal-Wallis test)亦称“K-W检验”、“H检验”等。 用以检验两个以上样本是否来自 同一个概率分布的一种非参数方法。 被检验的几个样本必须是独立的或不相关的。 与此检验对等的参数检验是单因素方差分析,但与之不同的是,K-W检验不假设样本来自正态分布。与参数检验相比,非参数检验具有检验条件宽松、对样本数据要求较低、计算相对简单的优点。SPSS提供的非参数检验方法较多,包括二项检验、卡方检验、两独立样本检验、两配对样本检验、多独立样本检验、多配对样本检验、游程检验和单样本K-S检验等八种检验方法。 Kruskal-Wallis秩和检验属于多个独立样本的非参数检验,用于在总体分布未知的情况下检验多个样本是否来自于相同分布的总体。Kruskal-Wallis H检验是Mann-Whitney U检验法的扩展,是一种推广的评价值检验。其基本思路是,首先对所有样本合并并且按照升序排列得出每个数据的秩,然后对各组样本求平均秩。如果平均秩相差很大,则认为两组样本所属的总体有显著差异。
资源推荐
资源详情
资源评论
收起资源包目录
多重比较代码更新.zip (5个子文件)
boxplerk.R 3KB
boxplert.R 3KB
ggplert.R 5KB
一元统计部分多重比较代码更新.r 3KB
ggplerk.R 5KB
共 5 条
- 1
资源评论
phylogenetic
- 粉丝: 66
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功