### 1. Kurva Lonceng Distribusi Normal Standar
# Menentukan rentang nilai x dari -4 sampai 4
x <- seq(-4, 4, length.out = 1000)
# Menghitung densitas normal standar (mean = 0, sd = 1)
y <- dnorm(x, mean = 0, sd = 1)
# Membuat kurva lonceng
plot(x, y, type = "l", col = "#1f77b4", lwd = 2,
main = "Kurva Lonceng Distribusi Normal Standar",
xlab = "Nilai Z", ylab = "Densitas",
panel.first = grid())
# Menambahkan warna arsir di bawah kurva
polygon(c(-4, x, 4), c(0, y, 0), col = rgb(0.12, 0.47, 0.71, alpha = 0.3), border = NA)

### 2. Distribusi Normal dengan Parameter Spesifik ($\mu = 10, \sigma = 2$)
# Parameter
mu <- 10
sigma <- 2
# Rentang nilai x dari 2 sampai 18 (±4 SD dari rata-rata)
x <- seq(mu - 4 * sigma, mu + 4 * sigma, length.out = 1000)
y <- dnorm(x, mean = mu, sd = sigma)
# Plot kurva lonceng
plot(x, y, type = "l", col = "#1f77b4", lwd = 2,
main = expression(paste("Distribusi Normal (", mu, " = 10, ", sigma, " = 2)")),
xlab = "Nilai", ylab = "Densitas",
panel.first = grid())
# Arsir bagian bawah kurva
polygon(c(min(x), x, max(x)), c(0, y, 0), col = rgb(0.12, 0.47, 0.71, alpha = 0.3), border = NA)

### 3. Perbandingan Distribusi Normal (Base R)
# Rentang nilai x
x <- seq(2, 22, length.out = 1000)
# Plot kurva utama (mu = 10, sigma = 2)
plot(x, dnorm(x, mean = 10, sd = 2), type = "l", col = "#1f77b4", lwd = 2,
ylim = c(0, 0.45), main = "Perbandingan Distribusi Normal",
xlab = "Nilai", ylab = "Densitas", panel.first = grid())
# Tambah kurva kedua (mu = 10, sigma = 1) -> lebih lancip/tinggi
lines(x, dnorm(x, mean = 10, sd = 1), col = "#d95f02", lwd = 2)
# Tambah kurva ketiga (mu = 14, sigma = 2) -> bergeser ke kanan
lines(x, dnorm(x, mean = 14, sd = 2), col = "#2ca02c", lwd = 2)
# Legend/Petunjuk Warna
legend("topright",
legend = c("mu = 10, sigma = 2", "mu = 10, sigma = 1", "mu = 14, sigma = 2"),
col = c("#1f77b4", "#d95f02", "#2ca02c"), lwd = 2, bty = "n")

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.5.3
### 4. Simulasi Distribusi Normal (Versi Dipisah)
# Menyiapkan data parameter untuk df_all terlebih dahulu
params <- data.frame(
label = c(
"Kurva 1 (mu=0, sigma=1)",
"Kurva 2 (mu=2, sigma=1)",
"Kurva 3 (mu=5, sigma=5)",
"Kurva 4 (mu=10, sigma=3)",
"Kurva 5 (mu=1.5, sigma=0.1)"
),
mu = c(0, 2, 5, 10, 1.5),
sigma = c(1, 1, 5, 3, 0.1)
)
df_list <- list()
for (i in 1:nrow(params)) {
m <- params$mu[i]
s <- params$sigma[i]
x_vals <- seq(m - 4 * s, m + 4 * s, length.out = 500)
df_list[[i]] <- data.frame(
Kurva = params$label[i],
x = x_vals,
y = dnorm(x_vals, mean = m, sd = s)
)
}
df_all <- do.call(rbind, df_list)
# Plot versi dipisah
ggplot(df_all, aes(x = x, y = y, fill = Kurva, color = Kurva)) +
geom_area(alpha = 0.4) +
geom_line(linewidth = 1) +
facet_wrap(~ Kurva, scales = "free", ncol = 3) +
labs(
title = "Simulasi Distribusi Normal (Versi Dipisah)",
x = "Nilai X",
y = "Densitas"
) +
theme_minimal() +
theme(
legend.position = "none",
strip.text = element_text(face = "bold", size = 10)
)

### 5. Simulasi Distribusi Normal (Versi Digabung)
# 1. Buat rentang nilai X yang seragam untuk semua kurva
x_range <- seq(-12, 22, length.out = 2000)
# 2. Susun data frame secara langsung
df_gabung <- data.frame(
x = rep(x_range, 5),
y = c(
dnorm(x_range, mean = 0, sd = 1),
dnorm(x_range, mean = 2, sd = 1),
dnorm(x_range, mean = 5, sd = 5),
dnorm(x_range, mean = 10, sd = 3),
dnorm(x_range, mean = 1.5, sd = 0.1)
),
Kurva = factor(rep(c(
"Kurva 1 (mu=0, sigma=1)",
"Kurva 2 (mu=2, sigma=1)",
"Kurva 3 (mu=5, sigma=5)",
"Kurva 4 (mu=10, sigma=3)",
"Kurva 5 (mu=1.5, sigma=0.1)"
), each = length(x_range)))
)
# 3. Plot grafik gabungan
ggplot(df_gabung, aes(x = x, y = y, color = Kurva)) +
geom_line(linewidth = 1) +
coord_cartesian(ylim = c(0, 0.5)) +
labs(
title = "Simulasi Distribusi Normal (Versi Digabung)",
subtitle = "Catatan: Sumbu Y dipotong pada 0.5 karena Kurva 5 puncaknya mencapai 4.0",
x = "Nilai X",
y = "Densitas",
color = "Parameter"
) +
theme_minimal() +
theme(legend.position = "bottom")

### 6. Empat Distribusi Utama (Panel 2x2 - Base R)
par(mfrow = c(2, 2), mar = c(4, 4, 3, 1))
# 1. Normal
x_norm <- seq(-4, 4, length.out = 500)
y_norm <- dnorm(x_norm, mean = 0, sd = 1)
plot(x_norm, y_norm, type = "l", col = "#1f77b4", lwd = 2,
main = "Distribusi Normal (mu=0, sd=1)", xlab = "x", ylab = "Densitas", panel.first = grid())
polygon(c(-4, x_norm, 4), c(0, y_norm, 0), col = rgb(0.12, 0.47, 0.71, 0.3), border = NA)
# 2. t-Student
x_t <- seq(-4, 4, length.out = 500)
y_t <- dt(x_t, df = 5)
plot(x_t, y_t, type = "l", col = "#ff7f0e", lwd = 2,
main = "Distribusi t-Student (df=5)", xlab = "x", ylab = "Densitas", panel.first = grid())
polygon(c(-4, x_t, 4), c(0, y_t, 0), col = rgb(1, 0.5, 0.05, 0.3), border = NA)
# 3. Chi-Square
x_chisq <- seq(0, 15, length.out = 500)
y_chisq <- dchisq(x_chisq, df = 5)
plot(x_chisq, y_chisq, type = "l", col = "#2ca02c", lwd = 2,
main = "Distribusi Chi-Square (df=5)", xlab = "x", ylab = "Densitas", panel.first = grid())
polygon(c(0, x_chisq, 15), c(0, y_chisq, 0), col = rgb(0.17, 0.63, 0.17, 0.3), border = NA)
# 4. F
x_f <- seq(0, 5, length.out = 500)
y_f <- df(x_f, df1 = 5, df2 = 10)
plot(x_f, y_f, type = "l", col = "#d62728", lwd = 2,
main = "Distribusi F (df1=5, df2=10)", xlab = "x", ylab = "Densitas", panel.first = grid())
polygon(c(0, x_f, 5), c(0, y_f, 0), col = rgb(0.84, 0.15, 0.16, 0.3), border = NA)

par(mfrow = c(1, 1))
### 7. Perbandingan 4 Distribusi Utama dalam 1 Grafik Overlay (`ggplot2`)
x_seq <- seq(-4, 14, length.out = 1000)
df_gabung_4 <- data.frame(
x = rep(x_seq, 4),
y = c(
dnorm(x_seq, mean = 0, sd = 1),
dt(x_seq, df = 5),
dchisq(x_seq, df = 5),
df(x_seq, df1 = 5, df2 = 10)
),
Distribusi = factor(rep(c(
"Normal (mu=0, sd=1)",
"t-Student (df=5)",
"Chi-Square (df=5)",
"F (df1=5, df2=10)"
), each = length(x_seq)))
)
ggplot(df_gabung_4, aes(x = x, y = y, color = Distribusi)) +
geom_line(linewidth = 1.1) +
coord_cartesian(xlim = c(-4, 12), ylim = c(0, 0.7)) +
labs(
title = "Perbandingan Distribusi Normal, t, Chi-Square, dan F",
x = "Nilai X",
y = "Densitas",
color = "Jenis Distribusi"
) +
scale_color_manual(values = c(
"Normal (mu=0, sd=1)" = "#1f77b4",
"t-Student (df=5)" = "#ff7f0e",
"Chi-Square (df=5)" = "#2ca02c",
"F (df1=5, df2=10)" = "#d62728"
)) +
theme_minimal() +
theme(legend.position = "bottom")

### 8. Dashboard Simulasi 10 Distribusi Probabilitas Kontinu
set.seed(123)
n_samples <- 5000
par(mfrow = c(2, 5), mar = c(3.5, 3.5, 2.5, 1), oma = c(0, 0, 3, 0))
plot_sim <- function(data, density_fn, title, col_bg = "#a8dadc", col_line = "#e63946") {
hist(data, probability = TRUE, main = title, xlab = "", ylab = "",
col = col_bg, border = "white", breaks = 35)
x_seq <- seq(min(data), max(data), length.out = 300)
lines(x_seq, density_fn(x_seq), col = col_line, lwd = 2)
grid()
}
# 1. t-Student
r_t <- rt(n_samples, df = 5)
plot_sim(r_t, function(x) dt(x, df = 5), "1. t-Student (df=5)")
# 2. F
r_f <- rf(n_samples, df1 = 5, df2 = 10)
plot_sim(r_f, function(x) df(x, df1 = 5, df2 = 10), "2. F (df1=5, df2=10)")
# 3. Chi-Square
r_chisq <- rchisq(n_samples, df = 4)
plot_sim(r_chisq, function(x) dchisq(x, df = 4), "3. Chi-Square (df=4)")
# 4. Gamma
r_gamma <- rgamma(n_samples, shape = 2, rate = 1)
plot_sim(r_gamma, function(x) dgamma(x, shape = 2, rate = 1), "4. Gamma (k=2, r=1)")
# 5. Weibull
r_weibull <- rweibull(n_samples, shape = 2, scale = 1)
plot_sim(r_weibull, function(x) dweibull(x, shape = 2, scale = 1), "5. Weibull (k=2, l=1)")
# 6. Eksponensial
r_exp <- rexp(n_samples, rate = 1)
plot_sim(r_exp, function(x) dexp(x, rate = 1), "6. Eksponensial (r=1)")
# 7. Pareto
r_pareto <- 1 / (1 - runif(n_samples))^(1/3)
d_pareto <- function(x, xm = 1, alpha = 3) ifelse(x >= xm, (alpha * (xm^alpha)) / (x^(alpha + 1)), 0)
plot_sim(r_pareto[r_pareto <= 4], function(x) d_pareto(x, 1, 3), "7. Pareto (xm=1, a=3)")
# 8. Uniform
r_unif <- runif(n_samples, min = 0, max = 1)
plot_sim(r_unif, function(x) dunif(x, min = 0, max = 1), "8. Uniform (0, 1)")
# 9. Beta
r_beta <- rbeta(n_samples, shape1 = 2, shape2 = 5)
plot_sim(r_beta, function(x) dbeta(x, shape1 = 2, shape2 = 5), "9. Beta (a=2, b=5)")
# 10. Inverse-Gaussian
d_invgauss <- function(x, mu = 1, lambda = 1) {
ifelse(x > 0, sqrt(lambda / (2 * pi * x^3)) * exp(-lambda * (x - mu)^2 / (2 * mu^2 * x)), 0)
}
rinvgauss_custom <- function(n, mu = 1, lambda = 1) {
y <- rnorm(n)^2
x <- mu + (mu^2 * y)/(2*lambda) - (mu/(2*lambda))*sqrt(4*mu*lambda*y + mu^2 * y^2)
u <- runif(n)
ifelse(u <= mu / (mu + x), x, mu^2 / x)
}
r_invg <- rinvgauss_custom(n_samples, 1, 1)
plot_sim(r_invg[r_invg <= 4], function(x) d_invgauss(x, 1, 1), "10. Inv-Gaussian (u=1, l=1)")
mtext("Simulasi 10 Distribusi Probabilitas Kontinu (Histogram + Kurva Densitas)",
outer = TRUE, cex = 1.2, font = 2)

par(mfrow = c(1, 1))