使用lapply在R中运行乘法分解。

huangapple go评论70阅读模式
英文:

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")})

但这似乎不起作用,因为有以下问题:

  1. tss中嵌套了列表
  2. 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 &lt;- tapply(data$sales, data$account, ts, start = c(2021, 5), frequency = 4)
dcs &lt;- lapply(tss, decompose)

The above equation runs additive type by default. I want to run multiplicative type. I also came about another solution

dcs &lt;- lapply(names(tss), function(x) {decompose(tss[, x], type = &quot;additive&quot;)})

but this seems to not work because of

  1. nested list in tss
  2. 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 &lt;- lapply(tss, decompose(type = &quot;multiplicative&quot;)

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 &lt;- lapply(tss, decompose, type = &quot;multiplicative&quot;)

huangapple
  • 本文由 发表于 2023年7月10日 20:21:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定