我正在使用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"`.谢谢
发布于 2020-12-23 03:02:43
“闭包”只是一个带有封闭环境的函数。当有人定义了一个名为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的列。
然而,在这种情况下,在data$sample不存在的情况下,dplyr::filter首先在相应的帧(在data内)中查找被引用的符号,并且在全局环境和一般R搜索路径中搜索缺少被引用的符号。它找到了sample,也就是base::sample,一个用于采样数据的函数。
所以我们回到关于“闭包”的错误的前提:
as.double(sample)
# Error in as.double(sample) :
# cannot coerce type 'closure' to vector of type 'double'因此,无论它是不是在内部尝试这样做,底线是您可能引用了框架中不作为列存在的变量。
https://stackoverflow.com/questions/65413707
复制相似问题