r:在函数的公式中使用傅立叶时找不到对象

r: object not found when using fourier within a formula within a function

(使用 r 中的 forecast 包)。如果我写:

tslmf <- function(y, h, myK,...) {
  fourier(y, myK)
}

我可以用 tslmf(taylor, h=1, c(1,1)) 调用它,一切正常。 但如果我写:

tslmf <- function(y, h, myK,...) {
  fit <- tslm(y ~ trend + fourier(y, K=myK))
}

并以完全相同的方式调用它,我得到: object 'myK' not found

如果我这样做:

tslmf <- function(y, h, myK,...) {
  z <- fourier(y, myK)
  fit <- tslm(y ~ trend + z)
}

确实有效!

这是为什么?

tslm 中调用的

fourier 正在 forcastbaseglobal 环境中寻找 myK,但它没有找到他们。 tslm 封闭环境 不是你函数的环境,你有 myK.

Function environments, or