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 : Nadhif Muhammad Firaas
# NRP : 5003251160
# Kelas : D
# ==============================================================================
# ==============================================================================
# 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(x)
hasil <- 0
n <- length(x)
k <- floor(n * alpha)
y <- x
for(i in 1:n){
if(i <= k){
y[i] <- x[k+1]
}else if(i > k && i <= (n-k)){
y[i] <- x[i]
}else if(i > (n-k)){
y[i] <- x[n-k]
}
}
for(i in 1:n){
hasil <- hasil + y[i]
}
return(hasil/n)
}
# --- [b] Hitung ordinary mean (alpha=0) dan Winsorized mean 20% (alpha=0.2) ---
# Ordinary mean
ordinary_mean = winsorized_mean(x, 0)
print("Ordinary Mean = ")
ordinary_mean
# Winsorized mean 20%
winsorized_mean = winsorized_mean(x, 0.2)
print("Winsorized Mean = ")
winsorized_mean
# ==============================================================================
# Soal 2 [TOTAL 60 poin] - Weighted Multivariate Descriptive Statistics
# ==============================================================================
# --- Baca data CSV ---
df <- read.csv("C:/Users/Hp/Downloads/RSTUDIO/Quiz 1 Komstat/data_quiz1.csv", header = T, sep = ",")
X <- as.matrix(df[, c("x1", "x2", "x3")])
w <- df$w
dfkota <chr> | x1 <int> | x2 <int> | x3 <dbl> | w <dbl> |
|---|---|---|---|---|
| Kab. Pacitan | 86 | 55 | 10.2 | 14.34 |
| Kab. Ponorogo | 78 | 64 | 15.8 | 14.19 |
| Kab. Trenggalek | 83 | 58 | 11.5 | 12.49 |
| Kab. Tulungagung | 75 | 68 | 17.3 | 11.45 |
| Kab. Blitar | 80 | 60 | 13.6 | 17.45 |
| Kab. Kediri | 74 | 66 | 16.8 | 15.24 |
| Kab. Malang | 66 | 74 | 23.5 | 34.73 |
| Kab. Lumajang | 77 | 62 | 14.7 | 17.97 |
| Kab. Jember | 69 | 72 | 21.2 | 33.13 |
| Kab. Banyuwangi | 73 | 67 | 19.4 | 35.93 |
# --- [a] Buatlah fungsi weighted_corr(X, w) ---
weighted_corr <- function(X, w) {
n <- nrow(X)
p <- ncol(X)
#matriks bobot w
W <- matrix(0,n,n)
for(i in 1:n){
W[i,i] <- w[i]
}
#hitung jumlah bobot
hasil <- 0
for(i in 1:n){
hasil <- hasil + w[i]
}
#weighted mean
x_bar_w <- rep(0,p)
for(i in 1:p){
total <- 0
for(j in 1:n){
total <- total + w[j]*X[j,i]
}
x_bar_w[i] <- total/hasil
}
#matriks deviasi
D <- matrix(0,n,p)
for(i in 1:n){
for(j in 1:p){
D[i,j] <- X[i,j] - x_bar_w[j]
}
}
#weighted covarians matriks
S_w <- matrix(0,p,p)
for(i in 1:p){
for(j in 1:p){
total <- 0
for(k in i:n){
total <- total + w[k]*D[k,i]*D[k,j]
}
S_w[i,j] <- total/hasil
}
}
#weighted standar deviasi
s_w <- rep(0,p)
for(i in 1:p){
s_w[i] <- sqrt(S_w[i,i])
}
#weighted matriks korelasi
R_w <- matrix(0,p,p)
for(i in 1:p){
for(j in 1:p){
R_w[i,j] <- S_w[i,j]/(s_w[i]*s_w[j])
}
}
return(list(
W = W,
x_bar_w = x_bar_w,
S_w = S_w,
s_w = s_w,
R_w = R_w
))
}
# --- [b] Aplikasikan fungsi pada data ---
# Panggil fungsi
hasil <- weighted_corr(X,w)
# Tampilkan vektor mean tertimbang
hasil$x_bar_w[1] 73.88530 65.39059 17.00938
# Tampilkan matriks varians-kovarians tertimbang
hasil$S_w [,1] [,2] [,3]
[1,] 38.16362 -37.75105 -27.15386
[2,] -33.99339 37.88479 27.05378
[3,] -24.54432 27.00410 19.72023
# Tampilkan vektor standar deviasi tertimbang
hasil$s_w[1] 6.177671 6.155062 4.440747
# Tampilkan matriks korelasi tertimbang
hasil$R_w [,1] [,2] [,3]
[1,] 1.0000000 -0.9928229 -0.9898074
[2,] -0.8939995 1.0000000 0.9897818
[3,] -0.8946849 0.9879644 1.0000000
```
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.