gpt4 book ai didi

r - R中magrittr和arima的兼容性问题

转载 作者:行者123 更新时间:2023-12-05 00:10:58 25 4
gpt4 key购买 nike

考虑以下示例:

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几乎相同,唯一的异常(exception)是插槽 callseries .所以我猜这就是 fitted函数开始它的魔力。

我真的很想和 y1一起工作而不是 y2 .
有谁知道 fitted 的替代函数哪个产生相同的结果?

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

编辑:
由于代码似乎不可重现,我添加了我的“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.



如果您查看 arima 的来源 fitted 的版本您可以看到您认为 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>

关于r - R中magrittr和arima的兼容性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55361812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com