Question 1
x <- c(2.1, 3.5, 4.0, 5.2, 6.8)
z <- c(1.9, 3.0, 4.2, 5.0, 6.5)
show_vec(x, "x")
## x: c(2.1, 3.5, 4.0, 5.2, 6.8)
show_vec(z, "z")
## z: c(1.9, 3.0, 4.2, 5.0, 6.5)
r <- cor(x, z, method = "pearson")
r_sq <- r^2
r_sqrt <- sqrt(r)
cat("Pearson correlation r =", round(r, 4), "\n")
## Pearson correlation r = 0.9897
cat("r^2 =", round(r_sq, 4), "\n")
## r^2 = 0.9795
cat("sqrt(r) =", round(r_sqrt, 4), "\n\n")
## sqrt(r) = 0.9948
par(mfrow = c(1, 2))
viz_bar(x, "x")
viz_bar(z, "z")

par(mfrow = c(1, 1))
viz_arrows_2d(x, z, xlab = "x", ylab = "z", main = "Vectors (x_i, z_i) as arrows from origin")

Question 2
x <- 1
vals <- c()
while (x < 12) {
print(x)
vals <- c(vals, x)
x <- x + 1
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
## [1] 11
show_vec(vals, "values")
## values: c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
viz_line(vals, "values")

Question 3
v <- 1:10
show_vec(v, "v")
## v: c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
dotchart(v, labels = seq_along(v), main = "Dotchart", xlab = "Value", col = "blue")

viz_bar(v, "vector")

Question 4
vec_seq <- 1:12
show_vec(vec_seq, "vec_seq")
## vec_seq: c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
cat("Maximum value:", max(vec_seq), "\n")
## Maximum value: 12
cat("Minimum value:", min(vec_seq), "\n")
## Minimum value: 1
par(mfrow = c(1, 2))
viz_bar(vec_seq, "vector sequence")
viz_line(vec_seq, "vector sequence")

par(mfrow = c(1, 1))
Question 5
vec_rep <- c(4, 7, 7, 2, 9, 4, 7, 3, 4)
show_vec(vec_rep, "vec_rep")
## vec_rep: c(4, 7, 7, 2, 9, 4, 7, 3, 4)
v_mean <- mean(vec_rep)
v_median <- median(vec_rep)
get_mode <- function(v) {
tbl <- table(v)
as.numeric(names(tbl)[tbl == max(tbl)])
}
v_mode <- get_mode(vec_rep)
cat("Mean:", round(v_mean, 4), "\n")
## Mean: 5.2222
cat("Median:", v_median, "\n")
## Median: 4
cat("Mode(s):", paste(v_mode, collapse = ", "), "\n")
## Mode(s): 4, 7
barplot(table(vec_rep), main = "Frequencies", col = "orange", xlab = "Value", ylab = "Count")

Question 6
x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
y <- c(1.5, 2.9, 3.2, 5.1, 5.9, 6.8, 7.2, 8.9, 9.5)
show_vec(x, "x")
## x: c(1, 2, 3, 4, 5, 6, 7, 8, 9)
show_vec(y, "y")
## y: c(1.5, 2.9, 3.2, 5.1, 5.9, 6.8, 7.2, 8.9, 9.5)
model <- lm(y ~ x)
print(summary(model))
##
## Call:
## lm(formula = y ~ x)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.4767 -0.1867 0.1383 0.2333 0.4283
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.69167 0.25442 2.719 0.0298 *
## x 0.99500 0.04521 22.008 1.01e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.3502 on 7 degrees of freedom
## Multiple R-squared: 0.9858, Adjusted R-squared: 0.9837
## F-statistic: 484.3 on 1 and 7 DF, p-value: 1.01e-07
newdata <- data.frame(x = 19)
pred <- predict(model, newdata, interval = "prediction", level = 0.95)
print(pred)
## fit lwr upr
## 1 19.59667 17.86402 21.32931
plot(x, y, pch = 19, col = "blue", main = "Scatter with regression line", xlab = "x", ylab = "y")
abline(model, col = "red", lwd = 2)

plot(model$fitted.values, resid(model), pch = 19, col = "purple", main = "Residuals vs Fitted", xlab = "Fitted", ylab = "Residuals")
abline(h = 0, lty = 2)

Question 7
vals <- c(10, 20, 15, 25, 5, 25)
labels <- c("A", "B", "C", "D", "E", "F")
show_vec(vals, "vals")
## vals: c(10, 20, 15, 25, 5, 25)
pie(vals, labels = paste0(labels, " (", vals, ")"), main = "Pie Chart", col = rainbow(length(vals)))

Question 8
data("mtcars")
df_box <- mtcars[, c("mpg", "hp")]
show_vec(df_box$mpg, "mtcars$mpg")
## mtcars$mpg: c(21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8, 16.4, 17.3, 15.2, 10.4, 10.4, 14.7, 32.4, 30.4, 33.9, 21.5, 15.5, 15.2, 13.3, 19.2, 27.3, 26.0, 30.4, 15.8, 19.7, 15.0, 21.4)
show_vec(df_box$hp, "mtcars$hp")
## mtcars$hp: c(110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180, 205, 215, 230, 66, 52, 65, 97, 150, 150, 245, 175, 66, 91, 113, 264, 175, 335, 109)
print(head(df_box))
## mpg hp
## Mazda RX4 21.0 110
## Mazda RX4 Wag 21.0 110
## Datsun 710 22.8 93
## Hornet 4 Drive 21.4 110
## Hornet Sportabout 18.7 175
## Valiant 18.1 105
boxplot(df_box, main = "Boxplots of mpg and hp (mtcars)", names = c("mpg", "hp"), col = c("lightblue", "lightgreen"))
