Repository navigation
Expand file tree
/
Copy pathall_the_R_you_need_tidy.Rmd
More file actions
211 lines (168 loc) · 4.63 KB
/
Copy pathall_the_R_you_need_tidy.Rmd
File metadata and controls
211 lines (168 loc) · 4.63 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
---
title: "All the R you need for STAT 220 - tidy"
author: "Professor McNamara"
date: "Fall 2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(eval = TRUE, warning = FALSE)
```
## Packages
We use three packages in this course:
```{r, message=FALSE}
library(Lock5Data)
library(tidyverse)
library(infer)
```
## Reading in data
```{r, message=FALSE}
GSS <- read_csv("data/GSS_clean.csv")
```
## Plotting
```{r}
# one categorical
ggplot(GSS) + geom_bar(aes(x = marital_status))
# two categorical
ggplot(GSS) + geom_bar(aes(x = marital_status, fill = born_in_us))
ggplot(GSS) + geom_bar(aes(x = marital_status, fill = born_in_us), position = "dodge")
# one numeric
ggplot(GSS) + geom_histogram(aes(x = highest_year_of_school_completed), binwidth = 2)
ggplot(GSS) + geom_boxplot(aes(x = 1, y = highest_year_of_school_completed))
# one numeric, one categorical
ggplot(GSS) + geom_boxplot(aes(x = marital_status, y = highest_year_of_school_completed))
# two numeric
ggplot(GSS) + geom_point(aes(
x = highest_year_of_school_completed,
y = highest_year_school_completed_spouse
))
```
## Summary statistics
```{r}
# one categorical
GSS |>
group_by(marital_status) |>
summarize(n = n()) |>
mutate(prop = n / sum(n))
# two categorical
GSS |>
group_by(marital_status, general_happiness) |>
summarize(n = n()) |>
mutate(prop = n / sum(n))
# one numeric
GSS |>
summarize(
mean = mean(highest_year_of_school_completed, na.rm = TRUE),
median = median(highest_year_of_school_completed, na.rm = TRUE)
)
GSS |>
drop_na(highest_year_of_school_completed) |>
summarize(
mean = mean(highest_year_of_school_completed),
median = median(highest_year_of_school_completed)
)
GSS |>
drop_na(highest_year_of_school_completed) |>
summarize(sd(highest_year_of_school_completed))
GSS |>
drop_na(highest_year_of_school_completed) |>
summarize(
range = max(highest_year_of_school_completed) - min(highest_year_of_school_completed),
IQR = IQR(highest_year_of_school_completed)
) # range and IQR
GSS |>
drop_na(highest_year_of_school_completed) |>
summarize(
min = min(highest_year_of_school_completed),
lower_hinge = quantile(highest_year_of_school_completed, .25),
median = median(highest_year_of_school_completed),
upper_hinge = quantile(highest_year_of_school_completed, .75),
max = max(highest_year_of_school_completed)
) # five number summary
GSS |>
pull(highest_year_of_school_completed) |>
fivenum() # five number summary
GSS |>
summarize(correlation = cor(
x = highest_year_of_school_completed,
y = highest_year_school_completed_spouse,
use = "complete.obs"
)) # correlation
```
## Working with data
```{r}
# fix NA issue
GSS <- GSS |>
drop_na(highest_year_of_school_completed)
# turn character to numeric
GSS <- GSS |>
mutate(Age = parse_number(age_of_respondent))
# filter observations
GSS |>
filter(Age > 91)
```
## Modeling/inference
```{r}
# linear model
m1 <- lm(highest_year_of_school_completed ~ highest_year_school_completed_spouse,
data = GSS
)
m1 |>
summary() # model summary
m1 |> predict(
newdata = data.frame(highest_year_school_completed_spouse = 9),
interval = "confidence"
)
m1 |> predict(
newdata = data.frame(highest_year_school_completed_spouse = 9),
interval = "prediction"
)
# bootstrapping
bootstrap_sample <- GSS |>
specify(response = Age) |>
generate(reps = 1, type = "bootstrap")
boot <- GSS |>
specify(response = Age) |>
generate(reps = 1000, type = "bootstrap") |>
calculate(stat = "mean")
# one proportion
GSS |>
prop_test(
response = self_emp_or_works_for_somebody,
explanatory = NULL,
alternative = "greater", p = 0.1
)
# two proportions
GSS |>
prop_test(response = should_marijuana_be_made_legal,
explanatory = self_emp_or_works_for_somebody,
order = c("Self-employed", "Someone else"))
# one mean
GSS |>
drop_na(born_in_us) |>
t_test(response = highest_year_of_school_completed,
explanatory = NULL,
mu = 12, alternative = "greater")
# two means
GSS |>
drop_na(born_in_us) |>
t_test(
response = highest_year_of_school_completed,
explanatory = born_in_us, order = c("No", "Yes")
)
# ANOVA
a1 <- aov(Age ~ marital_status, data = GSS)
a1 |>
summary()
a1 |>
TukeyHSD(conf.level = 0.90) # differences
# Chi-square
GSS |>
chisq_test(response = marital_status, explanatory = general_happiness) # independence
GSS |>
chisq_test(response = general_happiness, p = c(0.14, 0.56, 0.30)) # GoF
```
## Probability
```{r}
pnorm(1.96, lower.tail = FALSE) # normal distribution function
qnorm(0.25, mean = 100, sd = 5) # normal quantile function
```