---
title: modelldef
date: '2026-07-14'
categories:
- paper
- todo
---
```{r libs, include = FALSE}
library(tidyverse)
library(exams)
```
```{r global-knitr-options, include=FALSE}
knitr::opts_chunk$set(fig.pos = 'H',
fig.asp = 0.618,
fig.width = 4,
fig.cap = "",
fig.path = "",
echo = FALSE,
message = FALSE,
fig.show = "hold")
options(mc.cores = parallel::detectCores())
```
Question
========
Betrachten Sie den folgenden R-Befehl,
mit dem ein lineares Modell mit Hilfe der Bayes-Statistik berechnet wurde:
```{r echo = TRUE, eval = FALSE}
m1 <- stan_glm(y ~ x1, data = d)
```
Welches der folgenden *Modelldefinitionen* passt am besten zum obigen R-Befehl (zur Berechnung des Modells `m1`)?
Hinweise:
- Der Prädiktor bzw. die Prädiktoren sind z-standardisiert.
- Die AV ist z-standardisiert.
- Gehen Sie davon aus, dass Daten und notwendige R-Pakete und sonstige technischen Voraussetzungen erfüllt sind.
- $\mathcal{N}()$ bzw. $N()$ meint die Normalverteilung und $\mathcal{E}()$ bzw. $E()$ die Exponentialverteilung.
```{r}
def_correct <- c(
"y_i ", " \\sim \\mathcal{N}(\\mu_i, \\sigma)",
"\\mu_i ", " = \\beta_0 + \\beta_1 x_i",
"\\beta_0, \\beta_1 ", " \\sim \\mathcal{N}(0, 2.5)",
"\\sigma ", " \\sim \\mathcal{E}(1)"
)
```
```{r}
print_model_def <- function(mod_def) {
# mod_def is expected to be a 8 element string where to adjacent elements are LHS and RHS of one equation
eq1 <- paste0(mod_def[1], mod_def[2])
eq2 <- paste0(mod_def[3], mod_def[4])
eq3 <- paste0(mod_def[5], mod_def[6])
eq4 <- paste0(mod_def[7], mod_def[8])
output_string1 <-
glue::glue("${eq1}$ ")
output_string2 <-
glue::glue("${eq2}$ ")
output_string3 <-
glue::glue("${eq3}$ ")
output_string4 <-
glue::glue("${eq4}$ ")
cat(output_string1)
cat(" \n")
cat(output_string2)
cat(" \n")
cat(output_string3)
cat(" \n")
cat(output_string4)
cat(" \n")
}
```
```{r}
permutate_lhs <- function(mod_def) {
lhs_vec <- c(mod_def[1], mod_def[3], mod_def[5], mod_def[7])
lhs_incorrect<- lhs_vec
while(str_c(lhs_incorrect, collapse = "\n") == str_c(lhs_vec, collapse = "\n")) {
lhs_incorrect <- sample(lhs_vec)
}
mod_def_output <- c(lhs_incorrect[1], mod_def[2],
lhs_incorrect[2], mod_def[4],
lhs_incorrect[3], mod_def[6],
lhs_incorrect[4], mod_def[8])
return(mod_def_output)
}
mod_def2 <- permutate_lhs(def_correct)
```
```{r}
make_sign_neg <- function(mod_def) {
out <- str_replace_all(mod_def, Hmisc::escapeRegex("\\mathcal{N}(0, 2.5)"), Hmisc::escapeRegex("\\mathcal{N}(0, -2.5)"))
out2 <- str_replace_all(out, Hmisc::escapeRegex("\\mathcal{E}(1)"), Hmisc::escapeRegex("\\mathcal{1}(-1)"))
return(out2)
}
mod_def3 <- make_sign_neg(def_correct)
```
```{r}
drop_suffix <- function(mod_def){
out <- str_remove_all(mod_def, "_i")
return(out)
}
mod_def4 <- drop_suffix(def_correct)
```
```{r}
replace_tilde <- function(mod_def) {
out <- str_replace_all(mod_def, Hmisc::escapeRegex("\\sim"), "=")
}
mod_def5 <- replace_tilde(def_correct)
```
```{r}
sol_df <-
tibble(
id = 1:5,
correct_answer = c(T, F, F, F, F),
mod_def = list(def_correct, mod_def2, mod_def3, mod_def4, mod_def5),
explanations = c(
"korrekt",
"Die linken Seiten der Gleichungen passen nicht zu den rechten Seiten",
"Streuungen dürfen kein negatives Vorzeichen haben",
"Die Indizes i fehlen",
"Die Tilde-Zeichen sind fälschlich durch Gleichheitszeichen ersetzt"
)
) %>%
slice_sample(n = nrow(.)) %>%
mutate(id2 = LETTERS[1:5])
```
*Modelldefinition A:*
```{r results = 'asis'}
print_model_def(sol_df$mod_def[[1]])
```
*Modelldefinition B:*
```{r results = 'asis'}
print_model_def(sol_df$mod_def[[2]])
```
*Modelldefinition C:*
```{r results = 'asis'}
print_model_def(sol_df$mod_def[[3]])
```
*Modelldefinition D:*
```{r results = 'asis'}
print_model_def(sol_df$mod_def[[4]])
```
*Modelldefinition E:*
```{r results = 'asis'}
print_model_def(sol_df$mod_def[[5]])
```
```{r questionlist, echo = FALSE, results = "asis"}
answerlist(sol_df$id2, markup = "markdown")
```
Solution
========
---
Aufgaben-ID: Modelldef
---
Aufgabenname: `modelldef`
```{r sollist, echo = FALSE, results = "asis"}
answerlist(ifelse(sol_df$correct_answer, "Richtig", "Falsch"), sol_df$explanations, markup = "markdown")
```
```{r}
sol <- exams::mchoice2string(sol_df$correct_answer, single = TRUE)
```
Meta-information
================
exname: modelldef
extype: schoice
exsolution: `r sol`
exshuffle: false
expoints: 1