-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemos.Rmd
128 lines (95 loc) · 2.94 KB
/
demos.Rmd
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
---
title: "R Notebook"
output: html_notebook
---
```{r}
library(tidyverse)
library(palmerpenguins)
library(boot)
```
```{r}
penguins_df <- penguins
penguins_df
```
```{r}
bootstrap_penguin_test <- function(measurement, interval){
func_stat <- function(data, i) {
stat <- data[i, ] %>%
summarize(boot_stat = mean({{measurement}}, na.rm = TRUE)) %>%
pull(boot_stat)
return(stat)
}
adelie_ci <- penguins_df %>%
filter(species == "Adelie") %>%
boot(statistic = func_stat, R = 1000, sim = "ordinary") %>%
boot.ci(conf = interval, type = "bca")
chinstrap_b_ci <- penguins_df %>%
filter(species == "Chinstrap") %>%
boot(statistic = func_stat, R = 1000, sim = "ordinary") %>%
boot.ci(conf = interval, type = "bca")
gentoo_b_ci <- penguins_df %>%
filter(species == "Gentoo") %>%
boot(statistic = func_stat, R = 1000, sim = "ordinary") %>%
boot.ci(conf = interval, type = "bca")
ci_plot <- rbind(adelie_ci$bca, chinstrap_b_ci$bca, gentoo_b_ci$bca) %>%
cbind(c("Adelie", "Chinstrap", "Gentoo")) %>%
as_tibble() %>%
ggplot() +
geom_errorbarh(aes(y = V6, xmin = V4, xmax = V5)) +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1)) +
ylab("Species") +
ggtitle(paste0("Confidence Interval (", interval, ") of Penguin Size"))
return(ci_plot)
}
```
```{r}
bootstrap_penguin_test(
measurement = `flipper_length_mm`,
interval = 0.95
)
```
# Hypothesis Test Example 1:
Is there a difference in flipper size between chinstrap and gentoo penguins?
H_null: There is no difference in flipper size between chinstrap and gentoo penguins
- mean_flipper_size_chinstrap == mean_flipper_size_gentoo
H_alternative: There is a difference in flipper size between chinstrap and gentoo penguins
- mean_flipper_size_chinstrap != mean_flipper_size_gentoo
Have enough evidence at the 5 percent significant level to reject the null hypothesis and conclude that there is a difference in mean flipper size between chinstrap and gentoo penguins.
## Assumptions
### QQ Plot
```{r}
penguins_df %>%
drop_na() %>%
filter(species == "Gentoo") %>%
ggplot() +
stat_qq(aes(sample = flipper_length_mm, color = sex)) +
stat_qq_line(aes(sample = flipper_length_mm, color = sex))
```
### Shapiro Wilk Test
```{r}
penguins_df %>%
drop_na() %>%
filter(species == "Gentoo", sex == "male") %>%
pull(flipper_length_mm) %>%
shapiro.test()
```
```{r}
penguins_df %>%
drop_na() %>%
filter(species == "Gentoo", sex == "female") %>%
pull(flipper_length_mm) %>%
shapiro.test()
```
## t test
```{r}
male_flippers <- penguins_df %>%
filter(species == "Gentoo", sex == "male") %>%
pull(flipper_length_mm)
female_flippers <- penguins_df %>%
filter(species == "Gentoo", sex == "female") %>%
pull(flipper_length_mm)
t.test(male_flippers, female_flippers, alternative = "two.sided")
```
```{r}
wilcox.test(male_flippers, female_flippers, alternative = "two.sided")
```