英文:
Run multiplicative decompose using lapply in r
问题
我有一个包含3列数据的情况
账户:大约1500个(数值ID)
月份:24个月的数据(日期时间格式)
销售额:每个账户的月销售额
我想在账户级别运行乘法分解。我从网络搜索中整理了以下内容。(将时间序列分解为趋势、季节性和随机成分的R代码,适用于月度数据)
tss <- tapply(data$sales, data$account, ts, start = c(2021, 5), frequency = 4)
dcs <- lapply(tss, decompose)
上述方程默认运行加法类型。我想要运行乘法类型。我还找到了另一种解决方案
dcs <- lapply(names(tss), function(x) {decompose(tss[, x], type = "additive")})
但这似乎不起作用,因为有以下问题:
- tss中嵌套了列表
- ID是从数值值转换而来的字符,这导致错误
我期望有一个更简单的解决方案,像下面这样,但不幸的是这不起作用。
dcs <- lapply(tss, decompose(type = "multiplicative")
任何帮助将不胜感激。
英文:
I have a data with 3 columns
account: ~1500 of them (numeric ids)
month: 24month of data (date time format)
sales: monthly sales for each account
I want to run multiplicative decompose at account level. I curated the following from web search. (Decompose Time series into Trend, Seasonality and Random for multiple groups in R for monthly data)
tss <- tapply(data$sales, data$account, ts, start = c(2021, 5), frequency = 4)
dcs <- lapply(tss, decompose)
The above equation runs additive type by default. I want to run multiplicative type. I also came about another solution
dcs <- lapply(names(tss), function(x) {decompose(tss[, x], type = "additive")})
but this seems to not work because of
- nested list in tss
- the id's are characters transformed from numeric values which is throwing an error
I am expecting a simpler solution like the following but unfortunately this doesn't work.
dcs <- lapply(tss, decompose(type = "multiplicative")
any help would be appreciated.
答案1
得分: 0
你可以在lapply函数内部使用逗号分隔的可选参数来添加到函数中:
dcs <- lapply(tss, decompose, type = "multiplicative")
英文:
you can add optional arguments to a function within lapply separated by comma's:
dcs <- lapply(tss, decompose, type = "multiplicative")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论