Мы можем сделать group_split
и map
над элементами list
и затем применить rollapply
library(zoo)
library(dplyr)
library(purrr)
out <- df %>%
group_split(stock) %>%
map(~ rollapply(.x,
width = 30,
FUN = function(dat) {
LinearModel = lm(formula = Close ~ dates, as.data.frame(dat))
LinearModel$coef
}, by.column = FALSE, fill = NA_real_, align = "right"))
length(out)
#[1] 2
Если мы хотим обновить исходный набор данных с большим количеством столбцов
out <- df %>%
group_split(stock) %>%
map_dfr(~ {
subdat <- .x
rollapply(subdat,
width = 30,
FUN = function(dat) {
LinearModel = lm(formula = Close ~ dates, as.data.frame(dat))
LinearModel$coef
}, by.column = FALSE, fill = NA_real_, align = "right") %>%
as.data.frame %>%
bind_cols(subdat, .)
}
)
ncol(out)
#[1] 38
ncol(df)
#[1] 8
В devel-версии dplyr
мы также можем сделать
out1 <- df %>%
group_by(stock) %>%
condense(out =rollapply(cur_data(), width = 30,
FUN = function(dat) lm(Close ~ dates, as.data.frame(dat))$coef,
by.column = FALSE, fill = NA_real_, align = "right") %>%
as.data.frame %>%
bind_cols(cur_data(), .))
out1
# A tibble: 2 x 2
# Rowwise: stock
# stock out
# <chr> <list>
#1 1 <tibble [3,309 × 37]>
#2 2 <tibble [3,309 × 37]>
Столбец list
может быть unnest
ed, когда это требуется
out1 %>%
unnest(c(out)) %>%
head(3)
# A tibble: 3 x 38
# stock Open High Low Close Volumn Adjusted dates `(Intercept)` `dates2007-01-0…
# <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <date> <dbl> <dbl>
#1 1 232. 237. 230. 233. 1.55e7 233. 2007-01-03 NA NA
#2 1 234. 241. 233. 241. 1.58e7 241. 2007-01-04 NA NA
#3 1 240. 243. 238. 243. 1.38e7 243. 2007-01-05 NA NA
# … with 28 more variables: `dates2007-01-05` <dbl>, `dates2007-01-08` <dbl>,
# `dates2007-01-09` <dbl>, `dates2007-01-10` <dbl>, `dates2007-01-11` <dbl>,
# `dates2007-01-12` <dbl>, `dates2007-01-16` <dbl>, `dates2007-01-17` <dbl>,
# `dates2007-01-18` <dbl>, `dates2007-01-19` <dbl>, `dates2007-01-22` <dbl>,
# `dates2007-01-23` <dbl>, `dates2007-01-24` <dbl>, `dates2007-01-25` <dbl>,
# `dates2007-01-26` <dbl>, `dates2007-01-29` <dbl>, `dates2007-01-30` <dbl>,
# `dates2007-01-31` <dbl>, `dates2007-02-01` <dbl>, `dates2007-02-02` <dbl>,
# `dates2007-02-05` <dbl>, `dates2007-02-06` <dbl>, `dates2007-02-07` <dbl>,
# `dates2007-02-08` <dbl>, `dates2007-02-09` <dbl>, `dates2007-02-12` <dbl>,
# `dates2007-02-13` <dbl>, `dates2007-02-14` <dbl>
Мы можем применить tidy
в пределах condense
library(broom)
out3 <- df %>%
group_split(stock) %>%
map_dfr(~ {
subdat <- .x
rollapply(subdat,
width = 30,
FUN = function(dat) {
LinearModel = lm(formula = Close ~ dates, as.data.frame(dat))
tidy(LinearModel)
}, by.column = FALSE, fill = NA_real_, align = "right") %>%
as.data.frame %>%
bind_cols(subdat, .)
}
)
dim(out3)
#[1] 6618 13
names(out3)
# [1] "Open" "High" "Low" "Close" "Volumn" "Adjusted" "stock"
# [8] "dates" "term" "estimate" "std.error" "statistic" "p.value"