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  : Tiurna Cikhal Khinanti
# NRP   : 5003251021
# 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) {
  # TULIS KODE ANDA DI SINI
  sort(x)
  n<-length(x)
  k<-floor(n*alpha)
  
  jumlah<-0
  for(i in 1:n){
    if(i<=k){
      jumlah<-jumlah+x[k+1]
    }
    else if(i>k && i<=(n-k)){
      jumlah<-jumlah+x[i]
    }
    else{
      jumlah<-jumlah+x[n-k]
    }
  }
  mean<- jumlah/n
  return (mean)
}

# --- [b] Hitung ordinary mean (alpha=0) dan Winsorized mean 20% (alpha=0.2) ---

# Ordinary mean
# TULIS KODE ANDA DI SINI
winsorized_mean(x,0)
## [1] 81.86667
# Winsorized mean 20%
# TULIS KODE ANDA DI SINI
winsorized_mean(x, 0.2)
## [1] 69.73333
#VISUALISASI DATA
#Boxplot Biasa Data x
boxplot(x,
        main = "Boxplot Data x",
        ylab = "Nilai",
        col = "brown")

#Boxplot Data x dan Winsorized
winsor<-winsorized_mean(x, 0.2)
boxplot(x, winsor,
        names = c("Data x","Winsorized (alpha=0.2)"),
        col = c("brown","darkred"),
        main = "Boxplot Data x dan Winsorized",
        ylab = "Nilai")

cat("Berdasarkan boxplot terdapat dua outlier yaitu 12 dan 310, dimana 310 merupakan outlier ekstrem yang menyebabkan distribusi data menjadi miring ke kanan, dengan sebagian besar data berada pada rentang tengah sekitar 50–80. Setelah dilakukan winsorized nilai eksrem ditekan sehingga tidak ada outlier yang mencolok.")
## Berdasarkan boxplot terdapat dua outlier yaitu 12 dan 310, dimana 310 merupakan outlier ekstrem yang menyebabkan distribusi data menjadi miring ke kanan, dengan sebagian besar data berada pada rentang tengah sekitar 50–80. Setelah dilakukan winsorized nilai eksrem ditekan sehingga tidak ada outlier yang mencolok.
# ==============================================================================
# Soal 2 [TOTAL 60 poin] - Weighted Multivariate Descriptive Statistics
# ==============================================================================

# --- Baca data CSV ---
df <- read.csv("C:/Users/HP/Downloads/Quiz 1 Komstat/data_quiz1.csv", header = TRUE, sep=",")
df
X <- as.matrix(df[, c("x1", "x2", "x3")])
w <- df$w

# --- [a] Buatlah fungsi weighted_corr(X, w) ---

weighted_corr <- function(X, w) {
  # TULIS KODE ANDA DI SINI
  n<-nrow (X)
  p<-ncol(X)
  W<-diag(w)
  
  n_w<-0
  for(i in 1:n){
    n_w<- n_w+w[i]
  }
  
  one_vec<-as.matrix(rep(1,n), ncol=1)
  x_bar_w<-(t(X)%*%W%*%one_vec)/n_w
  
  D<- X-one_vec%*%(t(x_bar_w))
  S_w<-((t(D))%*%W%*%D)/n_w
  s_w<-sqrt(diag(S_w))
  V<- diag(s_w)
  R_w<-solve(V)%*%S_w%*%solve(V)
  
  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
# TULIS KODE ANDA DI SINI
result<-weighted_corr(X,w)

#Matriks Diagonal Bobot W
result$W
##        [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10] [,11] [,12]
##  [1,] 14.34  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
##  [2,]  0.00 14.19  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
##  [3,]  0.00  0.00 12.49  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
##  [4,]  0.00  0.00  0.00 11.45  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
##  [5,]  0.00  0.00  0.00  0.00 17.45  0.00  0.00  0.00  0.00  0.00  0.00  0.00
##  [6,]  0.00  0.00  0.00  0.00  0.00 15.24  0.00  0.00  0.00  0.00  0.00  0.00
##  [7,]  0.00  0.00  0.00  0.00  0.00  0.00 34.73  0.00  0.00  0.00  0.00  0.00
##  [8,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00 17.97  0.00  0.00  0.00  0.00
##  [9,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 33.13  0.00  0.00  0.00
## [10,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 35.93  0.00  0.00
## [11,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 15.55  0.00
## [12,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 16.54
## [13,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [14,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [15,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [16,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [17,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [18,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [19,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [20,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [21,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [22,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [23,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [24,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [25,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [26,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [27,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [28,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [29,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [30,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [31,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [32,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [33,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [34,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [35,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [36,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [37,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [38,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
##       [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
##  [1,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
##  [2,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
##  [3,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
##  [4,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
##  [5,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
##  [6,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
##  [7,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
##  [8,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
##  [9,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [10,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [11,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [12,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [13,] 17.25  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [14,]  0.00 14.93  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [15,]  0.00  0.00  7.24  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [16,]  0.00  0.00  0.00  9.85   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [17,]  0.00  0.00  0.00  0.00  11.1  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [18,]  0.00  0.00  0.00  0.00   0.0 12.89  0.00  0.00  0.00  0.00  0.00  0.00
## [19,]  0.00  0.00  0.00  0.00   0.0  0.00 11.14  0.00  0.00  0.00  0.00  0.00
## [20,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  7.06  0.00  0.00  0.00  0.00
## [21,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00 13.96  0.00  0.00  0.00
## [22,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00 23.13  0.00  0.00
## [23,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00 19.74  0.00
## [24,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00 17.53
## [25,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [26,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [27,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [28,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [29,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [30,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [31,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [32,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [33,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [34,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [35,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [36,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [37,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
## [38,]  0.00  0.00  0.00  0.00   0.0  0.00  0.00  0.00  0.00  0.00  0.00  0.00
##       [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36]
##  [1,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
##  [2,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
##  [3,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
##  [4,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
##  [5,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
##  [6,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
##  [7,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
##  [8,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
##  [9,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [10,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [11,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [12,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [13,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [14,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [15,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [16,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [17,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [18,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [19,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [20,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [21,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [22,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [23,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [24,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [25,] 12.56  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [26,]  0.00 13.01  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [27,]  0.00  0.00 12.28  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [28,]  0.00  0.00  0.00  7.95  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [29,]  0.00  0.00  0.00  0.00 20.84  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [30,]  0.00  0.00  0.00  0.00  0.00  0.67  0.00  0.00  0.00  0.00   0.0  0.00
## [31,]  0.00  0.00  0.00  0.00  0.00  0.00  0.33  0.00  0.00  0.00   0.0  0.00
## [32,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  1.11  0.00  0.00   0.0  0.00
## [33,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.55  0.00   0.0  0.00
## [34,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.39   0.0  0.00
## [35,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.2  0.00
## [36,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.36
## [37,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
## [38,]  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00   0.0  0.00
##       [,37] [,38]
##  [1,]  0.00  0.00
##  [2,]  0.00  0.00
##  [3,]  0.00  0.00
##  [4,]  0.00  0.00
##  [5,]  0.00  0.00
##  [6,]  0.00  0.00
##  [7,]  0.00  0.00
##  [8,]  0.00  0.00
##  [9,]  0.00  0.00
## [10,]  0.00  0.00
## [11,]  0.00  0.00
## [12,]  0.00  0.00
## [13,]  0.00  0.00
## [14,]  0.00  0.00
## [15,]  0.00  0.00
## [16,]  0.00  0.00
## [17,]  0.00  0.00
## [18,]  0.00  0.00
## [19,]  0.00  0.00
## [20,]  0.00  0.00
## [21,]  0.00  0.00
## [22,]  0.00  0.00
## [23,]  0.00  0.00
## [24,]  0.00  0.00
## [25,]  0.00  0.00
## [26,]  0.00  0.00
## [27,]  0.00  0.00
## [28,]  0.00  0.00
## [29,]  0.00  0.00
## [30,]  0.00  0.00
## [31,]  0.00  0.00
## [32,]  0.00  0.00
## [33,]  0.00  0.00
## [34,]  0.00  0.00
## [35,]  0.00  0.00
## [36,]  0.00  0.00
## [37,]  3.36  0.00
## [38,]  0.00  1.94
# Tampilkan vektor mean tertimbang
# TULIS KODE ANDA DI SINI
result$x_bar_w
##        [,1]
## x1 73.88530
## x2 65.39059
## x3 17.00938
# Tampilkan matriks varians-kovarians tertimbang
# TULIS KODE ANDA DI SINI
result$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
# TULIS KODE ANDA DI SINI
result$s_w
##       x1       x2       x3 
## 6.177671 6.411527 4.598649
# Tampilkan matriks korelasi tertimbang
# TULIS KODE ANDA DI SINI
result$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
pairs(df[, c("x1","x2","x3")],
     main = "Data Matriks Scatter Plot",
     pch = 19,
     col = "steelblue",
     cex = 1.5)

hist(df$x1,
     main = "Histogram x1",
     xlab = "Data x1",
     col = "skyblue",
     breaks = 10)

hist(df$x2,
     main = "Histogram x2",
     xlab = "Data x2",
     col = "lightpink",
     breaks = 10)

hist(df$x3,
     main = "Histogram x3",
     xlab = "Data x3",
     col = "lightgreen",
     breaks = 10)

cat("Berdasarkan visualisasi, variabel x1 memiliki hubungan negatif dengan x2 dan x3, sedangkan x2 dan x3 memiliki hubungan positif. Berdasarkan histogram, x1 berdistribusi relatif simetris, sedangkan x2 dan x3 cenderung miring ke kanan, dan tidak terdapat outlier yang signifikan")
## Berdasarkan visualisasi, variabel x1 memiliki hubungan negatif dengan x2 dan x3, sedangkan x2 dan x3 memiliki hubungan positif. Berdasarkan histogram, x1 berdistribusi relatif simetris, sedangkan x2 dan x3 cenderung miring ke kanan, dan tidak terdapat outlier yang signifikan

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.