```{r data generation, echo = FALSE, results = "hide"}
library("exams")
library("exams.forge")
instruction <- NULL
n <- 6
x <- sample(1:10,6)
y <- x+sample(1:2,6,replace=TRUE)
x[6] <- x[6]+sample(5:10,1)
y[6] <- y[6]-min(y)+1
sumXi <- sum(x)
sumYi <- sum(y)
sumYi2 <- sum(y^2)
sumXiYi <- sum(x*y)
sumXi2 <- sum(x^2)
tab  <- cbind(x,y)
colnames(tab) <- c("$X$", "$Y$")
row.names(tab) <- 1:n
htab <- html_matrix_sk(tab, "$i$", fmt=rep("%i", ncol(tab)))
#part (b)
b_0 <- (sumYi*sumXi2 - sumXi*sumXiYi)/(n*sumXi2 - sumXi^2)
b_1 <- (n*sumXiYi - sumXi*sumYi)/(n*sumXi2 - sumXi^2)
b01 <- runif(1)<0.5
sc  <- num_result(if (b01) b_0 else b_1, 3)
```

Question
========

```{r, error=TRUE, child=instruction}
```

Untersuchen Sie den linearen Zusammenhang zwischen den Variablen $X$ und $Y$ anhand der folgenden Daten aus einer Umfrage:

```{r, echo=FALSE, results='asis'}
cat(toHTMLorLatex(htab))
```

Berechnen Sie den Koeffizienten `r if(b01) "$b_0$" else "$b_1$"` der linearen Regression.  `r round_de(sc$digits)`.

Solution
========

$$b_0 = \frac{ \sum\limits_{i=1}^{ `r n`} y_i \cdot \sum\limits_{i=1}^{ `r n`} x_i^2 - \sum\limits_{i=1}^{ `r n`} x_i \cdot \sum\limits_{i=1}^{ `r n`} x_i y_i } {  `r n` \cdot \sum\limits_{i=1}^{ `r n`} x_i^2 - \left( \sum\limits_{i=1}^{ `r n`} x_i \right)^2 } = \frac{ `r sumYi` \cdot  `r sumXi2` -  `r sumXi` \cdot  `r sumXiYi`}{ `r n` \cdot  `r sumXi2` - \left( `r sumXi`\right)^2} =  `r b_0`$$

$$b_1 = \frac{ `r n` \cdot \sum\limits_{i=1}^{ `r n`} x_i y_i - \left( \sum\limits_{i=1}^{ `r n`} x_i \right) \cdot \left( \sum\limits_{i=1}^{ `r n`} y_i \right) }{ `r n` \cdot \sum\limits_{i=1}^{ `r n`} x_i^2 - \left( \sum\limits_{i=1}^{ `r n`} x_i \right)^2 } = \frac{ `r n` \cdot  `r sumXiYi`- `r sumXi` \cdot  `r sumYi`}{ `r n` \cdot  `r sumXi2` - \left( `r sumXi`\right)^2} =  `r b_1`$$

Meta-information
================
extype: num
exsolution: `r sc$fx[1]`
extol: `r sc$tolerance`
exname: ../../statistik_i_ii/AARnw/lr_koef12_1a.Rmd
