I am writing a function to group together actions I regularly take on time series data. I have included all libraries I am using in the script as I think my issue may be to do with plyr / dplyr being (rightly) super specific about the environment of each variable.
我正在编写一个功能,将我经常采用时间序列数据的操作组合在一起。我已经在脚本中包含了我正在使用的所有库,因为我认为我的问题可能与plyr / dplyr(正确)关于每个变量的环境有关。
The first function works great, but when getting to the second one, R doesn't recognise the input as 'x', but spits out the error: 'Error in eval(predvars, data, env) : object 'x' not found.'
第一个函数效果很好,但是当到达第二个函数时,R不会将输入识别为'x',但是会发出错误:'eval中的错误(predvars,data,env):找不到对象'x' “。
Why is this happening?
为什么会这样?
library(plyr)
library(dplyr)
library(caret)
library(xts)
library(forecast)
library(imputeTS)
library(lubridate)
x1 = arima.sim(list(order = c(0,1,0)), n = 119)
timetrend <- function(x, starts, ends, frequency) {
y <- list()
y[[1]] <- ts(x, start = starts, end = ends, frequency = frequency)
y[[2]] <- decompose(y[[1]])
y[[3]] <- y[[1]] - y[[2]]$seasonal - y[[2]]$random
return(y)
}
plottime <- function(x) { #takes a timetrend list as input
t <- tslm(x[[3]] ~ trend)
plot(x[[3]])
lines(t$fitted.values)
return(t)
}
use functions from here
result <- timetrend(x = x1,
starts = c(2000, 01, 01), ends = c(2009, 12, 01), frequency = 12)
plottime(x = result)
1 个解决方案
#1
2
I could make it work with the following code.
我可以使用以下代码。
plottime <- function(x) { #takes a timetrend list as input
y=x[[3]]
t <- tslm(formula = y ~ trend)
plot(x[[3]])
lines(t$fitted.values)
return(t)
}
Not sure why it is happening, maybe the use of indexing x[[3]] in the formula argument is a problem?
不知道为什么会发生这种情况,也许在公式参数中使用索引x [[3]]是个问题?
#1
2
I could make it work with the following code.
我可以使用以下代码。
plottime <- function(x) { #takes a timetrend list as input
y=x[[3]]
t <- tslm(formula = y ~ trend)
plot(x[[3]])
lines(t$fitted.values)
return(t)
}
Not sure why it is happening, maybe the use of indexing x[[3]] in the formula argument is a problem?
不知道为什么会发生这种情况,也许在公式参数中使用索引x [[3]]是个问题?