-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_2018-11-12.R
134 lines (95 loc) · 2.49 KB
/
3_2018-11-12.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
mds <- cmdscale(dist(mtcars))
mds
plot(mds)
library(ggplot2)
mds <- as.data.frame(mds)
ggplot(mds, aes(V1, V2, label = rownames(mtcars))) + geom_text()
mtcars
which(rownames(mtcars) == 'Camaro Z28')
mds[24, ]
str(dist(mtcars))
str(as.matrix(dist(mtcars)))
sort(as.matrix(dist(mtcars))[, 24])
str(mtcars)
library(data.table)
dtcars <- data.table(mtcars, keep.rownames = TRUE)
dtcars
setorder(dtcars, rn)
dtcars
setorder(dtcars, hp)
dtcars
mds
## standardizing --> transform data to mean = 0, sd = 1
dtcars$hp
dtcars$am
summary(dtcars)
x <- (dtcars$hp - mean(dtcars$hp)) / sd(dtcars$hp)
sd(x)
hist(x)
scale(dtcars$hp)
x
plot(x, scale(dtcars$hp)) # manually calculated vs with the scale function -> pretty much the same! :)
scale(mtcars)
mds <- cmdscale(dist(scale(mtcars)))
mds <- as.data.frame(mds)
ggplot(mds, aes(V1, V2, label = rownames(mtcars))) + geom_text()
##########################################################################################
## time-series
install.packages('devtools')
devtools::install_github('daroczig/binancer') #using solely that function from the package
library(binancer)
?binance_klines
binance_klines('ETHBTC')
prices <- binance_klines('BTCUSDT', interval = '1d')
str(prices)
?ts
tsx <- ts(prices$close, frequency = 7)
plot(tsx)
plot(decompose(tsx))
str(decompose(tsx))
decompose(tsx)$seasonal[1:7]
install.packages('forecast')
library(forecast)
plot(tsx)
fit <- naive(tsx)
plot(fit)
fit
accuracy(fit) ## in-sample => cross validation
?ma
lines(ma(tsx, 2), col = 'blue')
lines(ma(tsx, 7), col = 'red')
lines(ma(tsx, 4 * 7), col = 'green')
fit <- ses(tsx)
plot(fit)
lines(fitted(fit), col = 'red')
accuracy(fit)
accuracy(fit)
fit <- auto.arima(tsx)
plot(fit)
fit
fit
plot(predict(fit))
#############################################################################################
install.packages('fpp2')
library(fpp2)
plot(gasoline)
autoplot(forecast(gasoline))
naive(gasoline)
autoplot(forecast(naive(gasoline)))
autoplot(forecast(naive(gasoline, h = 52))) # h -> how many forecasts I would like to see here
ma(gasoline, 4)
ses(gasoline)
?ma
autoplot(forecast(ses(gasoline, h = 52)))
accuracy(forecast(ses(gasoline, h = 52)))
accuracy(forecast(naive(gasoline, h = 52)))
fit <- ets(gasoline)
frequency(gasoline)
fit <- ets(ts(gasoline, frequency = 4)) # frequency = 4 -> monthly patterns (4 weeks)
autoplot(forecast(fit, h = 52))
accuracy(forecast(fit, h = 52))
## slow
fit <- tbats(gasoline)
fit
autoplot(forecast(fir, h = 52))
autoplot(forecast(fir, h = 52))