library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.4.3
library(knitr)
## Warning: package 'knitr' was built under R version 4.4.3
set.seed(123)
#parameter
beta0 <- -1
beta1 <- 2
beta2 <- 0.5
true_param<- c(beta0, beta1, beta2)
# Fungsi Simulasi
simulasi_logistik <- function(
    n,
    rho,
    n_rep = 100){

  hasil <- matrix(NA, nrow = n_rep, ncol = 3)

  colnames(hasil) <- c(
    "Intercept",
    "X1",
    "X2")
  
for(i in 1:n_rep){
    X1 <- rnorm(n)
    e <- rnorm(n)
    X2 <- rho*X1 + sqrt(1-rho^2)*e
    
# Linear predictor
    g <- beta0 + beta1*X1 + beta2*X2
    p <- exp(g)/(1+exp(g))
# Response    
    Y <- rbinom(n, size = 1, prob = p)
    
# Model dengan tryCatch untuk menangani error
     model <- tryCatch( 
       glm(Y ~ X1 + X2, 
           family = binomial), 
       error = function(e) NULL) 
     if(!is.null(model)){ 
       if(length(coef(model)) == 3){ 
         hasil[i, ] <- coef(model) 
       } 
     } 
} 
hasil <- as.data.frame(hasil) 
return(hasil) 
}
desain <- expand.grid(
  n = c(30,100,500),
  rho = c(0,0.7,0.9,0.99)
)

desain
##      n  rho
## 1   30 0.00
## 2  100 0.00
## 3  500 0.00
## 4   30 0.70
## 5  100 0.70
## 6  500 0.70
## 7   30 0.90
## 8  100 0.90
## 9  500 0.90
## 10  30 0.99
## 11 100 0.99
## 12 500 0.99
hasil_semua <- data.frame()

n <- c(30,100,500)
rho <- c(0,0.7,0.9,0.99)

for(n_now in n){
  for(rho_now in rho){
  
  temp_df <- simulasi_logistik(
    n = n_now,
    rho = rho_now,
    n_rep = 100)
  
  temp_df$Replikasi <- 1:nrow(temp_df) 
  temp_df$n <- n_now 
  temp_df$rho <- rho_now

  hasil_semua <- rbind(
    hasil_semua,
    temp_df)
  }
}
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
hasil_long <- hasil_semua %>%
  pivot_longer(
    cols = c("Intercept","X1","X2"),
    names_to = "Parameter",
    values_to = "Estimasi"
  )
head(hasil_long)
## # A tibble: 6 × 5
##   Replikasi     n   rho Parameter Estimasi
##       <int> <dbl> <dbl> <chr>        <dbl>
## 1         1    30     0 Intercept   -1.97 
## 2         1    30     0 X1           2.26 
## 3         1    30     0 X2          -0.308
## 4         2    30     0 Intercept   -1.12 
## 5         2    30     0 X1           2.51 
## 6         2    30     0 X2           1.07
bias_hasil <- hasil_long %>%
  group_by(n,rho,Parameter) %>%
  summarise(
    Mean_Estimasi = mean(Estimasi, na.rm=TRUE),
    SD = sd(Estimasi, na.rm=TRUE),
    .groups = "drop"
  ) %>%
  mutate(
    True = case_when(
      Parameter == "Intercept" ~ beta0,
      Parameter == "X1" ~ beta1,
      Parameter == "X2" ~ beta2
    ),
    Bias = Mean_Estimasi - True,
    MSE = Bias^2 + SD^2
  )
kable(bias_hasil, digits = 4, caption = "Statistik Estimasi Parameter")
Statistik Estimasi Parameter
n rho Parameter Mean_Estimasi SD True Bias MSE
30 0.00 Intercept -1.3480 0.8743 -1.0 -0.3480 0.8855
30 0.00 X1 2.4890 1.0462 2.0 0.4890 1.3336
30 0.00 X2 0.6027 0.8775 0.5 0.1027 0.7805
30 0.70 Intercept -8.7149 76.8823 -1.0 -7.7149 5970.4114
30 0.70 X1 27.9473 207.1061 2.0 25.9473 43566.2005
30 0.70 X2 2.3595 36.3681 0.5 1.8595 1326.0942
30 0.90 Intercept -4.6279 20.0836 -1.0 -3.6279 416.5133
30 0.90 X1 5.1264 15.7733 2.0 3.1264 258.5721
30 0.90 X2 4.7651 22.4118 0.5 4.2651 520.4798
30 0.99 Intercept -2.3984 5.9385 -1.0 -1.3984 37.2212
30 0.99 X1 8.5021 45.3061 2.0 6.5021 2094.9235
30 0.99 X2 -2.0732 27.7276 0.5 -2.5732 775.4439
100 0.00 Intercept -0.9876 0.3292 -1.0 0.0124 0.1085
100 0.00 X1 2.1314 0.5069 2.0 0.1314 0.2742
100 0.00 X2 0.5520 0.3227 0.5 0.0520 0.1068
100 0.70 Intercept -1.0686 0.3212 -1.0 -0.0686 0.1079
100 0.70 X1 2.2203 0.5973 2.0 0.2203 0.4053
100 0.70 X2 0.4563 0.4923 0.5 -0.0437 0.2443
100 0.90 Intercept -1.0714 0.3238 -1.0 -0.0714 0.1100
100 0.90 X1 2.1555 0.7584 2.0 0.1555 0.5994
100 0.90 X2 0.5547 0.8054 0.5 0.0547 0.6516
100 0.99 Intercept -1.0605 0.3410 -1.0 -0.0605 0.1200
100 0.99 X1 2.2477 2.2187 2.0 0.2477 4.9840
100 0.99 X2 0.3842 2.1952 0.5 -0.1158 4.8322
500 0.00 Intercept -1.0255 0.1522 -1.0 -0.0255 0.0238
500 0.00 X1 2.0258 0.1973 2.0 0.0258 0.0396
500 0.00 X2 0.4959 0.1229 0.5 -0.0041 0.0151
500 0.70 Intercept -1.0069 0.1362 -1.0 -0.0069 0.0186
500 0.70 X1 2.0282 0.2496 2.0 0.0282 0.0631
500 0.70 X2 0.5165 0.1763 0.5 0.0165 0.0313
500 0.90 Intercept -1.0204 0.1544 -1.0 -0.0204 0.0243
500 0.90 X1 2.0310 0.3291 2.0 0.0310 0.1093
500 0.90 X2 0.5333 0.2954 0.5 0.0333 0.0884
500 0.99 Intercept -1.0158 0.1320 -1.0 -0.0158 0.0177
500 0.99 X1 1.8706 0.9161 2.0 -0.1294 0.8561
500 0.99 X2 0.6593 0.8736 0.5 0.1593 0.7885
hasil_long <- hasil_long %>%
  mutate(
    True = case_when(
      Parameter=="Intercept" ~ -1,
      Parameter=="X1" ~ 2,
      Parameter=="X2" ~ 0.5
    ),
    Error = Estimasi - True
  )

anova_interaksi <- aov(
  abs(Error) ~ factor(n) * factor(rho),
  data = hasil_long
)

summary(anova_interaksi)
##                         Df  Sum Sq Mean Sq F value   Pr(>F)    
## factor(n)                2   30366   15183  10.157 3.99e-05 ***
## factor(rho)              3    9630    3210   2.147   0.0922 .  
## factor(n):factor(rho)    6   18584    3097   2.072   0.0533 .  
## Residuals             3588 5363523    1495                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Ranking skenario
ranking_skenario <- bias_hasil %>%
  mutate(
    Skor = abs(Bias) + SD
  ) %>%
  arrange(Skor)

head(ranking_skenario)
## # A tibble: 6 × 9
##       n   rho Parameter Mean_Estimasi    SD  True     Bias    MSE  Skor
##   <dbl> <dbl> <chr>             <dbl> <dbl> <dbl>    <dbl>  <dbl> <dbl>
## 1   500  0    X2                0.496 0.123   0.5 -0.00410 0.0151 0.127
## 2   500  0.7  Intercept        -1.01  0.136  -1   -0.00690 0.0186 0.143
## 3   500  0.99 Intercept        -1.02  0.132  -1   -0.0158  0.0177 0.148
## 4   500  0.9  Intercept        -1.02  0.154  -1   -0.0204  0.0243 0.175
## 5   500  0    Intercept        -1.03  0.152  -1   -0.0255  0.0238 0.178
## 6   500  0.7  X2                0.516 0.176   0.5  0.0165  0.0313 0.193
tail(ranking_skenario)
## # A tibble: 6 × 9
##       n   rho Parameter Mean_Estimasi    SD  True  Bias    MSE  Skor
##   <dbl> <dbl> <chr>             <dbl> <dbl> <dbl> <dbl>  <dbl> <dbl>
## 1    30  0.9  X2                 4.77  22.4   0.5  4.27   520.  26.7
## 2    30  0.99 X2                -2.07  27.7   0.5 -2.57   775.  30.3
## 3    30  0.7  X2                 2.36  36.4   0.5  1.86  1326.  38.2
## 4    30  0.99 X1                 8.50  45.3   2    6.50  2095.  51.8
## 5    30  0.7  Intercept         -8.71  76.9  -1   -7.71  5970.  84.6
## 6    30  0.7  X1                27.9  207.    2   25.9  43566. 233.
ggplot(
  bias_hasil,
  aes(
    x = factor(rho),
    y = Bias,
    fill = factor(rho)
  )
)+
  geom_col()+
  facet_grid(Parameter~n)+
  labs(
    title = "Bias Estimasi Parameter",
    x = "Korelasi (rho)",
    y = "Bias"
  )+
  theme_minimal()

ggplot(
  bias_hasil,
  aes(
    x = factor(rho),
    y = SD,
    fill = factor(rho)
  )
)+
  geom_col()+
  facet_grid(Parameter~n)+
  labs(
    title = "Standar Deviasi Estimasi Parameter",
    x = "Korelasi (rho)",
    y = "SD"
  )+
  theme_minimal()

ggplot(
  bias_hasil,
  aes(
    x = rho,
    y = abs(Bias),
    color = factor(n)
  )
)+
  geom_line(linewidth = 1)+
  geom_point(size = 3)+
  facet_wrap(~Parameter,
             scales = "free_y")+
  labs(
    title = "Pengaruh Multikolinearitas terhadap Besarnya Bias",
    x = "Korelasi (rho)",
    y = "|Bias|",
    color = "Ukuran Sampel"
  )+
  theme_minimal()