```{r echo = FALSE, warning = FALSE, message=FALSE}
source("init.R")
```
# Overview {#sec-temp}
*Last modified on `r format(fs::file_info("chapter-51-statistical-modeling.qmd")$modification_time, '%d. %B %Y at %H:%M:%S')`*
> *"A quote." --- Dan Meyer*
## General background
```{r}
#| message: false
#| echo: false
#| warning: false
#| fig-align: center
#| fig-height: 14
#| fig-width: 11
#| fig-cap: "foo"
#| label: fig-reg-cross
geom_coord_cross <- function(x, y, yticks = "numeric", xticks = "numeric", zero = FALSE) {
list(
annotate("segment", x = x-0.2, xend = x+4, y = y, yend = y,
arrow = arrow(length = unit(0.0075, "npc"), type = "closed")),
annotate("segment", x = x, xend = x, y = y-0.2, yend = y+4,
arrow = arrow(length = unit(0.0075, "npc"), type = "closed")),
if(yticks == "numeric") {
if(zero){
list(
annotate("segment", x = x-0.1, xend = x, y = y + seq(0, 3.5, 0.5), yend = y+ seq(0, 3.5, 0.5)),
annotate("text", x = x -0.25, y = y + seq(0, 3.5, 0.5), label = seq(0, 70, 10),
hjust = "right"),
annotate("text", x = x - 1, y = y +2, label = c("Count of hairleg"), fontface = 2, size = 5,
angle = 90)
)
} else {
list(
annotate("segment", x = x-0.1, xend = x, y = y + seq(0, 3.5, 0.5), yend = y+ seq(0, 3.5, 0.5)),
annotate("text", x = x -0.25, y = y + seq(0.5, 3.5, 0.5),
label = round(seq(15, 40, length.out = 7)), hjust = "right") ,
annotate("text", x = x - 1, y = y +2, label = c("Jump length"), fontface = 2, size = 5,
angle = 90)
)
}
},
if(xticks == "numeric") {
list(
annotate("segment", x = x+ seq(0, 3.5, 0.5), xend = x+ seq(0, 3.5, 0.5), y = y -0.1 , yend = y),
annotate("text", x = x +seq(0.5, 3.5, 0.5), y = y -0.35, label = seq(0.5, 3.5, 0.5)),
annotate("text", x = x + 2, y = y -1, label = c("Weight"), fontface = 2, size = 5)
)
},
if(yticks == "3lvl") {
list(
annotate("segment", x = x-0.1, xend = x, y = y + c(1,2,3)-0.25, yend = y+ c(1,2,3)-0.25),
annotate("text", x = x -0.25, y = y + c(1,2,3)-0.25, label = c("1", "2", "3"), hjust = "right") ,
annotate("text", x = x - 1, y = y +2, label = c("Grade"), fontface = 2, size = 5, angle = 90)
)
},
if(xticks == "3lvl") {
list(
annotate("segment", x = x + c(1,2,3), xend = x+ c(1,2,3), y = y -0.1 , yend = y),
annotate("text", x = x + c(1,2,3), y = y -0.35, label = c("dog", "cat", "fox")),
annotate("text", x = x + 2, y = y -1, label = c("Host"), fontface = 2, size = 5)
)
},
if(yticks == "2lvl") {
list(
annotate("segment", x = x-0.1, xend = x, y = y + c(0.5, 3), yend = y+ c(0.5, 3)),
annotate("text", x = x -0.25, y = y + c(0.5, 3), label = c("0", "1"), hjust = "right") ,
annotate("text", x = x - 1, y = y +2, label = c("Infected"), fontface = 2, size = 5, angle = 90)
)
},
if(xticks == "2lvl") {
list(
annotate("segment", x = x+ c(1.25, 2.75), xend = x+ c(1.25, 2.75), y = y -0.1 , yend = y),
annotate("text", x = x + c(1.25, 2.75), y = y -0.35, label = c("dog", "cat")),
annotate("text", x = x + 2, y = y -1, label = c("Host"), fontface = 2, size = 5)
)
}
)
}
ggplot() +
##theme_void() +
theme_minimal() +
coord_cartesian(xlim = c(-1, 20), ylim = c(0, 27)) +
annotate("segment", x = c(2, 8, 14, 20), xend = c(2, 8, 14, 20),
y = c(0, 0, 0, 0), yend = c(25.75, 25.75, 24.75, 25.75)) +
annotate("segment", x = c(0.25, 0.25, 0.25, 0.25, 0.25), xend = c(20, 20, 20, 20, 20),
y = c(0, 6, 12, 18, 24), yend = c(0, 6, 12, 18, 24)) +
scale_x_continuous(breaks = 0:22, expand = expansion(mult = 0)) +
scale_y_continuous(breaks = 0:28, expand = expansion(mult = 0)) +
## outer text
annotate("text", x = 11, y = 26.5, label = "Characteristic of the influencer (X)",
size = 8, fontface = 2) +
annotate("text", x = c(5, 14), y = 25.25,
label = c("Covariate", "Factor"), size = 7, fontface = 2) +
annotate("text", x = c(5, 11, 17), y = 24.5,
label = c("X is numeric", "X has 3 or more levels", "X has 2 levels"),
size = 6, fontface = 3) +
annotate("text", x = -0.5, y = 12, label = "Distribution of the outcome (Y)", size = 8,
fontface = 2, angle = 90) +
annotate("text", x = 0.75, y = c(3, 9, 15, 21),
label = c("Binomial", "Ordinal", "Poisson", "Normal"), size = 7, fontface = 2, angle = 90) +
annotate("text", x = 1.5, y = c(3, 9, 15, 21),
label = c("Y is yes or no", "Y has discrete grades", "Y has counts", "Y is continuous"),
size = 6, fontface = 3, angle = 90) +
## coords
geom_coord_cross(x = 3.5, y = 19.5, yticks = "numeric", xticks = "numeric") +
geom_coord_cross(x = 9.5, y = 19.5, yticks = "numeric", xticks = "3lvl") +
geom_coord_cross(x = 15.5, y = 19.5, yticks = "numeric", xticks = "2lvl") +
geom_coord_cross(x = 3.5, y = 13.5, yticks = "numeric", xticks = "numeric", zero = TRUE) +
geom_coord_cross(x = 9.5, y = 13.5, yticks = "numeric", xticks = "3lvl", zero = TRUE) +
geom_coord_cross(x = 15.5, y = 13.5, yticks = "numeric", xticks = "2lvl", zero = TRUE) +
geom_coord_cross(x = 3.5, y = 7.5, yticks = "3lvl", xticks = "numeric") +
geom_coord_cross(x = 9.5, y = 7.5, yticks = "3lvl", xticks = "3lvl") +
geom_coord_cross(x = 15.5, y = 7.5, yticks = "3lvl", xticks = "2lvl") +
geom_coord_cross(x = 3.5, y = 1.5, yticks = "2lvl", xticks = "numeric") +
geom_coord_cross(x = 9.5, y = 1.5, yticks = "2lvl", xticks = "3lvl") +
geom_coord_cross(x = 15.5, y = 1.5, yticks = "2lvl", xticks = "2lvl") +
## normal
### covariate
annotate("point", x = c(0.5, 1, 1.25, 1.7, 2.5, 3.2) + 3.5,
y = c(20.5, 21, 21.5, 22, 22.5, 22), size = 1.5) +
annotate("segment", x = 3.75, xend = 7.25, y = 20.5, yend = 23, linewidth = 0.65,
color = "#B12A90FF") +
geom_curve(aes(x = 4, y = 20, xend = 7.25, yend = 22.5),
curvature = -0.4, color = "#FCA636FF", linewidth = 0.65) +
annotate("label", hjust = "right", x = 5.25, y = 22.5, color = "#FCA636FF",
label = "non-linear", fill = "gray95") +
annotate("label", hjust = "left", x = 5.75, y = 21.25, color = "#B12A90FF",
label = "linear", fill = "gray95") +
### 3lvl
geom_rect(aes(xmin = 10.1, xmax = 10.35, ymin = 19.5, ymax = 22.25), fill = "#0D088780",
color = "black") +
geom_segment(aes(x = 10.225, xend = 10.225, y = 22.25, yend = 22.75)) +
geom_segment(aes(x = 10.15, xend = 10.3, y = 22.75, yend = 22.75)) +
annotate("point", x = c(10.5),
y = c(21.5, 22, 22.5, 22.75, 23), size = 1.5) +
geom_rect(aes(xmin = 10.65, xmax = 10.9, ymin = 21.5, ymax = 23), fill = "#0D088780",
color = "black") +
geom_segment(aes(x = 10.65, xend = 10.9, y = 22.5, yend = 22.5)) +
geom_segment(aes(x = 10.775, xend = 10.775, y = c(21.1, 23), yend = c(21.5, 23.25))) +
geom_rect(aes(xmin = 11.1, xmax = 11.35, ymin = 19.5, ymax = 20.5), fill = "#B12A9080",
color = "black") +
geom_segment(aes(x = 11.225, xend = 11.225, y = 20.5, yend = 21)) +
geom_segment(aes(x = 11.15, xend = 11.3, y = 21, yend = 21)) +
annotate("point", x = c(11.5),
y = c(19.75, 20, 20.5, 21, 21.25), size = 1.25) +
geom_rect(aes(xmin = 11.65, xmax = 11.9, ymin = 20, ymax = 21), fill = "#B12A9080",
color = "black") +
geom_segment(aes(x = 11.65, xend = 11.9, y = 20.5, yend = 20.5)) +
geom_segment(aes(x = 11.775, xend = 11.775, y = c(19.7, 21), yend = c(20, 21.3))) +
geom_rect(aes(xmin = 12.1, xmax = 12.35, ymin = 19.5, ymax = 21.75), fill = "#FCA63680",
color = "black") +
geom_segment(aes(x = 12.225, xend = 12.225, y = 21.75, yend = 22.25)) +
geom_segment(aes(x = 12.15, xend = 12.3, y = 22.25, yend = 22.25)) +
annotate("point", x = c(12.5),
y = c(20.75, 21.25, 21.5, 22.2, 22.5), size = 1.25) +
geom_rect(aes(xmin = 12.65, xmax = 12.9, ymin = 21, ymax = 22.25), fill = "#FCA63680",
color = "black") +
geom_segment(aes(x = 12.65, xend = 12.9, y = 21.5, yend = 21.5)) +
geom_segment(aes(x = 12.775, xend = 12.775, y = c(20.5, 22.25), yend = c(21, 22.75))) +
### 2lvl
geom_rect(aes(xmin = 10.1+6.25, xmax = 10.35+6.25, ymin = 19.5, ymax = 22.25), fill = "#0D088780",
color = "black") +
geom_segment(aes(x = 10.225+6.25, xend = 10.225+6.25, y = 22.25, yend = 22.75)) +
geom_segment(aes(x = 10.15+6.25, xend = 10.3+6.25, y = 22.75, yend = 22.75)) +
annotate("point", x = c(10.5)+6.25,
y = c(21.5, 22, 22.5, 22.75, 23), size = 1.25) +
geom_rect(aes(xmin = 10.65+6.25, xmax = 10.9+6.25, ymin = 21.5, ymax = 23), fill = "#0D088780",
color = "black") +
geom_segment(aes(x = 10.65+6.25, xend = 10.9+6.25, y = 22.5, yend = 22.5)) +
geom_segment(aes(x = 10.775+6.25, xend = 10.775+6.25, y = c(21.1, 23), yend = c(21.5, 23.25))) +
geom_rect(aes(xmin = 11.1+6.75, xmax = 11.35+6.75, ymin = 19.5, ymax = 20.5), fill = "#B12A9080",
color = "black") +
geom_segment(aes(x = 11.225+6.75, xend = 11.225+6.75, y = 20.5, yend = 21)) +
geom_segment(aes(x = 11.15+6.75, xend = 11.3+6.75, y = 21, yend = 21)) +
annotate("point", x = c(11.5+6.75),
y = c(19.75, 20, 20.5, 21, 21.25), size = 1.25) +
geom_rect(aes(xmin = 11.65+6.75, xmax = 11.9+6.75, ymin = 20, ymax = 21), fill = "#B12A9080",
color = "black") +
geom_segment(aes(x = 11.65+6.75, xend = 11.9+6.75, y = 20.5, yend = 20.5)) +
geom_segment(aes(x = 11.775+6.75, xend = 11.775+6.75, y = c(19.7, 21), yend = c(20, 21.3))) +
## pois
### covariate
annotate("label", x = 6.8, y = 16, label = " ", fill = "gray95") +
annotate("label", x = 3.8, y = 13.5, label = " ", fill = "gray95") +
annotate("point", x = c(0.1, 0.25, 1, 1.3, 1.7, 0.5, 1.75, 2, 2.4,
2.2, 2.3, 2.6, 2.9, 3.1, 3.5, 3.7, 3.5, 3.9, 3.3) + 3.5,
y = c(13.5, 13.5, 14, 14, 14, 13.5, 14.5, 14.5, 14.5,
15, 15, 15, 16, 16, 16, 16, 17, 17, 17), size = 1.5) +
geom_curve(aes(x = 3.5, y = 13.5, xend = 7.25, yend = 17),
curvature = 0.3, color = "#FCA636FF", linewidth = 0.65) +
annotate("text", hjust = "right", x = 5.75, y = 16.4, label = "Discrete counts") +
geom_curve(aes(x = 5.55, y = 16.25, xend = 6.2, yend = 16),
arrow = arrow(length = unit(0.005, "npc"), type = "closed"),
curvature = 0.2, color = "black", linewidth = 0.5) +
annotate("text", hjust = "left", x = 4.25, y = 15.5, label = "Zero counts") +
geom_curve(aes(x = 4.2, y = 15.25, xend = 3.75, yend = 13.8),
arrow = arrow(length = unit(0.005, "npc"), type = "closed"),
curvature = 0.2, color = "black", linewidth = 0.5) +
### 3lvl
geom_rect(aes(xmin = 10.1, xmax = 10.35, ymin = 13.5, ymax = 15.25), fill = "#0D088780",
color = "black") +
geom_segment(aes(x = 10.225, xend = 10.225, y = 15.25, yend = 16.75)) +
geom_segment(aes(x = 10.15, xend = 10.3, y = 16.75, yend = 16.75)) +
annotate("point", x = c(10.5),
y = c(14.75, 15, 14.4, 16, 16.75), size = 1.5) +
geom_rect(aes(xmin = 10.65, xmax = 10.9, ymin = 14.25, ymax = 15.25), fill = "#0D088780",
color = "black") +
geom_segment(aes(x = 10.65, xend = 10.9, y = 14.75, yend = 14.75)) +
geom_segment(aes(x = 10.775, xend = 10.775, y = c(14, 15.25), yend = c(14.25, 16.5))) +
geom_rect(aes(xmin = 11.1, xmax = 11.35, ymin = 13.5, ymax = 14), fill = "#B12A9080",
color = "black") +
geom_segment(aes(x = 11.225, xend = 11.225, y = 14, yend = 14.75)) +
geom_segment(aes(x = 11.15, xend = 11.3, y = 14.75, yend = 14.75)) +
annotate("point", x = c(11.5),
y = c(13.6, 13.8, 14.0, 14.4, 15), size = 1.25) +
geom_rect(aes(xmin = 11.65, xmax = 11.9, ymin = 13.6, ymax = 14.25), fill = "#B12A9080",
color = "black") +
geom_segment(aes(x = 11.65, xend = 11.9, y = 13.8, yend = 13.8)) +
geom_segment(aes(x = 11.775, xend = 11.775, y = c(13.5, 14.25), yend = c(13.6, 15))) +
geom_rect(aes(xmin = 12.1, xmax = 12.35, ymin = 13.5, ymax = 15), fill = "#FCA63680",
color = "black") +
geom_segment(aes(x = 12.225, xend = 12.225, y = 15, yend = 17)) +
geom_segment(aes(x = 12.15, xend = 12.3, y = 17, yend = 17)) +
annotate("point", x = c(12.5),
y = c(14, 14.5, 14.8, 15.2, 17), size = 1.25) +
geom_rect(aes(xmin = 12.65, xmax = 12.9, ymin = 14.25, ymax = 15.75), fill = "#FCA63680",
color = "black") +
geom_segment(aes(x = 12.65, xend = 12.9, y = 14.5, yend = 14.5)) +
geom_segment(aes(x = 12.775, xend = 12.775, y = c(14, 15.75), yend = c(14.25, 16.8))) +
geom_shape(data = tibble(x = c(13.25, 13.1, 13.4), y = c(14.5, 16.5, 16.5)),
aes(x, y), fill = "gray75", color = "black") +
annotate("text", x = 13.5, y = 15.5, label = "variance", angle = 90, fontface = 2) +
### 2lvl
geom_rect(aes(xmin = 10.1+6.25, xmax = 10.35+6.25, ymin = 13.5, ymax = 15.25), fill = "#0D088780",
color = "black") +
geom_segment(aes(x = 10.225+6.25, xend = 10.225+6.25, y = 15.25, yend = 16.75)) +
geom_segment(aes(x = 10.15+6.25, xend = 10.3+6.25, y = 16.75, yend = 16.75)) +
annotate("point", x = c(10.5)+6.25,
y = c(14.75, 15, 14.4, 16, 16.75), size = 1.5) +
geom_rect(aes(xmin = 10.65+6.25, xmax = 10.9+6.25, ymin = 14.25, ymax = 15.25), fill = "#0D088780",
color = "black") +
geom_segment(aes(x = 10.65+6.25, xend = 10.9+6.25, y = 14.75, yend = 14.75)) +
geom_segment(aes(x = 10.775+6.25, xend = 10.775+6.25, y = c(14, 15.25), yend = c(14.25, 16.5))) +
geom_rect(aes(xmin = 11.1+6.75, xmax = 11.35+6.75, ymin = 13.5, ymax = 14), fill = "#B12A9080",
color = "black") +
geom_segment(aes(x = 11.225+6.75, xend = 11.225+6.75, y = 14, yend = 14.75)) +
geom_segment(aes(x = 11.15+6.75, xend = 11.3+6.75, y = 14.75, yend = 14.75)) +
annotate("point", x = c(11.5)+6.75,
y = c(13.6, 13.8, 14.0, 14.4, 15), size = 1.25) +
geom_rect(aes(xmin = 11.65+6.75, xmax = 11.9+6.75, ymin = 13.6, ymax = 14.25), fill = "#B12A9080",
color = "black") +
geom_segment(aes(x = 11.65+6.75, xend = 11.9+6.75, y = 13.8, yend = 13.8)) +
geom_segment(aes(x = 11.775+6.75, xend = 11.775+6.75, y = c(13.5, 14.25), yend = c(13.6, 15))) +
geom_shape(data = tibble(x = c(19.25, 19.1, 19.4), y = c(14.5, 16.5, 16.5)),
aes(x, y), fill = "gray75", color = "black") +
annotate("text", x = 19.5, y = 15.5, label = "variance", angle = 90, fontface = 2) +
## ordinal
annotate("point", x = c(0.45, 0.7, 1.1, 1.25, 1.65, 1.87, 2.15, 2.5, 2.7, 3.2) + 3.5,
y = c(8.5, 8.5, 8.5, 9.5, 9.5, 9.5, 9.5, 10.5, 10.5, 10.5)-0.25, size = 1.5) +
### 3lvl
annotate("rect", xmin = 10, xmax = 11, ymin = 7.5, ymax = 8.5, fill = "#0D088780", color = "black") +
annotate("rect", xmin = 10, xmax = 11, ymin = 8.5, ymax = 9.5, fill = "#2F059680", color = "black") +
annotate("rect", xmin = 10, xmax = 11, ymin = 9.5, ymax = 11, fill = "#4903A080", color = "black") +
annotate("rect", xmin = 11, xmax = 12, ymin = 7.5, ymax = 9.15, fill = "#A21D9A80", color = "black") +
annotate("rect", xmin = 11, xmax = 12, ymin = 9.15, ymax = 9.75, fill = "#B42E8D80", color = "black") +
annotate("rect", xmin = 11, xmax = 12, ymin = 9.75, ymax = 11, fill = "#C43E7F80", color = "black") +
annotate("rect", xmin = 12, xmax = 13, ymin = 7.5, ymax = 8.15, fill = "#F3854B80", color = "black") +
annotate("rect", xmin = 12, xmax = 13, ymin = 8.15, ymax = 9.5, fill = "#F99A3E80", color = "black") +
annotate("rect", xmin = 12, xmax = 13, ymin = 9.5, ymax = 11, fill = "#FDAF3180", color = "black") +
### 2lvl
annotate("rect", xmin = 16, xmax = 17.5, ymin = 7.5, ymax = 8.5, fill = "#0D088780", color = "black") +
annotate("rect", xmin = 16, xmax = 17.5, ymin = 8.5, ymax = 9.5, fill = "#2F059680", color = "black") +
annotate("rect", xmin = 16, xmax = 17.5, ymin = 9.5, ymax = 11, fill = "#4903A080", color = "black") +
annotate("rect", xmin = 17.5, xmax = 19, ymin = 7.5, ymax = 9.15, fill = "#A21D9A80",
color = "black") +
annotate("rect", xmin = 17.5, xmax = 19, ymin = 9.15, ymax = 9.75, fill = "#B42E8D80",
color = "black") +
annotate("rect", xmin = 17.5, xmax = 19, ymin = 9.75, ymax = 11, fill = "#C43E7F80", color = "black") +
## binomial
annotate("point", x = c(0.45, 0.7, 1.1, 1.25, 1.65, 1.87, 2.15, 2.5, 2.7, 3.2) + 3.5,
y = c(2, 2, 2, 2, 2, 4.5, 4.5, 4.5, 4.5, 4.5), size = 1.5) +
geom_curve(aes(x = 4, y = 2, xend = 5.5, yend = 3.25),
curvature = 0.4, color = "#FCA636FF", linewidth = 0.65) +
geom_curve(aes(x = 5.5, y = 3.25, xend = 7, yend = 4.5),
curvature = -0.4, color = "#FCA636FF", linewidth = 0.65) +
annotate("label", hjust = "right", x = 5.25, y = 3.75, color = "#FCA636FF",
label = "logit", fill = "gray95") +
annotate("segment", x = 4.25, xend = 6.75, y = 1.75, yend = 4.75, linewidth = 0.65,
color = "#B12A90FF") +
annotate("label", hjust = "left", x = 6, y = 3.25, color = "#B12A90FF",
label = "probit", fill = "gray95") +
annotate("rect", xmin = 10, xmax = 11, ymin = 1.5, ymax = 3.5, fill = "#0D088780", color = "black") +
annotate("rect", xmin = 10, xmax = 11, ymin = 3.5, ymax = 5, fill = "#3E049C80", color = "black") +
annotate("rect", xmin = 11, xmax = 12, ymin = 1.5, ymax = 3, fill = "#A6209880", color = "black") +
annotate("rect", xmin = 11, xmax = 12, ymin = 3, ymax = 5, fill = "#C03A8380", color = "black") +
annotate("rect", xmin = 12, xmax = 13, ymin = 1.5, ymax = 2, fill = "#F58C4680", color = "black") +
annotate("rect", xmin = 12, xmax = 13, ymin = 2, ymax = 5, fill = "#FDAD3280", color = "black") +
## 2lvl
annotate("rect", xmin = 16, xmax = 17.5, ymin = 1.5, ymax = 3.5, fill = "#0D088780", color = "black") +
annotate("rect", xmin = 16, xmax = 17.5, ymin = 3.5, ymax = 5, fill = "#3E049C80", color = "black") +
annotate("rect", xmin = 17.5, xmax = 19, ymin = 1.5, ymax = 3, fill = "#A6209880", color = "black") +
annotate("rect", xmin = 17.5, xmax = 19, ymin = 3, ymax = 5, fill = "#C03A8380", color = "black") +
## (A) to (L)
annotate("text", label = str_c("(", (LETTERS[1:12]), ")"), fontface = 2,
x = rep(c(2.5, 8.5, 14.5), times = 4),
y = rep(c(23.6, 17.6, 11.6, 5.6), each = 3), size = 6)
```
## Theoretical background
## R packages used
## Data
## Alternatives
Further tutorials and R packages on XXX
## Glossary
term
: what does it mean.
## The meaning of "Models of Reality" in this chapter.
- itemize with max. 5-6 words
## Summary
## References {.unnumbered}