forked from matthieugomez/benchmark-stata-r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-benchmark-r.r
164 lines (135 loc) · 5.15 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
# To run the script, download the relevant packages:
# install.packages("data.table")
# install.packages("tidyr")
# install.packages("statar")
# install.packages("lfe")
# install.packages("fst")
# loading packages
library(data.table)
library(tidyr)
library(lfe)
library(statar)
library(fst)
# setting options
options(mc.cores=4)
# creating the file to merge with
write.fst(fread("~/merge_string.csv", data.table = FALSE), "~/merge_string.fst")
write.fst(fread("~/merge_int.csv", data.table = FALSE), "~/merge_int.fst")
# define the time function
time <- function(x){sum(system.time(x)[1:2])}
out <- NULL
names <- NULL
# write and read
names[length(names)+1] <- "open csv"
out[length(out)+1] <- time(DT <- fread("~/1e7.csv", data.table = FALSE))
names[length(names)+1] <- "save binary"
out[length(out)+1] <- time(write.fst(DT, "~/1e7.fst"))
names[length(names)+1] <- "open binary"
out[length(out)+1] <- time(DT <- read.fst("~/1e7.fst"))
# sort and duplicates
setDT(DT)
names[length(names)+1] <- "sort string"
out[length(out)+1] <- time(setkeyv(DT, c("id3")))
names[length(names)+1] <- "sort int"
out[length(out)+1] <- time(setkeyv(DT, c("id6")))
names[length(names)+1] <- "sort float"
out[length(out)+1] <- time(setkeyv(DT, c("v3")))
names[length(names)+1] <- "count distinct strings"
out[length(out)+1] <- time(uniqueN(DT, by = c("id3")))
names[length(names)+1] <- "count distinct ints"
out[length(out)+1] <- time(uniqueN(DT, by = c("id6")))
# merge
DT <- read.fst("~/1e7.fst")
setDT(DT)
f <- function(){
DT_merge <- read.fst("~/merge_string.fst")
setDT(DT_merge)
setkeyv(DT, c("id1", "id3"))
setkeyv(DT_merge, c("id1", "id3"))
merge(DT, DT_merge, all.x = TRUE, all.y = FALSE)
}
names[length(names)+1] <- "merge string"
out[length(out)+1] <- time(f())
DT <- read.fst("~/1e7.fst")
setDT(DT)
f <- function(){
DT_merge <- read.fst("~/merge_int.fst")
setDT(DT_merge)
setkeyv(DT, c("id4", "id6"))
setkeyv(DT_merge, c("id4", "id6"))
merge(DT, DT_merge, all.x = TRUE, all.y = FALSE)
}
names[length(names)+1] <- "merge int"
out[length(out)+1] <- time(f())
# append
names[length(names)+1] <- "append"
DT1 <- copy(DT)
out[length(out)+1] <- time(rbindlist(list(DT,DT1), fill = TRUE))
# reshape
DT <- read.fst("~/1e7.fst")
setDT(DT)
DT1 <- unique(DT, by = c("id1", "id2", "id3"))
DT1 <- DT1[1:(nrow(DT1)/10)]
names[length(names)+1] <- "reshape long"
out[length(out)+1] <- time(DT2 <- gather(DT1, variable, value, id4, id5, id6, v1, v2, v3))
rm(DT1)
names[length(names)+1] <- "reshape wide"
out[length(out)+1] <- time(DT3 <- spread(DT2, variable, 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[length(names)+1] <- "recode"
out[length(out)+1] <- time(f())
DT[, v1_name := NULL]
# functions
names[length(names)+1] <- "xtile"
out[length(out)+1] <- time(DT[, temp := xtile(v3, 10)])
DT[, temp := NULL]
names[length(names)+1] <- "group strings"
out[length(out)+1] <- time(DT[, temp := .GRP, by = c("id1", "id3")])
DT[, temp := NULL]
names[length(names)+1] <- "group int"
out[length(out)+1] <- time(DT[, temp := .GRP, by = c("id4", "id6")])
DT[, temp := NULL]
# sum groups
names[length(names)+1] <- "sum over large groups (string)"
out[length(out)+1] <- time(DT[, temp := sum(v3, na.rm = TRUE), by = c("id1")])
DT[, temp := NULL]
names[length(names)+1] <- "sum over small groups (string)"
out[length(out)+1] <- time(DT[, temp := sum(v3, na.rm = TRUE), by = c("id3")])
DT[, temp := NULL]
names[length(names)+1] <- "sum over large groups (int)"
out[length(out)+1] <- time(DT[, temp := sum(v3, na.rm = TRUE), by = c("id4")])
DT[, temp := NULL]
names[length(names)+1] <- "sum over small groups (int)"
out[length(out)+1] <- time(DT[, temp := sum(v3, na.rm = TRUE), by = c("id6")])
DT[, temp := NULL]
# sd groups
names[length(names)+1] <- "sd over large groups (int)"
out[length(out)+1] <- time(DT[, temp := sd(v3, na.rm = TRUE), by = c("id4")])
DT[, temp := NULL]
names[length(names)+1] <- "sd over small groups (int)"
out[length(out)+1] <- time(DT[, temp := sd(v3, na.rm = TRUE), by = c("id6")])
DT[, temp := NULL]
# collapse large groups
names[length(names)+1] <- "collapse over large groups"
out[length(out)+1] <- 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[length(names)+1] <- "collapse over small groups"
out[length(out)+1] <- 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[length(names)+1] <- "reg"
out[length(out)+1] <- time(felm(v3 ~ v1 + v2 + id4 + id5, DT1))
names[length(names)+1] <- "reg fe"
out[length(out)+1] <- time(felm(v3 ~ v2 + id4 + id5 + as.factor(v1), DT1))
names[length(names)+1] <- "reg hfe"
out[length(out)+1] <- time(felm(v3 ~ v2 + id4 + id5 + as.factor(v1) | id6 | 0 | id6, DT1))
names[length(names)+1] <- "reg 2 hfe"
out[length(out)+1] <- time(felm(v3 ~ v2 + id4 + id5 + as.factor(v1) | id6 + id3 | 0 | id6, DT1))
# run benchmark
fwrite(data.table(command = names, result = out), "~/resultR1e7.csv")