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.

## SOAL NOMOR 1 ##
x <- c(12, 45, 52, 58, 61, 63, 67, 70, 72, 75, 78, 82, 88, 95, 310)

winsorized_mean <- function(x, alpha){
  n <- length(x)
  k <- floor(n * alpha)
  
  x_sorted <- sort(x)
  x_w <- x_sorted
  
  if(k > 0){
    x_w[1:k] <- x_sorted[k+1]
    x_w[(n-k+1):n] <- x_sorted[n-k]
  }
  
  return(mean(x_w))
}


mean_biasa <- mean(x)
mean_winsor <- winsorized_mean(x, 0.2)

mean_biasa
## [1] 81.86667
mean_winsor
## [1] 69.73333
#=========================
# VISUALISASI DATA 
#=========================

winsorize_data <- function(x, alpha) {
  n <- length(x)
  k <- floor(n * alpha)
  x_sorted <- sort(x)
  
  if (k > 0) {
    x_sorted[1:k] <- x_sorted[k+1]
    x_sorted[(n-k+1):n] <- x_sorted[n-k]
  }
  
  return(x_sorted)
}

plot_winsorized <- function(x, alpha) {
  
  x_w <- winsorize_data(x, alpha)
  par(mfrow = c(1, 2))
  
  plot(1:length(x), x,
       pch = 19, col = "blue",
       main = "Sebelum Winsorizing",
       xlab = "Index", ylab = "Nilai")
  
  abline(lm(x ~ I(1:length(x))), col = "red", lwd = 2)
  
  plot(1:length(x_w), x_w,
       pch = 19, col = "darkgreen",
       main = "Setelah Winsorizing",
       xlab = "Index", ylab = "Nilai")
  
  abline(lm(x_w ~ I(1:length(x_w))), col = "red", lwd = 2)
  
  par(mfrow = c(1,1))
}

plot_winsorized(x, 0.2)

#========================
# INTERPRETASI DATA 
#========================
# Sebelum winsorizing, terdapat nilai ekstrem yang membuat pola data kurang stabil dan garis regresi menjadi bias. Setelah winsorizing, nilai ekstrem tersebut dikurangi pengaruhnya sehingga data menjadi lebih merata dan garis regresi lebih merepresentasikan tren sebenarnya. Dengan demikian, winsorizing membantu mengurangi efek outlier pada data. 





## SOAL NOMOR 2##
data <- read.csv("C:/Users/AISYAH/OneDrive/Documents/data_quiz1.csv", header = TRUE)
data
w <- as.numeric(data$w)
X <- as.matrix(data[, c("x1","x2","x3")])

weighted_corr <- function(X, w){
  
  n <- nrow(X)
  p <- ncol(X)
  nw <- sum(w)
  W <- diag(w)
  one <- matrix(1, n, 1)
  x_bar <- (t(X) %*% W %*% one) / nw
  D <- X - one %*% t(x_bar)
  S_w <- (t(D) %*% W %*% D) / nw
  s_w <- sqrt(diag(S_w))
  V <- diag(s_w)
  R_w <- solve(V) %*% S_w %*% solve(V)
  
  cat("=== Matriks Bobot (W) ===\n")
  print(round(W, 4))
  
  cat("\n=== Mean Tertimbang ===\n")
  print(round(x_bar, 4))
  
  cat("\n=== Matriks Kovarians (S_w) ===\n")
  print(round(S_w, 4))
  
  cat("\n=== Standar Deviasi (s_w) ===\n")
  print(round(s_w, 4))
  
  cat("\n=== Matriks Korelasi (R_w) ===\n")
  print(round(R_w, 4))
  
}

weighted_corr(X, w)
## === Matriks Bobot (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
## 
## === Mean Tertimbang ===
##       [,1]
## x1 73.8853
## x2 65.3906
## x3 17.0094
## 
## === Matriks Kovarians (S_w) ===
##          x1       x2       x3
## x1  38.1636 -37.7510 -27.1539
## x2 -37.7510  41.1077  29.1659
## x3 -27.1539  29.1659  21.1476
## 
## === Standar Deviasi (s_w) ===
##     x1     x2     x3 
## 6.1777 6.4115 4.5986 
## 
## === Matriks Korelasi (R_w) ===
##         [,1]    [,2]    [,3]
## [1,]  1.0000 -0.9531 -0.9558
## [2,] -0.9531  1.0000  0.9892
## [3,] -0.9558  0.9892  1.0000
# =========================
# VISUALISASI DATA
# =========================

data <- read.csv("C:/Users/AISYAH/OneDrive/Documents/data_quiz1.csv", header = TRUE)
data
x1 <- data$x1
x2 <- data$x2
x3 <- data$x3
w  <- data$w

plot_scatter <- function(x, y, judul, xlab, ylab) {
  plot(x, y,
       main = judul,
       xlab = xlab,
       ylab = ylab,
       pch = 16)
  
  model <- lm(y ~ x)
  abline(model, col = "red", lwd = 2)
}

par(mfrow = c(1,3), oma = c(0,0,3,0))

plot_scatter(x1, x2, "Plot 1", "x1", "x2")
plot_scatter(x1, x3, "Plot 2", "x1", "x3")
plot_scatter(x2, x3, "Plot 3", "x2", "x3")

mtext("Visualisasi Data", outer = TRUE, cex = 1.5, font = 2)

#======================
# INTERPRETASI DATA
#======================

# Dari ketiga plot, hubungan antar variabel terlihat cukup jelas. Pada plot x1 dengan x2 dan x1 dengan x3, terlihat kecenderungan hubungan negatif, artinya semakin besar nilai x1 maka nilai x2 dan x3 cenderung menurun. Sebaliknya, pada plot x2 dengan x3, terlihat hubungan positif, di mana kenaikan x2 diikuti dengan kenaikan x3.

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.