-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab 1 Question 2.qmd
More file actions
106 lines (76 loc) · 2.03 KB
/
Copy pathLab 1 Question 2.qmd
File metadata and controls
106 lines (76 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
---
title: "Untitled"
format: html
editor: visual
---
## Question 2
```{r}
library(tidyverse)
library(purrr)
library(readr)
```
```{r}
oscars_data <- read_csv("Oscars-demographics-DFE.csv")
```
```{r Warmups}
#1
oscars_data |>
select(movie, award, year_of_award) |>
count(movie) |>
slice_max(order_by = n,n = 1)
#2
oscars_data |>
filter(award == "Best Actress") |>
select(year_of_award, person) |>
mutate(person = str_replace(person, "(?s) .*", "")) |>
count(person) |>
slice_max(order_by = n, n = 1)
#3 - Brandon's Code
oscars_data |>
mutate(birthplace = ifelse(birthplace=="New York City", "Ny", birthplace),
region = sub(".*, ", "", birthplace)) |>
count(region) |>
filter(n == max(n))
```
```{r Bootstrapping - Tidy}
#What is an approximate 95% confidence interval for percent of "Big 5 Award" award winners who are not white?
prop_function <- function(race_data) {
white_winners <- race_data |>
filter(race_ethnicity == "White") |>
count() |>
as.numeric()
nonwhite_winners <- race_data |>
filter(race_ethnicity != "White") |>
count() |>
as.numeric()
return(nonwhite_winners / white_winners)
}
#old_sample <- oscars_data |>
# select(race_ethnicity)
#new_sample <- sample(old_sample, replace = TRUE)
#oscars_data |>
# select(race_ethnicity) |>
# map_int(.f = prop_function)
```
```{r Copied Code from Stack Overflow for Testing}
race_ethnicity <- oscars_data |>
select(race_ethnicity)
# number of bootstrap replicates
B <- 100
# create empty storage container
result_vec <- vector(length=B)
for(b in 1:B) {
# draw a bootstrap sample
this_sample <- sample(race_ethnicity, size=length(race_ethnicity), replace=TRUE)
# calculate your statistic
m <- prop_function(this_sample)
# save your calucated statistic
result_vec[b] <- m
}
# then probably draw a histogram of your bootstrapped replicates
hist(result_vec)
# get 95% confidence interval
result_vec <- result_vec[order(result_vec)]
lower_bound <- result_vec[round(0.025*B)]
upper_bound <- result_vec[round(0.0975*B)]
```