在2-y轴散点图中添加单侧重复测量试验的中值趋势线和p值[R]
加载样本数据帧在2-y轴散点图中添加单侧重复测量试验的中值趋势线和p值[R],r,R,加载样本数据帧 df <- structure(list(ID = c(1,1,1,2,2,2,3,3,3), time = c(0L,1L,2L,0L,1L,2L,0L,1L,2L), M1a = c(0, 0.2, 0.3, 0, 1.5, 2.9,0, 2.4, 3.9), M2a = c(0, 0.4, 0.6,0,0.9, 0.9,0,0.5, 0.7), M3a = c(0,0.3, 0.4, 0, 0.6, 0.9,0, 0.5, 0.8), M4a = c(0,0.
df <- structure(list(ID = c(1,1,1,2,2,2,3,3,3),
time = c(0L,1L,2L,0L,1L,2L,0L,1L,2L),
M1a = c(0, 0.2, 0.3, 0, 1.5, 2.9,0, 2.4, 3.9),
M2a = c(0, 0.4, 0.6,0,0.9, 0.9,0,0.5, 0.7),
M3a = c(0,0.3, 0.4, 0, 0.6, 0.9,0, 0.5, 0.8),
M4a = c(0,0.6, 0.6,0, 0.4, 0.6,0, 0.2, 0.9),
M1b = c(0L, 200L, 300L,0L, 300L, 900L,0L, 900L, 1000L),
M2b = c(0L, 400L, 600L,0L, 600L, 900L,0L, 600L, 1000L),
M3b = c(0L, 300L, 400L,0L, 200L, 800L,0L, 200L, 900L),
M4b = c(0L, 600L, 600L,0L, 800L, 1000L,0L, 400L, 1100L)),
.Names = c("ID", "time", "M1a", "M2a", "M3a", "M4a","M1b", "M2b","M3b", "M4b"), class = "data.frame", row.names = c(NA, -9L))
我被什么困住了?
1.中位数趋势线
我可以添加回归线,但我希望有一条中间趋势线连接三个时间点的M1a和M1b中间值
2.向图中添加p值,重复单向方差分析测试
fit1=aov(df$M1a~df$time+Error(ID/time),na.action=na.exclude,data=df);
sig1=summary(fit1)$”错误:在“$”Pr(>F)”内
如果(sig=0.001&sig首先回答#2,则需要查看summary.aov对象的内部结构:
dput(summary(fit1))
structure(list(`Error: ID` = structure(list(structure(list(Df = 1,
`Sum Sq` = 5.60666666666667, `Mean Sq` = 5.60666666666667,
`F value` = NA_real_, `Pr(>F)` = NA_real_), .Names = c("Df",
"Sum Sq", "Mean Sq", "F value", "Pr(>F)"), class = c("anova",
"data.frame"), row.names = "Residuals")), class = c("summary.aov",
"listof")), `Error: ID:time` = structure(list(structure(list(
Df = 1, `Sum Sq` = 11.3157142857143, `Mean Sq` = 11.3157142857143), .Names = c("Df",
"Sum Sq", "Mean Sq"), class = c("anova", "data.frame"), row.names = "df$time")), class = c("summary.aov",
"listof")), `Error: Within` = structure(list(structure(list(Df = c(1,
5), `Sum Sq` = c(0.325952380952381, 0.573888888888889), `Mean Sq` = c(0.325952380952381,
0.114777777777778), `F value` = c(2.83985617480293, NA), `Pr(>F)` = c(0.152766396924706,
NA)), .Names = c("Df", "Sum Sq", "Mean Sq", "F value", "Pr(>F)"
), class = c("anova", "data.frame"), row.names = c("df$time ",
"Residuals"))), class = c("summary.aov", "listof"))), .Names = c("Error: ID",
"Error: ID:time", "Error: Within"), class = "summary.aovlist")
请注意,summary(fit1)$“Error:”
中的值实际上埋得更深一层(并且没有名称,因此需要数字索引。请执行以下操作:
summary(fit1)$"Error: Within"[[1]]$`Pr(>F)`
[1] 0.1527664 NA
现在看看我是否能解决二坐标绘图问题。非常确定的是,在执行par(new=TRUE)
操作之前,需要进行任何中值绘图,因为这会根据新数据更改用户坐标系
通过@VincentBonhome提供的有用评论,将具有提取值的标题添加到情节中:
plot(df$time,df$M1a,type="p",col="red", cex=0.5, cex.main=2, cex.lab=1.0, cex.axis=0.7)
lines(unique(df$time),
tapply(df$M1a, df$time, median))
par(new = TRUE)
plot( df$time, df$M1b,type="p", col="blue", xaxt="n", yaxt="n", xlab="",ylab="")
lines(unique(df$time),
tapply(df$M1b, df$time, median))
mtext("Relative change (%)",side=4,line=3)
axis(4)
legend("topleft",col=c("red","blue"), lty=1,legend=c("Absolute Change","Relative Change"))
title(main=bquote("P-value for M1 (absolute scale)"==
.(round(summary(fit1)$"Error: Within"[[1]]$`Pr(>F)`, 3) ) ) )
你对par(new=TRUE)
的看法是正确的,这似乎是可行的:绘图(df$time,df$M1a,type=“p”,col=“red”,cex=0.5,cex.main=2,cex.lab=1.0,cex.axis=0.7)
行(唯一(df$time),tapply(df$M1a,df$time,median))
par(new=TRUE)
绘图(df$time,df$M1b,type=“p”,col=”blue=“xan”,yan”,xlab=“”,ylab=“”)行(唯一(df$time),tapply(df$M1b,df$time,中位数))通常,人们希望根据值的联合范围在每个图中设置ylim,但这里的目标完全不同。两个坐标图被严厉批评为具有扭曲关系的巨大潜力。quantreg包提供了量化回归。库(quantreg);abline(rq(M1b~time,data=df,tau=0.5,col=2)
。这里使用50%的量度(tau)谢谢你的估算。@Vincentbonhome-谢谢。我试过你的样本测向脚本,它成功了,但对我的大数据不起作用。后来模拟了样本测向结构,你的脚本运行顺利,没有任何错误/警告,但我没有看到中间趋势线,实际上没有变化,不知道为什么?@42-谢谢!你的提示很好,此代码获取p值:sig1=summary(fit1)$”错误:在“[[1]]$
Pr(>F)[[1]]
。我不想在标题中明确提及p值,只想提及**或*符号,以便它们表示sig1=0.001和sig0.05的有效**水平,即标题可能是M1**或M1*或M1。基本原理-保持绘图整洁和对称,因为我可能会对300个独立变量运行它。期待!
summary(fit1)$"Error: Within"[[1]]$`Pr(>F)`
[1] 0.1527664 NA
plot(df$time,df$M1a,type="p",col="red", cex=0.5, cex.main=2, cex.lab=1.0, cex.axis=0.7)
lines(unique(df$time),
tapply(df$M1a, df$time, median))
par(new = TRUE)
plot( df$time, df$M1b,type="p", col="blue", xaxt="n", yaxt="n", xlab="",ylab="")
lines(unique(df$time),
tapply(df$M1b, df$time, median))
mtext("Relative change (%)",side=4,line=3)
axis(4)
legend("topleft",col=c("red","blue"), lty=1,legend=c("Absolute Change","Relative Change"))
title(main=bquote("P-value for M1 (absolute scale)"==
.(round(summary(fit1)$"Error: Within"[[1]]$`Pr(>F)`, 3) ) ) )