尝试将公式应用到R中的每一列,如何将数据输入到公式中?

时间:2022-11-05 13:08:54

So I'm trying to apply an exponential smoothing model to each column in a data frame called 'cities'. I have used apply to identify the data frame, go by columns, and I thought to run the model. However, when I try to do so, it tells me that I need to specify data for the exponential smoothing model...I thought I already had by putting it in the apply loop.

所以我试着在一个叫做“城市”的数据框架中对每一列应用指数平滑模型。我使用apply来识别数据框架,按列前进,我想运行模型。然而,当我尝试这么做时,它告诉我需要为指数平滑模型指定数据……我想我已经把它放到应用循环中了。

apply(x=cities,2,FUN=HoltWinters(x=x,gamma=FALSE))

Also, eventually I'd like to predict the next 4 periods using the HW model developed using forecast.predict. Do I need to use a different loop or can I combine it all in this one?

最后,我想用使用预测开发的HW模型来预测未来的4个周期。我是否需要使用不同的循环,或者我可以将它们合并到这个循环中?

1 个解决方案

#1


4  

FUN takes a function, but you're trying to give it the output of a function. Try this:

乐趣需要一个函数,但你要给它一个函数的输出。试试这个:

apply(cities, 2, FUN=function(x) HoltWinters(x=x,gamma=FALSE))

#1


4  

FUN takes a function, but you're trying to give it the output of a function. Try this:

乐趣需要一个函数,但你要给它一个函数的输出。试试这个:

apply(cities, 2, FUN=function(x) HoltWinters(x=x,gamma=FALSE))