R中magrittr和arima的兼容性问题

compatibility issue of magrittr and arima in R

考虑以下示例:

library(tidyverse)
set.seed(1)
forecast::forecast
x <- cumsum(rnorm(10))
y1 <- arima(x, order = c(1, 0, 0))
y2 <- x %>% arima(order = c(1, 0, 0))

length(fitted(y1))
[1] 10
length(fitted(y2))
[1] 0

对象 y1y2 几乎相同,唯一的例外是插槽 callseries。所以我想这就是 fitted 函数开始发挥其魔力的地方。

我真的很想使用 y1 而不是 y2。 有谁知道 fitted 的替代函数会产生相同的结果?

编辑2: 如果 forecast 包未加载到命名空间(通过例如 forecast::forecast),则上述 "bug" 不会出现。 我不知道将包加载到命名空间会改变某些函数的行为。

编辑: 因为代码似乎不可重现,所以我添加了我的“sessionInfo()”

R version 3.5.2 (2018-12-20)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=German_Austria.1252  LC_CTYPE=German_Austria.1252    LC_MONETARY=German_Austria.1252 LC_NUMERIC=C                    LC_TIME=German_Austria.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] forcats_0.4.0   stringr_1.3.1   dplyr_0.8.0.1   purrr_0.3.0     readr_1.3.1     tidyr_0.8.2     tibble_2.0.1    ggplot2_3.1.0   tidyverse_1.2.1 magrittr_1.5   

loaded via a namespace (and not attached):
 [1] zoo_1.8-4         tidyselect_0.2.5  urca_1.3-0        aTSA_3.1.2        haven_2.0.0       lattice_0.20-38   colorspace_1.4-0  generics_0.0.2    yaml_2.2.0        utf8_1.1.4        rlang_0.3.1       pillar_1.3.1     
[13] withr_2.1.2       glue_1.3.0        forecast_8.5      TTR_0.23-4        modelr_0.1.2      readxl_1.2.0      plyr_1.8.4        quantmod_0.4-13   timeDate_3043.102 munsell_0.5.0     gtable_0.2.0      cellranger_1.1.0 
[25] rvest_0.3.2       tseries_0.10-46   lmtest_0.9-36     parallel_3.5.2    curl_3.3          fansi_0.4.0       broom_0.5.1       xts_0.11-2        Rcpp_1.0.0        scales_1.0.0      backports_1.1.3   jsonlite_1.6     
[37] fracdiff_1.4-2    hms_0.4.2         stringi_1.3.1     grid_3.5.2        cli_1.0.1         quadprog_1.5-5    tools_3.5.2       lazyeval_0.2.1    crayon_1.3.4      pkgconfig_2.0.2   xml2_1.2.0        lubridate_1.7.4 

你确定的是评估不规范导致的问题,在the technical note中关于magrittr管道有简要提及:

The magrittr pipe operators use non-standard evaluation. They capture their inputs and examines them to figure out how to proceed. First a function is produced from all of the individual right-hand side expressions, and then the result is obtained by applying this function to the left-hand side. For most purposes, one can disregard the subtle aspects of magrittr's evaluation, but some functions may capture their calling environment, and thus using the operators will not be exactly equivalent to the "standard call" without pipe-operators.

如果您查看 fittedarima 版本的源代码,您会发现您认为 call 属性对于方法的操作必不可少:

getAnywhere(fitted.Arima)
A single object matching ‘fitted.Arima’ was found
It was found in the following places
  registered S3 method for fitted from namespace TSA
  namespace:TSA
with value

function (object, ...) 
{
    fitted = eval(object$call$x) - object$residuals
    fitted
}
<bytecode: 0x000000001e8ff4d8>
<environment: namespace:TSA>