Skip to content

Instantly share code, notes, and snippets.

@marutter
Created October 14, 2010 18:09
Show Gist options
  • Save marutter/626688 to your computer and use it in GitHub Desktop.
Save marutter/626688 to your computer and use it in GitHub Desktop.
#
# What Polynomial?
#
data <- read.csv("sparrow.csv")
attach(data)
plot(ppha,pairs,xlab="Pedestrians per ha per min",ylab="Breeding pairs per ha")
res3 <- lm(pairs~ppha+I(ppha^2)+I(ppha^3))
summary(res3)
res3b <- lm(pairs~poly(ppha,3))
summary(res3b)
plot(ppha,pairs,xlab="Pedestrians per ha per min",ylab="Breeding pairs per ha")
nd <- data.frame(ppha=seq(min(ppha),max(ppha),(max(ppha)-min(ppha))/250))
matlines(nd,predict(res3,nd),type="l",col="red",lwd=8)
matlines(nd,predict(res3b,nd),type="l",col="blue",lwd=2)
#
# Design Matrix
#
model.matrix(res3)
model.matrix(res3b)
(a <- model.matrix(res3b)[,2])
(b <- model.matrix(res3b)[,4])
crossprod(a,b)
summary(lm(pairs~poly(ppha,2)))
summary(lm(pairs~poly(ppha,5)))
summary(lm(pairs~poly(ppha,10)))
res3c <- lm(pairs~poly(ppha,3,raw=TRUE))
summary(res3c)
model.matrix(res3c)
res11 <- lm(pairs~poly(ppha,11,raw=TRUE))
summary(res11)
resorth <- lm(pairs~poly(ppha,11))
summary(resorth)
#
# Is the 11th degree polynomial better than the cubic?
#
res3 <- lm(pairs~poly(ppha,3))
res11 <- lm(pairs~poly(ppha,11))
summary(res3)$r.squared
summary(res11)$r.squared
summary(res3)$adj.r.squared
summary(res11)$adj.r.squared
anova(res3,res11)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment