R:一段时期(月)的左侧移动平均线

发布于 2025-01-03 08:48:06 字数 685 浏览 1 评论 0原文

我有一个问题对于你们大多数人来说可能是微不足道的。我尝试了很多,但没有找到解决方案,所以如果有人能给我提示,我会很高兴。起点是每周的 xts 时间序列。

Month    Week Value Goal
Dec 2011 W50  a     a  
Dec 2011 W51  b     mean(a,b)
Dec 2011 W52  c     mean(a,b,c)
Dec 2011 W53  d     mean(a,b,c,d)
Jan 2012 W01  e     e
Jan 2012 W02  f     mean(e,f)
Jan 2012 W03  g     mean(e,f,g)
Jan 2012 W04  h     mean(e,f,g,h)
Feb 2012 W05  i     i
Feb 2012 W06  j     mean(i,j)

请原谅 Excel 符号,但我认为它非常清楚地说明了我想要做什么:我想计算“值”列的左侧移动平均值,但仅计算相应月份的左侧移动平均值,因为它显示在“目标”列中。我尝试了 apply.monthly()period.apply()。但它并没有得到我想要的。你们中有人能给我一个如何解决这个问题的提示吗?只需提示我应该使用哪个函数就足够了!

非常感谢!

最好的问候,

安德烈亚斯

I have a question which might be trivial for most of you guys. I tried a lot, didn't come to a solution, so I would be glad if somebody could give me a hint. The starting point is a weekly xts-time series.

Month    Week Value Goal
Dec 2011 W50  a     a  
Dec 2011 W51  b     mean(a,b)
Dec 2011 W52  c     mean(a,b,c)
Dec 2011 W53  d     mean(a,b,c,d)
Jan 2012 W01  e     e
Jan 2012 W02  f     mean(e,f)
Jan 2012 W03  g     mean(e,f,g)
Jan 2012 W04  h     mean(e,f,g,h)
Feb 2012 W05  i     i
Feb 2012 W06  j     mean(i,j)

Please excuse the Excel notation, but I think it makes it pretty clear what I want to do: I want to calculate a left sided moving average for the column "Value" but just for the respective month, as it is displayed in the column Goal. I experimented with apply.monthly() and period.apply(). But it didn't get me what I want. Can sombody of you give me a hint how to solve the problem? Just a hint which function I should use would be already enough!

Thank you very much!

Best regards,

Andreas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肩上的翅膀 2025-01-10 08:48:06

apply.monthly 不起作用,因为它只为周期的终点分配一个值,而您希望为每个月周期分配多个值。

您可以很容易地做到这一点,只需按月分割 xts 数据,对每个数据应用累积平均值函数,然后将列表重新绑定在一起。

library(quantmod)
# Sample data
getSymbols("SPY")
spy <- to.weekly(SPY)
# Cumulative mean function
cummean <- function(x) cumsum(x)/seq_along(x)
# Expanding average calculation
spy$EA <- do.call(rbind, lapply(split(Cl(spy),'months'), cummean))

apply.monthly will not work because it only assigns one value to the endpoint of the period, whereas you want to assign many values to each monthly period.

You can do this pretty easily by splitting your xts data by month, applying a cumulative mean function to each, and rbind'ing the list back together.

library(quantmod)
# Sample data
getSymbols("SPY")
spy <- to.weekly(SPY)
# Cumulative mean function
cummean <- function(x) cumsum(x)/seq_along(x)
# Expanding average calculation
spy$EA <- do.call(rbind, lapply(split(Cl(spy),'months'), cummean))
英雄似剑 2025-01-10 08:48:06

我希望我答对了你的问题。但这是您正在寻找的吗:

 require(plyr)
 require(PerformanceAnalytics)
 ddply(data, .(Week), summarize, Goal=apply.fromstart(Value,fun="mean"))

这应该可行 - 尽管 可重现的示例 会很好。

这就是它的作用。

df <- data.frame(Week=rep(1:5, each=5), Value=c(1:25)*runif(25)) #sample data

require(plyr)
require(PerformanceAnalytics)

df$Goal <- ddply(df, .(Week), summarize, Goal=apply.fromstart(Value,FUN="mean"))[,2]

结果:

    Week      Value       Goal
 1     1  0.7528037  0.7528037
 2     1  1.9622622  1.3575330
 3     1  0.3367802  1.0172820
 4     1  2.5177284  1.3923936

当然,您可以通过帮助获取更多信息:?ddply?apply.fromstart

I hope I got your question right. but is it that what you are looking for:

 require(plyr)
 require(PerformanceAnalytics)
 ddply(data, .(Week), summarize, Goal=apply.fromstart(Value,fun="mean"))

this should work - though a reproducible expample would have been nice.

here's what it does.

df <- data.frame(Week=rep(1:5, each=5), Value=c(1:25)*runif(25)) #sample data

require(plyr)
require(PerformanceAnalytics)

df$Goal <- ddply(df, .(Week), summarize, Goal=apply.fromstart(Value,FUN="mean"))[,2]

outcome:

    Week      Value       Goal
 1     1  0.7528037  0.7528037
 2     1  1.9622622  1.3575330
 3     1  0.3367802  1.0172820
 4     1  2.5177284  1.3923936

of course you may obtain further info via the help: ?ddply or ?apply.fromstart.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文