Вы находитесь на странице: 1из 15

simon sheater book data

http://www.stat.tamu.edu/~sheather/book/r_code.php
http://dist.stat.tamu.edu/pub/rvideos/
http://www.stat.tamu.edu/~sheather/book/data_sets.php

MetroArea PriceChange LoanPaymentsOverdue


Atlanta 1.2 4.55
Boston -3.4 3.31
Chicago -0.9 2.99
Dallas 0.8 4.26
Denver -0.7 3.56
Detroit -9.7 4.71
LasVegas -6.1 4.9
LosAngeles -4.8 3.05
MiamiFt.Lauderdale -6.4 5.63
MinneapolisStPaul -3.4 3.01
NewYork -3.8 3.29
Phoenix -7.3 3.26
Portland 3.8 1.93
SanDiego -7.8 3.45
SanFrancisco -4.1 2.29
Seattle 6.9 1.65
Tampa -8.8 4.6
WashingtonDC -7.2 3.14

Day Invoices Time


1 149 2.1
2 60 1.8
3 188 2.3
4 23 0.8
5 201 2.7
6 58 1
7 77 1.7
8 222 3.1
9 181 2.8
10 30 1
11 110 1.5
12 83 1.2
13 60 0.8
14 25 1
15 173 2
16 169 2.5
17 190 2.9
18 233 3.4
19 289 4.1
20 45 1.2
21 193 2.5
22 70 1.8
23 241 3.8
24 103 1.5
25 163 2.8
26 120 2.5
27 201 3.3
28 135 2
29 80 1.7
30 29 1.5
#March 17, 2009

#Please change the file path in the command below to coincide with where you have
stored the data files
setwd("C:/Users/sheather.ADSTAT/Documents/docs/AModernApproachToRegression/Data")

kicker <- read.csv("FieldGoals2003to2006.csv",header=T)

attach(kicker)

#Figure 1.1 on page 2


plot(kicker$FGtM1,kicker$FGt,
main="Unadjusted Correlation = -0.139",
xlab="Field Goal Percentage in Year t-1",ylab="Field Goal Percentage in Year t")

#p-values on page 3
fit.1 <- lm(FGt~FGtM1 +Name +FGtM1:Name,data=kicker)
anova(fit.1)

#slope and interecepts of lines in Figure 1.2 on page 3


fit.2 <- lm(FGt ~ Name + FGtM1,data=kicker)
fit.2

#Figure 1.2 on page 3


plot(kicker$FGtM1,kicker$FGt,
main="Slope of each line = -0.504",
xlab="Field Goal Percentage in Year t-1",
ylab="Field Goal Percentage in Year t")
tt <- seq(60,100,length=1001)
slope.piece <- summary(fit.2)$coef[20]*tt
lines(tt,summary(fit.2)$coef[1]+slope.piece,lty=2)
for (i in 2:19)
{lines(tt,summary(fit.2)$coef[1]+summary(fit.2)$coef[i]+slope.piece,lty=2)}

detach(kicker)

circulation <- read.table("circulation.txt", header=TRUE, sep="\t")


attach(circulation)

#Figure 1.3 on page 5


plot(Weekday,Sunday,xlab="Weekday Circulation",ylab="Sunday Circulation",
pch=Tabloid.with.a.Serious.Competitor+1,col=Tabloid.with.a.Serious.Competitor+1)
legend(110000, 1600000,legend=c("0","1"),
pch=1:2,col=1:2,title="Tabloid dummy variable")

#Figure 1.4 on page 5


plot(log(Weekday),log(Sunday),xlab="log(Weekday Circulation)",ylab="log(Sunday
Circulation)",
pch=Tabloid.with.a.Serious.Competitor+1,
col=Tabloid.with.a.Serious.Competitor+1)
legend(11.6, 14.1,legend=c("0","1"),pch=1:2,col=1:2,
title="Tabloid dummy variable")

detach(circulation)

nyc <- read.csv("nyc.csv",header=TRUE)


attach(nyc)

#Figure 1.5 on page 7


pairs(Price~Food+Decor+Service,data=nyc,gap=0.4,
cex.labels=1.5)

#Figure 1.6 on page 10


boxplot(Price~East,ylab="Price",
xlab="East (1 = East of Fifth Avenue)")

detach(nyc)

Bordeaux <- read.csv("Bordeaux.csv", header=TRUE)


attach(Bordeaux)

#Figure 1.7 on page 10


pairs(Price~ParkerPoints+CoatesPoints,data=Bordeaux,gap=0.4,cex.labels=1.5)

#Figure 1.8 on page 11


par(mfrow=c(2,3))
boxplot(Price~P95andAbove,ylab="Price",xlab="P95andAbove")
boxplot(Price~FirstGrowth,ylab="Price",xlab="First Growth")
boxplot(Price~CultWine,ylab="Price",xlab="Cult Wine")
boxplot(Price~Pomerol,ylab="Price",xlab="Pomerol")
boxplot(Price~VintageSuperstar,ylab="Price",xlab="Vintage Superstar")

#Figure 1.9 on page 12


par(mfrow=c(1,1))
pairs(log(Price)~log(ParkerPoints)
+log(CoatesPoints),data=Bordeaux,gap=0.4,cex.labels=1.5)

#Figure 1.10 on page 13


par(mfrow=c(2,3))
boxplot(log(Price)~P95andAbove,ylab="log(Price)",
xlab="P95andAbove")
boxplot(log(Price)~FirstGrowth,ylab="log(Price)",
xlab="First Growth")
boxplot(log(Price)~CultWine,ylab="log(Price)",
xlab="Cult Wine")
boxplot(log(Price)~Pomerol,ylab="log(Price)",
xlab="Pomerol")
boxplot(log(Price)~VintageSuperstar,ylab="log(Price)",
xlab="Vintage Superstar")

detach(Bordeaux)

==========================
#March 17, 2009

#Please change the file path in the command below to coincide with where you have
stored the data files
setwd("C:/Users/sheather.ADSTAT/Documents/docs/AModernApproachToRegression/Data")

production <- read.table("production.txt",header=TRUE)


attach(production)

#Figure 2.1 on page 16


par(mfrow=c(1,1))
plot(production$RunSize,production$RunTime,xlab="Run Size", ylab="Run Time")

#R output on page 19
m1 <- lm(RunTime~RunSize)
summary(m1)

#Figure 2.3 on page 20


plot(production$RunSize,production$RunTime,xlab="Run Size", ylab="Run Time")
abline(lsfit(production$RunSize,production$RunTime))

#t-value on page 23
tval <- qt(1-0.05/2,18)
tval

#95% confidence intervals on page 24


round(confint(m1,level=0.95),3)

#R output on page 27
predict(m1,newdata=data.frame(RunSize=c(50,100,150,200,250,300,350)),interval="conf
idence",level=0.95)
predict(m1,newdata=data.frame(RunSize=c(50,100,150,200,250,300,350)),interval="pred
iction",level=0.95)

#R output on page 30
anova(m1)
detach(production)

changeover_times <- read.table("changeover_times.txt",header=TRUE)


attach(changeover_times)

#R output on page 31
m1 <- lm(Changeover~New)
summary(m1)

#Figure 2.5 on page 32


par(mfrow=c(2,2))
plot(New,Changeover,xlab="Dummy variable, New",ylab="Change Over Time")
abline(lsfit(New,Changeover))
boxplot(Changeover~New,xlab="Dummy variable, New",ylab="Change Over Time")
boxplot(Changeover~Method,ylab="Change Over Time",xlab="Method")

#t-value on page 33
tval <- qt(1-0.05/2,118)
tval

detach(changeover_times)

#################EXERCISES

#Exercise 2.8.1

playbill <- read.csv("C:playbill.csv",header=TRUE)


attach(playbill)
#Figure 2.6 on page 38
par(mfrow=c(1,1))
plot(LastWeek,CurrentWeek,xlab="Gross box office results previous week",
ylab="Gross box office results current week")

detach(playbill)

#Exercise 2.8.3

invoice <- read.table("invoices.txt",header=TRUE)


attach(invoice)

#Figure 2.7 on page 40


par(mfrow=c(1,1))
plot(Invoices,Time,xlab="Number of Invoices",ylab="Processing Time")
abline(lsfit(Invoices,Time))

#Output from R on page 40


m1 <- lm(Time~Invoices)
summary(m1)
mean(Time)
median(Time)
mean(Invoices)
median(Invoices)

detach(invoice)

#Exercise 2.8.5
problem5 <- read.table("Ch2Problem5.txt",header=TRUE)
attach(problem5)

#Figure 2.8 on page 42


par(mfrow=c(1,2))
plot(x1,y,main="Model 1")
abline(lsfit(x1,y))
plot(x2,y,main="Model 2")
abline(lsfit(x2,y))

detach(problem5)

======================================

#March 17, 2009

#Please change the file path in the command below to coincide with where you have
stored the data files
setwd("C:/Users/sheather.ADSTAT/Documents/docs/AModernApproachToRegression/Data")

anscombe <- read.table("anscombe.txt",header=TRUE)


attach(anscombe)

#Figure 3.1 on page 46


par(mfrow=c(2,2))
plot(x1,y1,xlim=c(4,20),ylim=c(3,14),main="Data Set 1")
abline(lsfit(x1,y1))
plot(x2,y2,xlim=c(4,20),ylim=c(3,14),main="Data Set 2")
abline(lsfit(x2,y2))
plot(x3,y3,xlim=c(4,20),ylim=c(3,14),main="Data Set 3")
abline(lsfit(x3,y3))
plot(x4,y4,xlim=c(4,20),ylim=c(3,14),main="Data Set 4")
abline(lsfit(x4,y4))

#Regression output on page 47


m1 <- lm(y1~x1)
summary(m1)
m2 <- lm(y2~x2)
summary(m2)
m3 <- lm(y3~x3)
summary(m3)
m4 <- lm(y4~x4)
summary(m4)

#Figure 3.2 on page 48


par(mfrow=c(2,2))
plot(x1,m1$residuals,ylab="Residuals",xlim=c(4,20),ylim=c(-3.5,3.5),main="Data Set
1")
plot(x2,m2$residuals,ylab="Residuals",xlim=c(4,20),ylim=c(-3.5,3.5),main="Data Set
2")
plot(x3,m3$residuals,ylab="Residuals",xlim=c(4,20),ylim=c(-3.5,3.5),main="Data Set
3")
plot(x4,m4$residuals,ylab="Residuals",xlim=c(4,20),ylim=c(-3.5,3.5),main="Data Set
4")

#Figure 3.3 on page 50


par(mfrow=c(1,2))
plot(x2,y2,,ylim=c(3,10))
abline(lsfit(x2,y2))
plot(x2,m2$residuals,ylab="Residuals",ylim=c(-2,2),main="Data Set 2")

detach(anscombe)

huber <- read.table("huber.txt",header=TRUE)


attach(huber)

#Regression output on page 54


mBad <- lm(YBad~x)
summary(mBad)
mGood <- lm(YGood~x)
summary(mGood)

#Figure 3.7 on page 55


par(mfrow=c(1,2))
plot(x,YBad,ylim=c(-12,3))
abline(lsfit(x,YBad))
plot(x,YGood,ylim=c(-12,3))
abline(lsfit(x,YGood))

#Leverage values in Table 3.3 on page 57


lm.influence(mBad)$hat
lm.influence(mGood)$hat

#Regression output and Figure 3.8 on page 58


xq <- x^2
mBadq <- lm(YBad~x+I(x^2))
summary(mBadq)
xx <- c(-4:10)
yy <- summary(mBadq)$coef[1] + summary(mBadq)$coef[2]*xx + summary(mBadq)
$coef[3]*xx^2
par(mfrow=c(1,1))
plot(xx,yy,ylim=c(-3,3),type="l",ylab="YBad",xlab="x")
points(x,YBad)

detach(huber)

bonds <- read.table("bonds.txt",header=TRUE)


attach(bonds)

#Figure 3.9 on page 63


par(mfrow=c(1,1))
plot(CouponRate,BidPrice,xlab="Coupon Rate (%)", ylab="Bid Price
($)",ylim=c(85,120),xlim=c(2,14))
abline(lsfit(CouponRate,BidPrice))

#Regression output on page 63


m1 <- lm(BidPrice~CouponRate)
summary(m1)

#95% confidence intervals on page 63


round(confint(m1,level=0.95),3)

#Table 3.4 on page 62


leverage1 <- hatvalues(m1)
StanRes1 <- rstandard(m1)
residual1 <- m1$residuals
cbind(Case,CouponRate,BidPrice,round(leverage1,3),round(residual1,3),round(StanRes1
,3))

#Figure 3.10 on page 64


plot(CouponRate,StanRes1,xlab="Coupon Rate (%)", ylab="Standardized
Residuals",xlim=c(2,14))
abline(h=2,lty=2)
abline(h=-2,lty=2)
identify(CouponRate,StanRes1,Case)
# Click near a point to identify its Case.
# This continues until you select "Stop" after clicking the right mouse button.

#Regression output on page 66


m2 <- update(m1, subset=(1:35)[-c(4,13,35)])
summary(m2)

#Figure 3.11 on page 65


plot(CouponRate[-c(4,13,35)],BidPrice[-c(4,13,35)],xlab="Coupon Rate (%)",
ylab="Bid Price ($)",ylim=c(85,120),xlim=c(2,14),main="Regular Bonds")
abline(m2)

#Figure 3.12 on page 67


StanRes2 <- rstandard(m2)
plot(CouponRate[-c(4,13,35)],StanRes2,xlab="Coupon Rate (%)", ylab="Standardized
Residuals",xlim=c(2,14),main="Regular Bonds")
abline(h=2,lty=2)
abline(h=-2,lty=2)

#Figure 3.13 on page 68


cd1 <- cooks.distance(m1)
plot(CouponRate,cd1,xlab="Coupon Rate (%)", ylab="Cook's Distance")
abline(h=4/(35-2),lty=2)
identify(CouponRate,cd1,Case)
# Click near a point to identify its Case.
# This continues until you select "Stop" after clicking the right mouse button.

detach(bonds)

production <- read.table("production.txt",header=TRUE)


attach(production)

m1 <- lm(RunTime~RunSize)

#Figure 3.14 on page 70


par(mfrow=c(2,2))
plot(m1)

detach(production)

cleaning <- read.table("cleaning.txt",header=TRUE)


attach(cleaning)

#Figure 3.15 on page 71


par(mfrow=c(1,1))
plot(Crews,Rooms,xlab="Number of Crews",ylab="Number of Rooms Cleaned")
abline(lsfit(Crews,Rooms))

#Regression output on pages 72 and 73


m1 <- lm(Rooms~Crews)
summary(m1)
predict(m1,newdata=data.frame(Crews=c(4,16)),interval="prediction",level=0.95)

#Figure 3.16 on page 73


StanRes1 <- rstandard(m1)
plot(Crews,StanRes1,xlab="Number of Crews", ylab="Standardized Residuals")

#Figure 3.17 on page 74


sabs <- sqrt(abs(StanRes1))
plot(Crews,sabs,xlab="Number of Crews", ylab="Square Root(|Standardized
Residuals|)")
abline(lsfit(Crews,sabs))

#Figure 3.18 on page 75


par(mfrow=c(2,2))
plot(m1)

#Figure 3.19 on page 75


sqrt(tapply(Rooms,Crews,var))
sds <- c(3.000000,4.966555,4.690416,6.642665,7.927123,7.28991,12.000463)
xx <- c(2,4,6,8,10,12,16)
par(mfrow=c(1,1))
plot(xx,sds,xlab="Number of Crews", ylab="Standard deviation(Rooms Cleaned)")
abline(lsfit(xx,sds))

#Regression output on page 77


sqrtcrews <- sqrt(Crews)
sqrtrooms <- sqrt(Rooms)
m2 <- lm(sqrtrooms~sqrtcrews)
summary(m2)
predict(m2,newdata=data.frame(sqrtcrews=c(2,4)),interval="prediction",level=0.95)

#Figure 3.20 on page 78


par(mfrow=c(1,2))
plot(sqrt(Crews),sqrt(Rooms),xlab="Square Root(Number of Crews)",ylab="Square
Root(Number of Rooms Cleaned)")
abline(lsfit(sqrt(Crews),sqrt(Rooms)))
StanRes2 <- rstandard(m2)
plot(sqrtcrews,StanRes2,xlab="Square Root(Number of Crews)", ylab="Standardized
Residuals")

#Figure 3.21 on page 78


par(mfrow=c(2,2))
plot(m2)

detach(cleaning)

confood1 <- read.table("confood1.txt",header=TRUE)


attach(confood1)

#Figure 3.22 on page 80


par(mfrow=c(1,1))
plot(Price,Sales)
abline(lsfit(Price,Sales))

#Figure 3.23 on page 81


plot(log(Price),log(Sales),xlab="log(Price)",ylab="log(Sales)")
abline(lsfit(log(Price),log(Sales)))

#Regression output on page 82


m1 <- lm(log(Sales)~log(Price))
summary(m1)

#Figure 3.24 on page 82


StanRes1 <- rstandard(m1)
plot(log(Price),StanRes1,xlab="log(Price)", ylab="Standardized Residuals")

detach(confood1)

responsetransformation <- read.table("responsetransformation.txt",header=TRUE)


attach(responsetransformation)

#Figure 3.25 on page 84


plot(x,y)

#Figure 3.26 on page 85


plot(x,y)
m1 <- lm(y~x)
par(mfrow=c(1,2))
StanRes1 <- rstandard(m1)
absrtsr1 <- sqrt(abs(StanRes1))
plot(x,StanRes1,ylab="Standardized Residuals")
plot(x,absrtsr1,ylab="Square Root(|Standardized Residuals|)")

#Figure 3.27 on page 86


par(mfrow=c(3,2))
plot(density(y,bw="SJ",kern="gaussian"),type="l",
main="Gaussian kernel density estimate",xlab="y")
rug(y)
boxplot(y,ylab="Y")
qqnorm(y, ylab = "Y")
qqline(y, lty = 2, col=2)
sj <- bw.SJ(x,lower = 0.05, upper = 100)
plot(density(x,bw=sj,kern="gaussian"),type="l",
main="Gaussian kernel density estimate",xlab="x")
rug(x)
boxplot(x,ylab="x")
qqnorm(x, ylab = "x")
qqline(x, lty = 2, col=2)

#Figure 3.28 on page 87


install.packages("alr3")
#You will be asked to
#--- Please select a CRAN mirror for use in this session ---
library(alr3)
par(mfrow=c(1,1))
inverse.response.plot(m1,key=TRUE)
# Click on the section of the plot that you wish to put the figure legend

#Figure 3.29 on page 88


inverse.response.plot(m1,lam=c(-1,-0.5, -0.33, -0.25, 0, 0.25, 0.33, 0.5,1))
lambda <- c(-1,-0.5, -0.33, -0.25, 0, 0.25, 0.33, 0.5,1)
RSS <- c(46673.9,24090.7,15264.2,11637.1,3583.8,440,266,880.2,7136.9)
plot(lambda,RSS,type="l",ylab=expression(RSS(lambda)),xlab=expression(lambda))

#Figure 3.30 on page 92


library(MASS)
par(mfrow=c(1,2))
boxcox(m1,lambda=seq(0.28,0.39,length=20))
boxcox(m1,lambda=seq(0.325,0.34,length=20))

#Regression output & Figure 3.31 on page 93


ty <- y^(1/3)
par(mfrow=c(2,2))
sj <- bw.SJ(ty,lower = 0.05, upper = 100)
plot(density(ty,bw=sj,kern="gaussian"),type="l",
main="Gaussian kernel density estimate",xlab=expression(Y^(1/3)))
rug(ty)
boxplot(ty,ylab=expression(Y^(1/3)))
qqnorm(ty, ylab = expression(Y^(1/3)))
qqline(ty, lty = 2, col=2)
m2 <- lm(ty~x)
plot(x,ty,ylab=expression(Y^(1/3)))
abline(m2)
summary(m2)

detach(responsetransformation)

library(alr3)
data(salarygov)
attach(salarygov)

#Figure 3.32 on page 96


m1 <- lm(MaxSalary~Score)
par(mfrow=c(2,2))
plot(Score,MaxSalary)
abline(m1,lty=2,col=2)
StanRes1 <- rstandard(m1)
absrtsr1 <- sqrt(abs(StanRes1))
plot(Score,StanRes1,ylab="Standardized Residuals")
plot(Score,absrtsr1,ylab="Square Root(|Standardized Residuals|)")
abline(lsfit(Score,absrtsr1),lty=2,col=2)

#Output from R on page 96


summary(tranxy <- bctrans(~MaxSalary + Score))

#Figure 3.33 on page 97


par(mfrow=c(3,2))
plot(density(MaxSalary,bw="SJ",kern="gaussian"),type="l",
main="Gaussian kernel density estimate",xlab="MaxSalary")
rug(MaxSalary)
boxplot(MaxSalary,ylab="MaxSalary")
qqnorm(MaxSalary, ylab = "MaxSalary")
qqline(MaxSalary, lty = 2, col=2)
plot(density(Score,bw="SJ",kern="gaussian"),type="l",
main="Gaussian kernel density estimate",xlab="Score")
rug(Score)
boxplot(Score,ylab="Score")
qqnorm(Score, ylab = "Score")
qqline(Score, lty = 2, col=2)

#Figure 3.34 on page 97


par(mfrow=c(1,1))
plot(sqrt(Score),log(MaxSalary),xlab=expression(sqrt(Score)))
abline(lsfit(sqrt(Score),log(MaxSalary)),lty=2,col=2)

#Figure 3.35 on page 98


par(mfrow=c(3,2))
plot(density(log(MaxSalary),bw="SJ",kern="gaussian"),type="l",
main="Gaussian kernel density estimate",xlab="log(MaxSalary)")
rug(log(MaxSalary))
boxplot(log(MaxSalary),ylab="log(MaxSalary)")
qqnorm(log(MaxSalary), ylab = "log(MaxSalary)")
qqline(log(MaxSalary), lty = 2, col=2)
sj <- bw.SJ(sqrt(Score),lower = 0.05, upper = 100)
plot(density(sqrt(Score),bw=sj,kern="gaussian"),type="l",
main="Gaussian kernel density estimate",xlab=expression(sqrt(Score)))
rug(sqrt(Score))
boxplot(sqrt(Score),ylab=expression(sqrt(Score)))
qqnorm(sqrt(Score), ylab=expression(sqrt(Score)))
qqline(sqrt(Score), lty = 2, col=2)

#Figure 3.36 on page 99


m2 <- lm(log(MaxSalary)~sqrt(Score))
par(mfrow=c(1,2))
StanRes2 <- rstandard(m2)
absrtsr2 <- sqrt(abs(StanRes2))
plot(sqrt(Score),StanRes2,ylab="Standardized
Residuals",xlab=expression(sqrt(Score)))
plot(sqrt(Score),absrtsr2,ylab="Square Root(|Standardized
Residuals|)",xlab=expression(sqrt(Score)))
abline(lsfit(sqrt(Score),absrtsr2),lty=2,col=2)
#R output on page 99
summary(tranx <- bctrans(~Score))

#Figure 3.37 on page 100


m3 <- lm(MaxSalary~sqrt(Score))
par(mfrow=c(1,1))
inverse.response.plot(m3,key=TRUE)
#Click on the plot where you want to put the legend

#Figure 3.38 on page 101


par(mfrow=c(2,2))
plot(density(MaxSalary^-0.25,bw="SJ",kern="gaussian"),type="l",
main="Gaussian kernel density estimate",xlab=expression(MaxSalary^-0.25))
rug(MaxSalary^-0.25)
boxplot(MaxSalary^-0.25,ylab=expression(MaxSalary^-0.25))
qqnorm(MaxSalary^-0.25,ylab=expression(MaxSalary^-0.25))
qqline(MaxSalary^-0.25,lty=2, col=2)

#Figure 3.39 on page 102


par(mfrow=c(2,2))
plot(sqrt(Score),MaxSalary^-
0.25,xlab=expression(sqrt(Score)),ylab=expression(MaxSalary^-0.25))
abline(lsfit(sqrt(Score),MaxSalary^-0.25),lty=2,col=2)
m3 <- lm(MaxSalary^-0.25~sqrt(Score))
StanRes3 <- rstandard(m3)
absrtsr3 <- sqrt(abs(StanRes3))
plot(sqrt(Score),StanRes3,ylab="Standardized
Residuals",xlab=expression(sqrt(Score)))
plot(sqrt(Score),absrtsr3,ylab="Square Root(|Standardized
Residuals|)",xlab=expression(sqrt(Score)))
abline(lsfit(sqrt(Score),absrtsr3),lty=2,col=2)

detach(salarygov)

#################EXERCISES

#Exercise 3.5.1
airfares <- read.table("airfares.txt",header=TRUE)
attach(airfares)

#R output on page 104


m1 <- lm(Fare~Distance)
summary(m1)

#Figure 3.41 on page 104


par(mfrow=c(1,2))
plot(Distance,Fare)
abline(lsfit(Distance,Fare))
leverage1 <- hatvalues(m1)
StanRes1 <- rstandard(m1)
residual1 <- m1$residuals
plot(Distance,StanRes1, ylab="Standardized Residuals")
abline(h=2,lty=2)
abline(h=-2,lty=2)

detach(airfares)

#Exercise 3.5.4
glakes <- read.table("glakes.txt",header=TRUE)
attach(glakes)

#R output on page 107


m1 <- lm(Time~Tonnage)
summary(m1)

#Figure 3.42 on page 106


par(mfrow=c(2,2))
plot(Tonnage,Time)
abline(lsfit(Tonnage,Time))
leverage1 <- hatvalues(m1)
StanRes1 <- rstandard(m1)
absrtsr1 <- sqrt(abs(StanRes1))
residual1 <- m1$residuals
plot(Tonnage,StanRes1, ylab="Standardized Residuals")
abline(h=2,lty=2)
abline(h=-2,lty=2)
plot(Tonnage,absrtsr1,ylab="Square Root(|Standardized Residuals|)")
abline(lsfit(Tonnage,absrtsr1),lty=2,col=1)
qqnorm(StanRes1, ylab = "Standardized Residuals")
qqline(StanRes1, lty = 2, col=1)

#Figure 3.43 on page 107


par(mfrow=c(3,2))
plot(density(Time,bw="SJ",kern="gaussian"),type="l",
main="Gaussian kernel density estimate",xlab="Time")
rug(Time)
boxplot(Time,ylab="Time")
qqnorm(Time, ylab = "Time")
qqline(Time, lty = 2, col=1)
sj <- bw.SJ(Tonnage,lower = 0.1, upper = 1000)
plot(density(Tonnage,bw=sj,kern="gaussian"),type="l",
main="Gaussian kernel density estimate",xlab="Tonnage")
rug(Tonnage)
boxplot(Tonnage,ylab="Tonnage")
qqnorm(Tonnage, ylab = "Tonnage")
qqline(Tonnage, lty = 2, col=1)

#R output on page 108


library(alr3)
summary(tranxy <- bctrans(~Time+Tonnage))

#R output on page 108


m2 <- lm(log(Time)~I(Tonnage^0.25))
summary(m2)

#Figure 3.44 on page 108


tTime <- log(Time)
tTonnage <- Tonnage^0.25
par(mfrow=c(2,2))
plot(tTonnage,tTime,xlab=expression(Tonnage^0.25),ylab="log(Time)")
abline(lsfit(tTonnage,tTime))
leverage2 <- hatvalues(m2)
StanRes2 <- rstandard(m2)
absrtsr2 <- sqrt(abs(StanRes2))
residual2 <- m2$residuals
plot(tTonnage,StanRes2, ylab="Standardized
Residuals",xlab=expression(Tonnage^0.25))
abline(h=2,lty=2)
abline(h=-2,lty=2)
plot(tTonnage,absrtsr2,ylab="Square Root(|Standardized
Residuals|)",xlab=expression(Tonnage^0.25))
abline(lsfit(tTonnage,absrtsr2),lty=2,col=1)
qqnorm(StanRes2, ylab = "Standardized Residuals")
qqline(StanRes2, lty = 2, col=1)

#Figure 3.45 on page 109


par(mfrow=c(3,2))
plot(density(tTime,bw="SJ",kern="gaussian"),type="l",
main="Gaussian kernel density estimate",xlab="log(Time)")
rug(tTime)
boxplot(tTime,ylab="log(Time)")
qqnorm(tTime, ylab = "log(Time)")
qqline(tTime, lty = 2, col=1)
sj <- bw.SJ(tTonnage,lower = 0.1, upper = 1000)
plot(density(tTonnage,bw=sj,kern="gaussian"),type="l",
main="Gaussian kernel density estimate",xlab=expression(Tonnage^0.25))
rug(tTonnage)
boxplot(tTonnage,ylab=expression(Tonnage^0.25))
qqnorm(tTonnage, ylab=expression(Tonnage^0.25))
qqline(tTonnage, lty = 2, col=1)

detach(glakes)

#Exercise 3.5.5
cars04 <- read.csv("cars04.csv",header=TRUE)
attach(cars04)

#Output from R on pages 110 and 111


m1 <- lm(SuggestedRetailPrice~DealerCost)
summary(m1)

#Figure 3.46 on page 110


par(mfrow=c(2,2))
plot(DealerCost,SuggestedRetailPrice)
abline(lsfit(DealerCost,SuggestedRetailPrice),lty = 2, col=1)
leverage1 <- hatvalues(m1)
StanRes1 <- rstandard(m1)
absrtsr1 <- sqrt(abs(StanRes1))
residual1 <- m1$residuals
plot(DealerCost,StanRes1, ylab="Standardized Residuals")
abline(h=2,lty=2)
abline(h=-2,lty=2)
plot(DealerCost,absrtsr1,ylab="Square Root(|Standardized Residuals|)")
abline(lsfit(DealerCost,absrtsr1),lty=2,col=1)
qqnorm(StanRes1, ylab = "Standardized Residuals")
qqline(StanRes1, lty = 2, col=1)

#Output from R on page 111


m2 <- lm(log(SuggestedRetailPrice)~log(DealerCost))
summary(m2)

#Figure 3.47 on page 111


par(mfrow=c(2,2))
plot(log(DealerCost),log(SuggestedRetailPrice))
abline(lsfit(log(DealerCost),log(SuggestedRetailPrice)),lty = 1, col=1)
leverage2 <- hatvalues(m2)
StanRes2 <- rstandard(m2)
absrtsr2 <- sqrt(abs(StanRes2))
residual2 <- m2$residuals
plot(log(DealerCost),StanRes2, ylab="Standardized Residuals")
abline(h=2,lty=2)
abline(h=-2,lty=2)
plot(log(DealerCost),absrtsr2,ylab="Square Root(|Standardized Residuals|)")
abline(lsfit(log(DealerCost),absrtsr2),lty=2,col=1)
qqnorm(StanRes2, ylab = "Standardized Residuals")
qqline(StanRes2, lty = 2, col=1)

detach(cars04)

#Exercise 3.5.6 based on a different set of generated data


n<-500
x <- runif(n,0,1)^3
e <- rnorm(n,0,0.1)
y <- exp(+2.5 + 1*x + e)

#Figure 3.48 on page 112


m1 <- lm(y~x)
library(alr3)
par(mfrow=c(1,1))
inverse.response.plot(m1,key=TRUE)

Вам также может понравиться