2013-12-20 71 views
1

我对R进行了多项式回归以下数据,但无法显示2度多项式的正确图。我得到了2级多项式的正确方程,但是我在脚本的最后部分做了错误的处理。谁能帮忙?由于R中的多项式回归图(二阶)R

这里是我的脚本:

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) 
+1

为什么你认为该图不正确? –

+1

你应该学会发送数据帧到'lm'的'data'参数,然后对lm对象使用'predict'方法。 –

回答

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)