如何在 R 中进行 ANCOVA
How to do ANCOVA in R
我有一个样本与数字的关系图
红点用红色回归线建模,蓝点用蓝色回归线建模。我想比较两个回归斜率,看看它们是否有显着差异。我相信这是通过 ANCOVA 完成的。有谁知道如何在 R 中实现这个?
执行图表的数据如下:
structure(list(code = structure(c(25L, 37L, 13L,
29L, 17L, 10L, 24L, 20L, 38L, 35L, 32L, 28L, 26L, 39L, 21L, 15L,
31L, 9L, 16L, 23L, 7L, 18L, 42L, 34L, 4L, 33L, 19L, 22L, 27L), .Label = c("LP6005500-DNA_D01",
"LP6005334-DNA_E02", "LP6005334-DNA_G03", "LP6007427-DNA_A01",
"LP6005935-DNA_C03", "LP2000104-DNA_A01", "LP6005690-DNA_D01",
"LP6005409-DNA_C02", "LP6005500-DNA_D03", "LP6005334-DNA_D03",
"LP6005334-DNA_D01", "LP6007514-DNA_A01", "LP6005334-DNA_B02",
"LP6005334-DNA_F03", "LP6005500-DNA_B01", "LP6005500-DNA_E01",
"LP6005334-DNA_C03", "LP6005690-DNA_H01", "LP6007538-DNA_A01",
"LP6005334-DNA_E03", "LP6005500-DNA_A01", "LP6007540-DNA_A01",
"LP6005500-DNA_F01", "LP6005334-DNA_E01", "LP6005334-DNA_A02",
"LP6005409-DNA_A03", "LP6007542-DNA_A01", "LP6005334-DNA_H03",
"LP6005334-DNA_C02", "LP6007409-DNA_A01", "LP6005500-DNA_C01",
"LP6005334-DNA_H01", "LP6007512-DNA_A01", "LP6007424-DNA_A01",
"LP6005334-DNA_G02", "LP6005334-DNA_C01", "LP6005334-DNA_A03",
"LP6005334-DNA_F01", "LP6005409-DNA_C04", "LP6005334-DNA_D02",
"LP6007418-DNA_A02", "LP6007396-DNA_A01", "LP6005334-DNA_F02"
), class = "factor"), freq = c(503, 597, 354, 522, 399, 338,
498, 430, 606, 590, 561, 518, 508, 618, 436, 373, 559, 328, 382,
491, 313, 408, 683, 585, 261, 570, 423, 477, 515), CNI = c(21L,
54L, 25L, 32L, 19L, 23L, 21L, 18L, 25L, 29L, 32L, 27L, 37L, 49L,
26L, 11L, 11L, 24L, 13L, 31L, 19L, 21L, 28L, 32L, 17L, 44L, 22L,
20L, 15L)), .Names = c("code", "freq", "CNI"), row.names = c(1L,
2L, 3L, 5L, 6L, 9L, 10L, 12L, 13L, 16L, 18L, 19L, 20L, 22L, 23L,
24L, 25L, 27L, 28L, 29L, 30L, 31L, 33L, 35L, 36L, 37L, 39L, 40L,
41L), class = "data.frame")
这是图表的代码
ggplot(FeqAndASCATmergeCell) +
geom_bar(aes(code,Cellul*100),stat="identity")+
geom_point(aes(code,freq),colour="blue")+
geom_smooth(aes(code,freq,group=2),fill = "blue",alpha=0.2)+
geom_point(aes(code,CNI*20),colour="red",size=5)+
geom_smooth(aes(code,CNI*20,group=2),fill = "red", colour="red", alpha=0.2)+
theme(axis.text=element_text(size=14)) +
theme(axis.title=element_text(size=14))+
theme(legend.title=element_blank())+
theme(legend.position = c(0.7, 0.7))+
theme(axis.text.x=element_text(angle=-90))+
scale_y_continuous(breaks=c(25, 50,75, 100,300,600,900))+
theme(axis.text.y=element_text( size=10)) +
theme(legend.position = "none")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black")) +
theme(plot.title = element_text(size=18,lineheight=.8, face="bold",vjust=1))+
theme(axis.text.x=element_text(angle=-90, size=10))
您可以将回归线的斜率与 ANCOVA 进行比较,但 仅 用于直线,因此斜率恒定。因为您没有为 geom_smooth
提供方法,所以它使用 loess
并且您的线没有恒定的斜率。例如,您可以使用 method = lm
:
ggplot(FeqAndASCATmergeCell) +
geom_point(aes(code,freq),colour="blue")+
geom_smooth(aes(code,freq,group=2),fill = "blue",alpha=0.2, method="lm")+
geom_point(aes(code,CNI*20),colour="red",size=5)+
geom_smooth(aes(code,CNI*20,group=2),fill = "red", colour="red", alpha=0.2, method="lm")+
theme(axis.text=element_text(size=14)) +
theme(axis.title=element_text(size=14))+
theme(legend.title=element_blank())+
theme(legend.position = c(0.7, 0.7))+
theme(axis.text.x=element_text(angle=-90))+
scale_y_continuous(breaks=c(25, 50,75, 100,300,600,900))+
theme(axis.text.y=element_text( size=10)) +
theme(legend.position = "none")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black")) +
theme(plot.title = element_text(size=18,lineheight=.8, face="bold",vjust=1))+
theme(axis.text.x=element_text(angle=-90, size=10))
这会给你直线:
接下来,您可以使用 ANCOVA 来测试这些线是否不同。基本上,您测试直线的斜率或截距是否有显着差异。你如何做到这一点会使答案太长,但请参阅 here 的 R 示例。
我有一个样本与数字的关系图
红点用红色回归线建模,蓝点用蓝色回归线建模。我想比较两个回归斜率,看看它们是否有显着差异。我相信这是通过 ANCOVA 完成的。有谁知道如何在 R 中实现这个?
执行图表的数据如下:
structure(list(code = structure(c(25L, 37L, 13L,
29L, 17L, 10L, 24L, 20L, 38L, 35L, 32L, 28L, 26L, 39L, 21L, 15L,
31L, 9L, 16L, 23L, 7L, 18L, 42L, 34L, 4L, 33L, 19L, 22L, 27L), .Label = c("LP6005500-DNA_D01",
"LP6005334-DNA_E02", "LP6005334-DNA_G03", "LP6007427-DNA_A01",
"LP6005935-DNA_C03", "LP2000104-DNA_A01", "LP6005690-DNA_D01",
"LP6005409-DNA_C02", "LP6005500-DNA_D03", "LP6005334-DNA_D03",
"LP6005334-DNA_D01", "LP6007514-DNA_A01", "LP6005334-DNA_B02",
"LP6005334-DNA_F03", "LP6005500-DNA_B01", "LP6005500-DNA_E01",
"LP6005334-DNA_C03", "LP6005690-DNA_H01", "LP6007538-DNA_A01",
"LP6005334-DNA_E03", "LP6005500-DNA_A01", "LP6007540-DNA_A01",
"LP6005500-DNA_F01", "LP6005334-DNA_E01", "LP6005334-DNA_A02",
"LP6005409-DNA_A03", "LP6007542-DNA_A01", "LP6005334-DNA_H03",
"LP6005334-DNA_C02", "LP6007409-DNA_A01", "LP6005500-DNA_C01",
"LP6005334-DNA_H01", "LP6007512-DNA_A01", "LP6007424-DNA_A01",
"LP6005334-DNA_G02", "LP6005334-DNA_C01", "LP6005334-DNA_A03",
"LP6005334-DNA_F01", "LP6005409-DNA_C04", "LP6005334-DNA_D02",
"LP6007418-DNA_A02", "LP6007396-DNA_A01", "LP6005334-DNA_F02"
), class = "factor"), freq = c(503, 597, 354, 522, 399, 338,
498, 430, 606, 590, 561, 518, 508, 618, 436, 373, 559, 328, 382,
491, 313, 408, 683, 585, 261, 570, 423, 477, 515), CNI = c(21L,
54L, 25L, 32L, 19L, 23L, 21L, 18L, 25L, 29L, 32L, 27L, 37L, 49L,
26L, 11L, 11L, 24L, 13L, 31L, 19L, 21L, 28L, 32L, 17L, 44L, 22L,
20L, 15L)), .Names = c("code", "freq", "CNI"), row.names = c(1L,
2L, 3L, 5L, 6L, 9L, 10L, 12L, 13L, 16L, 18L, 19L, 20L, 22L, 23L,
24L, 25L, 27L, 28L, 29L, 30L, 31L, 33L, 35L, 36L, 37L, 39L, 40L,
41L), class = "data.frame")
这是图表的代码
ggplot(FeqAndASCATmergeCell) +
geom_bar(aes(code,Cellul*100),stat="identity")+
geom_point(aes(code,freq),colour="blue")+
geom_smooth(aes(code,freq,group=2),fill = "blue",alpha=0.2)+
geom_point(aes(code,CNI*20),colour="red",size=5)+
geom_smooth(aes(code,CNI*20,group=2),fill = "red", colour="red", alpha=0.2)+
theme(axis.text=element_text(size=14)) +
theme(axis.title=element_text(size=14))+
theme(legend.title=element_blank())+
theme(legend.position = c(0.7, 0.7))+
theme(axis.text.x=element_text(angle=-90))+
scale_y_continuous(breaks=c(25, 50,75, 100,300,600,900))+
theme(axis.text.y=element_text( size=10)) +
theme(legend.position = "none")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black")) +
theme(plot.title = element_text(size=18,lineheight=.8, face="bold",vjust=1))+
theme(axis.text.x=element_text(angle=-90, size=10))
您可以将回归线的斜率与 ANCOVA 进行比较,但 仅 用于直线,因此斜率恒定。因为您没有为 geom_smooth
提供方法,所以它使用 loess
并且您的线没有恒定的斜率。例如,您可以使用 method = lm
:
ggplot(FeqAndASCATmergeCell) +
geom_point(aes(code,freq),colour="blue")+
geom_smooth(aes(code,freq,group=2),fill = "blue",alpha=0.2, method="lm")+
geom_point(aes(code,CNI*20),colour="red",size=5)+
geom_smooth(aes(code,CNI*20,group=2),fill = "red", colour="red", alpha=0.2, method="lm")+
theme(axis.text=element_text(size=14)) +
theme(axis.title=element_text(size=14))+
theme(legend.title=element_blank())+
theme(legend.position = c(0.7, 0.7))+
theme(axis.text.x=element_text(angle=-90))+
scale_y_continuous(breaks=c(25, 50,75, 100,300,600,900))+
theme(axis.text.y=element_text( size=10)) +
theme(legend.position = "none")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black")) +
theme(plot.title = element_text(size=18,lineheight=.8, face="bold",vjust=1))+
theme(axis.text.x=element_text(angle=-90, size=10))
这会给你直线:
接下来,您可以使用 ANCOVA 来测试这些线是否不同。基本上,您测试直线的斜率或截距是否有显着差异。你如何做到这一点会使答案太长,但请参阅 here 的 R 示例。