forked from luebby/Einstiegsfolien
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWMQD_10_nm.qmd
326 lines (224 loc) · 9.17 KB
/
WMQD_10_nm.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
---
title: "Wissenschaftliche Methodik -- Quantitative Datenanalyse"
subtitle: "Zehnter Termin"
institute: "FOM Dortmund"
author: "Norman Markgraf"
lang: de-DE
format:
revealjs:
#html-math-method: katex
#html-math-method: mathjax
main-font: Arial
hight: 1920
width: 1080
margin: 0
history: true
center: false
theme:
- colors.scss
- fontsstyles.scss
- fom.scss
- wmqd.scss
footer: "WMQD | Norman Markgraf"
chalkboard: true
title-slide-attributes:
data-background-image: fom-background.png
data-background-size: contain
---
```{r child="prelude_WMQD.Rmd"}
#| output: false
#| include: false
```
## Zur Erinnerung
🤷 Stellen Sie Fragen.
💪 <https://tweedback.de/zrzq>
::: center
{{< qrcode https://tweedback.de/zrzq tweedback >}}
:::
Hinweise zu den **Prüfungsleistungen** finden Sie im OC!
Das [Skript von Prof. Matthias Gehrke aus dem OC](https://campus.bildungscentrum.de/nfcampus/plpd/content/9168039) strukturiert Ihre Vor- und Nacharbeit.
------------------------------------------------------------------------
## Wie ist Ihre Stimmung heute?
::: center
```{r Skala_kuenstliche_Intelligenz_img, fig.align="center", out.width="40%", fig.cap="Quelle: [@mhaseneyer](https://twitter.com/mhaseneyer/)"}
knitr::include_graphics(here::here("img", "Skala_kuenstliche_Intelligenz.png"))
```
:::
------------------------------------------------------------------------
## Hausarbeit
::: {.center}
<iframe src="https://giphy.com/embed/13sozYO4hmSMUw" width="360" height="360" frameBorder="0" class="giphy-embed" allowFullScreen></iframe>
Anmeldung nicht vergessen!
:::
::: {.footnote}
[via GIPHY](https://giphy.com/gifs/please-hurry-13sozYO4hmSMUw)
:::
------------------------------------------------------------------------
## Tipps für den Vorlesungserfolg
- Kommen Sie zur Vorlesung!
- Vermeiden Sie Ablenkung.
- Arbeiten Sie die Vorlesung von Anfang an **vor** und nach. Nutzen Sie dafür die Studienbriefe.
- Stellen Sie Fragen.
- Unterstützen Sie sich gegenseitig.
------------------------------------------------------------------------
## UN Ziel 10: Reduced inequalities
::: center
<iframe src="https://upgrader.gapminder.org/q/71/embed?hasBorder=true&hasOpenSansFont=false" title="81% of people get this question wrong" width="100%" height="500" style="border:none;"></iframe>
:::
------------------------------------------------------------------------
## Heutiges Thema 🏫
- Fallstudie Vorhersagemodellierung
::: {.center}
<iframe src="https://giphy.com/embed/PMVL0gikEmYBygyzL0" width="480" height="270" frameBorder="0" class="giphy-embed" allowFullScreen></iframe>
:::
::: {.footnote}
<p><a href="https://giphy.com/gifs/mafs-mafsau-mafsaus-mafsaustralia-PMVL0gikEmYBygyzL0">via GIPHY</a></p>
:::
------------------------------------------------------------------------
## Modellierung: Beispiel `tips`
```{r, include=FALSE}
library(mosaic)
library(kableExtra)
data(tips, package = "reshape2")
tips <- as_tibble(tips)
set.seed(2009)
train <- tips %>%
sample_n(160) %>%
select(time, size, total_bill)
test <- tips %>%
sample_n(80) %>%
select(time, size, total_bill)
anwendung <- test %>%
select(-total_bill)
```
Modelliere die [abbhängige Variable]{.violet} `total_bill` ( $\color{violet} y$ ) auf Basis der [unabhängigen Variablen]{.olive} `time` ( $\color{olive}{x_1}$ ) und `size` ( $\color{olive}{x_2}$ ):
$${\color{violet}y}=f(\color{olive}{x_1,x_2})+\epsilon$$
------------------------------------------------------------------------
## Trainingsdaten
Der Trainingsdatensatz (`train`) enthält alle Variablen, d. h. $\color{olive}{x_1, x_2}, {\color{violet} y}$:
```{r echo=FALSE}
train %>%
head(4) %>%
kable() %>%
kable_styling("striped") %>%
add_header_above(c("Unabhängige Variablen" = 2, "Abhängige Variable" = 1)) %>%
column_spec(1:2, color = "white", background = "#808000") %>%
column_spec(3, color = "white", background = "#DA70D6")
```
------------------------------------------------------------------------
## Modellierung
Schätze $f(.\!)$ z. B. über lineare Regression auf den **Trainingsdaten**:
```{r echo=TRUE}
erg_lm <- lm(total_bill ~ time + size, data = train)
erg_lm
```
$$
\begin{align}
{\color{purple}\widehat{\text{total_bill}}_i}
= `r round(coef(erg_lm)[1],2)` &`r round(coef(erg_lm)[2],2)`
\cdot \begin{cases}
1 &: {\color{olive} \text{i ist Lunch}} \\
0 &: {\color{olive} \text{i ist nicht Lunch}} \\
\end{cases} \\
&+ `r round(coef(erg_lm)[3],2)` \cdot {\color{olive}{\text{size}_i}}
\end{align}
$$
------------------------------------------------------------------------
## Anwendungsdaten
Die **Anwendungsdaten** (`anwendung`) enthalten nur die unabhänigen Variablen $\color{olive}{x_1, x_2}$, nicht die abhängige Variable $\color{violet} y$:
```{r echo=FALSE}
anwendung %>%
head(4) %>%
kable() %>%
add_header_above(c("Unabhängige Variablen" = 2)) %>%
column_spec(1:2, color = "white", background = "#808000")
```
------------------------------------------------------------------------
## Vorhersage (I/III)
Zuvor gelerntes Modell (`erg_lm`) auf Basis der Trainingsdaten zur Prognose der [Zielvariable]{.violet} auf den Anwendungsdaten verwenden:
```{r echo=TRUE}
lm_predict <- predict(erg_lm, newdata = anwendung)
lm_predict
```
------------------------------------------------------------------------
## Vorhersage (II/III)
Für die Beobachtungen des Anwendungsdatensatzes gibt es jetzt [geschätze Werte]{.purple} für die Rechnungshöhe, $\color{purple}\widehat{\text{total_bill}}$.
Z. B. für $i=1$:
```{r echo=FALSE}
anwendung %>%
head(1) %>%
kable() %>%
add_header_above(c("Unabhängige Variablen" = 2)) %>%
column_spec(1:2, color = "white", background = "#808000")
```
------------------------------------------------------------------------
## Vorhersage (III/III)
$$
{\color{purple}\widehat{\text{total_bill}}_1}=`r round(coef(erg_lm)[1],2)` `r round(coef(erg_lm)[2],2)`\cdot{\color{olive}1} + `r round(coef(erg_lm)[3],2)` \cdot {\color{olive}6}={\color{purple}`r round(lm_predict[1],2)`}
$$
```{r echo=FALSE}
test %>%
mutate(hat_total_bill = round(lm_predict,2)) %>%
head(1) %>%
select(hat_total_bill) %>%
kable() %>%
add_header_above(c("Abhängige Variable" = 1)) %>%
column_spec(1, color = "white", background = "#7A378B")
```
------------------------------------------------------------------------
## Evaluierung (I/II)
Sind die [wahren]{.violet} Werte der Zielvariable bekannt, kann die [Vorhersage]{.purple} evaluiert werden:
```{r echo=FALSE}
test %>%
mutate(hat_total_bill = round(lm_predict,2)) %>%
head(4) %>%
kable() %>%
add_header_above(c("Unabhängige Variablen" = 2, "Abhängige Variable" = 2)) %>%
column_spec(1:2, color = "white", background = "#808000") %>%
column_spec(3, color = "white", background = "#DA70D6") %>%
column_spec(4, color = "white", background = "#7A378B")
```
------------------------------------------------------------------------
## Evaluierung (II/II)
Z. B.:
$$
MAE = \frac{1}{n_{test}} \cdot \sum_{i=1}^{n_{test}} |{\color{violet} y_i}-{\color{purple} \hat{y}_i}|
$$
------------------------------------------------------------------------
## Tipps (I/II)
- Je nachdem, welche Variablen zur Modellierung verwendet werden, ergeben sich i. d. R. verschiedene Prognosen: `lm(y ~ 1); lm(y ~ x1); lm(y ~ x1 + x2); lm (y ~ x1 * x2)`
- Werden im Trainingsdatensatz Ausreißer eliminiert ändert sich das geschätzte Modell und damit die Prognose.
- Werden Variablen transformiert (z. B. `mutate(x1l = log(x1))`) ändert sich das geschätzte Modell und damit die Prognose.
------------------------------------------------------------------------
## Tipps (II/II)
- Werden unterschiedliche Modellierungsmethoden (`lm(), rpart()`, ...) verwendet, ändert sich die Prognose.
- Vermeiden Sie Über-Anpassung!
::: center
**Beachten Sie die Hinweise zur Hausarbeit. Nutzen Sie die angegebene Literatur!**
:::
------------------------------------------------------------------------
## Transparenz
Wir sind es dem datenbasierten Entscheiden schuldig!
::: center
```{r rmarkdown_wizards_img, fig.align="center", out.width="450", fig.cap="Quelle: [@allisonhorst](https://github.com/allisonhorst/stats-illustrations)"}
knitr::include_graphics(here::here("img", "rmarkdown_wizards.png"))
```
:::
------------------------------------------------------------------------
## Overfitting
::: center
<iframe width="560" height="315" src="https://www.youtube.com/embed/pZTLFu79UbY" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
:::
::: {.footnote}
Quelle: [The Raf: Summary Song #9 - Overfitting (Stats Parody - Charlie Puth Attention)](https://youtu.be/pZTLFu79UbY)
:::
------------------------------------------------------------------------
## Was ist zu tun?
- Öffnen Sie Ihr RStudio-Projekt (Cloud, lokal)
- Vorlagedatei: `fallstudien\Template-Vorhersagemodellierung.Rmd`
- Anpassen:
- Zeile 3: Namen der Autor:innen
- Zeilen 32, 33: Namen der einzulesenden Datendatei
- Zeilen 44, 64: Namen der Variablen
- Zeile 80: Name
- `knit`