【问题标题】:Polynomial regression (second order) plot in RR中的多项式回归(二阶)图
【发布时间】:2014-01-09 19:53:39
【问题描述】:

我正在 R 中对以下数据进行多项式回归,但我无法显示 2 次多项式的正确图形。我得到了正确的 2 次多项式方程,但是我在脚本的最后一部分做错了。有人可以帮忙吗?谢谢

这是我的脚本:

Vegetation_Cover <- c(5,0,10,40,100,30,80,2,70,2,0)
NDVI <- c(0.35,0.32,0.36,0.68,0.75,0.48,0.75,0.35,0.70,0.34,0.28)

plot(Vegetation_Cover,NDVI, main=list
("Vegetation Cover and NDVI",cex=1.5),pch=20,cex=1.4,col="gray0")

sample1 <- data.frame(Vegetation_Cover, NDVI)
sample1

fit2 <- lm(sample1$NDVI ~ sample1$Vegetation_Cover + I(sample1$Vegetation_Cover^2))

summary(fit2)
lm(formula = sample1$NDVI ~ sample1$Vegetation_Cover + I(sample1$Vegetation_Cover^2))
anova(fit2)

pol2 <- function(x) fit2$coefficient[3]*x^2 + fit2$coefficient[2]*x + fit2$coefficient[1]
plot(sample1$Vegetation_Cover, sample1$NDVI, type="p", lwd=3)
pol2 <- function(x) fit2$coefficient[3]*x^2 + fit2$coefficient[2]*x + fit2$coefficient[1]
curve(pol2, col="red", lwd=2)
points(sample1$Vegetation_Cover, sample1$NDVI, type="p", lwd=3)

【问题讨论】:

  • 为什么你认为图表不正确?
  • 你应该学会将数据帧发送到lm的'data'参数,然后对lm-objects使用predict方法。

标签: r graph plot regression


【解决方案1】:

您似乎只是缺少add=TRUE 来拨打curve。这似乎绘制了您要查找的内容:

pol2 <- function(x) fit2$coefficient[3]*x^2 + fit2$coefficient[2]*x + fit2$coefficient[1]
plot(sample1$Vegetation_Cover, sample1$NDVI, type="p", lwd=3)
curve(pol2, col="red", lwd=2, add=T)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-07
    • 2015-08-31
    • 2021-07-25
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    相关资源
    最近更新 更多