|
| |
Lack of Fit Test
I wrote a small function which would do Lack of fit tet for you.
First copy avrything in red area, and paste it in Splus (you have to do
it only once.
LF.test<-function(fit){
my.paste<-function(x) return(paste(x,collapse="
"))
X<-model.matrix(fit)
Y<-X%*%fit$coef+fit$res
x<-apply(X,1,my.paste)
return(anova(lm(Y~X-1),lm(Y~x)))
}
Now to test linear model, which you say named fit, you just need to type
LF.test(fit).
Now let's do an example from the book. (Chapte 3, pages 115-124)
> data<-read.table("/afs/umich.edu/user/k/u/kutsyy/Public/html/classes/ALSM/CH03TA04.DAT")
> data
V1 V2
1 125 160
2 100 112
3 200 124
4 75 28
5 150 152
6 175 156
7 75 42
8 175 124
9 125 150
10 200 104
11 100 136
> X<-data[,1]
> Y<-data[,2]
> fit<-lm(Y~X)
> summary(fit)
Call: lm(formula = Y ~ X)
Residuals:
Min
1Q Median 3Q Max
-59.23 -34.06 12.61 32.44 48.44
Coefficients:
Value Std. Error t value Pr(>|t|)
(Intercept) 50.7225 39.3979
1.2874 0.2301
X 0.4867 0.2747 1.7717 0.1102
Residual standard error: 40.47 on 9 degrees
of freedom
Multiple R-Squared: 0.2586
F-statistic: 3.139 on 1 and 9 degrees of
freedom, the p-value is 0.1102
Correlation of Coefficients:
(Intercept)
X -0.9508
> anova(fit)
Analysis of Variance Table
Response: Y
Terms added sequentially (first to last)
Df Sum of Sq Mean Sq F Value Pr(F)
X
1 5141.34 5141.338 3.138882 0.1102125
Residuals 9 14741.57 1637.952
> LF.test(fit)
Analysis of Variance Table
Response: Y
Terms Resid. Df
RSS Test Df Sum of Sq F Value
Pr(F)
1 X - 1
9 14741.57
2 x
5 1148.00 1 vs. 2 4 13593.57 14.80136 0.005593812
Note that 1148.00 is Pure error sum of squares. 0.00559 is p-value
for that sum of squares, and it is exactly the same as in the book (page
121).
|