Jawaban 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)
sorted_data <- sort(x)
sum <- 0
for(i in 1:n){
if(i<=k){
sum <- sum + sorted_data[k+1]
}
else if(k < i & i <= n-k){
sum <- sum + sorted_data[i]
}
else if((n-k)<i){
sum <- sum + sorted_data[n-k]
}
}
return(sum/n)
}
winsorized_mean(x,0)
[1] 81.86667
winsorized_mean(x,0.2)
[1] 69.73333
Visualisasi dan Analisis Nomor 1:
boxplot(x,
main = "Boxplot Data x",
ylab = "Nilai",
col = "green")
Berdasarkan visualisasi boxplot, terlihat bahwa nilai 310 merupakan outlier. Outlier tersebut dapat mempengaruhi rata-rata, maka dari itu digunakan winsorized mean. Winsorized mean disini digunakan untuk mengurangi pengaruh outlier dengan menggantikan nilai-nilai ekstrem dalam dataset. Jika kita memilih alpha = 0.2, maka kita akan mengganti 20% dari data terendah dan tertinggi.
Jawaban nomor 2:
weighted_corr <- function(X, w) {
n <- nrow(X)
n_w <- 0
for(i in 1:n){
n_w <- n_w + w[i]
}
W <- diag(w)
matrix_satu <- matrix(1, n, 1)
x_bar_w <- (1/n_w) * t(X) %*% W %*% matrix_satu
D <- X - matrix_satu %*% t(x_bar_w)
S_w <- (1/n_w) * t(D) %*% W %*% D
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
))
}
hasil <- weighted_corr(X, w)
hasil$W
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
[1,] 14.34 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
[3,] 0.00 0.00 12.49 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
[5,] 0.00 0.00 0.00 0.00 17.45 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
[7,] 0.00 0.00 0.00 0.00 0.00 0.00 34.73 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
[9,] 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 33.13 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
[11,] 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 15.55
[12,] 0.00 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 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
[15,] 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
[17,] 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
[19,] 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
[21,] 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
[23,] 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
[25,] 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
[,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22]
[1,] 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
[2,] 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
[3,] 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
[4,] 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
[5,] 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
[6,] 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
[7,] 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
[8,] 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
[9,] 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
[10,] 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
[11,] 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
[12,] 16.54 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
[13,] 0.00 17.25 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
[14,] 0.00 0.00 14.93 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
[15,] 0.00 0.00 0.00 7.24 0.00 0.0 0.00 0.00 0.00 0.00 0.00
[16,] 0.00 0.00 0.00 0.00 9.85 0.0 0.00 0.00 0.00 0.00 0.00
[17,] 0.00 0.00 0.00 0.00 0.00 11.1 0.00 0.00 0.00 0.00 0.00
[18,] 0.00 0.00 0.00 0.00 0.00 0.0 12.89 0.00 0.00 0.00 0.00
[19,] 0.00 0.00 0.00 0.00 0.00 0.0 0.00 11.14 0.00 0.00 0.00
[20,] 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 7.06 0.00 0.00
[21,] 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 13.96 0.00
[22,] 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 23.13
[23,] 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
[24,] 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
[25,] 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
[26,] 0.00 0.00 0.00 0.00 0.00 0.0 0.00 0.00 0.00 0.00 0.00
[,23] [,24] [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33]
[1,] 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 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 0.00
[4,] 0.00 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 0.00
[6,] 0.00 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 0.00
[8,] 0.00 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 0.00
[10,] 0.00 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 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
[13,] 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
[15,] 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
[17,] 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
[19,] 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
[21,] 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
[23,] 19.74 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
[24,] 0.00 17.53 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
[25,] 0.00 0.00 12.56 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
[26,] 0.00 0.00 0.00 13.01 0.00 0.00 0.00 0.00 0.00 0.00 0.00
[,34] [,35] [,36] [,37] [,38]
[1,] 0.00 0.0 0.00 0.00 0.00
[2,] 0.00 0.0 0.00 0.00 0.00
[3,] 0.00 0.0 0.00 0.00 0.00
[4,] 0.00 0.0 0.00 0.00 0.00
[5,] 0.00 0.0 0.00 0.00 0.00
[6,] 0.00 0.0 0.00 0.00 0.00
[7,] 0.00 0.0 0.00 0.00 0.00
[8,] 0.00 0.0 0.00 0.00 0.00
[9,] 0.00 0.0 0.00 0.00 0.00
[10,] 0.00 0.0 0.00 0.00 0.00
[11,] 0.00 0.0 0.00 0.00 0.00
[12,] 0.00 0.0 0.00 0.00 0.00
[13,] 0.00 0.0 0.00 0.00 0.00
[14,] 0.00 0.0 0.00 0.00 0.00
[15,] 0.00 0.0 0.00 0.00 0.00
[16,] 0.00 0.0 0.00 0.00 0.00
[17,] 0.00 0.0 0.00 0.00 0.00
[18,] 0.00 0.0 0.00 0.00 0.00
[19,] 0.00 0.0 0.00 0.00 0.00
[20,] 0.00 0.0 0.00 0.00 0.00
[21,] 0.00 0.0 0.00 0.00 0.00
[22,] 0.00 0.0 0.00 0.00 0.00
[23,] 0.00 0.0 0.00 0.00 0.00
[24,] 0.00 0.0 0.00 0.00 0.00
[25,] 0.00 0.0 0.00 0.00 0.00
[26,] 0.00 0.0 0.00 0.00 0.00
[ reached 'max' / getOption("max.print") -- omitted 12 rows ]
hasil$x_bar_w
[,1]
x1 73.88530
x2 65.39059
x3 17.00938
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
hasil$s_w
x1 x2 x3
6.177671 6.411527 4.598649
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
Visualisasi dan Analisis nomor 2:
plot(X[,1], X[,2],
main = "Scatter Plot x1 vs x2",
xlab = "x1",
ylab = "x2",
pch = 19)
plot(X[,1], X[,3],
main = "Scatter Plot x1 vs x3",
xlab = "x1",
ylab = "x3",
pch = 19)
plot(X[,2], X[,3],
main = "Scatter Plot x2 vs x3",
xlab = "x2",
ylab = "x3",
pch = 19)
Dari scatter plot dapat diamati pola penyebaran titik data antar variabel. Jika titik-titik membentuk pola naik, maka menunjukkan korelasi positif antara kedua variabel. Jika nilai x1 naik maka nilai x2 juga naik, jika nilai x1 turun maka x2 juga turun. Begitu pula dengan x2 dengan x3 serta x1 dan x3.
Jika titik-titik memebentuk pola yang menurun, maka menunjukkan korelasi negatif. Jika nilai x1 naik maka nilai x2 akan turun, jika nilai x2 naik maka nilai x1 akan turun. Begitu pula dengan x2 dengan x3 serta x1 dan x3.