-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTheRsoftware.qmd
333 lines (228 loc) · 6.73 KB
/
TheRsoftware.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
---
title: "The `R` software"
title-slide-attributes:
data-background-image: Uni_greenwich.png
data-background-size: contain
data-background-opacity: "0.05"
author:
- Mikis Stasinopoulos
- Bob Rigby
- Gillian Heller
- Fernanda De Bastiani
- Niki Umlauf
format:
revealjs:
multiplex: true
slide-number: true
show-slide-number: print
chalkboard:
buttons: true
incremental: false
menu:
side: left
width: wide
logo: gamlss-trans.png
footer: "www.gamlss.com"
css: styles.css
theme: sky
---
## Introduction
- `residuals` in GAMLSS
- an example; the `rent99` data
- `R` packages
# Residuals
## Introduction
- GAMLSS uses as `residuals` the
- `normalised quantile residuals`$\equiv$ `z-scores`
## `PIT` and `z-scores` residuals
let $y_i$ and $F(y_i, \hat(\theta)_i)$ be the ith observation and its fitted cdf respectively. Then the Probability Integral Transformed (`PIT`) residuals are
$$u_i = F(y_i, \hat(\theta)_i) $$ and the `z-scores` residuals are
$$z_i = \Phi^{-1}(y_i, \hat(\theta)_i) $$
## properties
If the distribution of $y_i$ is specified correctly then `PIT` are `uniform`;
i.e $$u_i \sim U(0,1)$$
and `z-scores` are `normally` distributed
i.e. $$z_i \sim NO(0,1)$$
## PIT
```{r}
#| warning: false
library(gamlss.ggplots)
library(ggplot2)
da <- dbbmi[db$age>10&db$age<20,]
m6 <- gamlss(bmi~age, data=da, trace=FALSE, family=BCTo)
y100 <- da[100,]$bmi
u100 <- pBCTo(y100, mu=fitted(m6, "mu")[100], sigma=fitted(m6, "sigma")[100], nu=fitted(m6, "nu")[100], tau=fitted(m6, "tau")[100])
fitted_cdf_data(m6, 100, from=10, to=30, title="the 100th observation PIT")+
ylab("PIT")+
ggplot2::geom_vline(xintercept = y100, colour="pink")+
ggplot2::geom_hline(yintercept = u100, colour="pink")+
geom_text(x=y100+0.15, y=.01, label="Y")+
geom_text(x=10-0.15, y=u100, label="U")+
geom_segment(aes(x=y100, y=0, xend=y100, yend=u100), arrow = arrow(length=unit(0.4, 'cm')))+
geom_segment(aes(x=y100, y=u100, xend=10, yend=u100), arrow = arrow(length=unit(0.4, 'cm')))
```
## z-scores
```{r}
z100 <- qNO(u100)
p9 <- ggplot(data.frame(u = c(0, 1)), aes(x = u)) +
stat_function(fun = qnorm, lwd=1.5)+ylab("z-score")+
ggplot2::geom_vline(xintercept = u100, colour="pink")+
ggplot2::geom_hline(yintercept = z100, colour="pink")+
geom_text(x=u100+0.08, y=-2.3, label="U")+
geom_text(x=-0.01, y=u100, label="Z")+
geom_segment(aes(x=u100, y=-2.3, xend=u100, yend=z100), arrow = arrow(length=unit(0.4, 'cm')))+
geom_segment(aes(x=u100, y=z100, xend=0, yend=z100), arrow = arrow(length=unit(0.4, 'cm')))
p9
```
## diagnostics plots
- residuals plots against other variables
```
index
x-variable
parameters
quantiles
```
- qqplots
- worm plots
- density plots
- bucket plots
- skewness plots
# Example: `rent`
## Data {.smaller}
| obs number | y | x~1~ | x~2~ | x~3~ | ... | x~r-1~ | x~r~ |
|------------|--------|---------|---------|---------|-----|-----------|---------|
| 1 | y~1~ | x~11~ | x~12~ | x~13~ | ... | x~1r-1~ | x~1r~ |
| 2 | y~2~ | x~21~ | x~22~ | x~23~ | ... | x~2r-1~ | x~2r~ |
| 3 | y~3~ | x~31~ | x~32~ | x~33~ | ... | x~3r-1~ | x~3r~ |
| ... | ... | ... | ... | ... | ... | ... | ... |
| n-1 | y~n-1~ | x~n-11~ | x~n-12~ | x~n-12~ | ... | x~n-1r-1~ | x~n-1r~ |
| n | y~n~ | x~n1~ | x~n2~ | x~n3~ | ... | x~nr-1~ | x~nr~ |
: The Table of Data {#tbl-TheTableofData .striped .hover}
## The rent 1999 Munich data {.smaller}
```{r}
#| label: tbl-stats
#| tbl-cap: "Variables in Munich rent data"
#| warning: false
library(gamlss.ggplots)
library(gamlss2)
library(broom)
library(knitr)
library(gamlss.ggplots)
# remove two variables
da <- rent[, -c(4,5)]
da |> head() |> kable(digits = c(2, 0, 0, 0, 0,0,0), format="pipe")
```
## Fitting
```{r}
#| echo: true
r1 <- gamlss2(R~pb(Fl)+pb(A)+H+loc|pb(Fl)+pb(A)+H+loc,
family=GA, data=rent)
```
## residual plots agaist index
```{r}
#| echo: true
library(gamlss.ggplots)
resid_index(r1)
```
## against continuous x-variables
```{r}
#| echo: true
resid_xvar(r1, A)
```
## against factor x-variables
```{r}
#| echo: true
resid_xvar(r1,loc)
```
## QQ-plots
```{r}
#| echo: true
resid_qqplot(r1)
```
## worm plots
```{r}
#| echo: true
resid_wp(r1)
```
## density plots
```{r}
#| echo: true
resid_density(r1)
```
## bucket plots
```{r}
#| echo: true
moment_bucket(r1)
```
## symmetry plots
```{r}
#| echo: true
resid_symmetry(r1)
```
## ecdf plot
```{r}
#| echo: true
resid_ecdf(r1)
```
## detrended ecdf plot
```{r}
#| echo: true
resid_dtop(r1)
```
## all in one plots
```{r}
#| echo: true
resid_plots(r1)
```
## all in one plots (standard)
```{r}
#| echo: true
plot(r1,which="resid")
```
# R-packages
## Older Packages {.smaller}
- `gamlss`: the original (needs `dist` and `data`)
- `gamlss.dist`: defining the `gamlss.family` distributions
- `gamlss.data`: for extra data sets
- `gamlss.add`: connect with `mgcv`, `nnet` and `trees`
- `gamlss.tr`: for truncating `gamlss.family` distributions
- `gamlss.cens`: for censored response variables
- `gamlss.demo`: for demonstrating GAMLSS concepts
- `gamlss.mx`: for fitting finite mixtures
## New Packages {.smaller}
- `gamboostLSS` for GAMLSS boosting
- `bamlss` the Bayesian GAMLSS
- `gamlss2`$^*$: the new version of GAMLSS
- `gamlss.ggplots`: using `ggplot2` within GAMLSS
- `gamlss.foreach`: for parallel computing
- `gamlss.prepdata`: preparation of data before fitting
- `gamlss.lasso`: for LASSO. Ridge and elastic Net regression
- `gamlss.shiny`$^*$: similar to `gamlss.demo`
- `topmodels` distributional regression help (not necessary gamlss)
::: aside
$^*$ in GitHub needed testing
:::
## why gamlss2 {.smaller}
- `gamlss()` for very large data is slow
- `predict` in `gamlss` is not easy to use
- current implementation can cope with only 4 parameters $\mu$, $\sigma$, $\nu$ and $\tau$
- to connect different estimation statistical approaches
- `penalised likelihood`
- `Bayesian`
- `boosting`
- to implement extra algorithms i.e. `stepwise`, `robust`
- to implement `machine learning` methodology
## getting the libraries
- For `CRAN` use
- `install.packages(gamlss)`
- for `GitHub` use
- `devtools::install_github("gamlss-dev/gamlss2")`
- <https://gamlss-dev.r-universe.dev/builds>
- and then use
- `library(gamlss2)`
# Practical 1
## end
[back to the index](https://mstasinopoulos.github.io/Porto_short_course/)
::: {layout-ncol="3," layout-nrow="1"}
![](book-1.png){width="300"} ![](BOOK-2.png){width="323"} ![](book3.png){width="333"} The Books
:::