This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
# ==============================================================================
# TEMPLATE JAWABAN KUIS
# Komputasi Statistika, Kelas D
# Prodi S1 Statistika FSAD ITS - Semester Genap 2025/2026
# Kamis, 12 Maret 2026
# ==============================================================================
# Nama : ___________________________
# NRP : ___________________________
# Kelas : ___________________________
# ==============================================================================
# ==============================================================================
# Soal 1 [TOTAL 50 poin] - Winsorized Mean
# ==============================================================================
# Data
x <- c(12, 45, 52, 58, 61, 63, 67, 70, 72, 75, 78, 82, 88, 95, 310)
# --- [a] Buatlah fungsi winsorized_mean(x, alpha) ---
winsorized_mean <- function(x, alpha) {
x_sort <- sort(x)
n <- length(x)
k <- floor(n*alpha)
y <- x_sort
if (k>0){
y[1:k] <- x_sort[k +1]
y[(n-k+1):n] <- x_sort[n-k]
}
return(mean(y))
}
# --- [b] Hitung ordinary mean (alpha=0) dan Winsorized mean 20% (alpha=0.2) ---
# Ordinary mean
ordinary_mean0 <- winsorized_mean(x, 0)
cat("Ordinary mean alpha = 0: ", ordinary_mean0, "\n")
## Ordinary mean alpha = 0: 81.86667
# Winsorized mean 20%
ordinary_mean20 <- winsorized_mean(x, 0.2)
cat("Ordinary mean 20% alpha = 0.2: ", ordinary_mean20, "\n")
## Ordinary mean 20% alpha = 0.2: 69.73333
# ==============================================================================
# Soal 2 [TOTAL 60 poin] - Weighted Multivariate Descriptive Statistics
# ==============================================================================
# --- Baca data CSV ---
df <- read.csv("C:/Users/ASRUL SANI/Downloads/Quiz 1 Komstat/data_quiz1.csv")
X <- as.matrix(df[, c("x1", "x2", "x3")])
w <- df$w
# --- [a] Buatlah fungsi weighted_corr(X, w) ---
weighted_corr <- function(X, w) {
X <- as.matrix(X)
n <- nrow(X)
p <- ncol(X)
W <- diag(w)
nw <- sum(w)
ones <- rep(1, n)
x_bar_w <- (1/nw) * t(X) %*% W %*% ones
D <- X - (ones %*% t(x_bar_w))
Sw <- (1/nw) * t(D) %*% W %*% D
sw <- sqrt(diag(Sw))
V_inv <- diag(1/sw)
Rw <- V_inv %*% Sw %*% V_inv
return(list(
W = W,
x_bar_w = x_bar_w,
S_w = Sw,
s_w = sw,
R_w = Rw
))
}
# --- [b] Aplikasikan fungsi pada data ---
# Panggil fungsi
hasil <- weighted_corr(X,w)
# Tampilkan vektor mean tertimbang
print("Vektor Mean Tertimbang (x_bar_w):")
## [1] "Vektor Mean Tertimbang (x_bar_w):"
hasil$x_bar_w
## [,1]
## x1 73.88530
## x2 65.39059
## x3 17.00938
# Tampilkan matriks varians-kovarians tertimbang
print("Matriks Korelasi Tertimbang (Sw):")
## [1] "Matriks Korelasi Tertimbang (Sw):"
hasil$S_w
## x1 x2 x3
## x1 38.16362 -37.75105 -27.15386
## x2 -37.75105 41.10767 29.16587
## x3 -27.15386 29.16587 21.14757
# Tampilkan vektor standar deviasi tertimbang
print("Matriks Korelasi Tertimbang (sw):")
## [1] "Matriks Korelasi Tertimbang (sw):"
hasil$s_w
## x1 x2 x3
## 6.177671 6.411527 4.598649
# Tampilkan matriks korelasi tertimbang
print("Matriks Korelasi Tertimbang (Rw):")
## [1] "Matriks Korelasi Tertimbang (Rw):"
hasil$R_w
## [,1] [,2] [,3]
## [1,] 1.0000000 -0.9531095 -0.9558207
## [2,] -0.9531095 1.0000000 0.9891979
## [3,] -0.9558207 0.9891979 1.0000000
boxplot(x,
main = "boxplot distribusi data x",
ylab = "Nilai",
col = "pink",
border = "darkred",
pch = 19,
cex = 1.2,
horizontal = FALSE)
data_plot <- df[, c("x1", "x2", "x3")]
pairs(data_plot,
main = "Matriks Scatter Plot",
pch = 19,
col = "brown")
hist(df$x1,
main = "Histogram x1",
xlab = "Data x1",
col = "navyblue",
border = "black",
breaks = 10)
hist(df$x2,
main = "Histogram x2",
xlab = "Data x2",
col = "skyblue",
border = "black",
breaks = 10)
hist(df$x3,
main = "Histogram x3",
xlab = "Data x3",
col = "lightblue",
border = "black",
breaks = 10)
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.