[英]Plotting Polynomial Regression Curves in R
我已经对我包含的从二次到化粪池的数据进行多项式回归,但我被困在我的散点 plot 上的这些回归曲线上。 我正在寻求帮助创建适用于每个多项式顺序的代码。
Time <- Mangan_2008_total$YearMonthDay
Abundance <- Mangan_2008_total$`Total Nymph Aa`
上面是我用来将我在这篇文章中包含的数据与更大的数据集隔离开来的代码。 以下是供参考的数据。
(日期目前采用公历格式。我计划在某个时候将它们转换为儒略日日历。)
Time
1 20080301
2 20080316
3 20080402
4 20080416
5 20080428
6 20080514
7 20080527
8 20080608
9 20080627
10 20080709
11 20080722
12 20080806
13 20080818
14 20080901
15 20080915
16 20080930
17 20081013
18 20081029
19 20081110
20 20081124
Abundance
1 0
2 0
3 26
4 337
5 122
6 232
7 190
8 381
9 148
10 201
11 69
12 55
13 35
14 15
15 6
16 1
17 0
18 1
19 0
20 0
我将这些数据编译成一个数据框以便于操作:
Mangan_2008_nymph <- data.frame(Time, Abundance)
这是我在 ggplot 中制作的散点图 plot 的代码:
Nymph_2008_Plot <- ggplot(Mangan_2008_nymph, aes(Time, Abundance)) +
geom_point(size=4, col='red') + ggtitle("2008 Amblyomma americanum Abundance") +
xlab("Time") + ylab("Nymph Abundance")
Nymph_2008_Plot
这是我用于回归分析的代码(为了运行高阶多项式回归,我只是将 2(度值)交换为相应的多项式阶):
Quadratic_2008_Nymph <- lm(Abundance ~ poly(Time, 2))
summary(Quadratic_2008_Nymph)
这就是我卡住的地方。 如何将多项式回归曲线绘制到我的 plot 上? 如果有一种方法可以使用首选的 ggplot 格式。 如果将这些多项式曲线绘制到 ggplot plot 上不起作用,那么我将切换格式。
如果我需要澄清/提供更多信息,请提前致谢并发表评论。
我在这里要做的第一件事是将您视为日期的数字转换为实际日期。 如果你不这样做, lm
会给出错误的结果; 例如,数据框的第 1 行和第 2 行表示相隔 15 天的数据(20080316 - 20080301 = 15),但第 2 行和第 3 行相隔 17 天,但回归将它们视为相隔 86 天(20080402 - 20080316 = 86)。 这将导致lm
产生无意义的结果。
养成在分析中尽早将表示日期和时间数据的数字或字符串更改为实际日期和时间的习惯是一种很好的做法。 它使您的代码的 rest 变得更加容易。 在你的情况下,那就是:
Mangan_2008_nymph$Time <- as.Date(as.character(Mangan_2008_nymph$Time), "%Y%m%d")
对于 plot 本身,您可以使用geom_smooth
添加多项式回归线。 您指定方法lm
和公式(根据 x 和 y,而不是根据变量名称)。 将map 的线以一种颜色美学使其出现在图例中也是一个不错的主意。 对您希望添加的每个多项式顺序执行一次。
library(ggplot2)
ggplot(Mangan_2008_nymph, aes(Time, Abundance)) +
geom_point(size = 4, col = 'red') +
geom_smooth(method = "lm", formula = y ~ poly(x, 2), aes(color = "2"), se = F) +
geom_smooth(method = "lm", formula = y ~ poly(x, 3), aes(color = "3"), se = F) +
geom_smooth(method = "lm", formula = y ~ poly(x, 4), aes(color = "4"), se = F) +
geom_smooth(method = "lm", formula = y ~ poly(x, 5), aes(color = "5"), se = F) +
geom_smooth(method = "lm", formula = y ~ poly(x, 6), aes(color = "6"), se = F) +
geom_smooth(method = "lm", formula = y ~ poly(x, 7), aes(color = "7"), se = F) +
labs(x = "Time", y = "Nymph abundance", color = "Degree") +
ggtitle("2008 Amblyomma americanum Abundance")
就个人而言,我认为这最终会使 plot 有点混乱,我会考虑降低几个多项式级别以使 plot 更易于阅读。 您还可以轻松添加一些风格调整:
ggplot(Mangan_2008_nymph, aes(Time, Abundance)) +
geom_point(size = 2, col = 'gray50') +
geom_smooth(method = "lm", formula = y ~ poly(x, 3), aes(color = "3"), se = FALSE) +
geom_smooth(method = "lm", formula = y ~ poly(x, 5), aes(color = "5"), se = FALSE) +
geom_smooth(method = "lm", formula = y ~ poly(x, 7), aes(color = "7"), se = FALSE) +
scale_color_brewer(palette = "Set1") +
labs(x = "Time", y = "Nymph abundance", color = "Degree") +
ggtitle("2008 Amblyomma americanum Abundance") +
theme_classic() +
theme(panel.grid.major = element_line())
使用的数据
Mangan_2008_nymph <- structure(list(Time = c(20080301L, 20080316L, 20080402L,
20080416L, 20080428L, 20080514L, 20080527L, 20080608L, 20080627L, 20080709L,
20080722L, 20080806L, 20080818L, 20080901L, 20080915L, 20080930L,
20081013L, 20081029L, 20081110L, 20081124L), Abundance = c(0L,
0L, 26L, 337L, 122L, 232L, 190L, 381L, 148L, 201L, 69L, 55L,
35L, 15L, 6L, 1L, 0L, 1L, 0L, 0L)), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.