首页 > 解决方案 > R:不能将“闭包”类型强制为“双”类型的向量(时间序列预测)

问题描述

我正在使用 R 编程语言,并在此处关注本教程关于时间序列预测:https ://github.com/ahaeusser/echos

在本教程中,作者展示了如何使用一种神经网络来预测时间序列的未来值。

在开始之前,可以从 github 下载许多所需的包:

devtools::install_github("ahaeusser/echos")
devtools::install_github("ahaeusser/tscv")
install.packages("fable")
remotes::install_github("tidyverts/fable")
remotes::install_github("tidyverts/tsibble")

#load libraries
library(echos)
library(tscv)
library(dplyr)
library(tsibble)
library(fabletools)
library(fable)
Sys.setlocale("LC_TIME", "C")
#> [1] "C"

从这里,我复制并粘贴了作者的代码:

# Prepare dataset
data <- elec_price %>%
  tscv::clean_data()

# Setup for time series cross validation
n_init <- 2400   # size for training window
n_ahead <- 24    # size for testing window (forecast horizon)
mode <- "slide"  # fixed window approach
n_skip <- 23     # skip 23 observations
n_lag <- 0       # no lag

data <- data %>%
  tscv::split_data(
    n_init = n_init,
    n_ahead = n_ahead,
    mode = mode,
    n_skip = n_skip,
    n_lag = n_lag)

# Use only a small sample of data
data <- data %>%
  filter(BZN == "SE1") %>%
  filter(split == 10)

这是我遇到错误的地方:

models <- data %>%
  filter(sample == "train") %>%
  model(
    "ESN" = ESN(
      Value,
      inf_crit = "BIC",
      max_lag = 6,
      n_fourier = c(3, 3),
      n_initial = 50,
      n_res = 200,
      scale_inputs = c(-1, 1)),
    "sNaive" = SNAIVE(Value ~ lag("week")))

错误:

Warning message:
1 error encountered for ESN
[1] cannot coerce type 'closure' to vector of type 'double'

在上面的代码中,作者拟合了两个不同的时间序列模型。其中一个有效(蓝色模型),但另一个模型包含此错误。

有谁知道我做错了什么?还是后台有问题?

我还尝试使用我创建的一些虚拟数据运行相同的代码,但仍然出现错误:

data = data.frame(rnorm(1000,100,100))
data$value = data$rnorm.1000..100..100.
data$rnorm.1000..100..100. = NULL

models <- data %>%
    filter(sample == "train") %>%
    model(
        "ESN" = ESN(
            Value,
            inf_crit = "BIC",
            max_lag = 6,
            n_fourier = c(3, 3),
            n_initial = 50,
            n_res = 200,
            scale_inputs = c(-1, 1)),
        "sNaive" = SNAIVE(Value ~ lag("week")))

Error: Problem with `filter()` input `..1`.
x comparison (1) is possible only for atomic and list types
i Input `..1` is `sample == "train"`.

谢谢

标签: rtime-seriesdata-visualizationforecasting

解决方案


“闭包”只是一个带有封闭环境的函数。这是一个常见的错误,当有人定义了一个名为 的变量data,重新启动 R(没有重新定义变量),然后尝试对该数据做一些事情时。

set.seed(42)
data <- data.frame(value = rnorm(1000, 100, 100))
mean(data$value)
# [1] 97.41756
rm(data) ### or restart R
mean(data$value)
# Error in data$value : object of type 'closure' is not subsettable

这是大多数错误的根本原因,包括'closure'. 我想你的情况也差不多。

首先,一点效率,改变你的

data = data.frame(rnorm(1000,100,100))
data$value = data$rnorm.1000..100..100.
data$rnorm.1000..100..100. = NULL

data = data.frame(value = rnorm(1000,100,100))

不需要框架名称舞蹈。为了记录,我使用了set.seed(42),然后定义了这个data框架,然后我们看到了这个:

head(data)
#       value
# 1 237.09584
# 2  43.53018
# 3 136.31284
# 4 163.28626
# 5 140.42683
# 6  89.38755

现在,您的下一个命令...

data %>%
  filter(sample == "train")
# Error: Problem with `filter()` input `..1`.
# x comparison (1) is possible only for atomic and list types
# i Input `..1` is `sample == "train"`.
# Run `rlang::last_error()` to see where the error occurred.

从哪里来sample?我的猜测是,您之前的实例化data包含的列不仅仅是value. 也就是说,它有一个名为 的列sample

但是,在这种情况下, wheredata$sample不存在,dplyr::filter首先在相应的框架中(在 内data)查找引用的符号,如果没有,则在全局环境和通用 R 搜索路径中进行搜索。它找到了sampleakabase::sample采样数据的函数。

所以我们回到关于“关闭”的错误的前提:

as.double(sample)
# Error in as.double(sample) : 
#   cannot coerce type 'closure' to vector of type 'double'

因此,无论是否在内部尝试这样做,最重要的是您可能引用了框架中不作为列存在的变量。


推荐阅读