This report was automatically generated with the R package knitr (version 1.5).

library(faraway)
data(sexab, package = "faraway")
sexab
       cpa    ptsd       csa
1   2.0479  9.7136    Abused
2   0.8389  6.1693    Abused
3  -0.2414 15.1593    Abused
4  -1.1146 11.3128    Abused
5   2.0147  9.9538    Abused
6   6.7113  9.8388    Abused
7   1.2081  5.9849    Abused
8   2.3428 11.1105    Abused
9   0.9119  6.2553    Abused
10 -0.8531  7.0411    Abused
11  7.3567 13.8885    Abused
12  2.0936 14.9834    Abused
13  1.9457 10.8248    Abused
14 -0.4222 13.9145    Abused
15  1.4146 18.1686    Abused
16  6.0776 12.9163    Abused
17  6.0170 15.0832    Abused
18  3.7334  8.3089    Abused
19  2.6275  9.2801    Abused
20  0.4626  9.2912    Abused
21  7.0184 11.9274    Abused
22  3.1466 13.5168    Abused
23  2.3464  8.5351    Abused
24  8.6469 18.9925    Abused
25  4.3169 17.2038    Abused
26  2.1305 10.3740    Abused
27  4.0521  8.4799    Abused
28  1.5741 14.0794    Abused
29  3.7637 13.4460    Abused
30  5.1835  9.3743    Abused
31  1.1756 16.1314    Abused
32  2.7040 12.0746    Abused
33  1.1442 10.8378    Abused
34  4.1316 17.4098    Abused
35  1.4230 14.9013    Abused
36  6.3523 12.7361    Abused
37  7.6147 15.4218    Abused
38  1.9970 17.6411    Abused
39  3.2510 11.8932    Abused
40  3.0090  7.3264    Abused
41  3.0775 10.7658    Abused
42  5.2679 15.2489    Abused
43  3.4114 11.0824    Abused
44  1.3532  7.6223    Abused
45  5.1192 11.1280    Abused
46  1.4918  6.1420 NotAbused
47  0.6096  0.7446 NotAbused
48  1.4333  3.4596 NotAbused
49 -0.3366  6.9122 NotAbused
50 -3.1204  4.5417 NotAbused
51  2.6534  6.3393 NotAbused
52  3.7544  7.2969 NotAbused
53  1.5115 -3.3492 NotAbused
54  1.7539  4.4676 NotAbused
55 -0.4586  4.0182 NotAbused
56  0.7026  6.2162 NotAbused
57  5.0497  5.7945 NotAbused
58  0.7319  8.5371 NotAbused
59 -0.4164 -0.3755 NotAbused
60  2.8093  4.7527 NotAbused
61  2.9337  6.3937 NotAbused
62 -0.2278  6.8331 NotAbused
63  4.8204 10.4699 NotAbused
64  1.3216  1.1115 NotAbused
65  0.5622  7.4012 NotAbused
66  1.2230  6.1747 NotAbused
67  3.0495  5.4923 NotAbused
68  3.7686 10.9145 NotAbused
69 -2.1188  4.8386 NotAbused
70  3.6357  3.6294 NotAbused
71 -0.3140 -0.1434 NotAbused
72  2.1763  6.5728 NotAbused
73 -0.2321 -3.1162 NotAbused
74 -1.8575 -0.4700 NotAbused
75  2.8525  6.8430 NotAbused
76  0.8114  7.1292 NotAbused
by(sexab, sexab$csa, summary)
sexab$csa: Abused
      cpa             ptsd              csa    
 Min.   :-1.11   Min.   : 5.99   Abused   :45  
 1st Qu.: 1.42   1st Qu.: 9.37   NotAbused: 0  
 Median : 2.63   Median :11.31                 
 Mean   : 3.08   Mean   :11.94                 
 3rd Qu.: 4.32   3rd Qu.:14.90                 
 Max.   : 8.65   Max.   :18.99                 
-------------------------------------------------------- 
sexab$csa: NotAbused
      cpa             ptsd              csa    
 Min.   :-3.12   Min.   :-3.35   Abused   : 0  
 1st Qu.:-0.23   1st Qu.: 3.54   NotAbused:31  
 Median : 1.32   Median : 5.79                 
 Mean   : 1.31   Mean   : 4.70                 
 3rd Qu.: 2.83   3rd Qu.: 6.84                 
 Max.   : 5.05   Max.   :10.91                 
plot(ptsd ~ csa, sexab)

plot of chunk unnamed-chunk-1

plot(ptsd ~ cpa, pch = as.character(csa), sexab)

plot of chunk unnamed-chunk-1

t.test(ptsd ~ csa, sexab, var.equal = TRUE)

    Two Sample t-test

data:  ptsd by csa
t = 8.939, df = 74, p-value = 2.172e-13
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 5.63 8.86
sample estimates:
   mean in group Abused mean in group NotAbused 
                 11.941                   4.696 
d1 <- ifelse(sexab$csa == "Abused", 1, 0)
d2 <- ifelse(sexab$csa == "NotAbused", 1, 0)
lmod <- lm(ptsd ~ d1 + d2, sexab)
sumary(lmod)

Coefficients: (1 not defined because of singularities)
            Estimate Std. Error t value Pr(>|t|)
(Intercept)    4.696      0.624    7.53  1.0e-10
d1             7.245      0.811    8.94  2.2e-13

n = 76, p = 2, Residual SE = 3.47, R-Squared = 0.52
model.matrix(lmod)
   (Intercept) d1 d2
1            1  1  0
2            1  1  0
3            1  1  0
4            1  1  0
5            1  1  0
6            1  1  0
7            1  1  0
8            1  1  0
9            1  1  0
10           1  1  0
11           1  1  0
12           1  1  0
13           1  1  0
14           1  1  0
15           1  1  0
16           1  1  0
17           1  1  0
18           1  1  0
19           1  1  0
20           1  1  0
21           1  1  0
22           1  1  0
23           1  1  0
24           1  1  0
25           1  1  0
26           1  1  0
27           1  1  0
28           1  1  0
29           1  1  0
30           1  1  0
31           1  1  0
32           1  1  0
33           1  1  0
34           1  1  0
35           1  1  0
36           1  1  0
37           1  1  0
38           1  1  0
39           1  1  0
40           1  1  0
41           1  1  0
42           1  1  0
43           1  1  0
44           1  1  0
45           1  1  0
46           1  0  1
47           1  0  1
48           1  0  1
49           1  0  1
50           1  0  1
51           1  0  1
52           1  0  1
53           1  0  1
54           1  0  1
55           1  0  1
56           1  0  1
57           1  0  1
58           1  0  1
59           1  0  1
60           1  0  1
61           1  0  1
62           1  0  1
63           1  0  1
64           1  0  1
65           1  0  1
66           1  0  1
67           1  0  1
68           1  0  1
69           1  0  1
70           1  0  1
71           1  0  1
72           1  0  1
73           1  0  1
74           1  0  1
75           1  0  1
76           1  0  1
attr(,"assign")
[1] 0 1 2
lmod <- lm(ptsd ~ d2, sexab)
sumary(lmod)
            Estimate Std. Error t value Pr(>|t|)
(Intercept)   11.941      0.518   23.07  < 2e-16
d2            -7.245      0.811   -8.94  2.2e-13

n = 76, p = 2, Residual SE = 3.47, R-Squared = 0.52
lmod <- lm(ptsd ~ d1 + d2 - 1, sexab)
sumary(lmod)
   Estimate Std. Error t value Pr(>|t|)
d1   11.941      0.518   23.07   <2e-16
d2    4.696      0.624    7.53    1e-10

n = 76, p = 2, Residual SE = 3.47, R-Squared = 0.89
lmod <- lm(ptsd ~ csa, sexab)
sumary(lmod)
             Estimate Std. Error t value Pr(>|t|)
(Intercept)    11.941      0.518   23.07  < 2e-16
csaNotAbused   -7.245      0.811   -8.94  2.2e-13

n = 76, p = 2, Residual SE = 3.47, R-Squared = 0.52
class(sexab$csa)
[1] "factor"
sexab$csa <- relevel(sexab$csa, ref = "NotAbused")
lmod <- lm(ptsd ~ csa, sexab)
sumary(lmod)
            Estimate Std. Error t value Pr(>|t|)
(Intercept)    4.696      0.624    7.53  1.0e-10
csaAbused      7.245      0.811    8.94  2.2e-13

n = 76, p = 2, Residual SE = 3.47, R-Squared = 0.52
lmod4 <- lm(ptsd ~ cpa + csa + cpa:csa, sexab)
sumary(lmod4)
              Estimate Std. Error t value Pr(>|t|)
(Intercept)      3.696      0.711    5.20  1.8e-06
cpa              0.764      0.304    2.51    0.014
csaAbused        6.861      1.075    6.38  1.5e-08
cpa:csaAbused   -0.314      0.368   -0.85    0.397

n = 76, p = 4, Residual SE = 3.28, R-Squared = 0.58
model.matrix(lmod4)
   (Intercept)     cpa csaAbused cpa:csaAbused
1            1  2.0479         1        2.0479
2            1  0.8389         1        0.8389
3            1 -0.2414         1       -0.2414
4            1 -1.1146         1       -1.1146
5            1  2.0147         1        2.0147
6            1  6.7113         1        6.7113
7            1  1.2081         1        1.2081
8            1  2.3428         1        2.3428
9            1  0.9119         1        0.9119
10           1 -0.8531         1       -0.8531
11           1  7.3567         1        7.3567
12           1  2.0936         1        2.0936
13           1  1.9457         1        1.9457
14           1 -0.4222         1       -0.4222
15           1  1.4146         1        1.4146
16           1  6.0776         1        6.0776
17           1  6.0170         1        6.0170
18           1  3.7334         1        3.7334
19           1  2.6275         1        2.6275
20           1  0.4626         1        0.4626
21           1  7.0184         1        7.0184
22           1  3.1466         1        3.1466
23           1  2.3464         1        2.3464
24           1  8.6469         1        8.6469
25           1  4.3169         1        4.3169
26           1  2.1305         1        2.1305
27           1  4.0521         1        4.0521
28           1  1.5741         1        1.5741
29           1  3.7637         1        3.7637
30           1  5.1835         1        5.1835
31           1  1.1756         1        1.1756
32           1  2.7040         1        2.7040
33           1  1.1442         1        1.1442
34           1  4.1316         1        4.1316
35           1  1.4230         1        1.4230
36           1  6.3523         1        6.3523
37           1  7.6147         1        7.6147
38           1  1.9970         1        1.9970
39           1  3.2510         1        3.2510
40           1  3.0090         1        3.0090
41           1  3.0775         1        3.0775
42           1  5.2679         1        5.2679
43           1  3.4114         1        3.4114
44           1  1.3532         1        1.3532
45           1  5.1192         1        5.1192
46           1  1.4918         0        0.0000
47           1  0.6096         0        0.0000
48           1  1.4333         0        0.0000
49           1 -0.3366         0        0.0000
50           1 -3.1204         0        0.0000
51           1  2.6534         0        0.0000
52           1  3.7544         0        0.0000
53           1  1.5115         0        0.0000
54           1  1.7539         0        0.0000
55           1 -0.4586         0        0.0000
56           1  0.7026         0        0.0000
57           1  5.0497         0        0.0000
58           1  0.7319         0        0.0000
59           1 -0.4164         0        0.0000
60           1  2.8093         0        0.0000
61           1  2.9337         0        0.0000
62           1 -0.2278         0        0.0000
63           1  4.8204         0        0.0000
64           1  1.3216         0        0.0000
65           1  0.5622         0        0.0000
66           1  1.2230         0        0.0000
67           1  3.0495         0        0.0000
68           1  3.7686         0        0.0000
69           1 -2.1188         0        0.0000
70           1  3.6357         0        0.0000
71           1 -0.3140         0        0.0000
72           1  2.1763         0        0.0000
73           1 -0.2321         0        0.0000
74           1 -1.8575         0        0.0000
75           1  2.8525         0        0.0000
76           1  0.8114         0        0.0000
attr(,"assign")
[1] 0 1 2 3
attr(,"contrasts")
attr(,"contrasts")$csa
[1] "contr.treatment"
plot(ptsd ~ cpa, sexab, pch = as.numeric(csa))
abline(3.96, 0.764)
abline(3.96 + 6.86, 0.764 - 0.314, lty = 2)

plot of chunk unnamed-chunk-1

lmod3 <- lm(ptsd ~ cpa + csa, sexab)
sumary(lmod3)
            Estimate Std. Error t value Pr(>|t|)
(Intercept)    3.975      0.629    6.32  1.9e-08
cpa            0.551      0.172    3.21    0.002
csaAbused      6.273      0.822    7.63  6.9e-11

n = 76, p = 3, Residual SE = 3.27, R-Squared = 0.58
plot(ptsd ~ cpa, sexab, pch = as.numeric(csa))
abline(3.98, 0.551)
abline(3.98 + 6.27, 0.551, lty = 2)

plot of chunk unnamed-chunk-1

confint(lmod3)[3, ]
 2.5 % 97.5 % 
 4.635  7.911 
plot(fitted(lmod3), residuals(lmod3), pch = as.numeric(sexab$csa), xlab = "Fitted", 
    ylab = "Residuals")

plot of chunk unnamed-chunk-1

lmod1 <- lm(ptsd ~ cpa, sexab)
sumary(lmod1)
            Estimate Std. Error t value Pr(>|t|)
(Intercept)    6.552      0.707    9.26  5.3e-14
cpa            1.033      0.212    4.87  6.3e-06

n = 76, p = 2, Residual SE = 4.36, R-Squared = 0.24
data(whiteside, package = "MASS")
require(ggplot2)
ggplot(aes(x = Temp, y = Gas), data = whiteside) + geom_point() + facet_grid(~Insul) + 
    geom_smooth(method = "lm")

plot of chunk unnamed-chunk-1

lmod <- lm(Gas ~ Temp * Insul, whiteside)
sumary(lmod)
                Estimate Std. Error t value Pr(>|t|)
(Intercept)       6.8538     0.1360   50.41  < 2e-16
Temp             -0.3932     0.0225  -17.49  < 2e-16
InsulAfter       -2.1300     0.1801  -11.83  2.3e-16
Temp:InsulAfter   0.1153     0.0321    3.59  0.00073

n = 56, p = 4, Residual SE = 0.32, R-Squared = 0.93
mean(whiteside$Temp)
[1] 4.875
whiteside$ctemp <- whiteside$Temp - mean(whiteside$Temp)
lmodc <- lm(Gas ~ ctemp * Insul, whiteside)
sumary(lmodc)
                 Estimate Std. Error t value Pr(>|t|)
(Intercept)        4.9368     0.0642   76.85  < 2e-16
ctemp             -0.3932     0.0225  -17.49  < 2e-16
InsulAfter        -1.5679     0.0877  -17.87  < 2e-16
ctemp:InsulAfter   0.1153     0.0321    3.59  0.00073

n = 56, p = 4, Residual SE = 0.32, R-Squared = 0.93
data(fruitfly, package = "faraway")
plot(longevity ~ thorax, fruitfly, pch = unclass(activity))
legend(0.63, 100, levels(fruitfly$activity), pch = 1:5)

plot of chunk unnamed-chunk-1

require(ggplot2)
ggplot(aes(x = thorax, y = longevity), data = fruitfly) + geom_point() + facet_wrap(~activity)

plot of chunk unnamed-chunk-1

lmod <- lm(longevity ~ thorax * activity, fruitfly)
sumary(lmod)
                    Estimate Std. Error t value Pr(>|t|)
(Intercept)          -50.242     21.801   -2.30    0.023
thorax               136.127     25.952    5.25  7.3e-07
activityone            6.517     33.871    0.19    0.848
activitylow           -7.750     33.969   -0.23    0.820
activitymany          -1.139     32.530   -0.04    0.972
activityhigh         -11.038     31.287   -0.35    0.725
thorax:activityone    -4.677     40.652   -0.12    0.909
thorax:activitylow     0.874     40.425    0.02    0.983
thorax:activitymany    6.548     39.360    0.17    0.868
thorax:activityhigh  -11.127     38.120   -0.29    0.771

n = 124, p = 10, Residual SE = 10.71, R-Squared = 0.65
model.matrix(lmod)
    (Intercept) thorax activityone activitylow activitymany activityhigh
1             1   0.68           0           0            1            0
2             1   0.68           0           0            1            0
3             1   0.72           0           0            1            0
4             1   0.72           0           0            1            0
5             1   0.76           0           0            1            0
6             1   0.76           0           0            1            0
7             1   0.76           0           0            1            0
8             1   0.76           0           0            1            0
9             1   0.76           0           0            1            0
10            1   0.80           0           0            1            0
11            1   0.80           0           0            1            0
12            1   0.80           0           0            1            0
13            1   0.84           0           0            1            0
14            1   0.84           0           0            1            0
15            1   0.84           0           0            1            0
16            1   0.84           0           0            1            0
17            1   0.84           0           0            1            0
18            1   0.84           0           0            1            0
19            1   0.88           0           0            1            0
20            1   0.88           0           0            1            0
21            1   0.92           0           0            1            0
22            1   0.92           0           0            1            0
23            1   0.92           0           0            1            0
24            1   0.94           0           0            1            0
25            1   0.64           0           0            0            0
26            1   0.70           0           0            0            0
27            1   0.72           0           0            0            0
28            1   0.72           0           0            0            0
29            1   0.72           0           0            0            0
30            1   0.76           0           0            0            0
31            1   0.78           0           0            0            0
32            1   0.80           0           0            0            0
33            1   0.84           0           0            0            0
34            1   0.84           0           0            0            0
35            1   0.84           0           0            0            0
36            1   0.84           0           0            0            0
37            1   0.84           0           0            0            0
38            1   0.88           0           0            0            0
39            1   0.88           0           0            0            0
40            1   0.88           0           0            0            0
41            1   0.88           0           0            0            0
42            1   0.88           0           0            0            0
43            1   0.92           0           0            0            0
44            1   0.92           0           0            0            0
45            1   0.92           0           0            0            0
46            1   0.92           0           0            0            0
47            1   0.92           0           0            0            0
48            1   0.92           0           0            0            0
49            1   0.94           0           0            0            0
50            1   0.64           1           0            0            0
51            1   0.68           1           0            0            0
52            1   0.72           1           0            0            0
53            1   0.76           1           0            0            0
54            1   0.76           1           0            0            0
55            1   0.80           1           0            0            0
56            1   0.80           1           0            0            0
57            1   0.80           1           0            0            0
58            1   0.82           1           0            0            0
59            1   0.82           1           0            0            0
60            1   0.84           1           0            0            0
61            1   0.84           1           0            0            0
62            1   0.84           1           0            0            0
63            1   0.84           1           0            0            0
64            1   0.84           1           0            0            0
65            1   0.84           1           0            0            0
66            1   0.88           1           0            0            0
67            1   0.88           1           0            0            0
68            1   0.88           1           0            0            0
69            1   0.88           1           0            0            0
70            1   0.88           1           0            0            0
71            1   0.88           1           0            0            0
72            1   0.88           1           0            0            0
73            1   0.92           1           0            0            0
74            1   0.92           1           0            0            0
75            1   0.68           0           1            0            0
76            1   0.68           0           1            0            0
77            1   0.72           0           1            0            0
78            1   0.76           0           1            0            0
79            1   0.78           0           1            0            0
80            1   0.80           0           1            0            0
81            1   0.80           0           1            0            0
82            1   0.80           0           1            0            0
83            1   0.84           0           1            0            0
84            1   0.84           0           1            0            0
85            1   0.84           0           1            0            0
86            1   0.84           0           1            0            0
87            1   0.84           0           1            0            0
88            1   0.84           0           1            0            0
89            1   0.88           0           1            0            0
90            1   0.88           0           1            0            0
91            1   0.88           0           1            0            0
92            1   0.90           0           1            0            0
93            1   0.90           0           1            0            0
94            1   0.90           0           1            0            0
95            1   0.90           0           1            0            0
96            1   0.90           0           1            0            0
97            1   0.90           0           1            0            0
98            1   0.92           0           1            0            0
99            1   0.92           0           1            0            0
100           1   0.64           0           0            0            1
101           1   0.64           0           0            0            1
102           1   0.68           0           0            0            1
103           1   0.72           0           0            0            1
104           1   0.72           0           0            0            1
105           1   0.74           0           0            0            1
106           1   0.76           0           0            0            1
107           1   0.76           0           0            0            1
108           1   0.76           0           0            0            1
109           1   0.78           0           0            0            1
110           1   0.80           0           0            0            1
111           1   0.80           0           0            0            1
112           1   0.82           0           0            0            1
113           1   0.82           0           0            0            1
114           1   0.84           0           0            0            1
115           1   0.84           0           0            0            1
116           1   0.84           0           0            0            1
117           1   0.84           0           0            0            1
118           1   0.88           0           0            0            1
119           1   0.88           0           0            0            1
120           1   0.88           0           0            0            1
121           1   0.88           0           0            0            1
122           1   0.88           0           0            0            1
123           1   0.88           0           0            0            1
124           1   0.92           0           0            0            1
    thorax:activityone thorax:activitylow thorax:activitymany
1                 0.00               0.00                0.68
2                 0.00               0.00                0.68
3                 0.00               0.00                0.72
4                 0.00               0.00                0.72
5                 0.00               0.00                0.76
6                 0.00               0.00                0.76
7                 0.00               0.00                0.76
8                 0.00               0.00                0.76
9                 0.00               0.00                0.76
10                0.00               0.00                0.80
11                0.00               0.00                0.80
12                0.00               0.00                0.80
13                0.00               0.00                0.84
14                0.00               0.00                0.84
15                0.00               0.00                0.84
16                0.00               0.00                0.84
17                0.00               0.00                0.84
18                0.00               0.00                0.84
19                0.00               0.00                0.88
20                0.00               0.00                0.88
21                0.00               0.00                0.92
22                0.00               0.00                0.92
23                0.00               0.00                0.92
24                0.00               0.00                0.94
25                0.00               0.00                0.00
26                0.00               0.00                0.00
27                0.00               0.00                0.00
28                0.00               0.00                0.00
29                0.00               0.00                0.00
30                0.00               0.00                0.00
31                0.00               0.00                0.00
32                0.00               0.00                0.00
33                0.00               0.00                0.00
34                0.00               0.00                0.00
35                0.00               0.00                0.00
36                0.00               0.00                0.00
37                0.00               0.00                0.00
38                0.00               0.00                0.00
39                0.00               0.00                0.00
40                0.00               0.00                0.00
41                0.00               0.00                0.00
42                0.00               0.00                0.00
43                0.00               0.00                0.00
44                0.00               0.00                0.00
45                0.00               0.00                0.00
46                0.00               0.00                0.00
47                0.00               0.00                0.00
48                0.00               0.00                0.00
49                0.00               0.00                0.00
50                0.64               0.00                0.00
51                0.68               0.00                0.00
52                0.72               0.00                0.00
53                0.76               0.00                0.00
54                0.76               0.00                0.00
55                0.80               0.00                0.00
56                0.80               0.00                0.00
57                0.80               0.00                0.00
58                0.82               0.00                0.00
59                0.82               0.00                0.00
60                0.84               0.00                0.00
61                0.84               0.00                0.00
62                0.84               0.00                0.00
63                0.84               0.00                0.00
64                0.84               0.00                0.00
65                0.84               0.00                0.00
66                0.88               0.00                0.00
67                0.88               0.00                0.00
68                0.88               0.00                0.00
69                0.88               0.00                0.00
70                0.88               0.00                0.00
71                0.88               0.00                0.00
72                0.88               0.00                0.00
73                0.92               0.00                0.00
74                0.92               0.00                0.00
75                0.00               0.68                0.00
76                0.00               0.68                0.00
77                0.00               0.72                0.00
78                0.00               0.76                0.00
79                0.00               0.78                0.00
80                0.00               0.80                0.00
81                0.00               0.80                0.00
82                0.00               0.80                0.00
83                0.00               0.84                0.00
84                0.00               0.84                0.00
85                0.00               0.84                0.00
86                0.00               0.84                0.00
87                0.00               0.84                0.00
88                0.00               0.84                0.00
89                0.00               0.88                0.00
90                0.00               0.88                0.00
91                0.00               0.88                0.00
92                0.00               0.90                0.00
93                0.00               0.90                0.00
94                0.00               0.90                0.00
95                0.00               0.90                0.00
96                0.00               0.90                0.00
97                0.00               0.90                0.00
98                0.00               0.92                0.00
99                0.00               0.92                0.00
100               0.00               0.00                0.00
101               0.00               0.00                0.00
102               0.00               0.00                0.00
103               0.00               0.00                0.00
104               0.00               0.00                0.00
105               0.00               0.00                0.00
106               0.00               0.00                0.00
107               0.00               0.00                0.00
108               0.00               0.00                0.00
109               0.00               0.00                0.00
110               0.00               0.00                0.00
111               0.00               0.00                0.00
112               0.00               0.00                0.00
113               0.00               0.00                0.00
114               0.00               0.00                0.00
115               0.00               0.00                0.00
116               0.00               0.00                0.00
117               0.00               0.00                0.00
118               0.00               0.00                0.00
119               0.00               0.00                0.00
120               0.00               0.00                0.00
121               0.00               0.00                0.00
122               0.00               0.00                0.00
123               0.00               0.00                0.00
124               0.00               0.00                0.00
    thorax:activityhigh
1                  0.00
2                  0.00
3                  0.00
4                  0.00
5                  0.00
6                  0.00
7                  0.00
8                  0.00
9                  0.00
10                 0.00
11                 0.00
12                 0.00
13                 0.00
14                 0.00
15                 0.00
16                 0.00
17                 0.00
18                 0.00
19                 0.00
20                 0.00
21                 0.00
22                 0.00
23                 0.00
24                 0.00
25                 0.00
26                 0.00
27                 0.00
28                 0.00
29                 0.00
30                 0.00
31                 0.00
32                 0.00
33                 0.00
34                 0.00
35                 0.00
36                 0.00
37                 0.00
38                 0.00
39                 0.00
40                 0.00
41                 0.00
42                 0.00
43                 0.00
44                 0.00
45                 0.00
46                 0.00
47                 0.00
48                 0.00
49                 0.00
50                 0.00
51                 0.00
52                 0.00
53                 0.00
54                 0.00
55                 0.00
56                 0.00
57                 0.00
58                 0.00
59                 0.00
60                 0.00
61                 0.00
62                 0.00
63                 0.00
64                 0.00
65                 0.00
66                 0.00
67                 0.00
68                 0.00
69                 0.00
70                 0.00
71                 0.00
72                 0.00
73                 0.00
74                 0.00
75                 0.00
76                 0.00
77                 0.00
78                 0.00
79                 0.00
80                 0.00
81                 0.00
82                 0.00
83                 0.00
84                 0.00
85                 0.00
86                 0.00
87                 0.00
88                 0.00
89                 0.00
90                 0.00
91                 0.00
92                 0.00
93                 0.00
94                 0.00
95                 0.00
96                 0.00
97                 0.00
98                 0.00
99                 0.00
100                0.64
101                0.64
102                0.68
103                0.72
104                0.72
105                0.74
106                0.76
107                0.76
108                0.76
109                0.78
110                0.80
111                0.80
112                0.82
113                0.82
114                0.84
115                0.84
116                0.84
117                0.84
118                0.88
119                0.88
120                0.88
121                0.88
122                0.88
123                0.88
124                0.92
attr(,"assign")
 [1] 0 1 2 2 2 2 3 3 3 3
attr(,"contrasts")
attr(,"contrasts")$activity
[1] "contr.treatment"
plot(lmod)

plot of chunk unnamed-chunk-1plot of chunk unnamed-chunk-1plot of chunk unnamed-chunk-1plot of chunk unnamed-chunk-1

anova(lmod)
Analysis of Variance Table

Response: longevity
                 Df Sum Sq Mean Sq F value  Pr(>F)
thorax            1  15003   15003  130.73 < 2e-16
activity          4   9635    2409   20.99 5.5e-13
thorax:activity   4     24       6    0.05    0.99
Residuals       114  13083     115                
lmodp <- lm(longevity ~ thorax + activity, fruitfly)
drop1(lmodp, test = "F")
Single term deletions

Model:
longevity ~ thorax + activity
         Df Sum of Sq   RSS AIC F value Pr(>F)
<none>                13107 590               
thorax    1     12368 25476 670   111.3 <2e-16
activity  4      9635 22742 650    21.7  2e-13
sumary(lmodp)
             Estimate Std. Error t value Pr(>|t|)
(Intercept)    -48.75      10.85   -4.49  1.6e-05
thorax         134.34      12.73   10.55  < 2e-16
activityone      2.64       2.98    0.88     0.38
activitylow     -7.01       2.98   -2.35     0.02
activitymany     4.14       3.03    1.37     0.17
activityhigh   -20.00       3.02   -6.63  1.0e-09

n = 124, p = 6, Residual SE = 10.54, R-Squared = 0.65
plot(residuals(lmodp) ~ fitted(lmodp), pch = unclass(fruitfly$activity), xlab = "Fitted", 
    ylab = "Residuals")
abline(h = 0)

plot of chunk unnamed-chunk-1

lmodl <- lm(log(longevity) ~ thorax + activity, fruitfly)
plot(residuals(lmodl) ~ fitted(lmodl), pch = unclass(fruitfly$activity), xlab = "Fitted", 
    ylab = "Residuals")
abline(h = 0)

plot of chunk unnamed-chunk-1

sumary(lmodl)
             Estimate Std. Error t value Pr(>|t|)
(Intercept)    1.8442     0.1988    9.28  1.0e-15
thorax         2.7215     0.2333   11.67  < 2e-16
activityone    0.0517     0.0547    0.95    0.346
activitylow   -0.1239     0.0546   -2.27    0.025
activitymany   0.0879     0.0555    1.59    0.116
activityhigh  -0.4193     0.0553   -7.59  8.4e-12

n = 124, p = 6, Residual SE = 0.19, R-Squared = 0.7
exp(coef(lmodl)[3:6])
 activityone  activitylow activitymany activityhigh 
      1.0531       0.8835       1.0919       0.6575 
lmodh <- lm(thorax ~ activity, fruitfly)
anova(lmodh)
Analysis of Variance Table

Response: thorax
           Df Sum Sq Mean Sq F value Pr(>F)
activity    4  0.026 0.00639    1.11   0.36
Residuals 119  0.685 0.00576               
lmodu <- lm(log(longevity) ~ activity, fruitfly)
sumary(lmodu)
             Estimate Std. Error t value Pr(>|t|)
(Intercept)    4.1193     0.0564   72.99  < 2e-16
activityone    0.0234     0.0798    0.29     0.77
activitylow   -0.1195     0.0798   -1.50     0.14
activitymany   0.0240     0.0806    0.30     0.77
activityhigh  -0.5172     0.0798   -6.48  2.2e-09

n = 124, p = 5, Residual SE = 0.28, R-Squared = 0.36
contr.treatment(4)
  2 3 4
1 0 0 0
2 1 0 0
3 0 1 0
4 0 0 1
contr.helmert(4)
  [,1] [,2] [,3]
1   -1   -1   -1
2    1   -1   -1
3    0    2   -1
4    0    0    3
contr.sum(4)
  [,1] [,2] [,3]
1    1    0    0
2    0    1    0
3    0    0    1
4   -1   -1   -1
contrasts(sexab$csa) <- contr.sum(2)
sumary(lm(ptsd ~ csa, sexab))
            Estimate Std. Error t value Pr(>|t|)
(Intercept)    8.318      0.405   20.53  < 2e-16
csa1          -3.623      0.405   -8.94  2.2e-13

n = 76, p = 2, Residual SE = 3.47, R-Squared = 0.52

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] 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:02:29 BST"