[英]Dynamically insert variables into a fable model using rlang
I am trying to dynamically insert variables into a fable model.我正在尝试将变量动态插入到寓言 model 中。
library(dplyr)
library(fable)
library(stringr)
df <- tsibbledata::aus_retail %>%
filter(State == "Victoria", Industry == "Food retailing") %>%
mutate(reg_test = rnorm(441, 5, 2),
reg_test2 = rnorm(441, 5, 2))
Note that there can be an undetermined number of regressors included in the tsibble, but in this example, I have only two ( reg_test
and reg_test2
).请注意,tsibble 中可能包含不确定数量的回归量,但在本例中,我只有两个( reg_test
和reg_test2
)。 All regressor columns will start with reg_
所有回归量列都将以reg_
I have a function where I want to dynamically put the regressor columns into an ARIMA model using the fable package.我有一个 function,我想使用寓言 package 将回归量列动态放入 ARIMA model。
test_f <- function(df) {
var_names <- str_subset(names(df), "reg_") %>%
paste0(collapse = "+")
test <- enquo(var_names)
df %>%
model(ARIMA(Turnover ~ !!test))
}
test_f(df)
# A mable: 1 x 3
# Key: State, Industry [1]
State Industry `ARIMA(Turnover ~ ~"reg_test+reg_tes~
<chr> <chr> <model>
1 Victoria Food retaili~ <NULL model>
Warning message:
1 error encountered for ARIMA(Turnover ~ ~"reg_test+reg_test2")
[1] invalid model formula in ExtractVars
I know that it is just putting the string var_names
into the formula, which does not work, but I can't figure out how to create var_names
in such a way that I can enquo()
it correctly.我知道它只是将字符串var_names
放入公式中,这是行不通的,但我不知道如何以我可以正确enquo()
的方式创建var_names
。
I read through the Quasiquotation section here I searched SO but have not found the answer yet.我在这里阅读了 Quasiquotation 部分,我搜索了 SO 但还没有找到答案。
This question with pasre_expr()
seemed to get closer, but still not what I wanted. 这个问题与pasre_expr()
似乎越来越接近,但仍然不是我想要的。
I know that I can use sym()
if I have one variable, but I don't know how many reg_
variables there will be and I want to include them all.我知道我可以使用sym()
如果我有一个变量,但我不知道会有多少reg_
变量,我想把它们都包括在内。
By putting in the variables manually, I can show the output that I expect.通过手动输入变量,我可以显示我期望的 output。
test <- df %>%
model(ARIMA(Turnover ~ reg_test + reg_test2))
test$`ARIMA(Turnover ~ reg_test + reg_test2)`[[1]]
Series: Turnover
Model: LM w/ ARIMA(2,1,0)(0,1,2)[12] errors
Coefficients:
ar1 ar2 sma1 sma2 reg_test reg_test2
-0.6472 -0.3541 -0.4115 -0.0793 -0.0296 -0.6143
s.e. 0.0473 0.0479 0.0520 0.0446 0.5045 0.5273
sigma^2 estimated as 884.9: log likelihood=-2058.04
AIC=4130.08 AICc=4130.35 BIC=4158.5
I also imagine that there is a better way for me to make the formula in the ARIMA
function. If this can fix my problem as well, that will work too.我还想象有更好的方法让我在ARIMA
function 中制作公式。如果这也能解决我的问题,那也行。
I appreciate any help!感谢您的帮助!
You're possibly making this a bit more complicated than it needs to be.您可能会使它变得比需要的更复杂。 You can convert a string to a formula by doing as.formula(string)
, so simply build your formula as a string, convert it to a formula, then feed it to ARIMA
.您可以通过执行as.formula(string)
将字符串转换为公式,因此只需将公式构建为字符串,将其转换为公式,然后将其提供给ARIMA
即可。 Here's a reprex:这是一个代表:
library(dplyr)
library(fable)
library(stringr)
df <- tsibbledata::aus_retail %>%
filter(State == "Victoria", Industry == "Food retailing") %>%
mutate(reg_test = rnorm(441, 5, 2),
reg_test2 = rnorm(441, 5, 2))
test_f <- function(df) {
var_names <- paste0(str_subset(names(df), "reg_"), collapse = " + ")
mod <- model(df, ARIMA(as.formula(paste("Turnover ~", var_names))))
unclass(mod[1, 3][[1]])[[1]]
}
test_f(df)
#> Series: Turnover
#> Model: LM w/ ARIMA(2,1,0)(0,1,1)[12] errors
#>
#> Coefficients:
#> ar1 ar2 sma1 reg_test reg_test2
#> -0.6689 -0.376 -0.4765 0.3363 1.0194
#> s.e. 0.0448 0.045 0.0426 0.4978 0.5436
#>
#> sigma^2 estimated as 883.1: log likelihood=-2058.28
#> AIC=4128.56 AICc=4128.76 BIC=4152.91
Created on 2020-04-23 by the reprex package (v0.3.0)由reprex package (v0.3.0) 创建于 2020-04-23
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.