-
Notifications
You must be signed in to change notification settings - Fork 11
/
exm-plot-fits.qmd
211 lines (168 loc) · 3.44 KB
/
exm-plot-fits.qmd
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#### Example: PLOS Medicine title length data
(Adapted from @dobson4e, §6.7.1)
```{r}
#| fig-cap: "Number of authors versus title length in *PLOS Medicine* articles"
#| label: fig-plos
data(PLOS, package = "dobson")
library(ggplot2)
fig1 =
PLOS |>
ggplot(
aes(x = authors,
y = nchar)
) +
geom_point() +
theme(legend.position = "bottom") +
labs(col = "") +
guides(col=guide_legend(ncol=3))
fig1
```
---
##### Linear fit
```{r}
lm_PLOS_linear = lm(
formula = nchar ~ authors,
data = PLOS)
```
```{r}
#| fig-cap: "Number of authors versus title length in *PLOS Medicine*, with linear model fit"
#| label: fig-plos-lm
#| layout-ncol: 2
#| fig-subcap:
#| - "Data and fit"
#| - "Residuals vs fitted"
fig2 = fig1 +
geom_smooth(
method = "lm",
fullrange = TRUE,
aes(col = "lm(y ~ x)"))
fig2
library(ggfortify)
autoplot(lm_PLOS_linear, which = 1, ncol = 1)
```
---
##### Quadratic fit {.smaller}
```{r}
lm_PLOS_quad = lm(
formula = nchar ~ authors + I(authors^2),
data = PLOS)
```
```{r}
#| fig-cap: "Number of authors versus title length in *PLOS Medicine*, with quadratic model fit"
#| label: fig-plos-lm-quad
#| layout-ncol: 2
#| fig-subcap:
#| - "Data and fit"
#| - "Residuals vs fitted"
fig3 =
fig2 +
geom_smooth(
method = "lm",
fullrange = TRUE,
formula = y ~ x + I(x ^ 2),
aes(col = "lm(y ~ x + I(x^2))")
)
fig3
autoplot(lm_PLOS_quad, which = 1, ncol = 1)
```
---
##### Linear versus quadratic fits
```{r}
#| fig-cap: "Residuals versus fitted plot for linear and quadratic fits to `PLOS` data"
#| label: fig-plos-lm-resid2
#| layout-ncol: 2
#| fig-subcap:
#| - "Linear"
#| - "Quadratic"
library(ggfortify)
autoplot(lm_PLOS_linear, which = 1, ncol = 1)
autoplot(lm_PLOS_quad, which = 1, ncol = 1)
```
---
##### Cubic fit
```{r}
lm_PLOS_cub = lm(
formula = nchar ~ authors + I(authors^2) + I(authors^3),
data = PLOS)
```
```{r}
#| fig-cap: "Number of authors versus title length in *PLOS Medicine*, with cubic model fit"
#| label: fig-plos-lm-cubic
#| layout-ncol: 2
#| fig-subcap:
#| - "Data and fit"
#| - "Residuals vs fitted"
fig4 =
fig3 +
geom_smooth(
method = "lm",
fullrange = TRUE,
formula = y ~ x + I(x ^ 2) + I(x ^ 3),
aes(col = "lm(y ~ x + I(x^2) + I(x ^ 3))")
)
fig4
autoplot(lm_PLOS_cub, which = 1, ncol = 1)
```
---
##### Logarithmic fit
```{r}
lm_PLOS_log = lm(nchar ~ log(authors), data = PLOS)
```
```{r}
#| layout-ncol: 2
#| fig-cap: "logarithmic fit"
#| label: fig-plos-log
#| fig-subcap:
#| - "Data and fit"
#| - "Residuals vs fitted"
fig5 = fig4 +
geom_smooth(
method = "lm",
fullrange = TRUE,
formula = y ~ log(x),
aes(col = "lm(y ~ log(x))")
)
fig5
autoplot(lm_PLOS_log, which = 1, ncol = 1)
```
---
##### Model selection {.smaller}
```{r}
#| tbl-cap: "linear vs quadratic"
#| label: tbl-plos-lin-quad-anova
anova(lm_PLOS_linear, lm_PLOS_quad)
```
```{r}
#| tbl-cap: "quadratic vs cubic"
#| label: tbl-plos-quad-cub-anova
anova(lm_PLOS_quad, lm_PLOS_cub)
```
---
##### AIC/BIC {.smaller}
```{r}
#| code-fold: show
#|
AIC(lm_PLOS_quad)
AIC(lm_PLOS_cub)
```
```{r}
#| code-fold: show
#| label: plos-aic
AIC(lm_PLOS_cub)
AIC(lm_PLOS_log)
```
```{r}
#| code-fold: show
#| label: plos-bic
BIC(lm_PLOS_cub)
BIC(lm_PLOS_log)
```
---
##### Extrapolation is dangerous
```{r}
#| fig-cap: "Number of authors versus title length in *PLOS Medicine*"
#| label: fig-plos-multifit
fig_all = fig5 +
xlim(0, 60)
fig_all
```