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 Cmd+Shift+Enter.
#nomor 1
winsorized_mean <- function(x, alpha){
x <- sort(x)
n <- length(x)
k <- floor(n * alpha)
y <- x
if(k > 0){
y[1:k] <- x[k+1]
y[(n-k+1):n] <- x[n-k]
}
return(list(
data_winsor = y,
mean = mean(y)
))
}
x <- c(12,45,52,58,61,63,67,70,72,75,78,82,88,95,310)
mean(x)
## [1] 81.86667
boxplot(x, main="Sebelum Winsorizing")
result <- winsorized_mean(x,0.2)
boxplot(result$data_winsor, main="Setelah Winsorizing")
#nomor 2
library(readxl)
data <- read.csv("~/Documents/komstat/data_quiz1.csv")
head(data)
X <- as.matrix(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)
xw <- (1/nw) * t(X) %*% W %*% one
D <- X - one %*% t(xw)
Sw <- (1/nw) * t(D) %*% W %*% D
sw <- sqrt(diag(Sw))
V <- diag(sw)
Rw <- solve(V) %*% Sw %*% solve(V)
return(list(
W = W,
mean = xw,
cov = Sw,
sd = sw,
corr = Rw
))
}
hasil <- weighted_corr(X,w)
hasil$mean
## [,1]
## x1 73.88530
## x2 65.39059
## x3 17.00938
hasil$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
hasil$sd
## x1 x2 x3
## 6.177671 6.411527 4.598649
hasil$corr
## [,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(corrplot)
## corrplot 0.95 loaded
corrplot(hasil$corr, method="circle")
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Cmd+Option+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 Cmd+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.
##interpretasi no 1 Mean biasa (81.87) lebih besar dari winsorized mean (69.73) karena adanya nilai ekstrem yaitu 310. Setelah winsorizing, nilai ekstrem diganti sehingga rata-rata menjadi lebih stabil. ##interpretasi no 2 jika kualitas air meningkat, ruang hijau juga meningkat, tetapi kualitas udara cenderung berlawanan dengan keduanya, karena Kualitas udara (x1) memiliki hubungan negatif yang kuat dengan kualitas air (x2) dan ruang hijau (x3)dan Sedangkan kualitas air (x2) dan ruang hijau (x3) memiliki hubungan positif yang sangat kuat