在 R 中绘制多项式回归曲线
Plotting Polynomial Regression Curves in R
我对我包含的数据进行了 运行 多项式回归,从 Quadratic 到 Septic,但我一直试图在我的散点图上绘制这些回归曲线。我请求帮助创建适用于每个多项式阶数的代码。
Time <- Mangan_2008_total$YearMonthDay
Abundance <- Mangan_2008_total$`Total Nymph Aa`
以上是我用来将我包含在这个 post 中的数据与更大的数据集隔离开来的代码。以下为参考数据
(日期目前采用公历格式。我计划在某个时候将它们转换为儒略历。)
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 中制作的散点图的代码:
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)
这就是我卡住的地方。如何将多项式回归曲线绘制到我的绘图上?如果有一种方法可以使用首选的 ggplot 格式来执行此操作。如果将这些多项式曲线绘制到 ggplot 图上不起作用,那么我将切换格式。
提前致谢,如果我需要clarify/provide更多信息,请发表评论。
我在这里要做的第一件事是将您视为日期的数字转换为实际日期。如果不这样做,lm
将给出错误的结果;例如,数据框的第 1 行和第 2 行表示相隔 15 天的数据 (20080316 - 20080301 = 15),但是第 2 行和第 3 行相隔 17 天,但回归会将它们视为相隔 86 天 (20080402 - 20080316 = 86)。这将导致 lm
产生无意义的结果。
最好养成在分析中尽早将表示日期和时间数据的数字或字符串更改为实际日期和时间的习惯。它使您的其余代码更容易。在你的情况下,那只是:
Mangan_2008_nymph$Time <- as.Date(as.character(Mangan_2008_nymph$Time), "%Y%m%d")
对于绘图本身,您可以使用 geom_smooth
添加多项式回归线。您指定方法 lm
和公式(根据 x 和 y, 而不是 根据变量名称)。将线条映射到颜色美学以使其出现在图例中也是一个好主意。对每个要添加的多项式阶数执行一次。
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")
就我个人而言,我认为这最终会使情节变得有些混乱,我会考虑删除几个多项式级别以使该情节更易于阅读。您还可以轻松添加一些风格调整:
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"))
我对我包含的数据进行了 运行 多项式回归,从 Quadratic 到 Septic,但我一直试图在我的散点图上绘制这些回归曲线。我请求帮助创建适用于每个多项式阶数的代码。
Time <- Mangan_2008_total$YearMonthDay
Abundance <- Mangan_2008_total$`Total Nymph Aa`
以上是我用来将我包含在这个 post 中的数据与更大的数据集隔离开来的代码。以下为参考数据
(日期目前采用公历格式。我计划在某个时候将它们转换为儒略历。)
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 中制作的散点图的代码:
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)
这就是我卡住的地方。如何将多项式回归曲线绘制到我的绘图上?如果有一种方法可以使用首选的 ggplot 格式来执行此操作。如果将这些多项式曲线绘制到 ggplot 图上不起作用,那么我将切换格式。
提前致谢,如果我需要clarify/provide更多信息,请发表评论。
我在这里要做的第一件事是将您视为日期的数字转换为实际日期。如果不这样做,lm
将给出错误的结果;例如,数据框的第 1 行和第 2 行表示相隔 15 天的数据 (20080316 - 20080301 = 15),但是第 2 行和第 3 行相隔 17 天,但回归会将它们视为相隔 86 天 (20080402 - 20080316 = 86)。这将导致 lm
产生无意义的结果。
最好养成在分析中尽早将表示日期和时间数据的数字或字符串更改为实际日期和时间的习惯。它使您的其余代码更容易。在你的情况下,那只是:
Mangan_2008_nymph$Time <- as.Date(as.character(Mangan_2008_nymph$Time), "%Y%m%d")
对于绘图本身,您可以使用 geom_smooth
添加多项式回归线。您指定方法 lm
和公式(根据 x 和 y, 而不是 根据变量名称)。将线条映射到颜色美学以使其出现在图例中也是一个好主意。对每个要添加的多项式阶数执行一次。
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")
就我个人而言,我认为这最终会使情节变得有些混乱,我会考虑删除几个多项式级别以使该情节更易于阅读。您还可以轻松添加一些风格调整:
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"))