0% found this document useful (0 votes)
12 views10 pages

Exam Score Sampling Analysis in R

The document outlines various sampling methods applied to a dataset of exam scores, including simple random sampling, systematic sampling, stratified sampling, cluster sampling, and multi-stage sampling. Each method is demonstrated with code in R, showing how to estimate the mean math score and analyze the sample sizes. Additionally, it provides descriptive statistics for the entire population and the samples obtained from each method.

Uploaded by

riskicahya200
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

Exam Score Sampling Analysis in R

The document outlines various sampling methods applied to a dataset of exam scores, including simple random sampling, systematic sampling, stratified sampling, cluster sampling, and multi-stage sampling. Each method is demonstrated with code in R, showing how to estimate the mean math score and analyze the sample sizes. Additionally, it provides descriptive statistics for the entire population and the samples obtained from each method.

Uploaded by

riskicahya200
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

sampling

Riski C
2025-09-

library(readxl)

## Warning: package 'readxl' was built under R version 4.4.3

Exam_score <- read_excel("C:/Users/D3LL/Downloads/Exam_score.xlsx")


View(Exam_score)

# Load library
library(dplyr)

##
## 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)
library(readxl)
Exam_score <- read_excel("C:/Users/D3LL/Downloads/Exam_score.xlsx")

# Tampilkan struktur data


str(Exam_score)

18
## tibble [30,641 × 15] (S3: tbl_df/tbl/[Link])
## $ ID : num [1:30641] 0 1 2 3 4 5 6 7 8 9 ...
## $ Gender : chr [1:30641] "female" "female" "female" "male"
... ## $ EthnicGroup : chr [1:30641] NA "group C" "group B" "group
A" ...
## $ ParentEduc : chr [1:30641] "bachelor's degree" "some college" "master's degree" "associate's
degree" ... ## $ LunchType : chr [1:30641] "standard" "standard" "standard" "free/reduced" ...
## $ TestPrep : chr [1:30641] "none" NA "none" "none" ...
## $ ParentMaritalStatus: chr [1:30641] "married" "married" "single" "married" ...
## $ PracticeSport : chr [1:30641] "regularly" "sometimes" "sometimes" "never"
... ## $ IsFirstChild : chr [1:30641] "yes" "yes" "yes" "no" ...
## $ NrSiblings : num [1:30641] 3 0 4 1 0 1 1 1 3 NA ...
## $ TransportMeans : chr [1:30641] "school_bus" NA "school_bus" NA
... ## $ WklyStudyHours : chr [1:30641] "< 5" "45935" "< 5"
"45935" ...
## $ MathScore : num [1:30641] 71 69 87 45 76 73 85 41 65 37 ...
## $ ReadingScore : num [1:30641] 71 90 93 56 78 84 93 43 64 59 ...
## $ WritingScore : num [1:30641] 74 88 91 42 75 79 89 39 68 50 ...

head(Exam_score)

## # A tibble: 6 ×
15
## ID Gender EthnicGroup LunchType TestPrep
## ParentEduc
<dbl> <chr> <chr ParentMaritalStatus
<chr <chr <chr>
## <chr> >
bachelor's de…> standard > marrie
1
## 2 0 female <NA>
1 female group C none
some college standard <NA> d
married
## 3 2 female group B master's standard none single
degr…
## 4 3 male group A associate's free/red… none married
d…
## 5 4 male group C some college standard none married
## #
## 6 ℹ 8 more variables:
5 female group B PracticeSport
associate's<chr>, IsFirstChild
standard none married
<chr>, d…
## # NrSiblings <dbl>, TransportMeans <chr>, WklyStudyHours
<chr>, ## # MathScore <dbl>, ReadingScore <dbl>, WritingScore

# Cek total NA di seluruh dataset


sum([Link](Exam_score))

## [1] 13901
# Hapus satu kolom
Exam_score$math <- NULL

mengganti missing value


Exam_score$MathScore[[Link](Exam_score$MathScore)] <- 0
Exam_score$NrSiblings[[Link](Exam_score$NrSiblings)] <- 0
Exam_score$EthnicGroup[[Link](Exam_score$EthnicGroup)] <- "Unknown"
Exam_score$ParentEduc[[Link](Exam_score$ParentEduc)] <- "Unknown"
Exam_score$ParentMaritalStatus[[Link](Exam_score$ParentMaritalStatus)] <- "Unknown"
Exam_score$TestPrep[[Link](Exam_score$TestPrep)] <- "Unknown"
Exam_score$PracticeSport[[Link](Exam_score$PracticeSport)] <- "Unknown"
Exam_score$IsFirstChild[[Link](Exam_score$IsFirstChild)] <- "Unknown"
Exam_score$TransportMeans[[Link](Exam_score$TransportMeans)] <- "Unknown"
Exam_score$WklyStudyHours[[Link](Exam_score$WklyStudyHours)] <- "Unknown"

contoh simple
# Simple Random Sampling: ambil 100 sampel acak
[Link](29)
srs_sample <- Exam_score %>% sample_n(100)

# Hitung estimasi rata-rata matematika


srs_math_mean <- mean(srs_sample$MathScore)
print(paste("Estimasi Nilai Rata-rata Matematika (SRS):", round(srs_math_mean, 2)))

## [1] "Estimasi Nilai Rata-rata Matematika (SRS): 68.59"

# Tampilkan ukuran sampel


print(paste("Jumlah sampel SRS:", nrow(srs_sample)))

## [1] "Jumlah sampel SRS: 100"

contoh systematic
# Systematic Sampling
n_population <- nrow(Exam_score)
n_sample_sys <- 100
k <- floor(n_population / n_sample_sys) # interval

# Mulai dari titik acak antara 1 hingga k


[Link](29)
start <- sample(1:k, 1)

# Pilih indeks secara sistematis


indices <- seq(start, n_population, by = k)
systematic_sample <- Exam_score[indices, ]

systematic_math_mean <- mean(systematic_sample$MathScore)


print(paste("Estimasi Nilai Rata-rata Matematika (Systematic):", round(systematic_math_mean, 2)))

## [1] "Estimasi Nilai Rata-rata Matematika (Systematic): 63.7"

print(paste("Jumlah sampel Systematic:", nrow(systematic_sample)))

## [1] "Jumlah sampel Systematic: 100"

contoh stratified
# Stratified Sampling berdasarkan 'Gender'
[Link](29)
stratified_sample <- Exam_score %>%
group_by(Gender) %>%
sample_frac(0.1) # ambil 10% dari setiap strata

stratified_math_mean <- mean(stratified_sample$MathScore)


print(paste("Estimasi Nilai Rata-rata Matematika (Stratified by Gender):", round(stratified_math_mean, 2)))

## [1] "Estimasi Nilai Rata-rata Matematika (Stratified by Gender): 66.81"


# Tampilkan jumlah per strata
print("Jumlah sampel per gender:")

## [1] "Jumlah sampel per gender:"

table(stratified_sample$Gender)

##
## male
female
## 1542 1522

contoh cluster
# Cluster Sampling berdasarkan 'EthnicGroup'
[Link](29)
clusters <- unique(Exam_score$EthnicGroup)
selected_clusters <- sample(clusters, 2) # Pilih 2 cluster acak

cluster_sample <- Exam_score %>% filter(EthnicGroup %in% selected_clusters)

cluster_math_mean <- mean(cluster_sample$MathScore)


print(paste("Estimasi Nilai Rata-rata Matematika (Cluster):", round(cluster_math_mean, 2)))

## [1] "Estimasi Nilai Rata-rata Matematika (Cluster): 66.6"

print(paste("Cluster yang terpilih:", paste(selected_clusters, collapse = ", ")))

## [1] "Cluster yang terpilih: group D, group A"

print(paste("Jumlah sampel Cluster:", nrow(cluster_sample)))

## [1] "Jumlah sampel Cluster: 9722"


contoh multi-stage
# Multi-Stage Sampling
[Link](29)

# Stage 1: Pilih 3 cluster acak berdasarkan 'ParentEduc'


educ_clusters <- unique(Exam_score$ParentEduc)
selected_educ_clusters <- sample(educ_clusters, 3)

# Stage 2: Di setiap cluster terpilih, lakukan stratified sampling berdasarkan 'LunchType'


multi_stage_sample <- [Link]()

for(cluster in selected_educ_clusters) {
cluster_data <- Exam_score %>% filter(ParentEduc == cluster)

# Stratify by LunchType within the selected education cluster


stratified_cluster <- cluster_data %>%
group_by(LunchType) %>%
sample_n(500) # ambil 5 sampel dari setiap lunch type

multi_stage_sample <- bind_rows(multi_stage_sample, stratified_cluster)


}

multi_stage_math_mean <- mean(multi_stage_sample$MathScore)


print(paste("Estimasi Nilai Rata-rata Matematika (Multi-Stage):", round(multi_stage_math_mean, 2)))

## [1] "Estimasi Nilai Rata-rata Matematika (Multi-Stage): 64.48"

print(paste("Cluster Pendidikan Orang Tua yang terpilih:", paste(selected_educ_clusters, collapse = ", ")))

## [1] "Cluster Pendidikan Orang Tua yang terpilih: high school, associate's degree, Unknown"

print(paste("Jumlah sampel Multi-Stage:", nrow(multi_stage_sample)))

## [1] "Jumlah sampel Multi-Stage: 3000"


# Tampilkan distribusi per cluster dan strata
print("Distribusi sampel:")

## [1] "Distribusi sampel:"

table(multi_stage_sample$ParentEduc, multi_stage_sample$LunchType)

##
# free/reduced
# associate's standard 500 500
# degree 500 500
# high school 500 500
# Unknown

Analisis deskriptif populasi


# Analisis deskriptif populasi
Exam_descriptive <- Exam_score %>%
summarise(
N = n(),
Mean_MathScore = mean(MathScore),
Median_MathScore =
median(MathScore), SD_MathScore =
sd(MathScore),
Min_MathScore = min(MathScore),
Max_MathScore = max(MathScore),
Q1_MathScore = quantile(MathScore,
0.25), Q3_MathScore =
quantile(MathScore, 0.75),
IQR_MathScore = IQR(MathScore),
)

## [1] "DESKRIPTIF POPULASI ASLI:"

print(Exam_descriptive)
## # A tibble: 1 × 9
## N Mean_MathScore Median_MathScore SD_MathScore Min_MathScore
Max_MathScore <dbl <dbl <dbl <dbl <dbl
## 1 <int>
## > > > > >
30641 66.6 67 15.4 0 100
## # ℹ 3 more variables: Q1_MathScore <dbl>, Q3_MathScore
<dbl>,
## # IQR_MathScore <dbl>

Buat fungsi untuk analisis deskriptif


analyze_descriptive <- function(data, sample_name) {
result <- data %>%
summarise(
Sample =
sample_name, N =
n(),
Mean = mean(MathScore),
Median =
median(MathScore), SD =
sd(MathScore),
Min =
min(MathScore), Max
= max(MathScore),
Q1 = quantile(MathScore,
0.25), Q3 =
quantile(MathScore, 0.75),
IQR = IQR(MathScore),
SE = sd(MathScore)/sqrt(n()) # Standard Error

Analisis untuk semua sampel


all_analysis <- bind_rows(
analyze_descriptive(srs_sample, "SIMPLE RANDOM"),
analyze_descriptive(systematic_sample,
"SYSTEMATIC"),
analyze_descriptive(stratified_sample,
"STRATIFIED"),
analyze_descriptive(cluster_sample, "CLUSTER"),
analyze_descriptive(multi_stage_sample, "MULTI-STAGE")
)
## [1] "HASIL ANALISIS DESKRIPTIF SEMUA SAMPLING:"

print(all_analysis, digits = 3)

## Warning: `...` must be empty in


`[Link]()` ## Caused by error in
`format_tbl()`:
## ! `...` must be
empty. ## ✖ Problematic
argument: ## • digits =

## # A tibble: 6 ×
12
## Sample N Mean Median SD Min Max Q1 Q3 IQR SE Gende
r
## <chr> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
## 1 SIMPLE 100 68.6 70 16.5 29 100 58 81 23 1.65 <NA>
RA…
## 2 SYSTEMATI 100 63.7 63 16.7 25 100 52.8 75 22.2 1.67 <NA>
C
## 3 STRATIFIE 1542 64.2 64 15.6 9 100 54 76 22 0.396 femal
D e
## 4 STRATIFIE 1522 69.4 70 14.6 21 100 60 80 20 0.374 male
D
## 5 CLUSTER 9722 66.6 67 15.1 7 100 56 77 21 0.153 <NA>
## 6 MULTI- 3000 64.5 65 15.5 9 100 54 75 21 0.283 <NA>
STA…

You might also like