-
Notifications
You must be signed in to change notification settings - Fork 20
/
2-benchmark-r.r
202 lines (142 loc) · 5.49 KB
/
2-benchmark-r.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
# To run the script, download the relevant packages:
# install.packages("data.table")
# install.packages("fst")
# install.packages("statar")
# install.packages("fixest")
# install.packages("ggplot")
# loading packages
library(data.table)
library(fixest)
library(statar)
library(fst)
library(ggplot2)
# setting options
options(mc.cores=4)
setFixest_nthreads(4)
# creating the file to merge with
write.fst(fread("~/statabenchmark/merge_string.csv", data.table = FALSE), "~/statabenchmark/merge_string.fst")
write.fst(fread("~/statabenchmark/merge_int.csv", data.table = FALSE), "~/statabenchmark/merge_int.fst")
# define the time function
time <- function(x){system.time(x)[3]}
out <- NULL
names <- NULL
i <- 0
# write and read
names <- append(names, "open csv")
out <- append(out, time(DT <- fread("~/statabenchmark/1e7.csv", data.table = FALSE)))
names <- append(names, "save binary")
out <- append(out, time(write.fst(DT, "~/statabenchmark/1e7.fst")))
names <- append(names, "open binary")
out <- append(out, time(DT <- read.fst("~/statabenchmark/1e7.fst")))
# sort and duplicates
setDT(DT)
names <- append(names, "sort string")
out <- append(out, time(setkeyv(DT, c("id3"))))
names <- append(names, "sort int")
out <- append(out, time(setkeyv(DT, c("id6"))))
names <- append(names, "sort float")
out <- append(out, time(setkeyv(DT, c("v3"))))
names <- append(names, "count distinct strings")
out <- append(out, time(uniqueN(DT, by = c("id3"))))
names <- append(names, "count distinct ints")
out <- append(out, time(uniqueN(DT, by = c("id6"))))
# merge
DT <- read.fst("~/statabenchmark/1e7.fst")
setDT(DT)
f <- function(){
DT_merge <- read.fst("~/statabenchmark/merge_string.fst")
setDT(DT_merge)
setkey(DT, id1, id3)
setkey(DT_merge, id1, id3)
merge(DT, DT_merge, all.x = TRUE, all.y = FALSE)
}
names <- append(names, "merge string")
out <- append(out, time(f()))
DT <- read.fst("~/statabenchmark/1e7.fst")
setDT(DT)
f <- function(){
DT_merge <- read.fst("~/statabenchmark/merge_int.fst")
setDT(DT_merge)
setkey(DT, id4, id6)
setkey(DT_merge, id4, id6)
merge(DT, DT_merge, all.x = TRUE, all.y = FALSE)
}
names <- append(names, "merge int")
out <- append(out, time(f()))
# append
names <- append(names, "append")
DT1 <- copy(DT)
out <- append(out, time(rbindlist(list(DT,DT1), fill = TRUE)))
# reshape
DT <- read.fst("~/statabenchmark/1e7.fst")
setDT(DT)
DT1 <- unique(DT, by = c("id1", "id2", "id3"))
DT1 <- DT1[1:(nrow(DT1)/10),]
names <- append(names, "reshape long")
out <- append(out, time(DT2 <- melt(DT1, id.vars = c("id1", "id2", "id3"))))
rm(DT1)
names <- append(names, "reshape wide")
out <- append(out, time(DT3 <- dcast(DT2, id1 + id2 + id3 ~ variable, value.var = "value")))
rm(list = c("DT2", "DT3"))
# recode
f <- function(){
DT[v1 == 1, v1_name := "first"]
DT[v1 %in% c(2,3), v1_name := "second"]
DT[v1 %in% c(4,5), v1_name := "third"]
}
names <- append(names, "recode")
out <- append(out, time(f()))
DT[, v1_name := NULL]
# functions
names <- append(names, "xtile")
out <- append(out, time(DT[, temp := xtile(v3, 10)]))
DT[, temp := NULL]
names <- append(names, "group strings")
out <- append(out, time(DT[, temp := .GRP, by = c("id1", "id3")]))
DT[, temp := NULL]
names <- append(names, "group int")
out <- append(out, time(DT[, temp := .GRP, by = c("id4", "id6")]))
DT[, temp := NULL]
# sum groups
names <- append(names, "sum over few groups (string)")
out <- append(out, time(DT[, temp := sum(v3, na.rm = TRUE), by = c("id1")]))
DT[, temp := NULL]
names <- append(names, "sum over many groups (string)")
out <- append(out, time(DT[, temp := sum(v3, na.rm = TRUE), by = c("id3")]))
DT[, temp := NULL]
names <- append(names, "sum over few groups (int)")
out <- append(out, time(DT[, temp := sum(v3, na.rm = TRUE), by = c("id4")]))
DT[, temp := NULL]
names <- append(names, "sum over many groups (int)")
out <- append(out, time(DT[, temp := sum(v3, na.rm = TRUE), by = c("id6")]))
DT[, temp := NULL]
# sd groups
names <- append(names, "sd over few groups (int)")
out <- append(out, time(DT[, temp := sd(v3, na.rm = TRUE), by = c("id4")]))
DT[, temp := NULL]
names <- append(names, "sd over many groups (int)")
out <- append(out, time(DT[, temp := sd(v3, na.rm = TRUE), by = c("id6")]))
DT[, temp := NULL]
# collapse large groups
names <- append(names, "collapse over few groups")
out <- append(out, time(DT[, list(v1 = mean(v1, na.rm = TRUE), v2 = mean(v2, na.rm = TRUE), v3 = sum(v3, na.rm = TRUE), sd = sd(v3, na.rm = TRUE)), by = c("id1")]))
# collapse small groups
names <- append(names, "collapse over many groups")
out <- append(out, time(DT[, list(v1 = mean(v1, na.rm = TRUE), v2 = mean(v2, na.rm = TRUE), v3 = sum(v3, na.rm = TRUE), sd = sd(v3, na.rm = TRUE)), by = c("id3")]))
# regress
DT1 <- DT[1:(nrow(DT)/2),]
names <- append(names, "reg")
out <- append(out, time(feols(v3 ~ v1 + v2 + id4 + id5, DT1)))
names <- append(names, "reg fe")
out <- append(out, time(feols(v3 ~ v2 + id4 + id5 + as.factor(v1), DT1)))
names <- append(names, "reg hfe")
## Automatically clusters by id6 too
out <- append(out, time(feols(v3 ~ v2 + id4 + id5 + as.factor(v1) | id6, DT1)))
names <- append(names, "reg 2 hfe")
## Automatically clusters by id6 too)
out <- append(out, time(feols(v3 ~ v2 + id4 + id5 + as.factor(v1) | id6 + id3, DT1)))
# plot
names <- append(names, "plot 1000 points")
out <- append(out, time(ggsave("~/statabenchmark/plot.pdf", ggplot(DT1[1:1000], aes(x = v1, y = v2)) + geom_point())))
# run benchmark
fwrite(data.table(command = names, result = out), "~/statabenchmark/resultR1e7.csv")