2013-12-20 59 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)