R version 2.9.2 (2009-08-24)
Copyright (C) 2009 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

>
> supervisor.table <- read.table('http://www-stat.stanford.edu/~jtaylo/courses/stats191/data/supervisor.table', header=T)
> attach(supervisor.table)
>
> # original regression model
>
> supervisor.lm <- lm(Y ~ X1 + X2 + X3 + X4 + X5 + X6)
>
>
> # A function to do "nested" F-tests
>
> f.test.lm <- function(R.lm, F.lm) {
+ SSE.R <- sum(resid(R.lm)^2)
+ SSE.F <- sum(resid(F.lm)^2)
+ df.num <- R.lm$df - F.lm$df
+ df.den <- F.lm$df
+ F <- ((SSE.R - SSE.F) / df.num) / (SSE.F / df.den)
+ p.value <- 1 - pf(F, df.num, df.den)
+ return(data.frame(F, df.num, df.den, p.value))
+ }
>
> # reduced model: only X1 and X3
>
> reduced.lm = lm(Y ~ X1 + X3)
>
> print(anova(reduced.lm, supervisor.lm))
Analysis of Variance Table

Model 1: Y ~ X1 + X3
Model 2: Y ~ X1 + X2 + X3 + X4 + X5 + X6
Res.Df RSS Df Sum of Sq F Pr(>F)
1 27 1254.65
2 23 1149.00 4 105.65 0.5287 0.7158
>
> # enforce constraint of the coefficients for X1 and X3
> # we do this by making a new variable Z=X1+X3
>
> Z = X1 + X3
> equal.lm = lm(Y ~ Z)
>
> print(anova(equal.lm, reduced.lm))
Analysis of Variance Table

Model 1: Y ~ Z
Model 2: Y ~ X1 + X3
Res.Df RSS Df Sum of Sq F Pr(>F)
1 28 1424.59
2 27 1254.65 1 169.95 3.6572 0.06649 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> # another example from the book:
> # test that beta1+beta3=1
>
> Z = X1-X3
> W = Y-X3
> print(f.test.lm(lm(W ~ Z), reduced.lm))
F df.num df.den p.value
1 1.611813 1 27 0.2150713
>
> # this is the same as using the 'offset term' which is
> # subtracted from the response before fitting the model
>
> print(anova(lm(Y ~ Z, offset=X3), reduced.lm))
Analysis of Variance Table

Model 1: Y ~ Z
Model 2: Y ~ X1 + X3
Res.Df RSS Df Sum of Sq F Pr(>F)
1 28 1329.5
2 27 1254.7 1 74.9 1.6118 0.2151
>
> # look at the models themselves
>
> print(summary(lm(Y ~ Z, offset=X3)))

Call:
lm(formula = Y ~ Z, offset = X3)

Residuals:
Min 1Q Median 3Q Max
-9.799 -6.242 0.252 4.911 12.263

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.1665 1.7079 0.683 0.5
Z 0.6938 0.1129 6.147 1.23e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 6.891 on 28 degrees of freedom
Multiple R-squared: 0.7505, Adjusted R-squared: 0.7415
F-statistic: 84.21 on 1 and 28 DF, p-value: 6.196e-10

> print(summary(lm(W ~ Z)))

Call:
lm(formula = W ~ Z)

Residuals:
Min 1Q Median 3Q Max
-9.799 -6.242 0.252 4.911 12.263

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.1665 1.7079 0.683 0.5
Z 0.6938 0.1129 6.147 1.23e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 6.891 on 28 degrees of freedom
Multiple R-squared: 0.5744, Adjusted R-squared: 0.5592
F-statistic: 37.79 on 1 and 28 DF, p-value: 1.233e-06

>

>
> proc.time()
user system elapsed
0.644 0.028 0.711
R script