This report was automatically generated with the R package knitr (version 1.5).
library(faraway)
data(gala, package = "faraway")
lmod <- lm(Species ~ Area + Elevation + Nearest + Scruz + Adjacent, gala)
nullmod <- lm(Species ~ 1, gala)
anova(nullmod, lmod)
Analysis of Variance Table
Model 1: Species ~ 1
Model 2: Species ~ Area + Elevation + Nearest + Scruz + Adjacent
Res.Df RSS Df Sum of Sq F Pr(>F)
1 29 381081
2 24 89231 5 291850 15.7 6.8e-07
(rss0 <- deviance(nullmod))
[1] 381081
(rss <- deviance(lmod))
[1] 89231
(df0 <- df.residual(nullmod))
[1] 29
(df <- df.residual(lmod))
[1] 24
(fstat <- ((rss0 - rss)/(df0 - df))/(rss/df))
[1] 15.7
1 - pf(fstat, df0 - df, df)
[1] 6.838e-07
lmods <- lm(Species ~ Elevation + Nearest + Scruz + Adjacent, gala)
anova(lmods, lmod)
Analysis of Variance Table
Model 1: Species ~ Elevation + Nearest + Scruz + Adjacent
Model 2: Species ~ Area + Elevation + Nearest + Scruz + Adjacent
Res.Df RSS Df Sum of Sq F Pr(>F)
1 25 93469
2 24 89231 1 4238 1.14 0.3
sumary(lmod)
Estimate Std. Error t value Pr(>|t|)
(Intercept) 7.06822 19.15420 0.37 0.7154
Area -0.02394 0.02242 -1.07 0.2963
Elevation 0.31946 0.05366 5.95 3.8e-06
Nearest 0.00914 1.05414 0.01 0.9932
Scruz -0.24052 0.21540 -1.12 0.2752
Adjacent -0.07480 0.01770 -4.23 0.0003
n = 30, p = 6, Residual SE = 60.98, R-Squared = 0.77
sumary(lm(Species ~ Area, gala))
Estimate Std. Error t value Pr(>|t|)
(Intercept) 63.7829 17.5244 3.64 0.00109
Area 0.0820 0.0197 4.16 0.00027
n = 30, p = 2, Residual SE = 91.73, R-Squared = 0.38
lmods <- lm(Species ~ Elevation + Nearest + Scruz, gala)
anova(lmods, lmod)
Analysis of Variance Table
Model 1: Species ~ Elevation + Nearest + Scruz
Model 2: Species ~ Area + Elevation + Nearest + Scruz + Adjacent
Res.Df RSS Df Sum of Sq F Pr(>F)
1 26 158292
2 24 89231 2 69060 9.29 0.001
lmods <- lm(Species ~ I(Area + Adjacent) + Elevation + Nearest + Scruz, gala)
anova(lmods, lmod)
Analysis of Variance Table
Model 1: Species ~ I(Area + Adjacent) + Elevation + Nearest + Scruz
Model 2: Species ~ Area + Elevation + Nearest + Scruz + Adjacent
Res.Df RSS Df Sum of Sq F Pr(>F)
1 25 109591
2 24 89231 1 20360 5.48 0.028
lmods <- lm(Species ~ Area + offset(0.5 * Elevation) + Nearest + Scruz + Adjacent,
gala)
anova(lmods, lmod)
Analysis of Variance Table
Model 1: Species ~ Area + offset(0.5 * Elevation) + Nearest + Scruz +
Adjacent
Model 2: Species ~ Area + Elevation + Nearest + Scruz + Adjacent
Res.Df RSS Df Sum of Sq F Pr(>F)
1 25 131312
2 24 89231 1 42081 11.3 0.0026
(tstat <- (0.31946 - 0.5)/0.05366)
[1] -3.365
2 * pt(tstat, 24)
[1] 0.002572
tstat^2
[1] 11.32
lmod <- lm(Species ~ Nearest + Scruz, gala)
lms <- summary(lmod)
lms$fstatistic
value numdf dendf
0.602 2.000 27.000
1 - pf(lms$fstatistic[1], lms$fstatistic[2], lms$fstatistic[3])
value
0.5549
nreps <- 4000
set.seed(123)
fstats <- numeric(nreps)
for (i in 1:nreps) {
lmods <- lm(sample(Species) ~ Nearest + Scruz, gala)
fstats[i] <- summary(lmods)$fstat[1]
}
mean(fstats > lms$fstat[1])
[1] 0.5583
summary(lmod)$coef[3, ]
Estimate Std. Error t value Pr(>|t|)
-0.4406 0.4025 -1.0947 0.2833
tstats <- numeric(nreps)
set.seed(123)
for (i in 1:nreps) {
lmods <- lm(Species ~ Nearest + sample(Scruz), gala)
tstats[i] <- summary(lmods)$coef[3, 3]
}
mean(abs(tstats) > abs(lms$coef[3, 3]))
[1] 0.2677
lmod <- lm(Species ~ Area + Elevation + Nearest + Scruz + Adjacent, gala)
sumary(lmod)
Estimate Std. Error t value Pr(>|t|)
(Intercept) 7.06822 19.15420 0.37 0.7154
Area -0.02394 0.02242 -1.07 0.2963
Elevation 0.31946 0.05366 5.95 3.8e-06
Nearest 0.00914 1.05414 0.01 0.9932
Scruz -0.24052 0.21540 -1.12 0.2752
Adjacent -0.07480 0.01770 -4.23 0.0003
n = 30, p = 6, Residual SE = 60.98, R-Squared = 0.77
qt(0.975, 30 - 6)
[1] 2.064
-0.02394 + c(-1, 1) * 2.0639 * 0.02242
[1] -0.07021 0.02233
-0.0748 + c(-1, 1) * 2.0639 * 0.0177
[1] -0.11133 -0.03827
confint(lmod)
2.5 % 97.5 %
(Intercept) -32.46410 46.60054
Area -0.07022 0.02234
Elevation 0.20871 0.43022
Nearest -2.16649 2.18477
Scruz -0.68509 0.20404
Adjacent -0.11134 -0.03827
require(ellipse)
Loading required package: ellipse
plot(ellipse(lmod, c(2, 6)), type = "l", ylim = c(-0.13, 0))
points(coef(lmod)[2], coef(lmod)[6], pch = 19)
abline(v = confint(lmod)[2, ], lty = 2)
abline(h = confint(lmod)[6, ], lty = 2)
sample(10, rep = TRUE)
[1] 4 3 6 2 4 1 5 4 10 5
set.seed(123)
nb <- 4000
coefmat <- matrix(NA, nb, 6)
resids <- residuals(lmod)
preds <- fitted(lmod)
for (i in 1:nb) {
booty <- preds + sample(resids, rep = TRUE)
bmod <- update(lmod, booty ~ .)
coefmat[i, ] <- coef(bmod)
}
colnames(coefmat) <- c("Intercept", colnames(gala[, 3:7]))
coefmat <- data.frame(coefmat)
apply(coefmat, 2, function(x) quantile(x, c(0.025, 0.975)))
Intercept Area Elevation Nearest Scruz Adjacent
2.5% -24.53 -0.06279 0.2286 -1.713 -0.6060 -0.10425
97.5% 41.44 0.01853 0.4211 2.055 0.1725 -0.04094
require(ggplot2)
ggplot(coefmat, aes(x = Area)) + geom_density() + geom_vline(xint = c(-0.0628,
0.0185), lty = 2)
ggplot(coefmat, aes(x = Adjacent)) + geom_density() + geom_vline(xint = c(-0.104,
-0.0409), lty = 2)
The R session information (including the OS info, R version and all packages used):
sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-apple-darwin13.1.0 (64-bit)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] graphics grDevices utils datasets methods stats base
other attached packages:
[1] ellipse_0.3-8 faraway_1.0.6 knitr_1.5 ggplot2_0.9.3.1
loaded via a namespace (and not attached):
[1] colorspace_1.2-4 dichromat_2.0-0 digest_0.6.4
[4] evaluate_0.5.3 formatR_0.10 grid_3.1.0
[7] gtable_0.1.2 labeling_0.2 MASS_7.3-31
[10] munsell_0.4.2 plyr_1.8.1 proto_0.3-10
[13] RColorBrewer_1.0-5 Rcpp_0.11.1 reshape2_1.2.2
[16] scales_0.2.3 stringr_0.6.2 tools_3.1.0
Sys.time()
[1] "2014-06-16 14:01:23 BST"