嘿,我想试试第四版时间序列分析及其应用手册(Ex.10)中的一个例子.当我在R上运行书中的代码时,我有这个错误;

UseMethod("Filter")出错: 没有适用于"c(‘Double’,‘Numic’)"类的对象的"Filter"方法

以下是代码;

w = rnorm(150,0,1) # 50 extra to avoid startup problems 
x = filter(w, filter=c(1,-.9), method="recursive")[-(1:50)] # remove first 50 
plot.ts(x, main="autoregression")

你知道哪里出了问题吗?知道怎么解决吗?

推荐答案

当加载另一个具有filter函数的非基本R包时,就会发生这种情况.

Package 100 is the main responsible for the question's error.

第一个示例是在新的R会话中运行的.

w <- rnorm(150,0,1) # 50 extra to avoid startup problems 
x <- filter(w, filter=c(1,-.9), method="recursive")[-(1:50)] # remove first 50 
plot.ts(x, main="autoregression")

创建于2022-10-02,带有reprex v2.0.2

The error

现在加载包dplyr并运行完全相同的代码.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

w <- rnorm(150,0,1) # 50 extra to avoid startup problems 
x <- filter(w, filter=c(1,-.9), method="recursive")[-(1:50)] # remove first 50 
#> Error in UseMethod("filter"):  
#>   no applicable method for 'filter' applied to an object of class "c('double', 'numeric')"

创建于2022-10-02,带有reprex v2.0.2

The solution

解决方案是使用限定名称stats::filter.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

w <- rnorm(150,0,1) # 50 extra to avoid startup problems 
x <- stats::filter(w, filter=c(1,-.9), method="recursive")[-(1:50)] # remove first 50 
plot.ts(x, main="autoregression")

创建于2022-10-02,带有reprex v2.0.2

R相关问答推荐

在R中使用数据集名称

删除具有相同标题的tabPanel(shinly)

如何在R forestplot中为多条垂直线分配唯一的 colored颜色 ?

R中边际效应包中Logistic回归的交互作用风险比

有没有办法使用ggText,<;Sub>;&;<;sup>;将上标和下标添加到同一元素?

正在导出默认的RStudio主题,还是设置括号 colored颜色 ?

按列中显示的配对组估算NA值

R+reprex:在呈现R标记文件时创建可重现的示例

为什么函数toTitleCase不能处理english(1),而toupper可以?

R中治疗序列的相对时间指数

网络抓取NBA.com

如何在不使用SHINY的情况下将下拉滤镜列表添加到ggploy?

如何使用包含要子集的值的列表或数据框来子集多个列?

以列名的字符向量作为参数按行应用自定义函数

在R中,如果一个值在同一数据帧中的任何特定列中,如何计算?

从字符数据列中删除符号

将一个字符串拆分成R中的两行或多行,同时复制其他列

按多列筛选dplyr中的数据

如何让R gtsum程序包tbl_strata()函数不为一个完全缺少某一层的值的变量打印带有(nA%)的零?

根据其它列和相关非零值的个数过滤数据帧