-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtibbletime.R
80 lines (62 loc) · 2.19 KB
/
tibbletime.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
# Rolling calculations in tibbletime
library(tibbletime)
library(dplyr)
library(tidyr)
# Facebook stock prices.
data(FB)
# Only a few columns
FB <- select(FB, symbol, date, open, close, adjusted)
# The function to use at each step is `mean`.
# The window size is 5
rolling_mean <- rollify(mean, window = 5)
rolling_mean
mutate(FB, mean_5 = rolling_mean(adjusted))
rolling_mean_2 <- rollify(mean, window = 2)
rolling_mean_3 <- rollify(mean, window = 3)
rolling_mean_4 <- rollify(mean, window = 4)
FB %>% mutate(
rm10 = rolling_mean_2(adjusted),
rm20 = rolling_mean_3(adjusted),
rm30 = rolling_mean_4(adjusted)
)
# Our data frame summary
summary_df <- function(x) {
data.frame(
rolled_summary_type = c("mean", "sd", "min", "max", "median"),
rolled_summary_val = c(mean(x), sd(x), min(x), max(x), median(x))
)
}
# A rolling version, with unlist = FALSE
rolling_summary <- rollify(~summary_df(.x), window = 5,
unlist = FALSE)
FB_summarised <- mutate(FB, summary_list_col = rolling_summary(adjusted))
FB_summarised
FB_summarised %>%
filter(!is.na(summary_list_col)) %>%
unnest(cols = c(summary_list_col))
rolling_summary <- rollify(~summary_df(.x), window = 5,
unlist = FALSE, na_value = data.frame())
FB_summarised <- mutate(FB, summary_list_col = rolling_summary(adjusted))
FB_summarised
FB_summarised %>%
unnest(cols = c(summary_list_col))
rolling_summary <- rollify(~summary_df(.x), window = 5,
unlist = FALSE,
na_value = data.frame(rolled_summary_type = NA,
rolled_summary_val = NA))
FB_summarised <- mutate(FB, summary_list_col = rolling_summary(adjusted))
FB_summarised %>% unnest(cols = c(summary_list_col))
# Reset FB
data(FB)
rolling_lm <- rollify(.f = function(close, high, low, volume) {
lm(close ~ high + low + volume)
},
window = 5,
unlist = FALSE)
FB_reg <- mutate(FB, roll_lm = rolling_lm(close, high, low, volume))
FB_reg
FB_reg %>%
filter(!is.na(roll_lm)) %>%
mutate(tidied = purrr::map(roll_lm, broom::tidy)) %>%
unnest(tidied) %>%
select(symbol, date, term, estimate, std.error, statistic, p.value)