-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem Set 1a .Rmd
217 lines (158 loc) · 5.32 KB
/
Problem Set 1a .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
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
211
212
213
214
215
216
217
---
editor_options:
markdown:
wrap: 200
output:
html_document: default
word_document: default
pdf_document: default
---
# HM 878 Applied Biostatistics Problem Set #1a
## Descriptive Statistics, Confidence Intervals, 5-point Summary, Frequencies, Graphs
*Goals: This Problem Set will provide students with the opportunity to
learn how to use R to conduct basic statistical tests, replicating many
tests that were already done in HM 802. The main goal is to make sure
you are familiar with R as a system for statistical analysis. You did
these same analyses in HM 802, using the same datasets and SPSS. I've
included SPSS output here so that you can check your work. You don't
have to conduct hypothesis tests. Include your output from R. If you are
not getting the same information in R (it may be formatted differently
or called a different thing and that is ok), you should keep trying. For
now, I'm only interested in making sure you can use R to do these
analyses.*
Analyses include:
- Descriptive statistics
- Frequency distributions
- Graphical displays of data
------------------------------------------------------------------------
*Setting up the code environment*
```{r}
library(dplyr)
```
```{r}
df = read.csv("C:\\Users\\ljens\\Downloads\\Anticonvulsants.csv")
head(df)
```
------------------------------------------------------------------------
## Question 1
Data: Anticonvulsants.csv. Variable: convulsions. Use R to calculate
descriptive statistics: mean, median, mode, and standard deviation.
Calculate quartiles. Calculate a 5-point summary and the upper and lower
fences to determine outliers.
```{r}
summary(df$convulsions)
```
```{r}
mode_tbl = table(df$convulsions)
print(paste("Mode: ", substr(toString(names(mode_tbl[mode_tbl==max(mode_tbl)])), 1, 5)))
```
```{r}
print(paste("Standard Deviation: ", substr(toString(sd(df$convulsions)), 1, 5)))
```
```{r}
print(paste("IQR: ", substr(toString(IQR(df$convulsions)), 1, 5)))
```
```{r}
print(paste("Upper Fence: ", substr(toString(quantile(df$convulsions, 0.75) + 1.5*IQR(df$convulsions)), 1, 5)))
```
```{r}
print(paste("Lower Fence: ", substr(toString(quantile(df$convulsions, 0.25) - 1.5*IQR(df$convulsions)), 1, 5)))
```
------------------------------------------------------------------------
## Question 2
Data: Anticonvulsants.csv. Variable: center size. Use R to create a
frequency table.
```{r}
df <- df |> mutate(center_size_coded = recode(center_size, `1`="Small", `2`="Medium", `3`="Large" , .default = NA_character_))
table(df$center_size_code)
```
------------------------------------------------------------------------
## Question 3
Data: Cumulative Confirmed Cases and Deaths Among Confirmed Cases.csv.
Use R to create a dual line graph showing confirmed COVID-19 Deaths and
Cases on the same graph.
```{r}
df = read.csv("C:\\Users\\ljens\\Downloads\\Cumulative Confirmed Cases and Deaths Among Confirmed Cases.csv")
```
```{r}
head(df)
```
```{r}
colnames(df)
```
```{r}
library(tidyverse)
```
```{r}
df = df |> mutate(Date = mdy(Date))
```
```{r}
head(df)
```
```{r}
ggplot(df, aes(Date)) +
geom_line(aes(y=Cumulative_Cases, color = "Cases"), group=1)+
geom_line(aes(y=Cumulative_Deaths, color="Deaths"), group=1) +
labs(title="COVID Cases & Deaths", y="Cummulative Count", ) +
ggplot2::theme_classic()
```
------------------------------------------------------------------------
## Question 4
Data: COVID-19 Knowledge, Attitudes, and Behaviors.csv. Variable:
Generations. Using R, Create a Pie Chart and Histogram.
```{r}
df = read.csv("C:\\Users\\ljens\\Downloads\\COVID-19 Knowledge, Attitudes, and Behaviors.csv")
```
```{r}
tbl = df |> group_by(Generations) |> summarize(counts = length(Age))
```
```{r}
ggplot(tbl, aes(x='', y=counts, fill=Generations)) +
geom_bar(stat='identity', width=1) +
coord_polar('y', start=0) +
theme_void()
```
```{r}
df = df |> mutate(Gens = recode(Generations, 'Baby Boomers'=1, 'Gen X'=2, 'Millenials'=3, 'Gen Z'=4, .default = NA_real_))
```
```{r}
ggplot(df, aes(Gens)) +
geom_histogram(color='white', binwidth = 1 ) +
scale_x_continuous(breaks= seq(1,4), labels = c('Baby Boomers','Gen X','Millenials','Gen Z'))
```
------------------------------------------------------------------------
## Question 5
Data: COVID-19 Knowledge, Attitudes, and Behaviors.csv. Variable: AGE.
Using R, create a Box and Whisker plot.
```{r}
summary(df$Age)
```
```{r}
ggplot(df, aes(Age)) +
geom_boxplot() +
coord_flip()
```
------------------------------------------------------------------------
## Question 6
Data: COVID-19 Knowledge, Attitudes, and Behaviors.csv. Variables:
libcons and KnowledgeScore. Using R, Create a graph to show the
relationship between these two variables.
```{r}
df$LibCons = as.character(df$LibCons)
```
```{r}
df$KnowledgeScore = as.numeric(df$KnowledgeScore)
```
```{r}
tbl = df |> group_by(LibCons) |> summarize(mean=mean(KnowledgeScore), sd=sd(KnowledgeScore))
```
```{r}
tbl
```
```{r}
ggplot(tbl, aes(x=LibCons, y=mean)) +
geom_point() +
geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd, width=.2)) +
ylim(0,13) +
labs(y="Mean Knowledge Score", x="Liberal-Conservative Scale", )
```