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) {
n<-length(x)
k<-floor(n*alpha)
hasil<-0
for(i in 1:n){
if(i<=k){
w=k+1
}
else if(i>k&&i<=(n-k)){
w=i
}
else{
w=n-k
}
hasil<-hasil+x[w]
}
return(hasil/n)
}
# --- [b] Hitung ordinary mean (alpha=0) dan Winsorized mean 20% (alpha=0.2) ---
winsorized_mean(x,0)
## [1] 81.86667
winsorized_mean(x,0.2)
## [1] 69.73333
alpha_seq <- c(0, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40)
winsor_values <- c()
for(a in alpha_seq){
hasil <- winsorized_mean(x, a)
winsor_values <- c(winsor_values, hasil)
}
df_alpha <- data.frame(
alpha = alpha_seq,
winsor_mean = winsor_values
)
library(ggplot2)
ggplot(df_alpha, aes(x = alpha, y = winsor_mean)) +
geom_line(color = "pink", size = 1) +
geom_point(color="red",size = 2) +
labs(title = "Pengaruh Alpha terhadap Winsorized Mean",
x = "Alpha",
y = "Winsorized Mean") +
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

# --- Baca data CSV ---
df <- read.csv("C:/Users/Nasywa/Documents/SEMESTER 2/Komputasi Stat/Quiz 1 Komstat/data_quiz1.csv")
X <- as.matrix(df[, c("x1", "x2", "x3")])
w <- df$w
# --- [a] Buatlah fungsi weighted_corr(X, w) ---
weighted_corr <- function(X, w) {
X<-as.matrix(X)
n<-nrow(X)
p<-ncol(X)
W<-diag(w)
nw<-sum(w)
xbar<-(t(X)%*%w)/nw
D <- as.matrix(X) - matrix(1, nrow(X), 1)%*%t(xbar)
Sw<-(t(D)%*%W%*%D)/nw
sw<-sqrt(diag(Sw))
V<-diag(sw)
Rw<-solve(V)%*%Sw%*%solve(V)
cor=Rw
return(list(
mean = xbar,
cov = Sw,
sd = sw,
cor = Rw
))
}
weighted_corr(X, w)
## $mean
## [,1]
## x1 73.88530
## x2 65.39059
## x3 17.00938
##
## $cov
## x1 x2 x3
## x1 38.16362 -37.75105 -27.15386
## x2 -37.75105 41.10767 29.16587
## x3 -27.15386 29.16587 21.14757
##
## $sd
## x1 x2 x3
## 6.177671 6.411527 4.598649
##
## $cor
## [,1] [,2] [,3]
## [1,] 1.0000000 -0.9531095 -0.9558207
## [2,] -0.9531095 1.0000000 0.9891979
## [3,] -0.9558207 0.9891979 1.0000000
library(ggplot2)
library(reshape2)
hasil <- weighted_corr(X, w)
Rw <- hasil$cor
df_cor <- as.data.frame(as.table(Rw))
ggplot(df_cor, aes(Var1, Var2, fill = Freq)) +
geom_tile() + geom_text(aes(label = round(Freq,2))) +
scale_fill_gradient2(low="#A6CEE3", mid="#F7F7F7", high="#FB9A99") +
labs(title="Weighted Correlation Matrix") +
theme_minimal()
