-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEpiPackage - Childs.Rmd
373 lines (284 loc) · 13.5 KB
/
EpiPackage - Childs.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
Packages to use: epiR, incidence, epitools, epicalc, Epi
```{r}
#install.packages("epicalc", repos = "http://medipe.psu.ac.th/epicalc")
#http://www.columbia.edu/~cjd11/charles_dimaggio/DIRE/resources/R/packages.pdf
#summary tables and statistics - create & visualize
```
```{r}
library(epiR)
library(epitools)
library(incidence)
library(Epi)
library(epiDisplay)
library(epicalc)
library(devtools)
library(roxygen2)
library(plotly)
library(ggplot2)
library(RColorBrewer)
library(AER)
data("Fatalities")
#if comments for when people don't read instructions
test<-Fatalities[complete.cases(Fatalities),]
test1<-na.omit(Fatalities)
```
```{r}
#barplot
ggplot(data=test, aes(x=jail, y=breath)) + geom_bar(stat="identity")
```
```{r}
#boxplot
epi.box<-function(x,y, data, title, xlab, ylab){ #OR epi.box<-function(data, x,y, title, xlab, ylab){
boxplot(y~x, data=data, notch=TRUE,
main=title, xlab=xlab, ylab=ylab)
}
epi.box(Fatalities$jail, Fatalities$income, data=Fatalities, title="main1", xlab="xlab", ylab="ylab")
```
```{r}
#PLOTLY
#sideways hist
plot_ly(Fatalities, y = ~income, color = I("black"), alpha = 0.1, boxpoints = "suspectedoutliers")
#boxplot with scatter
plot_ly(y = ~Fatalities$income, type = "box", boxpoints = "all", jitter = 0.3,pointpos = -1.8) #%>%
#grouped hist
plot_ly(Fatalities,x=~breath, y = ~income, color=~jail, type = "box", boxpoints = "all", jitter = 0.3,
pointpos = -1.8) %>%
layout(boxmode = "group")
```
```{r}
#using plotly()
plot_ly(Fatalities, x = ~income, type = "histogram")
plot_ly(Fatalities) %>% add_histogram(name = "Income")
#price_hist <- function(method = "FD") {
#h <- hist(diamonds$price, breaks = method, plot = FALSE)
#plot_ly(x = h$mids, y = h$counts) %>% add_bars(name = method)
#}
#histogram
plot_ly(Fatalities, x = ~breath, color = ~jail) %>%
add_histogram()
```
```{r}
#HISTOGRAM
#histogram with line
ggplot(data=Fatalities, aes(Fatalities$income)) +
geom_histogram(aes(y =..density..),
col="blue",
fill="light blue",
alpha=.5) +
geom_density(col=2) +
labs(title="Histogram for Age", x="Age", y="Count")
#histogram w/o line
library(ggplot2)
ggplot(data=Fatalities, aes(income)) +
geom_histogram(col="black",
aes(fill=..count..)) +
scale_fill_gradient("Count", low="light blue", high="navy")+
labs(title="Histogram for Age", x="Age", y="Count")
```
```{r}
##Vignette for epiplot function
#1. Must have cleaned data with no NAs (Reference link: https://stackoverflow.com/questions/4862178/remove-rows-with-all-or-some-nas-missing-values-in-data-frame )
#2. Arguments: (data, x, y, graph, fill, title, xlab, ylab, legend)
#3. Graphs: bar plot ("bar"), grouped bar plot ("bargroup"), stacked bar plot ("barstack"), box plot ("boxplot"), box plot with correlating dots ("dotboxplot"), histogram ("hist"), density plot ("densityhist"), scatter plot ("scatter"), scatter plot with correlating line ("scatterline"), linear regression ("linreg").
#4. Input the correct arguments:
#bar plot: (data=data, x=data$x, graph="bar", title="Title of plot", xlab="x-axis label", ylab= "y-axis label")
#grouped bar plot: (data=data, x=data$x,y=data$y, graph="bargroup",fill=data$fill, title="Title of plot", xlab="x-axis labellab", ylab="y-axis label")
#stacked bar plot: (data=data, x=data$x,y=data$y, graph="barstack",fill=data$fill, title="Title of plot", xlab="x-axis labellab", ylab="y-axis label")
#box plot: (data=data, x=data$x,y=data$y, graph="boxplot", title="Title of plot", xlab="x-axis label", ylab="y-axis label")
#box plot with correlating dots:(data=data,y=data$y, graph="dotboxplot", title="Title of plot", xlab="x-axis labellab", ylab="y-axis label")
#histogram: (data=data, x=data$x, graph="hist", title="Title of plot", xlab="x-axis label", ylab="y-axis label")
#density plot: (data=data, x=data$x, graph="densityhist", title="Title of plot", xlab="x-axis label", ylab="y-axis label")
#scatter plot: (data=data, x=data$x, y=data$y, graph="scatter",fill=data$fill, title="Title of plot", xlab="x-axis label", ylab="y-axis label", legend="Title of legend fill")
#scatter plot with correlating line: (data=data, x=data$x, y=data$y, graph="scatterline",fill=data$fill, title="Title of plot", xlab="x-axis label", ylab="y-axis label", legend="Title of legend fill")
#linear regression: (data=data, x=data$x, y=data$y, graph="linreg",fill=data$fill, title="Title of plot", xlab="x-axis label", ylab="y-axis label", legend="Title of legend fill")
```
```{r}
epiplot<- function(data, x, y, graph, fill, title, xlab, ylab, legend){ ###add argument for error bars
data[complete.cases(data), ]
if(graph == "bar"){ #boxplot function
pic<- ggplot(data=data, aes(x=x, fill=x)) +
geom_bar( ) +
scale_fill_brewer(palette = "Paired")+
labs(title="title", x="xlab", y="ylab")
#table1 = table(data$x) ## get the cross tab
#pic<-barplot(table1, beside = TRUE, legend = levels(data$x), col=c("lightblue","darkblue"),main="title", xlab="xlab", ylab = "ylab")
return(pic)
#barplot(table(data$x), col=c("lightblue","darkblue"),main="title", xlab="xlab", ylab = "ylab")
} else if(graph=="bargroup"){ #works, but has column for NAs....why???
pic<-ggplot(data=data, aes(x=x, y=y, fill=fill)) +
geom_bar(position="dodge", stat="identity") + scale_fill_brewer(palette = "Paired")+theme_bw()+facet_wrap(~"fill")
return(pic)
} else if(graph=="barstack"){ #works, but has column for NAs....why???
pic<-ggplot(data=data, aes(fill=fill, y=y, x=x)) +
geom_bar( stat="identity")
return(pic)
} else if(graph=="boxplot"){ #GOOD
pic<-boxplot(y~x, data=data, notch=TRUE,
main="title", xlab="xlab", ylab="ylab")
return(pic)
} else if(graph=="dotboxplot"){ #GOOD
pic<-plot_ly(y = ~y, type = "box", boxpoints = "all", jitter = 0.3,pointpos = -1.8)
return(pic)
} else if(graph=="hist"){ #GOOD
pic<-ggplot(data=data, aes(x)) +
geom_histogram(col="black", aes(fill=..count..)) +
scale_fill_gradient("Count", low="light blue", high="navy")+
labs(title="title", x="xlab", y="ylab")
return(pic)
} else if(graph=="densityhist"){ #GOOD
pic<-ggplot(data=data, aes(x)) +
geom_histogram(aes(y =..density..),col="blue", fill="light blue", alpha=.5) +
geom_density(col=2) +
labs(title="title", x="xlab", y="ylab")
return(pic)
} else if(graph=="scatter"){ #GOOD
pic<-ggplot(data, aes(x, y, color = fill)) +
geom_point(shape = 16, size = 5, show.legend = TRUE) +
theme_minimal() +
#scale_color_gradient(color = "Blues")+
labs(title="title", x="xlab", y="ylab", color = "legend")
return(pic)
} else if(graph=="scatterline"){ #GOOD
pic<-ggplot(data, aes(x, y, color = fill)) +
geom_point(shape = 16, size = 5, show.legend = TRUE) +
theme_minimal() +
scale_color_gradient(low = "light blue", high = "dark blue")+
labs(title="title", x="xlab", y="ylab", color = "legend")+geom_smooth()
return(pic)
} else if(graph=="linreg"){ #GOOD
pic<-ggplot(data, aes(x, y, color = fill)) +
geom_point(shape = 16, size = 5, show.legend = TRUE) +
theme_minimal() +
scale_color_gradient(low = "light blue", high = "dark blue")+
labs(title="title", x="xlab", y="ylab", color = "legend")+ geom_smooth(method = 'lm', se = TRUE)
return(pic)
}
}
```
```{r}
epiplot(Fatalities, x=Fatalities$jail, graph = "bar")
epiplot(Fatalities, y=Fatalities$income, graph = "dotboxplot")
library(ggplot2)
ggplot(Fatalities, aes(Fatalities$income, Fatalities$fatal, color = Fatalities$drinkage)) +
geom_point(shape = 16, size = 5, show.legend = TRUE) +
theme_minimal() +
scale_color_gradient(low = "light blue", high = "dark blue")+
labs(title="title", x="xlab", y="ylab", color = "Drinkage")
ggplot(Fatalities, aes(Fatalities$income, Fatalities$fatal, color = Fatalities$drinkage)) +
geom_point(shape = 16, size = 5, show.legend = TRUE) +
theme_minimal() +
scale_color_gradient(low = "light blue", high = "dark blue")+
labs(title="title", x="xlab", y="ylab")+geom_smooth()
###link to complete cases code in stat exchange
class(data1.1$breath)
xlim(Fatalities$breath)
lims(Fatalities$breath)
#Fatalities, aes(income, fatal, color = drinkage
#<- function(data, x, y, graph, fill, title, xlab, ylab, legend){ ###add argument for error bars
```
```{r}
#table1 = table(data$x) ## get the cross tab
#barplot(table1, beside = TRUE, legend = levels(data$x))
#table1 = table(Fatalities$breath) ## get the cross tab
#barplot(table1, beside = TRUE, legend = levels(Fatalities$breath))
#barplot.default(table(data1.1$jail), col = c("lightblue", "darkblue"), main = "title", xlab = "xlab", ylab = "ylab")
#else if(graph=kaplan){ #survival
#}
# print a function with parmfrow
#ggplot(Fatalities, aes(income, fatal, color = drinkage)) +
#geom_point(shape = 16, size = 5, show.legend = TRUE) +
#theme_minimal() +
#scale_color_gradient(low = "light blue", high = "dark blue")+
#labs(title="title", x="xlab", y="ylab", color = "Drinks")+geom_smooth(method = 'lm', se = TRUE)
````
```{r}
#test
mtcars<-mtcars
counts <- table(mtcars$gear)
barplot(counts, main="Car Distribution",
xlab="Number of Gears")
fxn1<- function(data, x){ ###add argument for error bars
table1 <- table(data$x) ## get the cross tab
barplot(table1)
}
fxn1(data = mtcars, x=gear)
table1
table1 = table(data1.1$breath) ## get the cross tab
pic<-barplot(table1, beside = TRUE, legend = levels(data1.1$breath), col=c("lightblue","darkblue"),main="title", xlab="xlab", ylab = "ylab")
#barplot(table(data$x), col=c("lightblue","darkblue"),main="title", xlab="xlab", ylab = "ylab")
is.na(Fatalities$jail)
dat1<-complete.cases(Fatalities)
table(dat1)
```
```{r}
ggplot(data=Fatalities, aes(x=breath, y=income)) +
geom_bar(stat="identity")+
theme_bw()+
scale_fill_brewer(palette = "Blues")
barplot(table(data$x), col=c("lightblue","darkblue"),main="title", xlab="xlab", ylab = "ylab")
plot_ly(Fatalities, x =~income, y = ~drinkage, color = ~income, size = ~income)
ggplot(data, aes(fill=fill, y=y, x=x)) +
geom_bar(position="dodge", stat="identity") + scale_fill_gradient("Count", low="light blue", high="navy")
ggplot(data, aes(fill=fill, y=y, x=x)) +
geom_bar(position="dodge", stat="identity") + scale_fill_brewer(palette = "Blues")+theme_bw()
ggplot(Fatalities, aes(y=income, x=breath, color = jail)) +geom_bar( stat="identity")+ scale_fill_brewer(palette = "Blues")+theme_bw()+ facet_wrap(~breath)
table(Fatalities$jail)
##how to deal with NAs
bars <- c(4, 3, 5, 4.5, 5.1)
labels <- c('Anthrax', 'DRI', 'DOA', 'BIOHAZARD', 'Withstand')
barplot(bars, names.arg = labels, col = brewer.pal(5, name = "Blues"))
is.na(Fatalities$jail)
plot_ly(Fatalities, x =~income, y = ~fatal, color = ~income, size = ~income)
p<-plot_ly(Fatalities, x =~income, y = ~fatal, type="scatter", mode = "markers", marker=list( size=20 , opacity=0.5),
color = ~drinkage, colors=c("light blue","dark blue") %>%
layout(title = 'Primates Brain and Body Weight',
xaxis = list(title = 'Body Weight (kg)'),
yaxis = list(title = 'Brain Weight (g)'))
p
ggplot(Fatalities, aes(income, fatal, color = drinkage)) +
geom_point(shape = 16, size = 5, show.legend = TRUE) +
theme_minimal() +
scale_color_gradient(low = "light blue", high = "dark blue")+
labs(title="title", x="xlab", y="ylab", color = "Drinks")+geom_smooth()
ggplot(data = dfm, aes(x = TY, y = value, color = variable)) +
geom_point(size=5) +
labs(title = "Temperatures\n", x = "TY [°C]", y = "Txxx", color = "") +
scale_color_manual(labels = c("T999", "T888"), values = c("blue", "red")) +
theme_bw() +
theme(axis.text.x = element_text(size = 14), axis.title.x = element_text(size = 16),
axis.text.y = element_text(size = 14), axis.title.x = element_text(size = 16),
plot.title = element_text(size = 20, face = "bold", color = "darkgreen"))
iris, x = ~Petal.Length, y = ~Petal.Width , type="scatter", mode = "markers",
marker=list( size=20 , opacity=0.5), color = ~Sepal.Length ,
colors=c("green","blue")
```
```{r}
To include:
2x2 table
incidence
rate ratio
risk ratio
odds ratio
CIs ###****add CIs???
stargazer pckg
create basic statistics - stargazer to generate
produce stats and put into a table
shiny app - drag by year - scroll bar by year - define axis as based on the data
broom to tidy into a table
function to produce table with all their measures - beautify tables
r oxygen will generate manual
write function that formats data the way we want it to - enter column names
naming mechanism for variables and columns - survey makes you identify
function renames dataset with prevalence, incidence - output just anoter dataset
include links in the vignettes - explain and then for future details - insert links
epi.2by2
```
```{r}
#epi.2by2
#run all calculation and then use broom to clean up neatly
dat <- matrix(c(13,2163,5,3349), nrow = 2, byrow = TRUE)
rownames(dat) <- c("DF+", "DF-"); colnames(dat) <- c("FUS+", "FUS-"); dat
epi.2by2(dat = as.table(dat), method = "cross.sectional",
conf.level = 0.95, units = 100, homogeneity = "breslow.day",
outcome = "as.columns")
```