2013-12-20 6 views
1

다음 데이터에 대해 R의 다항식 회귀를 수행하고 있지만 2 차 다항식의 올바른 그래프를 표시 할 수 없습니다. 2 차항의 다항식 방정식을 얻었지만, 스크립트의 마지막 부분에서 뭔가 잘못되었습니다. 아무도 도와 줄 수 있습니까? 방금 curve로 호출 add=TRUE 누락있는 것처럼R의 다항식 회귀 (2 차 차수) 플롯 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-objects에'predict' 메소드를 사용하는 법을 배워야합니다. –

답변

1

것 같습니다 : 여기

감사

내 스크립트입니다. 이것은 당신이 찾고있는 것을 계획하는 것으로 보입니다 :

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)