NTHU STAT 5410, 2019 Solution to Homework 7 Linear Models...
Transcript of NTHU STAT 5410, 2019 Solution to Homework 7 Linear Models...
Linear Models Homework 7
1.
(i)
year model: temp~year year
year model: temp~year + (year)2 year year
2
linear trend
NTHU STAT 5410, 2019 Solution to Homework 7
made by 蔡明諺, 邱奕豪, 黃俊閔 助教
(ii)
AR(1) year 95% 0 temp
(iii)
year
(
orthogonality )
NTHU STAT 5410, 2019 Solution to Homework 7
made by 蔡明諺, 邱奕豪, 黃俊閔 助教
2020 :
(iv)
1930 1930 year 1930
linear trend
NTHU STAT 5410, 2019 Solution to Homework 7
made by 蔡明諺, 邱奕豪, 黃俊閔 助教
1930 fit model year
reject claim
NTHU STAT 5410, 2019 Solution to Homework 7
made by 蔡明諺, 邱奕豪, 黃俊閔 助教
(v)
R-square cubic spline
NTHU STAT 5410, 2019 Solution to Homework 7
made by 蔡明諺, 邱奕豪, 黃俊閔 助教
Plot the fit in comparison to the previous fits.
cubic spline (indicate some
fluctuation in temperature over time) model
extrapolation
NTHU STAT 5410, 2019 Solution to Homework 7
made by 蔡明諺, 邱奕豪, 黃俊閔 助教
2.
:
data <- read.table('http://www.stat.nthu.edu.tw/~swcheng/Teaching/sta
t5410/data/assess.txt', header = T)
1981 1982 :
plot(x=NULL,y=NULL,xlim=c(31,94),ylim=c(812,2013))
lines(sort(data$P),data$y1982[order(data$P)],type='b',col='green')
lines(sort(data$P),data$y1981[order(data$P)],type='l',col='blue')
points(data$P[1:13],data$y1981[1:13],pch='L')
points(data$P[14:29],data$y1981[14:29],pch='S')
points(data$P[44:60],data$y1981[44:60],pch='e')
points(data$P[30:43],data$y1981[30:43],pch='c')
legend("topleft", legend = c("1981", "1982"),lty=c(1,1),col = c('blue
','green'))
:
1. 1981 1982 1982 1981
2. P Sibley Meeker
3.
NTHU STAT 5410, 2019 Solution to Homework 7
made by 蔡明諺, 邱奕豪, 黃俊閔 助教
:
par(mfrow=c(2,2))
> plot(data$P[1:13],data$y1981[1:13],type='b',main=data$County[1])
> plot(data$P[14:29],data$y1981[14:29],type='b',main=data$County[14])
> plot(data$P[30:43],data$y1981[30:43],type='b',main=data$County[30])
> plot(data$P[44:60],data$y1981[44:60],type='b',main=data$County[44])
.
response weight ,
Sample variance
P score County predictor weighted
least square (
P score County interaction)
y = (data$y1981+data$y1982)/2
var = apply(cbind(data$y1981,data$y1982),1,var)
model1 = lm(y~data$P+data$County+data$P:data$County,weights = 1/var)
summary(model1)
NTHU STAT 5410, 2019 Solution to Homework 7
made by 蔡明諺, 邱奕豪, 黃俊閔 助教
:
= 1143.629 + 4.335 + 782.549 349.483 +
366.654 8.736 + 4.366 1.623
0.05 F p-value 0.05
correlation:
cor(model.matrix(model1)[,-1])
P McLeod McLeod 0.998 P Meeker
Meeker 0.934 P Sibley
Sibley 0.997
NTHU STAT 5410, 2019 Solution to Homework 7
made by 蔡明諺, 邱奕豪, 黃俊閔 助教
fit :
model2 = lm(y~data$P+data$County,weights = 1/var)
anova(model1,model2)
0.05
summary(model2)
:
= 898.438 + 8.018 + 55.592 72.002 + 151.665
NTHU STAT 5410, 2019 Solution to Homework 7
made by 蔡明諺, 邱奕豪, 黃俊閔 助教
plot(data$P,model2$fitted.values,cex=0.05)
points(data$P[1:13],model2$fitted.values[1:13],pch='L')
points(data$P[14:29],model2$fitted.values[14:29],pch='s')
points(data$P[30:43],model2$fitted.values[30:43],pch='c')
points(data$P[44:60],model2$fitted.values[44:60],pch='e')
Sibley McLeod LeSueur
Meeker
> 898.438+(0+55.592-72.002+151.665)/4
932.2518
=
NTHU STAT 5410, 2019 Solution to Homework 7
made by 蔡明諺, 邱奕豪, 黃俊閔 助教
.
1981 1982 1982 1981
:
> round(data$y1982/data$y1981, 2)
1.10 ~ 1.2
P score County predictor
:
0.05 F p-value 0.05
fit :
model5 = lm(y~data$P+data$County)
anova(model5,model4)
0.05
NTHU STAT 5410, 2019 Solution to Homework 7
made by 蔡明諺, 邱奕豪, 黃俊閔 助教
> summary(model5)
:
= 1082.564 + 5.445 + 70.564 83.446 + 187.916
> 1082.564+(0+70.564-83.446+187.916)/4
1126.322
= 1126.322 + 5.445
y
Y response
* Y
Y
NTHU STAT 5410, 2019 Solution to Homework 7
made by 蔡明諺, 邱奕豪, 黃俊閔 助教
3.
> data=read.table("C:/Users/Desktop/E2.7.txt",header=T)
> DAO=diff(data$AO)
> DGNP=diff(data$GNP);DCP=diff(data$CP);DOP=diff(data$OP)
DAO response DGNP, DCP and DOP
> lm3=lm(DAO~DGNP+DCP+DOP)
transformation
> library(MASS)
> b=boxcox(lm3,plotit=T,lambda=seq(-1,1,0.1))
> b$x[which.max(b$y)
maximum -0.2525253 95% [-1 , 0.7]
-0.2525253 transformation response
( ) response log
> logDAO=log(DAO)
> lm3new=lm(logDAO~DGNP+DCP+DOP)
NTHU STAT 5410, 2019 Solution to Homework 7
made by 蔡明諺, 邱奕豪, 黃俊閔 助教
transformation
> summary(lm3)
> summary(lm3new)
coefficients DOP DGNP
QQ-plot
studentized residuals QQ-plot
> lm3r=rstandard(lm3)
> qqnorm(lm3r) # draw the normal probabi
> qqnorm(lm3r) # draw the normal probabi
> abline(0,1)
> shapiro.test(lm3r)
> lm3newr=rstandard(lm3new)
> qqnorm(lm3newr) # draw the normal probabi
> abline(0,1)
NTHU STAT 5410, 2019 Solution to Homework 7
made by 蔡明諺, 邱奕豪, 黃俊閔 助教
> shapiro.test(lm3newr)
Transformation response normal
3.
> data=read.table("C:/Users/Desktop/E2.7.txt",header=T)
> DAO=diff(data$AO)
> DGNP=diff(data$GNP);DCP=diff(data$CP);DOP=diff(data$OP)
DAO response DGNP, DCP and DOP
> lm3=lm(DAO~DGNP+DCP+DOP)
transformation
> library(MASS)
> b=boxcox(lm3,plotit=T,lambda=seq(-1,1,0.1))
NTHU STAT 5410, 2019 Solution to Homework 7
made by 蔡明諺, 邱奕豪, 黃俊閔 助教
> b$x[which.max(b$y)
maximum -0.2525253 95% [-1 , 0.7]
-0.2525253 transformation response
( ) response log
> logDAO=log(DAO)
> lm3new=lm(logDAO~DGNP+DCP+DOP)
transformation
> summary(lm3)
NTHU STAT 5410, 2019 Solution to Homework 7
made by 蔡明諺, 邱奕豪, 黃俊閔 助教
> summary(lm3new)
coefficients DOP DGNP
QQ-plot
studentized residuals QQ-plot
> lm3r=rstandard(lm3)
> qqnorm(lm3r) # draw the normal probabi
> qqnorm(lm3r) # draw the normal probabi
> abline(0,1)
> shapiro.test(lm3r)
> lm3newr=rstandard(lm3new)
> qqnorm(lm3newr) # draw the normal probabi
> abline(0,1)
> shapiro.test(lm3newr)
NTHU STAT 5410, 2019 Solution to Homework 7
made by 蔡明諺, 邱奕豪, 黃俊閔 助教
Transformation response normal
NTHU STAT 5410, 2019 Solution to Homework 7
made by 蔡明諺, 邱奕豪, 黃俊閔 助教