So sánh phương pháp lấy mẫu

Lấy mẫu trong R

Richie Cotton

Data Evangelist at DataCamp

Ôn tập kỹ thuật lấy mẫu

Thiết lập

top_counted_countries <- c(
  "Mexico", "Colombia", "Guatemala",
  "Brazil", "Taiwan", "United States (Hawaii)"
)
coffee_ratings_top <- coffee_ratings %>%
  filter(country_of_origin %in% top_counted_countries)

Lấy mẫu ngẫu nhiên đơn

coffee_ratings_srs <- coffee_ratings_top %>% 
  slice_sample(prop = 1 / 3)

Lấy mẫu phân tầng

coffee_ratings_strat <- coffee_ratings_top %>% 
  group_by(country_of_origin) %>% 
  slice_sample(prop = 1 / 3) %>% 
  ungroup()

Lấy mẫu cụm

top_countries_samp <- sample(top_counted_countries, size = 2)
coffee_ratings_clust <- coffee_ratings_top %>% 
  filter(country_of_origin %in% top_countries_samp) %>% 
  group_by(country_of_origin) %>% 
  slice_sample(n = nrow(coffee_ratings_top) / 6) %>% 
  ungroup()
slice_sample(n = floor(nrow(coffee_ratings_top) / 6))
Lấy mẫu trong R

Tính điểm ly trung bình

Quần thể

coffee_ratings_top %>% 
  summarize(mean_points = mean(total_cup_points))
81.9

Mẫu ngẫu nhiên đơn

coffee_ratings_srs %>% 
  summarize(mean_points = mean(total_cup_points))
82.0

Mẫu phân tầng

coffee_ratings_strat %>% 
  summarize(mean_points = mean(total_cup_points))
81.8

Mẫu cụm

coffee_ratings_clust %>% 
  summarize(mean_points = mean(total_cup_points))
81.2
Lấy mẫu trong R

Điểm ly trung bình theo quốc gia: mẫu ngẫu nhiên đơn

Quần thể

coffee_ratings_top %>% 
  group_by(country_of_origin) %>% 
  summarize(mean_points = mean(total_cup_points))
# A tibble: 6 x 2
  country_of_origin      mean_points
  <chr>                        <dbl>
1 Brazil                        82.4
2 Colombia                      83.1
3 Guatemala                     81.8
4 Mexico                        80.9
5 Taiwan                        82.0
6 United States (Hawaii)        81.8

Mẫu ngẫu nhiên đơn

coffee_ratings_srs %>% 
  group_by(country_of_origin) %>% 
  summarize(mean_points = mean(total_cup_points))
# A tibble: 6 x 2
  country_of_origin      mean_points
  <chr>                        <dbl>
1 Brazil                        82.3
2 Colombia                      83.1
3 Guatemala                     81.5
4 Mexico                        81.1
5 Taiwan                        82.8
6 United States (Hawaii)        82.7
Lấy mẫu trong R

Điểm ly trung bình theo quốc gia: mẫu phân tầng

Quần thể

coffee_ratings_top %>% 
  group_by(country_of_origin) %>% 
  summarize(mean_points = mean(total_cup_points))
# A tibble: 6 x 2
  country_of_origin      mean_points
  <chr>                        <dbl>
1 Brazil                        82.4
2 Colombia                      83.1
3 Guatemala                     81.8
4 Mexico                        80.9
5 Taiwan                        82.0
6 United States (Hawaii)        81.8

Mẫu phân tầng

coffee_ratings_strat %>% 
  group_by(country_of_origin) %>% 
  summarize(mean_points = mean(total_cup_points))
# A tibble: 6 x 2
  country_of_origin      mean_points
  <chr>                        <dbl>
1 Brazil                        82.4
2 Colombia                      82.9
3 Guatemala                     81.7
4 Mexico                        80.7
5 Taiwan                        82.3
6 United States (Hawaii)        81.2
Lấy mẫu trong R

Điểm ly trung bình theo quốc gia: mẫu cụm

Quần thể

coffee_ratings_top %>% 
  group_by(country_of_origin) %>% 
  summarize(mean_points = mean(total_cup_points))
# A tibble: 6 x 2
  country_of_origin      mean_points
  <chr>                        <dbl>
1 Brazil                        82.4
2 Colombia                      83.1
3 Guatemala                     81.8
4 Mexico                        80.9
5 Taiwan                        82.0
6 United States (Hawaii)        81.8

Mẫu cụm

coffee_ratings_clust %>% 
  group_by(country_of_origin) %>% 
  summarize(mean_points = mean(total_cup_points))
# A tibble: 2 x 2
  country_of_origin mean_points
  <chr>                   <dbl>
1 Mexico                   80.8
2 Taiwan                   82.0
Lấy mẫu trong R

Ayo berlatih!

Lấy mẫu trong R

Preparing Video For Download...