# --- Baca data CSV ---
df <- read.csv("C:/Users/SHELLA YUASTARI/Downloads/Quiz 1 Komstat/data_quiz1.csv")
# Ambil variabel
X <- as.matrix(df[, c("x1", "x2", "x3")])
w <- df$w
# --- [a] Fungsi weighted correlation ---
weighted_corr <- function(X, w) {
X <- as.matrix(X)
n <- nrow(X)
# Matriks bobot
W <- diag(w)
# Vektor satuan
one_n <- matrix(1, nrow = n, ncol = 1)
# Total bobot
nw <- as.numeric(t(one_n) %*% w)
# Mean tertimbang
x_bar_w <- (1 / nw) * t(X) %*% W %*% one_n
# Deviasi
D <- X - (one_n %*% t(x_bar_w))
# Matriks kovarians tertimbang
S_w <- (1 / nw) * t(D) %*% W %*% D
# Standar deviasi tertimbang
s_w <- sqrt(diag(S_w))
# Matriks korelasi tertimbang
V_inv <- diag(1 / s_w)
R_w <- V_inv %*% S_w %*% V_inv
return(list(
W = W,
mean_w = x_bar_w,
S_w = S_w,
s_w = s_w,
R_w = R_w
))
}
# --- [b] Aplikasikan fungsi pada data ---
hasil <- weighted_corr(X, w)
# --- Tampilkan hasil ---
cat("Matriks Bobot (W):\n")
Matriks Bobot (W):
print(hasil$W)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 14.34 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
[3,] 0.00 0.00 12.49 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
[5,] 0.00 0.00 0.00 0.00 17.45 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
[7,] 0.00 0.00 0.00 0.00 0.00 0.00 34.73 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
[9,] 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 33.13 0.00
[10,] 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 35.93
[11,] 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
[12,] 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
[13,] 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
[15,] 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
[17,] 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
[19,] 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
[21,] 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
[23,] 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
[25,] 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
[,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20]
[1,] 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[2,] 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[3,] 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[4,] 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[5,] 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[6,] 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[7,] 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[8,] 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[9,] 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[10,] 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[11,] 15.55 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[12,] 0.00 16.54 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[13,] 0.00 0.00 17.25 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[14,] 0.00 0.00 0.00 14.93 0.00 0.00 0.0 0.00 0.00 0.00
[15,] 0.00 0.00 0.00 0.00 7.24 0.00 0.0 0.00 0.00 0.00
[16,] 0.00 0.00 0.00 0.00 0.00 9.85 0.0 0.00 0.00 0.00
[17,] 0.00 0.00 0.00 0.00 0.00 0.00 11.1 0.00 0.00 0.00
[18,] 0.00 0.00 0.00 0.00 0.00 0.00 0.0 12.89 0.00 0.00
[19,] 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 11.14 0.00
[20,] 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 7.06
[21,] 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[22,] 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[23,] 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[24,] 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[25,] 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[26,] 0.00 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29] [,30]
[1,] 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
[2,] 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 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
[4,] 0.00 0.00 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 0.00 0.00 0.00 0.00 0.00 0.00
[6,] 0.00 0.00 0.00 0.00 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 0.00 0.00 0.00 0.00
[8,] 0.00 0.00 0.00 0.00 0.00 0.00 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 0.00 0.00
[10,] 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 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
[12,] 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
[13,] 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
[15,] 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
[17,] 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
[19,] 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
[21,] 13.96 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
[22,] 0.00 23.13 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
[23,] 0.00 0.00 19.74 0.00 0.00 0.00 0.00 0.00 0.00 0.00
[24,] 0.00 0.00 0.00 17.53 0.00 0.00 0.00 0.00 0.00 0.00
[25,] 0.00 0.00 0.00 0.00 12.56 0.00 0.00 0.00 0.00 0.00
[26,] 0.00 0.00 0.00 0.00 0.00 13.01 0.00 0.00 0.00 0.00
[,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38]
[1,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[2,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[3,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[4,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[5,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[6,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[7,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[8,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[9,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[10,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[11,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[12,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[13,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[14,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[15,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[16,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[17,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[18,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[19,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[20,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[21,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[22,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[23,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[24,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[25,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[26,] 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00
[ reached 'max' / getOption("max.print") -- omitted 12 rows ]
cat("\nMean tertimbang:\n")
Mean tertimbang:
cat("\nMatriks Varians-Kovarians tertimbang (S_w):\n")
Matriks Varians-Kovarians tertimbang (S_w):
print(hasil$mean_w)
[,1]
x1 73.88530
x2 65.39059
x3 17.00938
cat("\nStandar Deviasi tertimbang:\n")
Standar Deviasi tertimbang:
print(hasil$s_w)
x1 x2 x3
6.177671 6.411527 4.598649
cat("\nMatriks Korelasi tertimbang (R_w):\n")
Matriks Korelasi tertimbang (R_w):
print(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
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.