Skip to content

Commit

Permalink
Update 04-ch4.Rmd
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocalak authored Sep 6, 2023
1 parent 6e3a5ec commit d770b55
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions 04-ch4.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ The following code reproduces Figure 4.1 from the textbook.

```{r , echo=TRUE, fig.align='center', cache=TRUE}
# create a scatterplot of the data
plot(TestScore ~ STR,ylab="Test Score")
plot(TestScore ~ STR,ylab="Test Score",pch=20)
# add the systematic relationship to the plot
abline(a = 713, b = -3)
Expand Down Expand Up @@ -744,7 +744,7 @@ curve(dnorm(x,
-2,
sqrt(var_b0)),
add = T,
col = "darkred")
col = "darkred",lwd=2)
# plot histograms of beta_hat_1
hist(fit[, 2],
Expand All @@ -758,7 +758,7 @@ curve(dnorm(x,
3.5,
sqrt(var_b1)),
add = T,
col = "darkred")
col = "darkred",lwd=2)
```


Expand Down Expand Up @@ -820,24 +820,7 @@ and
$$Cov(X,Y)=4.$$

Formally, this is written down as

\begin{equation}
\begin{pmatrix}
X \\
Y \\
\end{pmatrix}
\overset{i.i.d.}{\sim} \ \mathcal{N}
\left[
\begin{pmatrix}
5 \\
5 \\
\end{pmatrix}, \
\begin{pmatrix}
5 & 4 \\
4 & 5 \\
\end{pmatrix}
\right]. \tag{4.3}
\end{equation}
$$\begin{pmatrix} X \\ Y \end{pmatrix}\overset{i.i.d.}{\sim} \ \mathcal{N}\left[\begin{pmatrix} 5 \\ 5 \end{pmatrix}, \begin{pmatrix} 5 & 4 \\ 4 & 5 \end{pmatrix} \right].\tag{4.3} $$

To carry out the random sampling, we make use of the function `r ttcode("mvrnorm()")` from the package `r ttcode("MASS")` [@R-MASS] which allows to draw random samples from multivariate normal distributions, see `?mvtnorm`. Next, we use `r ttcode("subset()")` to split the sample into two subsets such that the first set, `r ttcode("set1")`, consists of observations that fulfill the condition $\lvert X - \overline{X} \rvert > 1$ and the second set, `r ttcode("set2")`, includes the remainder of the sample. We then plot both sets and use different colors to distinguish the observations.

Expand Down Expand Up @@ -871,6 +854,12 @@ plot(set1,
points(set2,
col = "steelblue",
pch = 19)
legend("topleft",
legend = c("Set1",
"Set2"),
cex = 1,
pch = 19,
col = c("black","steelblue"))
```


Expand All @@ -887,8 +876,15 @@ plot(set1, xlab = "X", ylab = "Y", pch = 19)
points(set2, col = "steelblue", pch = 19)
# add both lines to the plot
abline(lm.set1, col = "green")
abline(lm.set2, col = "red")
abline(lm.set1, col = "black",lwd=2)
abline(lm.set2, col = "steelblue",lwd=2)
legend("bottomright",
legend = c("Set1",
"Set2"),
cex = 1,
lwd=2,
col = c("black","steelblue"))
```


Expand Down

0 comments on commit d770b55

Please sign in to comment.