4.1

(c) Contour Plot

Plot \(Distance^2 < \chi_2^2(.50)=1.39\): \[f(\textbf x) = \frac{1}{2 \pi det(\Sigma)^{(1/2)}} e^{- \chi_2^2(.50)/2 }\]

library(ellipse)
library(latex2exp)
# mu <- c(1, 3)
# sigma <- matrix(c(2, -0.8*sqrt(2), -0.8*sqrt(2), 1), 2)
# mixtools::ellipse(mu, sigma, npoints = 1000, newplot = TRUE, alpha = 0.5,
#     pch = 16, cex = .1, xlab = TeX("x_1"), ylab = TeX("x_2"),
#     main="50% Contour Plot")

# ellipse::ellipse(corr, std(vector), centre = mu(vector), level = 0.5, npoints= 1000)
ell_data <- ellipse(-0.8, c(sqrt(2),1), centre = c(1,3), level = 0.5, npoints= 1000)
ggplot(as.data.frame(ell_data))+
    geom_point(aes(x=x, y=y),size = 0.1, color="#F8766D")+
    labs(x=TeX("$x_1$"), 
        y=TeX("$x_2$"), 
        title="50% Contour Plot") + theme+
    theme(axis.text.x = element_text(size = 7, face = "plain", angle = 0))

4.24

library(readr)
data <- read_table2("P1-4.txt", col_names = FALSE)
quantiles <- qnorm(p=seq(0.5,9.5, 1)/10)
QQ_1 <- cbind(x=sort(data$X1), quantiles)
QQ_2 <- cbind(x=sort(data$X2), quantiles)
QQ_3 <- cbind(x=sort(data$X3), quantiles)

Q-Q Plot

library(ggplot2)
library(scales)
library(cowplot)
library(latex2exp)

p1 <- ggplot(as.data.frame(QQ_1))+
        geom_point(aes(x=quantiles, y=x), 
                   color=hue_pal()(3)[1])+
    scale_x_continuous(breaks = seq(-1.8,1.8, 0.45))+
    scale_y_continuous(breaks = seq(50,300, 25))+
    labs(x=TeX("$q_{(j)}$"),
         y=TeX("$x_{(j)}$"),
         title=TeX("$x_1$ Q-Q Plot"))+ theme
p2 <- ggplot(as.data.frame(QQ_2))+
        geom_point(aes(x=quantiles, y=x), 
                   color=hue_pal()(3)[2])+
    scale_x_continuous(breaks = seq(-1.8,1.8, 0.45))+
    scale_y_continuous(breaks = seq(5,30, 2))+
    labs(x=TeX("$q_{(j)}$"),
         y=TeX("$x_{(j)}$"),
         title=TeX("$x_2$ Q-Q Plot"))+ theme

p3 <- ggplot(as.data.frame(QQ_3))+
        geom_point(aes(x=quantiles, y=x), 
                   color=hue_pal()(3)[3])+
    scale_x_continuous(breaks = seq(-1.8,1.8, 0.45))+
    scale_y_continuous(breaks = seq(180,1500, 150))+
    labs(x=TeX("$q_{(j)}$"),
         y=TeX("$x_{(j)}$"),
         title=TeX("$x_3$ Q-Q Plot"))+ theme

plot_grid(p1, p2, p3, nrow = 1)

4.26

Data

x_1 <- c(1,2,3,3,4,5,6,8,9,11)
x_2 <- c(18.95,19,17.95,15.54,14,12.95,8.94,7.49,6,3.99)
data <- as.data.frame(cbind(x_1,x_2))

Covariance Matrix

cov_mt <- cov(data)
mt_r <- round(cov_mt, digits = 2)

(a): Equations

\[ \textbf{S} = \begin{pmatrix} s_{11} & s_{12} \\ s_{12} & s_{22} \\ \end{pmatrix} = \begin{pmatrix} 10.62 & -17.71 \\ -17.71 & 30.85 \\ \end{pmatrix} \]

\[\overline{\textbf{x}}^T=(5.2, 12.481)\]


\[distance^2 = (\textbf{x}_j - \overline{\textbf{x}})^T \textbf{S}^{-1} (\textbf{x}_j - \overline{\textbf{x}})\]

\[ = \left( {{x}_{\mathit{j1}}}-{{x}_{1}}\right) \left( \frac{{{s}_{22}}\left( {{x}_{\mathit{j1}}}-{{x}_{1}}\right) }{{{s}_{11}}{{s}_{22}}-{{{{s}_{12}}}^{2}}}-\frac{{{s}_{12}}\left( {{x}_{\mathit{j2}}}-{{x}_{2}}\right) }{{{s}_{11}}{{s}_{22}}-{{{{s}_{12}}}^{2}}}\right) \\ +\left( {{x}_{\mathit{j2}}}-{{x}_{2}}\right) \left( \frac{{{s}_{11}}\left( {{x}_{\mathit{j2}}}-{{x}_{2}}\right) }{{{s}_{11}}{{s}_{22}}-{{{{s}_{12}}}^{2}}}-\frac{{{s}_{12}}\left( {{x}_{\mathit{j1}}}-{{x}_{1}}\right) }{{{s}_{11}}{{s}_{22}}-{{{{s}_{12}}}^{2}}}\right) \]

\[ = \left( 0.7538828748926251\left( {{x}_{\mathit{j2}}}-12.841\right) \\ +1.25693419723757\left( {{x}_{\mathit{j1}}}-5.2\right) \right) \left( {{x}_{\mathit{j2}}}-12.841\right) \\ +\left( {{x}_{\mathit{j1}}}-5.2\right) \left( 1.25693419723757\left( {{x}_{\mathit{j2}}}-12.841\right) \\ +2.189804123676667\left( {{x}_{\mathit{j1}}}-5.2\right) \right) \]

(b): Observations within \(\chi_2^2(.5)=1.39\)

x_b <- c(mean(x_1),mean(x_2))
dist <- rep(NA, 10)
for (i in 1:10) {
    dist[i] <- as.matrix(data[i,] - x_b) %*%  solve(cov_mt) %*% t(as.matrix(data[i,] - x_b))
}

distance2 = (1.88, 2.02, 2.9, 0.74, 0.31, 0.02, 3.73, 0.82, 1.38, 4.22)

50%: \(distance^2 < \chi_2^2(.5)\)

(c): Chi-square plot

ordered <- sort(dist)
q_chi <- qchisq((1:10-0.5)/10, 2)
chi_plot <- as.data.frame(cbind(ordered, q_chi))

Ordered distances: 0.02, 0.31, 0.74, 0.82, 1.38, 1.88, 2.02, 2.9, 3.73, 4.22

library(latex2exp)
library(ggplot2)
ggplot(chi_plot)+
    geom_point(aes(x=q_chi, y=ordered), color="#F8766D")+
    scale_x_continuous(breaks = seq(0,6.5, 0.5))+
    scale_y_continuous(breaks = seq(0,4.5, 0.5))+
    labs(x=TeX("$\\frac{\\chi_2^2(j-0.5)}{10}$"),
         y=TeX("$d_{(j)}^2$"),
         title="Chi-square Plot")