I am attempting to fit polynomials of different orders to a dataset, and plot the resulting curves on a scatterplot. My first order polynomial looks fine:
but when I add higher order terms a bunch of nonsense (to me) appears. Any ideas why that would be the case?
The following is my third-degree curve:
There is a vaguely third-degree polynomial-looking thing in there, but its y-intercept seems to be around 5, whereas a summary of the polynomial gives an intercept of 3.5:
Here is the relevant code:
PS1 <- read.csv("PhrynoSpermo.csv")
phryno <- PS1$Phrynosoma.solare[1:330]
spermo <- PS1$Spermophilus.tereticaudus[1:330]
plot(spermo, phryno, pch=20, ylab="P. solare", xlab = "S. tereticaudus")
fit1 <- lm(phryno~spermo)
fit2 <- lm(phryno~poly(spermo,2))
fit3 <- lm(phryno~poly(spermo,3))
fit4 <- lm(phryno~poly(spermo,4))
lines(spermo,predict(fit1),col="red")
lines(spermo,predict(fit2),col="green")
lines(spermo,predict(fit3),col="blue")
lines(spermo,predict(fit4),col="purple")
And I realize none of these fit very well, but I just want to understand what's going on.