Skip to content

Instantly share code, notes, and snippets.

@marutter
Created September 30, 2010 20:22
Show Gist options
  • Save marutter/605251 to your computer and use it in GitHub Desktop.
Save marutter/605251 to your computer and use it in GitHub Desktop.
library(car)
library(alr3)
#
# Example 1
#
data(sniffer)
plot(sniffer$TankTemp,sniffer$Y)
res <- lm(Y~TankTemp,data=sniffer)
# Combine plots on 1 page in R
par(mfrow=c(2,2))
plot(res,1)
qq.plot(res$res)
plot(res,3)
boxcox(res)
boxcox(res,lambda=seq(-1,1,1/20))
res2 <- lm(log(Y)~TankTemp,data=sniffer)
plot(res2,1)
qq.plot(res2$res)
plot(res2,3)
plot(sniffer$TankTemp,log(sniffer$Y))
abline(res2)
ncv.test(res2)
nd <- data.frame(TankTemp=c(35,60,90))
predict(res2,nd)
exp(predict(res2,nd))
exp(predict(res2,nd,interval="confidence"))
predict(res,nd,interval="confidence")
#
# Example 2
#
data(baeskel)
plot(Sulfur,Tension)
res <- lm(Tension~Sulfur)
summary(res)
res2 <- lm(Tension~sqrt(Sulfur))
res3 <- lm(Tension~log(Sulfur))
summary(res2)
summary(res3)
par(mfrow=c(2,2))
plot(res2,1)
qq.plot(res2$res)
plot(res3,1)
qq.plot(res3$res)
nd <- data.frame(Sulfur=c(.2,.4,.6,.8))
predict(res,nd,interval="confidence")
predict(res2,nd,interval="confidence")
predict(res3,nd,interval="confidence")
plot(Sulfur,Tension)
abline(res)
abline(res2,col="red")
abline(res3,col="blue")
plot(Sulfur,Tension)
abline(res)
newData <- data.frame(Sulfur=seq(min(Sulfur),max(Sulfur),by=(max(Sulfur)-min(Sulfur))/45))
pred.line2 <- predict(res2,newData)
pred.line3 <- predict(res3,newData)
matlines(newData$Sulfur,pred.line2,col="red")
matlines(newData$Sulfur,pred.line3,col="blue")
#
# To Try on your own
#
# Y is Average rent per acre planted to aflalfa
# X1 is Average rent paid for all tillable land
# Construct 95% CI for Y when X1 is 20,60 and 80
# Minimize the width of the CI
data(landrent)
attach(landrent)
plot(X1,Y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment