This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(b1 <- sum((x-mean(x))*(y-mean(y)))/sum((x-mean(x))^2)) | |
(b0 <- mean(y)-b1*mean(x)) | |
yhat <- b0+b1*x | |
e <- y-yhat | |
SSE <- sum(e^2) | |
MSE <- SSE/(length(y)-2) | |
plot(faithful$waiting,faithful$eruptions,main="Old Faithful",xlab="Wait time (minutes)", | |
ylab="Eruption duration (minutes)") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(b1 <- sum((x-mean(x))*(y-mean(y)))/sum((x-mean(x))^2)) | |
(b0 <- mean(y)-b1*mean(x)) | |
yhat <- b0+b1*x | |
e <- y-yhat | |
SSE <- sum(e^2) | |
MSE <- SSE/(length(y)-2) | |
plot(faithful$waiting,faithful$eruptions,main="Old Faithful",xlab="Wait time (minutes)", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unm <- read.csv("enrollment.csv") | |
plot(unm$unem,unm$enroll,xlab="Unemployment (%)",ylab="Enrollment") | |
# | |
# We could use our commands, but there is built in tools | |
# | |
res <- lm(enroll~unem,data=unm) | |
names(res) | |
res$coef | |
res$fitted | |
res$resid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unm <- read.csv("enrollment.csv") | |
plot(unm$unem,unm$enroll,xlab="Unemployment (%)",ylab="Enrollment") | |
# | |
# We could use our commands, but there are built in tools | |
# | |
res <- lm(enroll~unem,data=unm) | |
names(res) | |
res$coef | |
res$fitted | |
res$resid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install.packages("faraway") | |
library(faraway) # for data | |
# | |
# A regression that meets the assumptions | |
# | |
plot(stat500$midterm,stat500$final) | |
res <- lm(stat500$final~stat500$midterm) | |
res <- lm(final~midterm,data=stat500) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data <- read.csv("cdi.csv") | |
attach(data) | |
names(data) | |
plot(p.bach,per.cap.inc,pch=as.character(region),col=as.numeric(region)) | |
resNC <- lm(per.cap.inc~p.bach,data[region=="NC",]) | |
resNE <- lm(per.cap.inc~p.bach,data[region=="NE",]) | |
resS <- lm(per.cap.inc~p.bach,data[region=="S",]) | |
resW <- lm(per.cap.inc~p.bach,data[region=="W",]) | |
confint(resNC,level=.90) | |
confint(resNE,level=.90) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data <- read.csv("sparrow.csv") | |
attach(data) | |
plot(ppha,pairs,xlab="Pedestrians per ha per min",ylab="Breeding pairs per ha") | |
res <- lm(pairs~ppha) | |
summary(res) | |
abline(res) | |
# Add the quadratic term | |
res2 <- lm(pairs~ppha+ppha^2) | |
summary(res2) | |
res2 <- lm(pairs~ppha+I(ppha^2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(faraway) | |
data(savings) | |
attach(savings) | |
summary(savings) | |
help(savings) | |
pairs(savings) | |
# | |
# A better 'pairs' plot | |
#res <- lm(sr~pop15) | |
summary(res) |
OlderNewer