Professional Documents
Culture Documents
> ###########################
> ###########################
>#
>#
> (full.mod<-lm(MDBH~HD+Age+I(HD/N),trees))
Call:
Coefficients:
>#
> red.mod<-lm(MDBH~Age,trees)
> anova(red.mod,full.mod)
1 18 5.6467
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> # Part iv
> step(full.mod)
Start: AIC=-44.78
Call:
Coefficients:
>#
>#
> # Part v
> # If you propose a new model, you will need to verify the appropriateness
> # of your model and argue, based output from code you devlop and use here,
>#
> # .... put model construction and diagnostics code here (look back
> ###########################
> ###########################
>#
>#
> # Part i
>
> new.mod<-lm(y~x1+x2+x3+x4,teachers)
> summary(new.mod)
Call:
Residuals:
Coefficients:
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 55.92 on 18 degrees of freedom
>
> e<-residuals(new.mod)
> h<-hatvalues(new.mod)
> y.hat<-fitted.values(new.mod)
> s<-summary(new.mod)$sigma
> stud<-e/(s*sqrt(1-h))
>
>
>
>
+ ylim=c(-3,3), sub="(c)"))
>
>
+ ylim=c(-3,3), sub="(e)"))
>
+ ylim=c(-3,3), sub="(f)"))
>#
>#
> # Part ii
>
> old.mod<-lm(y~x1+x2+x3+x4+gender,teachers)
> anova(new.mod,old.mod)
Model 1: y ~ x1 + x2 + x3 + x4
Model 2: y ~ x1 + x2 + x3 + x4 + gender
1 18 56290
>
>
> PRESS.new<-sum((e/(1-h))^2)
> PRESS.old<-sum((residuals(old.mod)/(1-hatvalues(old.mod)))^2)
>
> data.frame(PRESS.new,PRESS.old,row.names="")
PRESS.new PRESS.old
84378.64 94980.15
>
> summary(old.mod)
Call:
Residuals:
Coefficients:
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Call:
Residuals:
Coefficients:
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> ########################
> ########################
>#
>#
> # Part i
> full.mod<-lm(y~x1+x2+x3+I(x1*x2)+I(x1*x3)
+ +I(x2*x3)+I(x1^2)+I(x2^2)+I(x3^2),bread)
>#
> no.int<-lm(y~x1+x2+x3+I(x1^2)+I(x2^2)+I(x3^2),bread)
>#
> no.sq<-lm(y~x1+x2+x3+I(x1*x2)+I(x1*x3)+I(x2*x3),bread)
>#
> no.quad<-lm(y~x1+x2+x3,bread)
>
> anova(no.int,full.mod)
I(x2^2) + I(x3^2)
1 13 27.427
> anova(no.sq,full.mod)
I(x2^2) + I(x3^2)
1 13 56.045
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> anova(no.quad,full.mod)
Model 1: y ~ x1 + x2 + x3
I(x2^2) + I(x3^2)
1 16 59.205
>#
> # Part ii
> step(full.mod,trace=2)
Start: AIC=23.87
I(x2^2) + I(x3^2)
I(x3^2)
Step: AIC=20.8
Step: AIC=20.32
y ~ x1 + x2 + x3 + I(x1^2) + I(x2^2) + I(x3^2)
Call:
data = bread)
Coefficients:
I(x3^2)
-2.307924
>
>#
> require(leaps)
Warning message:
> search.results<-regsubsets(y~x1+x2+x3+I(x1*x2)+I(x1*x3)
+ +I(x2*x3)+I(x1^2)+I(x2^2)+I(x3^2),
> selection.criteria<-summary(search.results)
> detach(package:leaps)
>
> n<-dim(bread)[1]
> q<-as.integer(row.names(selection.criteria$which))
Error in row.names(selection.criteria$which) :
> #Extract the mean square error for the test model
> mse<-selection.criteria$rss/(n-q-1)
> R.sq<-selection.criteria$rsq
> AdjR.sq<-selection.criteria$adjr2
> Cp<-selection.criteria$cp
> bic.f<-n*log(selection.criteria$rss)-n*log(n)+(q+1)*log(n)
> var<-as.matrix(selection.criteria$which[,2:10])
> criteria.table<-data.frame(cbind(q,mse,R.sq,AdjR.sq,Cp,aic.f,bic.f,
+ var[,1],var[,2],var[,3],var[,4],var[,5],var[,6],var[,7],var[,8],var[,9]),
+ row.names=NULL)
Error in cbind(q, mse, R.sq, AdjR.sq, Cp, aic.f, bic.f, var[, 1], var[, :
> names(criteria.table)<-c("q","MSE","Rsq","aRsq","Cp","AIC","BIC","x1","x2","x3",
+ "x1*x2","x1*x3","x2*x3","x1^2","x2^2","x3^2")
> rm(n,q,mse,R.sq,AdjR.sq,Cp,aic.f,bic.f,var)
Warning messages:
> round(criteria.table,2)
> # You will have to develop code for your new proposed model