Model Linear
Model Linear
Model Linear
Ivan Aliaga
16 de septiembre de 2015
demo("glm.vr")
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
demo(glm.vr)
---- ~~~~~~
> # Copyright (C) 1997-2009 The R Core Team
>
> #### -*- R -*> require(stats)
> Fr <- c(68,42,42,30, 37,52,24,43,
+
66,50,33,23, 47,55,23,47,
+
63,53,29,27, 57,49,19,29)
> Temp <- gl(2, 2, 24, labels = c("Low", "High"))
> Soft <- gl(3, 8, 24, labels = c("Hard","Medium","Soft"))
> M.user <- gl(2, 4, 24, labels = c("N", "Y"))
> Brand <- gl(2, 1, 24, labels = c("X", "M"))
> detg <- data.frame(Fr,Temp, Soft,M.user, Brand)
> detg.m0 <- glm(Fr ~ M.user*Temp*Soft + Brand, family = poisson, data = detg)
> summary(detg.m0)
Call:
glm(formula = Fr ~ M.user * Temp * Soft + Brand, family = poisson,
data = detg)
Deviance Residuals:
Min
1Q
Median
-2.20876 -0.99190 -0.00126
3Q
0.93542
Max
1.97601
Coefficients:
(Intercept)
M.userY
TempHigh
SoftMedium
SoftSoft
BrandM
M.userY:TempHigh
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
M.userY:SoftMedium
0.08323
M.userY:SoftSoft
0.12169
TempHigh:SoftMedium
-0.30442
TempHigh:SoftSoft
-0.30442
M.userY:TempHigh:SoftMedium 0.21189
M.userY:TempHigh:SoftSoft
-0.20387
--Signif. codes: 0 *** 0.001 ** 0.01
0.19685
0.19591
0.22239
0.22239
0.31577
0.32540
0.423
0.621
-1.369
-1.369
0.671
-0.627
0.67245
0.53449
0.17104
0.17104
0.50220
0.53098
* 0.05 . 0.1 1
on 23
on 11
degrees of freedom
degrees of freedom
Median
0.00253
3Q
0.33027
Max
0.92146
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept)
4.14887
0.10603 39.128 < 2e-16
M.userY
-0.40521
0.16188 -2.503 0.01231
TempHigh
-0.44275
0.17121 -2.586 0.00971
M.userY:TempHigh
-0.12692
0.26257 -0.483 0.62883
SoftMedium
0.05311
0.13308
0.399 0.68984
SoftSoft
0.05311
0.13308
0.399 0.68984
M.userY:SoftMedium
0.08323
0.19685
0.423 0.67245
M.userY:SoftSoft
0.12169
0.19591
0.621 0.53449
TempHigh:SoftMedium
-0.30442
0.22239 -1.369 0.17104
TempHigh:SoftSoft
-0.30442
0.22239 -1.369 0.17104
M.userY:TempHigh:SoftMedium 0.21189
0.31577
0.671 0.50220
M.userY:TempHigh:SoftSoft
-0.20387
0.32540 -0.627 0.53098
BrandM
-0.30647
0.10942 -2.801 0.00510
M.userY:BrandM
0.40757
0.15961
2.554 0.01066
TempHigh:BrandM
0.04411
0.18463
0.239 0.81119
M.userY:TempHigh:BrandM
0.44427
0.26673
1.666 0.09579
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Dispersion parameter for poisson family taken to be 1)
***
*
**
**
*
.
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
on 23
on 8
degrees of freedom
degrees of freedom
Median
0.00253
3Q
0.33027
Max
0.92146
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept)
4.14887
0.10603 39.128 < 2e-16
M.userY
-0.40521
0.16188 -2.503 0.01231
TempHigh
-0.44275
0.17121 -2.586 0.00971
M.userY:TempHigh
-0.12692
0.26257 -0.483 0.62883
SoftMedium
0.05311
0.13308
0.399 0.68984
SoftSoft
0.05311
0.13308
0.399 0.68984
M.userY:SoftMedium
0.08323
0.19685
0.423 0.67245
M.userY:SoftSoft
0.12169
0.19591
0.621 0.53449
TempHigh:SoftMedium
-0.30442
0.22239 -1.369 0.17104
TempHigh:SoftSoft
-0.30442
0.22239 -1.369 0.17104
M.userY:TempHigh:SoftMedium 0.21189
0.31577
0.671 0.50220
M.userY:TempHigh:SoftSoft
-0.20387
0.32540 -0.627 0.53098
BrandM
-0.30647
0.10942 -2.801 0.00510
M.userY:BrandM
0.40757
0.15961
2.554 0.01066
TempHigh:BrandM
0.04411
0.18463
0.239 0.81119
M.userY:TempHigh:BrandM
0.44427
0.26673
1.666 0.09579
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 118.627
Residual deviance:
5.656
AIC: 170.07
on 23
on 8
degrees of freedom
degrees of freedom
1
,
,
.
,
1
. 1
, , 1
. .
1
***
*
**
**
*
.
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
SoftSoft
,
M.userY:SoftMedium
.
M.userY:SoftSoft
.
TempHigh:SoftMedium
.
TempHigh:SoftSoft
.
M.userY:TempHigh:SoftMedium
M.userY:TempHigh:SoftSoft
BrandM
.
M.userY:BrandM
TempHigh:BrandM
M.userY:TempHigh:BrandM
attr(,"legend")
[1] 0 0.3 . 0.6 , 0.8
. .
.
,
. ,
,
. .
, . .
, . .
. . . .
. . .
1
. 1
, . 1
. .
1
.
. . 1
, . , . 1
. . , . , . 1
1
, 1
. . 1
. . , 1
.
. .
. .
+ 0.9 * 0.95 B 1
demo("lm.glm")
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
demo(lm.glm)
---- ~~~~~~
>
>
>
>
>
>
>
>
require(stats); require(graphics)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
group
1 0.6882 0.68820
Residuals 18 8.7292 0.48496
1.4191
0.249
Median
0.0685
3Q
0.2462
Max
1.3690
Coefficients:
Estimate Std. Error t value Pr(>|t|)
groupCtl
5.0320
0.2202
22.85 9.55e-15 ***
groupTrt
4.6610
0.2202
21.16 3.62e-14 ***
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 0.6964 on 18 degrees of freedom
Multiple R-squared: 0.9818, Adjusted R-squared: 0.9798
F-statistic: 485.1 on 2 and 18 DF, p-value: < 2.2e-16
2800
2400
birthw
3200
M
F
35
36
37
38
39
40
41
age
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
correlation=TRUE)
Call:
lm(formula = birthw ~ sex + age)
Residuals:
Min
1Q
-257.49 -125.28
Median
-58.44
3Q
169.00
Max
303.98
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1610.28
786.08 -2.049
0.0532 .
sexF
-163.04
72.81 -2.239
0.0361 *
age
120.89
20.46
5.908 7.28e-06 ***
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 177.1 on 21 degrees of freedom
Multiple R-squared:
0.64, Adjusted R-squared: 0.6057
6
42
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
p-value: 2.194e-05
Correlation of Coefficients:
(Intercept) sexF
sexF 0.07
age -1.00
-0.12
Median
-58.44
3Q
169.00
Max
303.98
Coefficients:
Estimate Std. Error t value Pr(>|t|)
sexM -1610.28
786.08 -2.049
0.0532 .
sexF -1773.32
794.59 -2.232
0.0367 *
age
120.89
20.46
5.908 7.28e-06 ***
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 177.1 on 21 degrees of freedom
Multiple R-squared: 0.9969, Adjusted R-squared: 0.9965
F-statistic: 2258 on 3 and 21 DF, p-value: < 2.2e-16
Correlation of Coefficients:
sexM sexF
sexF 1.00
age -1.00 -1.00
> anova(l1,l0)
Analysis of Variance Table
Model 1:
Model 2:
Res.Df
1
21
2
21
Median
-39.13
3Q
176.57
Max
274.28
Coefficients:
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
> anova(li,l0)
Analysis of Variance Table
Model 1:
Model 2:
Res.Df
1
20
2
21
birthw
birthw
RSS
652425
658771
~ sex + sex:age - 1
~ sex + age - 1
Df Sum of Sq
F Pr(>F)
-1
3Q
169.00
Max
303.98
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1610.28
786.08 -2.049
0.0532 .
sexF
-163.04
72.81 -2.239
0.0361 *
age
120.89
20.46
5.908 7.28e-06 ***
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Dispersion parameter for gaussian family taken to be 31370.04)
Null deviance: 1829873
Residual deviance: 658771
AIC: 321.39
on 23
on 21
degrees of freedom
degrees of freedom
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
3Q
169.00
Max
303.98
Coefficients:
Estimate Std. Error t value Pr(>|t|)
sexM -1610.28
786.08 -2.049
0.0532 .
sexF -1773.32
794.59 -2.232
0.0367 *
age
120.89
20.46
5.908 7.28e-06 ***
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Dispersion parameter for gaussian family taken to be 31370.04)
Null deviance: 213198964
Residual deviance:
658771
AIC: 321.39
on 24
on 21
degrees of freedom
degrees of freedom
3Q
165.04
Max
251.14
Coefficients:
Estimate Std. Error t value Pr(>|t|)
sexM -2318.03
801.57 -2.892 0.00902 **
sexF -2455.44
803.79 -3.055 0.00625 **
age
138.50
20.71
6.688 1.65e-06 ***
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Dispersion parameter for gaussian family taken to be 26925.39)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
on 23
on 20
degrees of freedom
degrees of freedom
3Q
176.57
Max
274.28
Coefficients:
Estimate Std. Error t value Pr(>|t|)
sexM
-1268.67
1114.64 -1.138 0.268492
sexF
-2141.67
1163.60 -1.841 0.080574 .
age
111.98
29.05
3.855 0.000986 ***
sexF:age
18.42
41.76
0.441 0.663893
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Dispersion parameter for gaussian family taken to be 32621.23)
Null deviance: 213198964
Residual deviance:
652425
AIC: 323.16
on 24
on 20
degrees of freedom
degrees of freedom
> anova(z0,zz)
Analysis of Deviance Table
Model 1:
Model 2:
Resid.
1
2
10
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
Min
-0.7019
1Q
-0.3377
Median
-0.1105
3Q
0.2958
Max
0.7184
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept)
7.4516
0.8841
8.428 < 2e-16 ***
x
4.9353
1.0892
4.531 5.86e-06 ***
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 18.4206
Residual deviance: 1.8947
AIC: 40.008
on 8
on 7
degrees of freedom
degrees of freedom
Median
0.9897
3Q
3.8553
Max
7.9087
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 36.96006
13.07128
2.828 0.01213 *
age
-0.11368
0.10933 -1.040 0.31389
wgt
-0.22802
0.08329 -2.738 0.01460 *
prot
1.95771
0.63489
3.084 0.00712 **
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 5.956 on 16 degrees of freedom
Multiple R-squared: 0.4805, Adjusted R-squared: 0.3831
F-statistic: 4.934 on 3 and 16 DF, p-value: 0.01297
11
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
3Q
0.2627
Max
1.3690
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)
5.0320
0.1971 25.527
<2e-16 ***
groupA
-0.3710
0.2788 -1.331
0.1944
groupB
0.4940
0.2788
1.772
0.0877 .
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 0.6234 on 27 degrees of freedom
Multiple R-squared: 0.2641, Adjusted R-squared: 0.2096
F-statistic: 4.846 on 2 and 27 DF, p-value: 0.01591
> coef(lmwg)
(Intercept)
5.032
groupA
-0.371
groupB
0.494
12
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
Call:
glm(formula = dead ~ dose, family = binomial(link = logit))
Deviance Residuals:
Min
1Q
Median
-1.5941 -0.3944
0.8329
3Q
1.2592
Max
1.5940
Coefficients:
13
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
on 7
on 6
degrees of freedom
degrees of freedom
> summary(
Call:
glm(formula = dead ~ dose, family = binomial(link = probit))
Deviance Residuals:
Min
1Q
Median
-1.5714 -0.4703
0.7501
3Q
1.0632
Max
1.3449
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -34.935
2.648 -13.19
<2e-16 ***
dose
19.728
1.487
13.27
<2e-16 ***
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 284.20
Residual deviance: 10.12
AIC: 40.318
on 7
on 6
degrees of freedom
degrees of freedom
Median
0.03089
3Q
0.38315
Max
1.28883
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -39.572
3.240 -12.21
<2e-16 ***
dose
22.041
1.799
12.25
<2e-16 ***
---
14
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
Signif. codes:
on 7
on 6
degrees of freedom
degrees of freedom
3
0.04414
4
0.42320
5
-0.60082
6
0.19522
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.1456719 0.1975451
0.737
0.4609
g2
0.7963143 0.3125046
2.548
0.0108 *
f
-0.0001227 0.0008782 -0.140
0.8889
15
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
g2:f
-0.0020493 0.0013483 -1.520
0.1285
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 10.45197
Residual deviance: 0.60387
AIC: 38.172
on 5
on 2
degrees of freedom
degrees of freedom
4
1.1558
5
-0.3688
6
-0.6584
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.306643
0.167629
1.829
0.0674 .
g2
0.405554
0.174560
2.323
0.0202 *
f
-0.000997
0.000665 -1.499
0.1338
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 10.4520
Residual deviance: 2.9218
AIC: 38.49
on 5
on 3
degrees of freedom
degrees of freedom
family=binomial(link="logit")))
Call:
glm(formula = y ~ g, family = binomial(link = "logit"))
Deviance Residuals:
1
2
3
0.17150 -0.10947 -0.06177
4
1.77208
5
-0.19040
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept)
0.1231
0.1140
1.080
0.2801
g2
0.3985
0.1741
2.289
0.0221 *
---
16
6
-1.39686
##
##
##
##
##
##
##
##
##
##
##
##
##
##
Signif. codes:
on 5
on 4
degrees of freedom
degrees of freedom
5
6
0.2
0.3
0.4
0.5
Normal QQ
4
10 10
Residuals
Residuals vs Fitted
glm(y ~ g)
1
6
1.0 0.5
0.4
0.5
Predicted values
##
##
##
##
##
##
##
##
##
##
0.3
1.0
Residuals vs Leverage
1
0.5
5
Cook's distance
4
6
ScaleLocation
0.2
0.5
Theoretical Quantiles
Predicted values
0.0
0.0
0.1
0.2
0.3
Leverage
> par(op)
> ## Tumour Data (Page 92)
> counts <- c(22,2,10,16,54,115,19,33,73,11,17,28)
> type <- gl(4,3,12,labels=c("freckle","superficial","nodular","indeterminate"))
> site <- gl(3,1,12,labels=c("head/neck","trunk","extremities"))
17
0.5
1
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
> data.frame(counts,type,site)
counts
type
site
1
22
freckle
head/neck
2
2
freckle
trunk
3
10
freckle extremities
4
16
superficial
head/neck
5
54
superficial
trunk
6
115
superficial extremities
7
19
nodular
head/neck
8
33
nodular
trunk
9
73
nodular extremities
10
11 indeterminate
head/neck
11
17 indeterminate
trunk
12
28 indeterminate extremities
> summary(z <- glm(counts ~ type + site, family=poisson()))
Call:
glm(formula = counts ~ type + site, family = poisson())
Deviance Residuals:
Min
1Q
Median
-3.0453 -1.0741
0.1297
3Q
0.5857
Max
5.1354
Coefficients:
Estimate Std. Error z
(Intercept)
1.7544
0.2040
typesuperficial
1.6940
0.1866
typenodular
1.3020
0.1934
typeindeterminate
0.4990
0.2174
sitetrunk
0.4439
0.1554
siteextremities
1.2010
0.1383
--Signif. codes: 0 *** 0.001 ** 0.01
value Pr(>|z|)
8.600 < 2e-16 ***
9.079 < 2e-16 ***
6.731 1.68e-11 ***
2.295 0.02173 *
2.857 0.00427 **
8.683 < 2e-16 ***
* 0.05 . 0.1 1
on 11
on 6
degrees of freedom
degrees of freedom
18
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
4
-0.21999
5
-0.95552
6
1.04939
7
0.84715
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.045e+00 1.709e-01 17.815
<2e-16 ***
outcome2
-4.543e-01 2.022e-01 -2.247
0.0246 *
outcome3
-2.930e-01 1.927e-01 -1.520
0.1285
treatment2
1.338e-15 2.000e-01
0.000
1.0000
treatment3
1.421e-15 2.000e-01
0.000
1.0000
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 10.5814
Residual deviance: 5.1291
AIC: 56.761
on 8
on 4
degrees of freedom
degrees of freedom
> cbind(group, blood, city, counts) # gives internal codes for the factors
group blood city counts
[1,]
1
1
1
579
[2,]
2
1
1
4219
[3,]
1
2
1
911
[4,]
2
2
1
4578
[5,]
1
1
2
246
[6,]
2
1
2
3775
[7,]
1
2
2
361
[8,]
2
2
2
4532
[9,]
1
1
3
291
[10,]
2
1
3
5261
[11,]
1
2
3
396
[12,]
2
2
3
6598
> summary(z1 <- glm(counts ~ group*(city + blood), family=poisson()))
Call:
19
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
4
-2.8137
12
2.0648
5
0.1713
6
-0.4339
7
-0.1405
8
0.3977
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept)
6.39239
0.03476 183.92 < 2e-16
groupcontrols
1.90813
0.03691
51.69 < 2e-16
cityManchester
-0.89800
0.04815 -18.65 < 2e-16
cityNewcastle
-0.77420
0.04612 -16.79 < 2e-16
bloodO
0.40187
0.03867
10.39 < 2e-16
groupcontrols:cityManchester 0.84069
0.05052
16.64 < 2e-16
groupcontrols:cityNewcastle
1.07287
0.04822
22.25 < 2e-16
groupcontrols:bloodO
-0.23208
0.04043
-5.74 9.46e-09
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
***
***
***
***
***
***
***
***
on 11
on 4
degrees of freedom
degrees of freedom
4
-3.4418
12
1.3275
5
-1.7675
6
0.2387
7
1.5565
8
-0.2174
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept)
6.51395
0.02663 244.60
<2e-16
groupcontrols
1.77563
0.02801
63.38
<2e-16
cityManchester
-0.89800
0.04815 -18.65
<2e-16
cityNewcastle
-0.77420
0.04612 -16.79
<2e-16
bloodO
0.18988
0.01128
16.84
<2e-16
groupcontrols:cityManchester 0.84069
0.05052
16.64
<2e-16
groupcontrols:cityNewcastle
1.07287
0.04822
22.25
<2e-16
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
20
***
***
***
***
***
***
***
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
on 11
on 5
degrees of freedom
degrees of freedom
groupcontrols
cityManchester
cityNewcastle
bloodO
0.00
groupcontrols:cityManchester -0.29
0.00
groupcontrols:cityNewcastle -0.96
0.00
groupcontrols:cityManchester
groupcontrols
cityManchester
cityNewcastle
bloodO
groupcontrols:cityManchester
groupcontrols:cityNewcastle
0.32
demo("nlm")
##
##
##
##
##
##
##
##
##
demo(nlm)
---- ~~~
> # Copyright (C) 1997-2009 The R Core Team
>
> ### Helical Valley Function
> ### Page 362 Dennis + Schnabel
21
>
> require(stats); require(graphics)
> theta <- function(x1,x2) (atan(x2/x1) + (if(x1 <= 0) pi else 0))/ (2*pi)
> ## but this is easier :
> theta <- function(x1,x2) atan2(x2, x1)/(2*pi)
> f <- function(x) {
+
f1 <- 10*(x[3] - 10*theta(x[1],x[2]))
+
f2 <- 10*(sqrt(x[1]^2+x[2]^2)-1)
+
f3 <- x[3]
+
return(f1^2+f2^2+f3^2)
+ }
> ## explore surface {at x3 = 0}
> x <- seq(-1, 2, length.out=50)
> y <- seq(-1, 1, length.out=50)
> z <- apply(as.matrix(expand.grid(x, y)), 1, function(x) f(c(x, 0)))
> contour(x, y, matrix(log10(z), 50, 50))
1.0
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
0.5
3
0
2.5
0.0
0.5
1.0
0.5
1.5
1.0
0.5
0.0
0.5
##
22
1.0
1.5
2.0
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
1.24e-14
[1:3] 1.00 3.07e-09 -6.06e-09
[1:3] -3.76e-07 3.49e-06 -2.20e-06
[1:3, 1:3] 2.00e+02 -4.07e-02 9.77e-07 -4.07e-02 5.07e+02 ...
2
27
>
>
+
+
+
+
## explore surface
fx <- function(x)
{
## vectorized version of fR()
x1 <- x[,1]; x2 <- x[,2]
100*(x2 - x1*x1)^2 + (1-x1)^2
}
fR <- function(x)
{
x1 <- x[1]; x2 <- x[2]
100*(x2 - x1*x1)^2 + (1-x1)^2
}
##
## > contour(x, y, matrix(log10(z), length(x)))
2
1.5
0.
0.5
1.5
0
zoomed in
1.10
1.5
2.
2.
0.5
1.05
1.10
.5
1.00
0.5
2
1
0.90
0.90
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
.5
2.
.5
.5
1
1
.5
0.95
1.00
24
0.5
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
$estimate
[1] 1 1
$gradient
[1] 2.583521e-09 -1.201128e-09
$hessian
[,1]
[,2]
[1,] 802.24 -400.02
[2,] -400.02 200.00
$code
[1] 1
$iterations
[1] 24
25
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
+
+
+
+
+
+
+
+
+ }
0.4711891
$gradient
[1] -0.4911201
2.1115987
$hessian
[,1]
[,2]
[1,] 366.1188 271.4593
[2,] 271.4593 200.0000
$code
[1] 4
$iterations
[1] 100
demo("smooth")
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
demo(smooth)
---- ~~~~~~
> ### This used to be in
example(smooth) before we had package-specific demos
> # Copyright (C) 1997-2009 The R Core Team
>
> require(stats); require(graphics); require(datasets)
> op <- par(mfrow = c(1,1))
> ## The help(smooth) examples:
> example(smooth, package="stats")
smooth> require(graphics)
smooth> ## see also
demo(smooth) !
smooth>
smooth> x1 <- c(4, 1, 3, 6, 6, 4, 1, 6, 2, 4, 2) # very artificial
26
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
10
5
15
misbehaviour of "3RSR"
10
15
Index
##
##
##
##
##
##
##
##
smooth> lines(sm.3RS(y))
smooth> lines(smooth(y))
smooth> lines(smooth(y, "3RSR"), col = 3, lwd = 2)
smooth> x <- c(8:10, 10, 0, 0, 9, 9)
27
# the horror
20
##
## smooth> plot(x, main = "breakdown of
3R
and
and hence
3RSS")
10
Index
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
used 7 iterations
Min. 1st Qu. Median
24.00
44.00
57.00
Max.
82.00
# 3RSS
=== 3R S S
60
40
0
20
presidents
80
1945
1950
1955
1960
1965
Time
##
##
##
##
##
##
##
##
##
##
##
##
##
1970
1975
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
+
+
+
+
+
+
+
+
+
+
+
+ }
k <- ncol(ss) - 1
n <- length(x)
slwd <- c(1,1,4,1,3,2)
slty <- c(0, 2:(k+1))
matplot(ss, main = "Tukey Smoothers", ylab = "y ; sm(y)",
type= c("p",rep("l",k)), pch= par("pch"), lwd= slwd, lty= slty)
legend(leg.x, leg.y,
c("Data",
"3
(copy)", "3 (Tukey)",
"3R (copy)", "3R (Tukey)", "smooth()"),
pch= c(par("pch"),rep(-1,k)), col=1:(k+1), lwd= slwd, lty= slty)
ss
Data
3 (copy)
3 (Tukey)
3R (copy)
3R (Tukey)
smooth()
y ; sm(y)
Tukey Smoothers
##
## x
## 3c
## 3
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
4
6
2
2
6
3
6
6
5
2
4
4
2
2
3
6
6
6
5
2
4
4
2
2
3
6
6
6
5
3
30
10
## 3Rc
## 3R
## sm
4
4
4
4
4
4
2
2
4
2
2
3
3
3
3
6
6
6
6
6
6
6
6
6
5
5
5
2
3
3
Data
3 (copy)
3 (Tukey)
3R (copy)
3R (Tukey)
smooth()
y ; sm(y)
Tukey Smoothers
##
##
##
##
##
##
##
x
3c
3
3Rc
3R
sm
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
3
2
1
4
5
1
3
2
4
5
2
3
2
2
4
4
3
2
3
4
4
2
2
2
2
4
4
3
2
3
4
4
4
3
2
2
4
4
3
3
3
4
4
2
2
2
2
4
4
3
3
3
4
4
4
2
2
2
2
3
3
3
3
4
4
4
31
10
Data
3 (copy)
3 (Tukey)
3R (copy)
3R (Tukey)
smooth()
y ; sm(y)
Tukey Smoothers
##
##
##
##
##
##
##
x
3c
3
3Rc
3R
sm
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
2
4
2
6
1
1
2
6
3
1
6
2
2
4
2
1
1
2
3
3
3
6
2
2
4
2
1
1
2
3
3
3
3
2
2
2
2
1
1
2
3
3
3
6
2
2
2
2
1
1
2
3
3
3
3
2
2
2
2
2
2
2
3
3
3
3
32
10
Data
3 (copy)
3 (Tukey)
3R (copy)
3R (Tukey)
smooth()
y ; sm(y)
Tukey Smoothers
##
##
##
##
##
##
##
##
##
x
3c
3
3Rc
3R
sm
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
4
1
3
6
6
4
1
6
2
4
2
4
3
3
6
6
4
4
2
4
2
2
3
3
3
6
6
4
4
2
4
2
2
4
3
3
6
6
4
4
4
2
2
2
3
3
3
6
6
4
4
4
2
2
2
3
3
3
3
4
4
4
4
2
2
2
> par(op)
33
10