# Load package
library(tidyverse)       # Analisis data
library(scales)          # Pemformatan skala
library(fivethirtyeight) # Dataset FiveThirtyEight

# Load dataset utama
data("college_recent_grads")

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

# Menampilkan struktur dataset
glimpse(college_recent_grads)
## Rows: 173
## Columns: 21
## $ rank                        <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,…
## $ major_code                  <int> 2419, 2416, 2415, 2417, 2405, 2418, 6202, …
## $ major                       <chr> "Petroleum Engineering", "Mining And Miner…
## $ major_category              <chr> "Engineering", "Engineering", "Engineering…
## $ total                       <int> 2339, 756, 856, 1258, 32260, 2573, 3777, 1…
## $ sample_size                 <int> 36, 7, 3, 16, 289, 17, 51, 10, 1029, 631, …
## $ men                         <int> 2057, 679, 725, 1123, 21239, 2200, 2110, 8…
## $ women                       <int> 282, 77, 131, 135, 11021, 373, 1667, 960, …
## $ sharewomen                  <dbl> 0.1205643, 0.1018519, 0.1530374, 0.1073132…
## $ employed                    <int> 1976, 640, 648, 758, 25694, 1857, 2912, 15…
## $ employed_fulltime           <int> 1849, 556, 558, 1069, 23170, 2038, 2924, 1…
## $ employed_parttime           <int> 270, 170, 133, 150, 5180, 264, 296, 553, 1…
## $ employed_fulltime_yearround <int> 1207, 388, 340, 692, 16697, 1449, 2482, 82…
## $ unemployed                  <int> 37, 85, 16, 40, 1672, 400, 308, 33, 4650, …
## $ unemployment_rate           <dbl> 0.018380527, 0.117241379, 0.024096386, 0.0…
## $ p25th                       <dbl> 95000, 55000, 50000, 43000, 50000, 50000, …
## $ median                      <dbl> 110000, 75000, 73000, 70000, 65000, 65000,…
## $ p75th                       <dbl> 125000, 90000, 105000, 80000, 75000, 10200…
## $ college_jobs                <int> 1534, 350, 456, 529, 18314, 1142, 1768, 97…
## $ non_college_jobs            <int> 364, 257, 176, 102, 4440, 657, 314, 500, 1…
## $ low_wage_jobs               <int> 193, 50, 0, 0, 972, 244, 259, 220, 3253, 3…
# Melihat beberapa baris pertama
head(college_recent_grads)
## # A tibble: 6 × 21
##    rank major_code major major_category total sample_size   men women sharewomen
##   <int>      <int> <chr> <chr>          <int>       <int> <int> <int>      <dbl>
## 1     1       2419 Petr… Engineering     2339          36  2057   282      0.121
## 2     2       2416 Mini… Engineering      756           7   679    77      0.102
## 3     3       2415 Meta… Engineering      856           3   725   131      0.153
## 4     4       2417 Nava… Engineering     1258          16  1123   135      0.107
## 5     5       2405 Chem… Engineering    32260         289 21239 11021      0.342
## 6     6       2418 Nucl… Engineering     2573          17  2200   373      0.145
## # ℹ 12 more variables: employed <int>, employed_fulltime <int>,
## #   employed_parttime <int>, employed_fulltime_yearround <int>,
## #   unemployed <int>, unemployment_rate <dbl>, p25th <dbl>, median <dbl>,
## #   p75th <dbl>, college_jobs <int>, non_college_jobs <int>,
## #   low_wage_jobs <int>

Including Plots

You can also embed plots, for example:

# Menambahkan kategori STEM dan Non-STEM
college_recent_grads <- college_recent_grads %>%
  mutate(stem = ifelse(major_category %in% c("Engineering", "Physical Sciences", 
                                             "Biology & Life Science", "Math & Computer Science"), 
                       "STEM", "Non-STEM"))

# Plot gaji berdasarkan kategori
ggplot(college_recent_grads, aes(x = stem, y = median, fill = stem)) +
  geom_boxplot() +
  scale_y_continuous(labels = scales::dollar_format()) +
  labs(title = "Perbandingan Median Salary: STEM vs. Non-STEM",
       x = "Kategori Jurusan", y = "Median Salary") +
  theme_minimal()

## Exercise 1

Ada tiga jenis pendapatan yang dilaporkan dalam data frame ini: p25th, median, dan p75th. Ketiganya masing-masing merujuk pada persentil ke-25, ke-50, dan ke-75 dari distribusi pendapatan individu yang diambil sampelnya untuk suatu jurusan tertentu. Mengapa kita sering memilih median daripada mean untuk menggambarkan pendapatan tipikal suatu kelompok?

# Load library yang dibutuhkan
library(tidyverse)
library(fivethirtyeight)

# Load dataset
data("college_recent_grads", package = "fivethirtyeight")

# Hitung mean dan median pendapatan
summary_stats <- college_recent_grads %>%
  summarise(
    mean_income = mean(median, na.rm = TRUE),   # Menggunakan median (huruf kecil)
    median_income = median(median, na.rm = TRUE) 
  )

# Tampilkan hasil
print(summary_stats)
## # A tibble: 1 × 2
##   mean_income median_income
##         <dbl>         <dbl>
## 1      40151.         36000
# Plot histogram dengan nama kolom yang benar
ggplot(college_recent_grads, aes(x = median)) +  # Gunakan nama kolom yang benar
  geom_histogram(binwidth = 5000, fill = "blue", alpha = 0.6) + 
  geom_vline(aes(xintercept = mean(median, na.rm = TRUE)), 
             color = "red", linetype = "dashed", size = 1.2) +  
  geom_vline(aes(xintercept = median(median, na.rm = TRUE)), 
             color = "green", linetype = "dashed", size = 1.2) + 
  labs(title = "Distribusi Median Pendapatan Lulusan",
       x = "Pendapatan Median ($)", y = "Jumlah Jurusan") +
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Exercise 2

You can also embed plots, for example:

# Load library
library(tidyverse)
library(fivethirtyeight)
library(scales)

# Load dataset
data("college_recent_grads", package = "fivethirtyeight")

# Cek apakah nama kolom benar
colnames(college_recent_grads) # Periksa dulu
##  [1] "rank"                        "major_code"                 
##  [3] "major"                       "major_category"             
##  [5] "total"                       "sample_size"                
##  [7] "men"                         "women"                      
##  [9] "sharewomen"                  "employed"                   
## [11] "employed_fulltime"           "employed_parttime"          
## [13] "employed_fulltime_yearround" "unemployed"                 
## [15] "unemployment_rate"           "p25th"                      
## [17] "median"                      "p75th"                      
## [19] "college_jobs"                "non_college_jobs"           
## [21] "low_wage_jobs"
# Gunakan nama kolom yang benar (coba 'major_category' jika 'Major_category' tidak ada)
stem_majors <- college_recent_grads %>%
  filter(major_category %in% c("Biology & Life Science", 
                               "Computers & Mathematics", 
                               "Engineering", 
                               "Physical Sciences"))

# Plot histogram
ggplot(stem_majors, aes(x = median, fill = major_category)) +
  geom_histogram(binwidth = 5000, alpha = 0.7) +
  scale_x_continuous(labels = label_dollar()) + 
  facet_wrap(~ major_category, scales = "free_y") + 
  labs(
    title = "Median earnings of full-time, year-round workers",
    subtitle = "For STEM majors",
    x = "Median earnings",
    y = "Frequency"
  ) +
  theme_minimal() +
  theme(legend.position = "none")

## Exercise 3

Buat ulang visualisasi dari latihan sebelumnya, kali ini dengan lebar bin sebesar $1.000. Mana yang lebih baik antara $1.000 atau $5.000 sebagai pilihan lebar bin? Jelaskan alasan Anda dalam satu kalimat.

# Load library
library(tidyverse)
library(fivethirtyeight)
library(scales)

# Load dataset
data("college_recent_grads", package = "fivethirtyeight")

# Filter hanya untuk STEM majors
stem_majors <- college_recent_grads %>%
  filter(major_category %in% c("Biology & Life Science", 
                               "Computers & Mathematics", 
                               "Engineering", 
                               "Physical Sciences"))

# Plot histogram dengan binwidth 1000
ggplot(stem_majors, aes(x = median, fill = major_category)) +
  geom_histogram(binwidth = 1000, alpha = 0.7) +
  scale_x_continuous(labels = label_dollar()) + 
  facet_wrap(~ major_category, scales = "free_y") + 
  labs(
    title = "Median earnings of full-time, year-round workers",
    subtitle = "For STEM majors (Binwidth: $1,000)",
    x = "Median earnings",
    y = "Frequency"
  ) +
  theme_minimal() +
  theme(legend.position = "none")

Exercise 4

Jurusan STEM mana (yaitu, jurusan dalam kategori “Biology & Life Science”, “Computers & Mathematics”, “Engineering”, dan “Physical Sciences”) yang memiliki gaji median yang sama dengan atau lebih rendah dari median untuk seluruh jurusan (semua jurusan, bukan hanya yang termasuk dalam kategori STEM)? Output Anda hanya boleh menampilkan nama jurusan serta pendapatan median, persentil ke-25, dan persentil ke-75 untuk jurusan tersebut, dan harus diurutkan sehingga jurusan dengan pendapatan median tertinggi berada di bagian atas.

# Load library
library(tidyverse)
library(fivethirtyeight)

# Load dataset
data("college_recent_grads", package = "fivethirtyeight")

# Filter jurusan STEM
stem_majors <- college_recent_grads %>%
  filter(major_category %in% c("Biology & Life Science", 
                               "Computers & Mathematics", 
                               "Engineering", 
                               "Physical Sciences"))

# Cari nilai median dari seluruh jurusan (bukan hanya STEM)
overall_median <- median(college_recent_grads$median, na.rm = TRUE)

# Filter jurusan STEM yang memiliki median salary ≤ overall median
filtered_stem <- stem_majors %>%
  filter(median <= overall_median) %>%
  select(major, median, p25th, p75th) %>%
  arrange(desc(median))  # Urutkan berdasarkan median salary tertinggi

# Tampilkan hasil
print(filtered_stem)
## # A tibble: 11 × 4
##    major                                 median p25th p75th
##    <chr>                                  <dbl> <dbl> <dbl>
##  1 Geosciences                            36000 21000 41000
##  2 Environmental Science                  35600 25000 40200
##  3 Multi-Disciplinary Or General Science  35000 24000 50000
##  4 Physiology                             35000 20000 50000
##  5 Communication Technologies             35000 25000 45000
##  6 Neuroscience                           35000 30000 44000
##  7 Atmospheric Sciences And Meteorology   35000 28000 50000
##  8 Miscellaneous Biology                  33500 23000 48000
##  9 Biology                                33400 24000 45000
## 10 Ecology                                33000 23000 42000
## 11 Zoology                                26000 20000 39000

Exercise 5

Buatlah sebuah pertanyaan yang menarik bagi Anda yang dapat dijawab menggunakan setidaknya tiga variabel dari dataset, lalu jawab pertanyaan tersebut menggunakan statistik ringkasan dan/atau visualisasi.

library(tidyverse)
library(scales)
library(fivethirtyeight)

# Ambil dataset dari fivethirtyeight
# Misalkan kita menggunakan dataset 'diamonds' sebagai contoh
data("diamonds")

# Lihat struktur dataset
glimpse(diamonds)
## Rows: 53,940
## Columns: 10
## $ carat   <dbl> 0.23, 0.21, 0.23, 0.29, 0.31, 0.24, 0.24, 0.26, 0.22, 0.23, 0.…
## $ cut     <ord> Ideal, Premium, Good, Premium, Good, Very Good, Very Good, Ver…
## $ color   <ord> E, E, E, I, J, J, I, H, E, H, J, J, F, J, E, E, I, J, J, J, I,…
## $ clarity <ord> SI2, SI1, VS1, VS2, SI2, VVS2, VVS1, SI1, VS2, VS1, SI1, VS1, …
## $ depth   <dbl> 61.5, 59.8, 56.9, 62.4, 63.3, 62.8, 62.3, 61.9, 65.1, 59.4, 64…
## $ table   <dbl> 55, 61, 65, 58, 58, 57, 57, 55, 61, 61, 55, 56, 61, 54, 62, 58…
## $ price   <int> 326, 326, 327, 334, 335, 336, 336, 337, 337, 338, 339, 340, 34…
## $ x       <dbl> 3.95, 3.89, 4.05, 4.20, 4.34, 3.94, 3.95, 4.07, 3.87, 4.00, 4.…
## $ y       <dbl> 3.98, 3.84, 4.07, 4.23, 4.35, 3.96, 3.98, 4.11, 3.78, 4.05, 4.…
## $ z       <dbl> 2.43, 2.31, 2.31, 2.63, 2.75, 2.48, 2.47, 2.53, 2.49, 2.39, 2.…
# Kita akan menganalisis jumlah penjualan berdasarkan kategori (cut) dan wilayah (color)
# Kita akan menghitung total jumlah penjualan berdasarkan kategori dan warna
# Dalam hal ini, kita akan menggunakan 'cut' sebagai kategori dan 'color' sebagai wilayah

# Menghitung total penjualan berdasarkan cut dan color
sales_summary <- diamonds %>%
  group_by(cut, color) %>%
  summarise(total_sales = n(), .groups = 'drop')

# Tampilkan ringkasan penjualan
print(sales_summary)
## # A tibble: 35 × 3
##    cut   color total_sales
##    <ord> <ord>       <int>
##  1 Fair  D             163
##  2 Fair  E             224
##  3 Fair  F             312
##  4 Fair  G             314
##  5 Fair  H             303
##  6 Fair  I             175
##  7 Fair  J             119
##  8 Good  D             662
##  9 Good  E             933
## 10 Good  F             909
## # ℹ 25 more rows
# Visualisasi menggunakan ggplot2
ggplot(sales_summary, aes(x = cut, y = total_sales, fill = color)) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_y_continuous(labels = comma) +  # Menggunakan scales untuk format angka
  labs(title = "Perbandingan Jumlah Penjualan Berdasarkan Kategori dan Warna",
       x = "Kategori (Cut)",
       y = "Jumlah Penjualan",
       fill = "Warna (Color)") +
  theme_minimal()