-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoding.r
351 lines (248 loc) · 7.49 KB
/
coding.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
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
# general
getwd()
setwd('/home/alex/code')
opions(...) # digits, error, width
options(stringsAsFactors = FALSE)
source('file.R')
rm(list=ls())
.libPaths()
installed.packages()
library(package)
require(package)
install.packages('package', dependencies = TRUE)
update.packages()
library('devtools')
devtools::install_github(package)
devtools # installing packages from github
vroom # reading csv, can read from internet, can read number of files at once
stringr # strings
dplyr # manipulation with dataframes
tidyr #
ggplot2 # visualization
forecast
sessionInfo()
# types
typeof(x1)
is.double(x)
is.null(l$string)
as.double(x)
as.Date(date, '%d %m %Y')
# conditions, loops
if {} else {}
if_else(x>1, 'high', 'low') # vector version of if. can be embedded
repeat {}
while {}
for (i in 1:8) {} # next, break
# functions
f <- function(x){
y <<- x+1 # to global environment
return x+1 # return ca be omitted
}
%>% # pipeline to avoid calling function inside the function. can use . in the next function to specify where to put result
replicate(5, fun(100))
outer(letters, LETTERS, paste0)
Vectorize(f, 'arg')
do.call(rbind, list(df1, df2, df3))
# S3 (method dispatch, generic functions)
z <- list(...)
class(z) = 'Z'
methods(print)
print.Z <- function(z) {...}
grade <- function(obj) { # generic function, such as print
UseMethod("grade")
}
# S4
setClass('Z', representation(name='character', salary='numeric'))
z = new('Z', name='Joe', salary=100)
z = Z(name='Joe', salary=100)
z@name
slot(z, name)
showMethods(show)
setMethod('show', 'Z', function(object){...}) # generic S4 function, such as show
# Reference classes
student <- setRefClass("student", fields = list(name = "character", age = "numeric", GPA = "numeric"
s <- student(name = "John", age = 21, GPA = 3.5, methods = list(inc_age = function(x) {age <<- age + x}))
square <- Polygon$new(sides = 4)
s$name
Person$methods(say_hello = function() message("Hi!"))
s$copy()
# R6
factory <- R6Class('Thing', inherit = parent_factory, private = list(...), public = list(fun = function(){})) # use shared for static
factory$new()
# useful functions
all.equal(.1 + .05 == .15)
sort(x)
unique(x)
all, any
sum, max
which(1:10==5) # position of elements which is true
which.max # only one element
1:5 %in% c(1, 2, 5)
# data structures
vector(mode='character', length=2)
matrix(1:6, nrow=2, ncol=3, byrow=FALSE)
list(name = 'john', 1, vec, c(l1, l2))
new.env()
data.frame(x = 1:4, b = c(T,F), row.names = c(1, 2, 3, 4))
## vector creating
5:8
c(5, 8)
c(uno = 1, dos = 2, 3)
seq(1, 2, by=.25)
rep(x, times=3)
rep(x, each=4)
set.seed(42)
sample(1:100, 50, replace = TRUE)
vec = unlist(l)
## indexing
x[-c(1,3)]
x(c(TRUE, FALSE))
x(x > 1 & x < 3)
x(c('uno', 'dos'))
m[2,]
m[, c(1,3,5)]
l[1]
l[[1]] # returns one element
l$node
l['node']
df$column # returns vector
df['column'] # returns subtable
df[3:4, -1]
df[c(F, T), c('x', 'z')]
df[, 1, drop = FALSE]
df[df$x > 2, ]
subset(df, x > 2, select = c(x, z))
## functions for structures
length(x) # can be changed
names(x) # can be changed
dim(x) # nrow, ncol
rowSums, colMeans
rownames, colnames # for dataframes
rbind, cbind
%*%
attr(x, 'author') <- 'Caesar' # for lists
attr(x, 'names')
attributes(x)
# dataframes
read.table(file='name', header=TRUE, sep=',') # na.strings, colClasses, skip
read.csv # different defailt values
write.table
write.csv
str, summary, head, tail
df[complete.cases(df)]
any(!complete.cases(df))
na.omit(df)
df$var <- NULL
within(df, a, b)
merge(df, df_salary, by = 'x')
# dplyr
filter(df, column == 'column_value' & column2 > 10 & column3 %in% c(1, 2, 3))
select(df, column1, column2)
select(df, column1:column5)
select_at(df, vars(matches('^s')))
select_if(df, is.numeric)
arrange(df, month)
rename(df, column_new_name=column_old_name) # there are rename_if, rename_at, rename_all
mutate(df, c = a/b) # mutate_if, mutate_at. transmute remove all columns besides specified
mutate(group_by(department), total_dep = sum(total)) # does not reduce number of rows
summarize(group_by(df, date), sessions = sum(sessions)) # sumamrize_if, sumamrize_at
summarize_at(group_by(df, date, medium), c('sessions', 'bounces'), min)
summarize_if(group_by(df, date, medium), is.numeric, list(avg=mean, count=length), na.rm=TRUE) # avg, cout are suffces for resulted colunns
bind_rows(df1, df2)
left_join(df1, df2, by=c('id'='employe_id', 'month')) # semi_join, anti_join
distinct(df)
# tidyr
fill(df, region, .direction='down')
pivot_longer(df, cols='jan:dec', names_to='month', values_to='sales')
separate(df, col='month', into=c('month', 'year'), remove=TRUE, sep=' ')
pivot_wider(df, names_from=year, values_from=sales)
build_wider_spec # store transformation
pivot_wider_spec # apply stored transformation
# factors
f = factor(c('a', 'b', 'a', 'c')) # numeric and character at the same time
f = ordered(c('a', 'b', 'a', 'c'), c('a', 'b', 'c'))
df$a <- as.factor(df$a)
levels(f)
levels[f][1] <- = 'bbb'
nlevels(f)
droplevels(f) # remove empty levels
table(cut(norm(10, -5:5)))
# apply
apply(xm, 1, f)
mapply(seq, from=1:4, to=2:5, by=.8)
lapply(l, length) # sapply simplifies it to vector
lapply(l, paste, collapse='|')
tapply(df$factor, df$number, max)
# strings
paste(c('a', 'b'), 'x', sep='_')
paste(c('a', 'b'), 'x', colapse=',')
strsplit(s, ' ', fixed = TRUE) # if fixed is FALSE then string is considered as regular expression
grep('work', s) # to find, grepl returns vector of booleans
gsub('work', '###', s) # to change
tolower, toupper
library(stringr)
str_extract # _all can be added to the function name
str_replace
format(date, '%d %B %Y')
formatC(pi, digits=3, format="e")
# files
dir(pattern='.*\\.csv$')
list.files(pattern='.*\\.csv$')
list.dirs('..', recursive = FALSE)
readLines('file.csv', 5)
# ggplot2
qplot(x=month, y=n, data=df, fill='darkcyan', geom='col', main='title', xlab='month', ylab='sales') # fill=shop. group='shop'
# geom=c('line', 'point'), c('boxplot'), c('histogram')
####
y <- ts(c(123,39,78,52,110), start=2012)
y <- ts(z, start=2003, frequency=12)
# plot
autoplot(melsyd[,"Economy.Class"]) +
ggtitle("Economy class passengers: Melbourne-Sydney") +
xlab("Year") +
ylab("Thousands")
# histogram
gghistogram(res) + ggtitle("Histogram of residuals")
# seasonal plot
ggseasonplot(a10, year.labels=TRUE, year.labels.left=TRUE) +
ylab("$ million") +
ggtitle("Seasonal plot: antidiabetic drug sales")
# scatterplot
qplot(Temperature, Demand, data=as.data.frame(elecdemand)) +
ylab("Demand (GW)") + xlab("Temperature (Celsius)")
# multiple plots
autoplot(visnights[,1:5], facets=TRUE) +
ylab("Number of visitor nights each quarter (millions)")
# scatterplot matrix
GGally::ggpairs(as.data.frame(visnights[,1:5]))
# lag plot
gglagplot(beer2)
# auto-correlation
ggAcf(beer2)
# naive methods
meanf(y, h)
naive(y, h)
snaive(y, h)
rwf(y, h, drift=TRUE)
rwf(eggs, drift=TRUE, lambda=0, h=50, level=80, biasadj=TRUE)
forecast(ausbeer, h=4)
residuals(naive(goog200))
Box.test(res, lag=10, fitdf=0)
Box.test(res,lag=10, fitdf=0, type="Lj")
checkresiduals(naive(goog200))
accuracy(beerfit1, beer3)
e <- tsCV(goog200, rwf, drift=TRUE, h=1)
goog200 %>% tsCV(forecastfunction=rwf, drift=TRUE, h=1) -> e
window(ausbeer, start=1995)
subset(ausbeer, quarter = 1)
head(ausbeer, 4*5)
tail(ausbeer, 4*5)
dframe <- cbind(Monthly = milk, DailyAverage = milk/monthdays(milk))
fit.consMR <- tslm(
Consumption ~ Income + Production + Unemployment + Savings,
data=uschange)
summary(fit.consMR)
fit.beer <- tslm(beer2 ~ trend + season)
fourier.beer <- tslm(beer2 ~ trend + fourier(beer2, K=2))
# measures of predictive accuracy
CV(fit.consMR)