-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathabd17-1.Rmd
75 lines (71 loc) · 2.1 KB
/
abd17-1.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
---
title: "Analysis of Biological Data Chapter 17 Practice Problems 1-3"
author: "your name"
date: "`r Sys.Date()`"
output:
html_document:
highlight: pygments
toc: yes
major: regression modeling; ABD
minor: ordinary least squares
---
# Data
The data are from Carré, J. M., and C. M McCormick. 2008. _Proceedings of the
Royal Society of London, Series B, Biological Sciences_ **275**:2651–2656.
```{r input}
require(rms)
knitrSet(lang='markdown')
face <- c(159,167,165,172,179,177,174,174,177,178,176,
181,183,183,184,187,192,195,198,199,207)/100
penalty <- c(44,143,157,14,27,35,85,113,147,151,199,
106,120,123,80,253,123,110,161,195,295)/100
d <- data.frame(face, penalty)
xl <- 'Face Width:Height Ratio'
yl <- 'Penalty Minutes'
d <- upData(d, labels=c(face = xl, penalty = yl))
```
```{r show}
d # same as print(d)
ggplot(d, aes(x=face, y=penalty)) + geom_point() + xlab(xl) + ylab(yl)
dd <- datadist(d); options(datadist='dd')
```
# Linear Regression Fit
We first use low-level computations to obtain the components of the regression analysis, then use high-level functions.
```{r fit}
x <- face
y <- penalty
n <- length(x)
sxx <- sum(x ^ 2) - sum(x) ^ 2 / n
syy <- sum(y ^ 2) - sum(y) ^ 2 / n
sxy <- sum(x * y) - sum(x) * sum(y) / n
b <- sxy / sxx
mse <- (syy - b * sxy) / (n - 2) # 1 d.f. intercept 1 d.f. slope
seb <- sqrt(mse / sxx)
c(xbar=mean(x), ybar=mean(y), sxx=sxx, syy=syy, sxy=sxy, b=b, mse=mse, seb=seb)
tcrit <- qt(0.975, n - 2)
tcrit
f <- ols(penalty ~ face, data=d)
f
anova(f)
```
# Diagnostics Based on Residuals
```{r resid,w=7}
r <- resid(f)
par(mfrow=c(1,2)) # 1x2 matrix of plots
plot(fitted(f), r); abline(h=0) # yhat vs. r
qqnorm(r) # linearity indicates normality
qqline(as.numeric(r))
```
# Partial Effects of Predictors With Raw Data
```{r peffects}
ggplot(Predict(f, face=seq(1.59, 2.07, length=100)), ylab=yl) +
geom_point(aes(x=face, y=penalty), d)
```
# Computing Environment
```{r rsession,echo=FALSE}
si <- sessionInfo(); si$loadedOnly <- NULL
print(si, locale=FALSE)
```
```{r cite,results='asis',echo=FALSE}
print(citation(), style='text')
```