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_sorted[1:k] <- x_sorted[k+1]
x_sorted[(n-k+1):n] <- x_sorted[n-k]
return(mean(x_sorted))
}
mean_x <- mean(x)
winsor_mean <- winsorized_mean(x, 0.2)
mean_x
## [1] 81.86667
winsor_mean
## [1] 69.73333
winsorized_data <- function(x, alpha){
n <- length(x)
k <- floor(n * alpha)
x_sorted <- sort(x)
x_sorted[1:k] <- x_sorted[k+1]
x_sorted[(n-k+1):n] <- x_sorted[n-k]
return(x_sorted)
}
x_win <- winsorized_data(x, 0.2)
hist(x,
col="skyblue",
main="Histogram Output Produksi Mesin",
xlab="Output Produksi",
ylab="Frekuensi")

boxplot(x,
col="orange",
main="Boxplot Output Produksi Mesin")

boxplot(x, x_win,
col=c("orange","lightgreen"),
names=c("Asli","Winsorized"),
main="Perbandingan Data Asli vs Winsorized")

data <- read.csv("C:/Users/Vicky/OneDrive/Dokumen/data_quiz1.csv")
head(data)
## kota x1 x2 x3 w
## 1 Kab. Pacitan 86 55 10.2 14.34
## 2 Kab. Ponorogo 78 64 15.8 14.19
## 3 Kab. Trenggalek 83 58 11.5 12.49
## 4 Kab. Tulungagung 75 68 17.3 11.45
## 5 Kab. Blitar 80 60 13.6 17.45
## 6 Kab. Kediri 74 66 16.8 15.24
X <- data[, c("x1","x2","x3")]
w <- data$w
weighted_corr <- function(X, w){
X <- as.matrix(X)
n <- nrow(X)
W <- diag(w)
nw <- sum(w)
one <- matrix(1, n, 1)
mean_w <- (1/nw) * t(X) %*% W %*% one
D <- X - matrix(one %*% t(mean_w), n, ncol(X))
Sw <- (1/nw) * t(D) %*% W %*% D
sw <- sqrt(diag(Sw))
V <- diag(sw)
Rw <- solve(V) %*% Sw %*% solve(V)
return(list(
mean_w = mean_w,
Sw = Sw,
sw = sw,
Rw = Rw
))
}
hasil <- weighted_corr(X, w)
hasil$mean_w
## [,1]
## x1 73.88530
## x2 65.39059
## x3 17.00938
hasil$Sw
## 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$sw
## x1 x2 x3
## 6.177671 6.411527 4.598649
hasil$Rw
## [,1] [,2] [,3]
## [1,] 1.0000000 -0.9531095 -0.9558207
## [2,] -0.9531095 1.0000000 0.9891979
## [3,] -0.9558207 0.9891979 1.0000000