简体   繁体   English

运行函数时出错:eval(expr,envir,enclos)中的错误:找不到对象

[英]Error running a function: Error in eval(expr, envir, enclos) : object not found

I wrote a function and each time running it, I get this error: 我编写了一个函数,每次运行它时,都会出现此错误:

Error in eval(expr, envir, enclos) : object 'y' not found eval(expr,envir,enclos)中的错误:找不到对象“ y”

However, if I go through my function and run each line separately, it works. 但是,如果我遍历函数并分别运行每一行,则它会起作用。 What am I doing wrong? 我究竟做错了什么? Here is my data: 这是我的数据:

library(vars)
library(forecast)
data <- ts(matrix(rnorm(144, mean=0, sd=1), ncol=6), start=c(2007,1), frequency=12) 
colnames(data) <- c("a", "b", "c", "d", "e", "f")
factors <- ts(t(t(eigen(cor(data))$vectors[,1:2] %*% 
                       sqrt(diag(eigen(cor(data))$values[1:2]))) %*% 
                     t(scale(data))), start=c(2007,1), frequency=12)
colnames(factors) <- c("f1", "f2")

And my function: 而我的功能:

prediction.flex <- function(x, y){
  model <- VAR(x, exogen=y, type="const", ic="FPE")
  factor.fcst <- sapply(y, function(z) predict(auto.arima(z), n.ahead=6))[1,]
  factor.fcst <- cbind(factor.fcst$f1, factor.fcst$f2)
  colnames(factor.fcst) <- colnames(y)
  forecasting <- predict(model, dumvar=factor.fcst, n.ahead=6, ci=0.95)$fcst$a[,1]
  return(forecasting)
}

As I said, when running each line seppeartely using x=data and y=factors , I get the forecast values without an error. 正如我所说,当使用x=datay=factors分别运行每行时,我得到的预测值没有错误。 However, if I run my function with prediction.flex(data, factors) , it tells me that object y wasn't found. 但是,如果我使用prediction.flex(data, factors)运行函数,则会告诉我未找到对象y。 Rerunning with debug shows that the line with the forecasting is the problem, even though I just use objects in it, which I generated during the function. 用debug重新运行表明,即使我只是使用其中的对象,也就是预测过程中的问题,这是我在函数中生成的。 I don't understand the error. 我不明白这个错误。 Do you know where my mistake is? 你知道我的错误在哪里吗?

Here is a possible workaround, assigning y to the global environment: 这是一个可能的解决方法,将y分配给全局环境:

library(vars)
library(forecast)

set.seed(42)
data <- ts(matrix(rnorm(144, mean=0, sd=1), ncol=6), start=c(2007,1), frequency=12) 
colnames(data) <- c("a", "b", "c", "d", "e", "f")
factors <- ts(t(t(eigen(cor(data))$vectors[,1:2] %*% 
                       sqrt(diag(eigen(cor(data))$values[1:2]))) %*% 
                     t(scale(data))), start=c(2007,1), frequency=12)
colnames(factors) <- c("f1", "f2")
prediction.flex <- function(x, y){
  model <- VAR(x, exogen=y, type="const", ic="FPE")
  # Assigning "y" to the global environment
  assign("y", "y", envir = .GlobalEnv)
  factor.fcst <- sapply(y, function(z) predict(auto.arima(z), n.ahead=6))[1,]
  factor.fcst <- cbind(factor.fcst$f1, factor.fcst$f2)
  colnames(factor.fcst) <- colnames(y)
  forecasting <- predict(model, dumvar=factor.fcst, n.ahead=6, ci=0.95)$fcst$a[,1]
  return(forecasting)
}
out1 <- prediction.flex(data, factors)

x <- data
y <- factors
model <- VAR(x, exogen=y, type="const", ic="FPE")
factor.fcst <- sapply(y, function(z) predict(auto.arima(z), n.ahead=6))[1,]
factor.fcst <- cbind(factor.fcst$f1, factor.fcst$f2)
colnames(factor.fcst) <- colnames(y)
out2 <- predict(model, dumvar=factor.fcst, n.ahead=6, ci=0.95)$fcst$a[,1]

all.equal(out1, out2)
# [1] TRUE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM