简体   繁体   English

在 R 中绘制多项式回归曲线

[英]Plotting Polynomial Regression Curves in R

I have run polynomial regressions on the data that I am including from Quadratic to Septic but I am stuck trying to plot these regression curves on my scatter plot.我已经对我包含的从二次到化粪池的数据进行多项式回归,但我被困在我的散点 plot 上的这些回归曲线上。 I am asking for help creating code that will work for each polynomial order.我正在寻求帮助创建适用于每个多项式顺序的代码。

Time <- Mangan_2008_total$YearMonthDay
Abundance <- Mangan_2008_total$`Total Nymph Aa`

Above is the code I used to isolate the data I included in this post from the larger data set.上面是我用来将我在这篇文章中包含的数据与更大的数据集隔离开来的代码。 Below is the data for reference.以下是供参考的数据。

(The dates are currently in the Gregorian calendar format. I plan to convert them to the Julian Days calendar at some point.) (日期目前采用公历格式。我计划在某个时候将它们转换为儒略日日历。)

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

I compiled that data into a data frame for easier manipulation:我将这些数据编译成一个数据框以便于操作:

Mangan_2008_nymph <- data.frame(Time, Abundance)

Here is the code for the scatter plot I made in ggplot:这是我在 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

Here is the code I used for the regression analysis (To run higher order polynomial regressions, I simply swapped the 2 (the degree value) for the corresponding polynomial order):这是我用于回归分析的代码(为了运行高阶多项式回归,我只是将 2(度值)交换为相应的多项式阶):

Quadratic_2008_Nymph <- lm(Abundance ~ poly(Time, 2))
summary(Quadratic_2008_Nymph)

This is where I get stuck.这就是我卡住的地方。 How do I graph the polynomial regression curves onto my plot?如何将多项式回归曲线绘制到我的 plot 上? If there is a way to do this with the ggplot format that would be preferred.如果有一种方法可以使用首选的 ggplot 格式。 If plotting these polynomial curves onto a ggplot plot wont work then I will switch my formatting.如果将这些多项式曲线绘制到 ggplot plot 上不起作用,那么我将切换格式。

Thanks in advance and comment if I need to clarify/provide more information.如果我需要澄清/提供更多信息,请提前致谢并发表评论。

The first thing I would do here is to convert the numbers you are treating as dates into actual dates.我在这里要做的第一件事是将您视为日期的数字转换为实际日期。 If you don't do this, lm will give the wrong result;如果你不这样做, lm会给出错误的结果; as an example, rows 1 and 2 of your data frame represent data 15 days apart (20080316 - 20080301 = 15), but then rows 2 and 3 are 17 days apart, yet the regression will see them as being 86 days apart (20080402 - 20080316 = 86).例如,数据框的第 1 行和第 2 行表示相隔 15 天的数据(20080316 - 20080301 = 15),但第 2 行和第 3 行相隔 17 天,但回归将它们视为相隔 86 天(20080402 - 20080316 = 86)。 This will cause lm to produce nonsensical results.这将导致lm产生无意义的结果。

It is good practice to get into the habit of changing numbers or character strings that represent date and time data into actual dates and times as early in your analysis as you can.养成在分析中尽早将表示日期和时间数据的数字或字符串更改为实际日期和时间的习惯是一种很好的做法。 It makes the rest of your code much easier.它使您的代码的 rest 变得更加容易。 In your case, that would just be:在你的情况下,那就是:

Mangan_2008_nymph$Time <- as.Date(as.character(Mangan_2008_nymph$Time), "%Y%m%d")

For the plot itself, you can add the polynomial regression lines using geom_smooth .对于 plot 本身,您可以使用geom_smooth添加多项式回归线。 You specify the method lm , and the formula (in terms of x and y, not in terms of the variable names).您指定方法lm和公式(根据 x 和 y,而不是根据变量名称)。 It is also good idea to map the line to a color aesthetic so that it appears in a legend.将map 的线以一种颜色美学使其出现在图例中也是一个不错的主意。 Do this once for each polynomial order you wish to add.对您希望添加的每个多项式顺序执行一次。

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") 

Personally, I think this ends up making the plot a little cluttered, and I would consider dropping a couple of polynomial levels to make this plot easier to read.就个人而言,我认为这最终会使 plot 有点混乱,我会考虑降低几个多项式级别以使 plot 更易于阅读。 You can also easily add a few stylistic tweaks:您还可以轻松添加一些风格调整:

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())

在此处输入图像描述


Data used使用的数据

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM