-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_analysis.R
237 lines (173 loc) · 6.41 KB
/
data_analysis.R
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
rm(list=ls())
#library(renv)
#install.packages(c("tidyverse", "dbplyr", "RPostgres", "DBI", "collapse",
# "dadjoke", "ggiraphExtra", "gtsummary", "ggstatsplot",
# "ggthemes", "viridis", "dlookr", "rmarkdown"))
install.packages("ggpie")
install.packages("ggsci")
library(tidyverse)
library(dbplyr)
library(RPostgres)
library(DBI)
library(dlookr)
library(collapse)
library(dadjoke)
library(ggiraphExtra)
library(gtsummary)
library(ggstatsplot)
library(ggthemes)
library(viridis)
library(ggpie)
library(ggsci)
# ggdonut(
# data = diamonds, group_key = "cut", count_type = "full",
# label_info = "all", label_type = "horizon",
# label_size = 4, label_pos = "in", label_threshold = 10
# ) +
# scale_fill_simpsons()
con <- DBI::dbConnect(RPostgres::Postgres(),
host = "localhost",
port = 5432,
dbname = "test",
user = "JoeH",
password = "a" )
dbListTables(con)
meta <- tbl(con, "metadata")
game <- tbl(con, "game_data")
white <- tbl(con, "white_data")
black <- tbl(con, "black_data")
glimpse(meta)
# note that the . syntax for passing in the argument is a magrittr thing
# If I used |> pipe (native to R 4.1.0), I wouldn't have this requirement
# since using enviornment and tidyverse is essentially core R at this point
# I'm not too concerned
meta %>%
select(termination) %>%
as.data.frame(.) %>%
tbl_summary(.)
meta %>%
filter(termination == "Rules infraction") %>%
glimpse()
g <- meta %>%
filter(rapid == TRUE & classical == TRUE) %>%
select(site) %>%
as.data.frame()
g
meta_names <- meta %>%
colnames()
meta_names
game_names <- game %>%
colnames()
game_names
left_join <- white %>%
left_join(black, by = "game_id") %>%
glimpse()
test <- left_join %>%
filter(game_id == 1) %>%
glimpse()
white %>%
inner_join(black, by = c("game_id")) %>%
filter(game_id == 1) %>%
glimpse()
white %>%
filter(game_id == 1) %>%
union_all(black) %>%
glimpse()
game %>%
filter(has_evals == TRUE) %>%
as.data.frame() %>%
tbl_summary(type = list(c(game_id) ~ "categorical"))
game %>%
filter(game_white_n_promotions == 1) %>%
left_join(meta, by = c("game_id")) %>%
select(site) %>%
glimpse()
# there are some bot v bot games
meta %>%
filter(time_control < 1500) %>%
ggplot(aes(x=time_control/60)) +
geom_histogram(bins = 100) +
theme_bw()
meta %>%
filter(time_control < 1500) %>%
ggplot(aes(x=white_elo)) +
geom_histogram(bins = 100) +
theme_bw()
meta %>%
ggplot(aes(x=blitz)) +
geom_bar(position = "stack") +
theme_bw()
meta %>%
filter(correspond == "TRUE") %>%
count()
## mutate to create a new column that has time control ##
df_eval <- game %>%
select(has_evals) %>%
group_by(has_evals) %>%
as.data.frame()
df_eval %>%
tbl_summary() %>%
modify_footnote(update = everything() ~ NA)
ggpiestats(
data = df_eval,
x = has_evals,
title = "Games with evaluation",
caption = "lichess.org",
package = "ggsci",
palette = "default_jama",
legend.title = "Has Evaluation?"
)
ecom2 %>%
dbplot::dbplot_bar(device) + ggplot2::xlab("Device") +
ggplot2::ylab("Count") + ggplot2::ggtitle("Device Distribution")
## ----line plot-----------------------------------------------------------
ecom2 %>%
dbplot::dbplot_line(n_visit) + ggplot2::xlab("Visits") + ggplot2::ylab("Count")
## ----simple regression---------------------------------------------------
ecom2 %>%
dplyr::select(duration, n_visit) %>%
modeldb::linear_regression_db(duration)
## ----multiple regression-------------------------------------------------
ecom2 %>%
dplyr::select(duration, n_visit, n_pages) %>%
modeldb::linear_regression_db(duration)
## ----categorical variables-----------------------------------------------
ecom2 %>%
dplyr::select(duration, device) %>%
modeldb::add_dummy_variables(device, values = c("laptop", "mobile", "tablet")) %>%
modeldb::linear_regression_db(duration)
## ----full example--------------------------------------------------------
ecom2 %>%
dplyr::select(duration, n_visit, n_pages, device) %>%
modeldb::add_dummy_variables(device, values = c("laptop", "mobile", "tablet")) %>%
modeldb::linear_regression_db(duration, auto_count = TRUE)
## ----linear model--------------------------------------------------------
model <- lm(duration ~ device + referrer + n_visit + n_pages, data = ecom2)
model
## ----add fitted values in a new column-----------------------------------
ecom2 %>%
tidypredict::tidypredict_to_column(model) %>%
dplyr::select(duration, fit)
## ----tidy eval formula-------------------------------
tidypredict::tidypredict_fit(model)
## ----use R code generated by tidypredict_fit to calculate fitted values--
ecom2 %>%
dplyr::mutate(
fit = 441.450192491919 + (ifelse(device == "mobile", 1, 0) *
-30.9522074131866) + (ifelse(device == "tablet", 1,
0) * -14.4972018107797) + (ifelse(referrer == "direct",
1, 0) * -8.98035001912995) + (ifelse(referrer == "google",
1, 0) * -10.038005625893) + (ifelse(referrer == "social",
1, 0) * -19.8411767075006) + (ifelse(referrer == "yahoo",
1, 0) * -32.0969778768984) + (n_visit * -1.4325653130794) +
(n_pages * -8.29825840984566)
) %>%
dplyr::select(duration, fit)
## ----sql translation of above step---------------------------------------
tidypredict::tidypredict_sql(model, con)
## ----disconnect----------------------------------------------------------
DBI::dbDisconnect(con)
set.seed(8)
cols <- grDevices::rainbow(50)
canvas_smoke(colors=cols)
dadjoke()