# ==============================================================================
# VISUALISASI DISTRIBUSI PELUANG KONTINU (TAMPILAN BESAR PER GRAFIK)
# ==============================================================================

# ------------------------------------------------------------------------------
# 1. PERSIAPAN PACKAGE & UTILITIES
# ------------------------------------------------------------------------------
if (!require("VGAM", quietly = TRUE)) install.packages("VGAM")
## Warning: package 'VGAM' was built under R version 4.5.3
if (!require("statmod", quietly = TRUE)) install.packages("statmod")
## Warning: package 'statmod' was built under R version 4.5.3
library(VGAM)
library(statmod)

# Pastikan layout grafik 1x1 (satu grafik penuh per layar)
par(mfrow = c(1, 1), mar = c(5, 5, 4, 2))

# ------------------------------------------------------------------------------
# 2. PLOT DISTRIBUSI KONTINU SATU PER SATU (UKURAN BESAR)
# ------------------------------------------------------------------------------

# --- 1. DISTRIBUSI NORMAL ---
mu_vals <- c(0, 2, 5, 10, 1.5)
sd_vals <- c(1, 1, 5, 3, 0.5)
colors_n <- c("blue", "red", "green3", "purple", "orange")
x_norm <- seq(-15, 25, length.out = 500)

plot(x_norm, dnorm(x_norm, mean = mu_vals[1], sd = sd_vals[1]), type = "l", 
     col = colors_n[1], lwd = 2, ylim = c(0, 0.8),
     main = "1. Distribusi Normal", xlab = "Nilai x", ylab = "f(x) - Densitas",
     cex.main = 1.3, cex.lab = 1.1)
for(i in 2:5) {
  lines(x_norm, dnorm(x_norm, mean = mu_vals[i], sd = sd_vals[i]), col = colors_n[i], lwd = 2)
}
grid()
legend("topright", legend = paste0("μ = ", mu_vals, ", σ = ", sd_vals), 
       col = colors_n, lty = 1, lwd = 2, cex = 0.9, bty = "n")

# --- 2. DISTRIBUSI t-STUDENT ---
x_t <- seq(-4, 4, length.out = 500)
plot(x_t, dt(x_t, df = 1), type = "l", col = "red", lwd = 2, ylim = c(0, 0.45),
     main = "2. Distribusi Student's t", xlab = "Nilai x", ylab = "f(x) - Densitas",
     cex.main = 1.3, cex.lab = 1.1)
lines(x_t, dt(x_t, df = 2), col = "green3", lwd = 2)
lines(x_t, dt(x_t, df = 5), col = "blue", lwd = 2)
lines(x_t, dt(x_t, df = 30), col = "black", lwd = 2)
grid()
legend("topright", legend = c("df = 1", "df = 2", "df = 5", "df = 30"), 
       col = c("red", "green3", "blue", "black"), lty = 1, lwd = 2, cex = 0.9, bty = "n")

# --- 3. DISTRIBUSI F ---
x_f <- seq(0.01, 5, length.out = 500)
plot(x_f, df(x_f, df1 = 1, df2 = 10), type = "l", col = "red", lwd = 2, ylim = c(0, 0.8),
     main = "3. Distribusi F", xlab = "Nilai x", ylab = "f(x) - Densitas",
     cex.main = 1.3, cex.lab = 1.1)
lines(x_f, df(x_f, df1 = 2, df2 = 10), col = "green3", lwd = 2)
lines(x_f, df(x_f, df1 = 5, df2 = 20), col = "blue", lwd = 2)
grid()
legend("topright", legend = c("df1 = 1, df2 = 10", "df1 = 2, df2 = 10", "df1 = 5, df2 = 20"), 
       col = c("red", "green3", "blue"), lty = 1, lwd = 2, cex = 0.9, bty = "n")

# --- 4. DISTRIBUSI CHI-SQUARE ---
x_chisq <- seq(0.01, 15, length.out = 500)
plot(x_chisq, dchisq(x_chisq, df = 1), type = "l", col = "red", lwd = 2, ylim = c(0, 0.5),
     main = "4. Distribusi Chi-Square", xlab = "Nilai x", ylab = "f(x) - Densitas",
     cex.main = 1.3, cex.lab = 1.1)
lines(x_chisq, dchisq(x_chisq, df = 2), col = "green3", lwd = 2)
lines(x_chisq, dchisq(x_chisq, df = 3), col = "blue", lwd = 2)
lines(x_chisq, dchisq(x_chisq, df = 5), col = "purple", lwd = 2)
grid()
legend("topright", legend = c("df = 1", "df = 2", "df = 3", "df = 5"), 
       col = c("red", "green3", "blue", "purple"), lty = 1, lwd = 2, cex = 0.9, bty = "n")

# --- 5. DISTRIBUSI GAMMA ---
x_gam <- seq(0.01, 15, length.out = 500)
plot(x_gam, dgamma(x_gam, shape = 1, rate = 1), type = "l", col = "red", lwd = 2, ylim = c(0, 1),
     main = "5. Distribusi Gamma", xlab = "Nilai x", ylab = "f(x) - Densitas",
     cex.main = 1.3, cex.lab = 1.1)
lines(x_gam, dgamma(x_gam, shape = 2, rate = 1), col = "green3", lwd = 2)
lines(x_gam, dgamma(x_gam, shape = 3, rate = 1), col = "blue", lwd = 2)
grid()
legend("topright", legend = c("α = 1, β = 1", "α = 2, β = 1", "α = 3, β = 1"), 
       col = c("red", "green3", "blue"), lty = 1, lwd = 2, cex = 0.9, bty = "n")

# --- 6. DISTRIBUSI WEIBULL ---
x_weib <- seq(0.01, 2.5, length.out = 500)
plot(x_weib, dweibull(x_weib, shape = 0.5, scale = 1), type = "l", col = "red", lwd = 2, ylim = c(0, 2.5),
     main = "6. Distribusi Weibull", xlab = "Nilai x", ylab = "f(x) - Densitas",
     cex.main = 1.3, cex.lab = 1.1)
lines(x_weib, dweibull(x_weib, shape = 1, scale = 1), col = "green3", lwd = 2)
lines(x_weib, dweibull(x_weib, shape = 1.5, scale = 1), col = "blue", lwd = 2)
lines(x_weib, dweibull(x_weib, shape = 5, scale = 1), col = "purple", lwd = 2)
grid()
legend("topright", legend = c("k = 0.5", "k = 1.0", "k = 1.5", "k = 5.0"), 
       col = c("red", "green3", "blue", "purple"), lty = 1, lwd = 2, cex = 0.9, bty = "n")

# --- 7. DISTRIBUSI EKSPONENSIAL ---
x_exp <- seq(0, 5, length.out = 500)
plot(x_exp, dexp(x_exp, rate = 0.5), type = "l", col = "red", lwd = 2, ylim = c(0, 2),
     main = "7. Distribusi Eksponensial", xlab = "Nilai x", ylab = "f(x) - Densitas",
     cex.main = 1.3, cex.lab = 1.1)
lines(x_exp, dexp(x_exp, rate = 1), col = "green3", lwd = 2)
lines(x_exp, dexp(x_exp, rate = 2), col = "purple", lwd = 2)
grid()
legend("topright", legend = c("λ = 0.5", "λ = 1.0", "λ = 2.0"), 
       col = c("red", "green3", "purple"), lty = 1, lwd = 2, cex = 0.9, bty = "n")

# --- 8. DISTRIBUSI PARETO ---
x_par <- seq(1, 5, length.out = 500)
plot(x_par, dpareto(x_par, scale = 1, shape = 1), type = "l", col = "red", lwd = 2, ylim = c(0, 3),
     main = "8. Distribusi Pareto", xlab = "Nilai x", ylab = "f(x) - Densitas",
     cex.main = 1.3, cex.lab = 1.1)
lines(x_par, dpareto(x_par, scale = 1, shape = 2), col = "green3", lwd = 2)
lines(x_par, dpareto(x_par, scale = 1, shape = 3), col = "blue", lwd = 2)
grid()
legend("topright", legend = c("α = 1", "α = 2", "α = 3"), 
       col = c("red", "green3", "blue"), lty = 1, lwd = 2, cex = 0.9, bty = "n")

# --- 9. DISTRIBUSI UNIFORM ---
x_unif <- seq(-1, 6, length.out = 500)
plot(x_unif, dunif(x_unif, min = 0, max = 5), type = "l", col = "navy", lwd = 2, ylim = c(0, 0.3),
     main = "9. Distribusi Uniform Kontinu (0,5)", xlab = "Nilai x", ylab = "f(x) - Densitas",
     cex.main = 1.3, cex.lab = 1.1)
grid()
legend("topright", legend = "a = 0, b = 5", col = "navy", lty = 1, lwd = 2, cex = 0.9, bty = "n")

# --- 10. DISTRIBUSI BETA ---
x_beta <- seq(0, 1, length.out = 500)
plot(x_beta, dbeta(x_beta, shape1 = 0.5, shape2 = 0.5), type = "l", col = "red", lwd = 2, ylim = c(0, 2.5),
     main = "10. Distribusi Beta", xlab = "Nilai x", ylab = "f(x) - Densitas",
     cex.main = 1.3, cex.lab = 1.1)
lines(x_beta, dbeta(x_beta, shape1 = 5, shape2 = 1), col = "green3", lwd = 2)
lines(x_beta, dbeta(x_beta, shape1 = 1, shape2 = 3), col = "blue", lwd = 2)
lines(x_beta, dbeta(x_beta, shape1 = 2, shape2 = 2), col = "purple", lwd = 2)
grid()
legend("top", legend = c("α = 0.5, β = 0.5", "α = 5, β = 1", "α = 1, β = 3", "α = 2, β = 2"), 
       col = c("red", "green3", "blue", "purple"), lty = 1, lwd = 2, cex = 0.85, bty = "n")

# --- 11. DISTRIBUSI INVERSE-GAUSSIAN ---
x_ig <- seq(0.01, 3, length.out = 500)
plot(x_ig, dinvgauss(x_ig, mean = 1, dispersion = 0.5), type = "l", col = "red", lwd = 2, ylim = c(0, 1.2),
     main = "11. Distribusi Inverse-Gaussian", xlab = "Nilai x", ylab = "f(x) - Densitas",
     cex.main = 1.3, cex.lab = 1.1)
lines(x_ig, dinvgauss(x_ig, mean = 1, dispersion = 1), col = "green3", lwd = 2)
lines(x_ig, dinvgauss(x_ig, mean = 1, dispersion = 3), col = "blue", lwd = 2)
grid()
legend("topright", legend = c("dispersion = 0.5", "dispersion = 1.0", "dispersion = 3.0"), 
       col = c("red", "green3", "blue"), lty = 1, lwd = 2, cex = 0.9, bty = "n")