#Distribusi Poison
y <- rpois(n = 10, lambda = 4) #tanpa set.set angkah berubah-ubah
print(y)
## [1] 5 3 2 5 3 2 4 2 3 1
set.seed(123) #set.seed untuk mempertahankan angkanya
y <- rpois(n = 10, lambda = 4)
print(y)
## [1] 3 6 3 6 7 1 4 7 4 4
set.seed(1)
y <- rpois(n = 10, lambda = 4)
print(y)
## [1] 3 3 4 7 2 7 7 5 5 1
set.seed(2)
y <- rpois(n = 10, lambda = 4)
print(y)
## [1] 2 5 4 2 7 7 2 6 4 4
set.seed(3)
y <- rpois(n = 10, lambda = 4)
print(y)
## [1] 2 6 3 3 4 4 2 3 4 5
set.seed(0)
y <- rpois(n = 10, lambda = 4)
print(y)
## [1] 7 3 3 4 7 2 7 7 5 5
set.seed(0123)
y <- rpois(n = 10, lambda = 4)
print(y)
## [1] 3 6 3 6 7 1 4 7 4 4
set.seed(12)
y <- rpois(n = 10, lambda = 4)
print(y)
## [1] 1 6 7 3 2 1 2 5 1 0
set.seed(23)
y <- rpois(n = 10, lambda = 4)
print(y)
## [1] 4 2 3 5 6 3 8 8 6 10
set.seed(13)
y <- rpois(n = 10, lambda = 4)
print(y)
## [1] 5 3 3 1 8 0 4 5 6 1
z <- rbinom(n = 10, size = 1, prob = 0.7)
z
## [1] 1 0 0 1 1 1 1 1 0 1
z <- rbinom(n = 10, size = 1, prob = 0.5)
z
## [1] 0 1 1 1 0 1 0 0 0 1
#Grafik Distribusi Poison
#Diagram Batang
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.6.1
# Membuat data frame
df_poisson <- data.frame(
k = factor(0:12),
probabilitas = dpois(0:12, lambda = 4)
)
# Membuat diagram batang
ggplot(df_poisson, aes(x = k, y = probabilitas)) +
geom_col(fill = "#2b5c8f", color = "black", width = 0.6) +
geom_text(aes(label = sprintf("%.3f", probabilitas)),
vjust = -0.5, size = 3.5) +
scale_y_continuous(expand = expansion(mult = c(0, 0.15))) +
labs(
title = "Diagram Batang Distribusi Poisson (\u03bb = 4)",
x = "Jumlah Kejadian (k)",
y = "Probabilitas P(X = k)"
) +
theme_minimal()
#Grafik Distribusi Bernouli
#Diagram Batang
library(ggplot2)
df_bernoulli <- data.frame(
k = factor(c("0 (Gagal)", "1 (Sukses)")),
prob = c(1 - 0.65, 0.65)
)
ggplot(df_bernoulli, aes(x = k, y = prob, fill = k)) +
geom_col(width = 0.4, color = "black", show.legend = FALSE) +
geom_text(aes(label = sprintf("%.2f", prob)), vjust = -0.5, fontface = "bold") +
scale_fill_manual(values = c("#d95f02", "#1b9e77")) +
scale_y_continuous(limits = c(0, 1)) +
labs(
title = "Diagram Batang Distribusi Bernoulli (p = 0.65)",
x = "Hasil (k)",
y = "Probabilitas P(X = k)"
) +
theme_minimal()
#Grafik Distribusi Multinomial
#Distribusi Multinomial Umum
library(ggplot2)
set.seed(42)
# Parameter
n <- 200
p <- c(0.15, 0.35, 0.30, 0.20)
kategori <- c("Kategori A", "Kategori B", "Kategori C", "Kategori D")
# Bangkitkan 2 kali percobaan multinomial (n = 2)
simulasi <- rmultinom(n = 2, size = n, prob = p)
ekspektasi <- n * p
# Menyiapkan data frame dengan 3 kelompok pembanding
df_multi <- data.frame(
Kategori = factor(rep(kategori, 3), levels = kategori),
Tipe = factor(
rep(c("Ekspektasi Teoritis", "Simulasi 1", "Simulasi 2"), each = length(kategori)),
levels = c("Ekspektasi Teoritis", "Simulasi 1", "Simulasi 2")
),
Frekuensi = c(ekspektasi, simulasi[, 1], simulasi[, 2])
)
# Plot diagram batang (3 warna)
ggplot(df_multi, aes(x = Kategori, y = Frekuensi, fill = Tipe)) +
geom_col(position = position_dodge(width = 0.85), width = 0.75, color = "black") +
geom_text(aes(label = Frekuensi),
position = position_dodge(width = 0.85),
vjust = -0.5, size = 3.2) +
scale_fill_manual(values = c("#2b5c8f", "#e27932", "#2ca02c")) + # 3 warna batang
scale_y_continuous(expand = expansion(mult = c(0, 0.15))) +
labs(
title = "Distribusi Multinomial (Ekspektasi vs 2 Hasil Simulasi)",
x = "Kategori",
y = "Frekuensi Kejadian",
fill = "Keterangan"
) +
theme_minimal()
#Grafik Tingkat Pendidikan
library(ggplot2)
set.seed(42)
n <- 200
p <- c(0.15, 0.25, 0.45, 0.15)
pendidikan <- c("SD", "SMP", "SMA", "Perguruan Tinggi")
# Bangkitkan 1 sampel simulasi multinomial
hasil_simulasi <- as.vector(rmultinom(n = 1, size = n, prob = p))
df_pendidikan <- data.frame(
Tingkat = factor(pendidikan, levels = pendidikan),
Jumlah = hasil_simulasi,
Persentase = paste0(round((hasil_simulasi / n) * 100, 1), "%")
)
# Plot diagram batang tiap jenjang (4 warna berbeda)
ggplot(df_pendidikan, aes(x = Tingkat, y = Jumlah, fill = Tingkat)) +
geom_col(color = "black", width = 0.6) +
geom_text(aes(label = paste0(Jumlah, " (", Persentase, ")")),
vjust = -0.5, fontface = "bold", size = 3.5) +
scale_fill_manual(values = c("#e41a1c", "#377eb8", "#4daf4a", "#984ea3")) + # 4 warna
scale_y_continuous(expand = expansion(mult = c(0, 0.15))) +
labs(
title = "Distribusi Responden Berdasarkan Tingkat Pendidikan (n = 200)",
x = "Tingkat Pendidikan",
y = "Frekuensi (Jumlah Orang)",
fill = "Tingkat"
) +
theme_minimal()
#Simulasi Responden Berdasarkan Pekerjaan
library(ggplot2)
set.seed(42)
n_responden <- 500
kategori_pekerjaan <- c(
"Karyawan Swasta", "Wirausaha", "Freelancer",
"PNS / ASN", "Pelajar / Mhs", "Lainnya"
)
proporsi <- c(0.35, 0.25, 0.15, 0.10, 0.10, 0.05)
data_responden <- data.frame(
id_responden = paste0("RESP-", sprintf("%03d", 1:n_responden)),
pekerjaan = factor(
sample(kategori_pekerjaan, size = n_responden, replace = TRUE, prob = proporsi),
levels = kategori_pekerjaan
)
)
# Hitung tabel frekuensi
df_plot <- as.data.frame(table(data_responden$pekerjaan))
colnames(df_plot) <- c("Pekerjaan", "Jumlah")
df_plot$Persen <- round((df_plot$Jumlah / n_responden) * 100, 1)
df_plot$Label <- paste0(df_plot$Jumlah, " (", df_plot$Persen, "%)")
# Plot dengan palet warna kategori (6 warna)
ggplot(df_plot, aes(x = Pekerjaan, y = Jumlah, fill = Pekerjaan)) +
geom_col(color = "black", width = 0.6, show.legend = FALSE) +
geom_text(aes(label = Label), vjust = -0.5, fontface = "bold", size = 3.3) +
scale_fill_brewer(palette = "Set2") +
scale_y_continuous(expand = expansion(mult = c(0, 0.15))) +
labs(
title = paste("Distribusi Responden Berdasarkan Pekerjaan (N =", n_responden, ")"),
x = "Jenis Pekerjaan",
y = "Jumlah Responden"
) +
theme_minimal()
#Hubungan Pendidikan dengan Pekerjaan
library(ggplot2)
set.seed(42)
n_responden <- 500
kategori_pend <- c("SD", "SMP", "SMA", "Perguruan Tinggi")
kategori_pekerjaan <- c(
"Karyawan Swasta", "Wirausaha", "Freelancer",
"PNS / ASN", "Pelajar / Mhs", "Lainnya"
)
# Pembuatan data frame utuh
data_gabungan <- data.frame(
id = paste0("RESP-", sprintf("%03d", 1:n_responden)),
pendidikan = factor(
sample(kategori_pend, size = n_responden, replace = TRUE, prob = c(0.15, 0.20, 0.40, 0.25)),
levels = kategori_pend
),
pekerjaan = factor(
sample(kategori_pekerjaan, size = n_responden, replace = TRUE, prob = c(0.35, 0.25, 0.15, 0.10, 0.10, 0.05)),
levels = kategori_pekerjaan
)
)
# Diagram batang berkelompok (Grouped Bar Chart)
ggplot(data_gabungan, aes(x = pendidikan, fill = pekerjaan)) +
geom_bar(position = position_dodge(preserve = "single"), color = "black", width = 0.8) +
scale_fill_brewer(palette = "Dark2") + # 6 warna pembeda kategori pekerjaan
labs(
title = "Distribusi Pendidikan vs Pekerjaan (N = 500)",
x = "Tingkat Pendidikan",
y = "Jumlah Responden",
fill = "Jenis Pekerjaan"
) +
theme_minimal()