将自定义函数映射到嵌套的daframe
我正在尝试将自定义函数映射到嵌套daframe。此函数使用dplyr和管道“%>%”运算符。
我尝试枚举该函数内部使用的变量,但是它一直无法正常工作。不确定如何执行此操作。
table <- expand.grid(x = c("bird", "dogs"), year = c(2018,2019), week= c(1:52)) %>%
mutate(christmas = case_when(week == 52 ~1, TRUE ~ 0),
ev = case_when(week == 30 ~ 1, TRUE ~ 0),
alpha = rnorm(n = 208, mean = 10))
myfun_model_alpha <- function(time_s, param, yr, wk){
event <- time_s %>% select(christmas,ev )
time_s <- ts(time_s$alpha, start = c(min(time_s$year),min(time_s$week[time_s$year == min(time_s$year)])),end = c(max(time_s$year),max(time_s$week[time_s$year == max(time_s$year)])), frequency = 52)
#time_s <- ts(time_s$alpha, start = c(2017,01),end = c(2019,20), frequency = 52)
ts_vec <- window(time_s, end = c(yr, wk))
leng <- length(ts_vec)
lambda <- BoxCox.lambda(ts_vec)
model <- auto.arima(ts_vec, lambda = lambda,
biasadj = TRUE,
xreg = matrix(c(fourier(ts_vec, K=param),
event$christmas[1:leng],
event$ev[1:leng]),
nrow = leng,
ncol = param*2+2),
seasonal = FALSE)
forecast <- forecast(model,
16,
xreg=matrix(c(fourier(ts_vec, K=param, h=16),
event$christmas[(leng+1):(leng+16)],
event$ev[(leng+1):(leng+16)]),
nrow = 16,
ncol = param*2+2))$mean
pred = tibble(forecast = forecast,
time_index = 1:16,
actual = window(time_s, start= c(yr,wk))[2:17])
return(pred)
}
# Applying the function t the nested df
table %>%
group_by(x) %>%
nest() %>%
mutate(data = map(data, ~arrange(.x, year, week)),
model = map(data, ~myfun_model_alpha(.,2,2019, 12))) %>%
unnest(model)
它返回错误,例如:“(函数(x)中的错误:未找到对象'圣诞节'”) 我正在尝试使用实际值(如果有)来预测16周的水平