# 滚动平方根等功能

``````dat <- data.frame(
date <- seq.Date(from = as.Date("2010-01-01"), by = 1, length.out = 100),
pred <- sample(1000, 100, replace = FALSE),
actual <- sample(1000, 100, replace = FALSE)
) %>%
setNames(c("date", "pred", "actual"))
``````

``````          date pred actual
1   2010-01-01   99    835
2   2010-01-02  429    779
3   2010-01-03  726    581
``````

``````window_size = 30 + 1 -1
dat %>%
arrange(desc(date)) %>%
mutate(
error = (pred - actual),
squared_error = error**2,

#rolling calcs
rolling_mean_error = c(rollapply(error, width = window_size, by = 1, FUN = mean), rep(NA, window_size - 1))
)
``````

``````squared_function <- function(err){
err**2
}

dat %>%
arrange(desc(date)) %>%
mutate(
error = (pred - actual),
squared_error = error**2,

#rolling calcs
rolling_mean_error = c(rollapply(error, width = window_size, by = 1, FUN = mean), rep(NA, window_size - 1)),
rolling_squared_error = c(rollapply(error, width = window_size, by = 1, FUN = squared_function), rep(NA, window_size - 1))
)
``````

``````library(dplyr)
library(zoo)
``````
r2evans

`squared_function`应该返回一个数字，而不是与输入长度相同的向量。我怀疑您需要`sum(err**2)`（平方和）或`sqrt(sum(err**2))`

``````
set.seed(42)
dat <- tibble(
date   = seq.Date(from = as.Date("2010-01-01"), by = 1, length.out = 100),
pred   = sample(1000, 100, replace = FALSE),
actual = sample(1000, 100, replace = FALSE)
) %>%
setNames(c("date", "pred", "actual"))

window_size <- 30 + 1 -1

squared_function <- function(err) sum(err**2)

dat2 <- dat %>%
arrange(desc(date)) %>%
mutate(
error = (pred - actual),
squared_error = error**2,
rolling_mean_error = zoo::rollapply(
error, width = window_size, by = 1, FUN = mean,
align = "left", fill = NA),
rolling_squared_error = zoo::rollapply(
error, width = window_size, by = 1, FUN = squared_function,
align = "left", fill = NA)
)
dat2
# # A tibble: 100 x 7
#    date        pred actual error squared_error rolling_mean_error rolling_squared_error
#    <date>     <int>  <int> <int>         <dbl>              <dbl>                 <dbl>
#  1 2010-04-10   558    659  -101         10201              -93.7               5334540
#  2 2010-04-09   672    671     1             1              -88.6               5326839
#  3 2010-04-08   466    102   364        132496              -70.7               5616282
#  4 2010-04-07   302    481  -179         32041              -67.0               5711315
#  5 2010-04-06   665     49   616        379456              -66.6               5707835
#  6 2010-04-05   839     66   773        597529              -86.8               5328479
#  7 2010-04-04   954    908    46          2116             -103.                4817975
#  8 2010-04-03   190    118    72          5184              -92.8               4935575
#  9 2010-04-02     1    713  -712        506944              -90.2               4952295
# 10 2010-04-01   608    944  -336        112896              -53.0               4610187
# # ... with 90 more rows
``````

``````vec <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
zoo::rollapply(vec, 5, FUN = mean)
#  [1] 3 4 5 6 7 6 5 4 3 2 3 4 5 6 7 6
``````

``````zoo::rollapply(vec, 5, FUN = mean, fill = NA)
#  1,  2,  3,  4,  5,  6,  7,  8,  9,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  0
# `------. ,--------'
#         v
# __, __, __, __, __
#  3,                 align="left"
# NA, NA,  3          align="right"
# NA, NA, NA, NA,  3  align="right"
``````

0 条评论