Created
September 23, 2010 18:24
-
-
Save marutter/594095 to your computer and use it in GitHub Desktop.
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) | |
summary(res) | |
plot(res$fit,res$res) | |
sr <- res$res/sd(res$res) | |
plot(res$fit,sr) | |
identify(res$fit,sr) | |
# | |
# Is it normal? | |
# | |
hist(res$fit) | |
x <- rnorm(100) | |
hist(x) | |
qqnorm(x) | |
qqline(x) | |
# | |
# A better plot | |
# | |
install.packages("car") | |
library(car) | |
qqPlot(x) | |
qqPlot(x,id.n=3) | |
# | |
# Our regression | |
# | |
qqnorm(res$fit) | |
qqline(res$fit) | |
qqPlot(res$fit,id.n=3) | |
# | |
# A test for normality | |
# | |
shapiro.test(res$res) | |
# | |
# A test for constant variance | |
# | |
ncvTest(res) | |
# | |
# Example 2 | |
# | |
install.packages("alr3") | |
library(alr3) | |
data(sniffer) | |
plot(sniffer$TankTemp,sniffer$Y) | |
res <- lm(sniffer$Y~sniffer$TankTemp) | |
abline(res) | |
plot(res$fit,res$res) | |
qqPlot(res$res) | |
shapiro.test(res$res) | |
ncvTest(res) | |
# | |
# Example 3 | |
# | |
data(forbes) | |
plot(forbes) | |
res <- lm(pres~bp,data=forbes) | |
abline(res) | |
plot(res$fit,res$res) | |
sr <- res$res/sd(res$res) | |
plot(res$fit,sr) | |
identify(res$fit,sr) | |
res2 <- lm(pres~bp,data=forbes,subset=c(-12)) | |
summary(res) | |
summary(res2) | |
plot(res2$fit,res2$res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment