R 时间序列 - 制作布林线时遇到问题 - 请需要简单的例子

发布于 2024-09-18 21:08:09 字数 488 浏览 2 评论 0原文

学习 R 语言 - 我知道如何做移动平均线,但我需要做更多 - 但我不是统计学家 - 不幸的是所有文档似乎都是为统计学家编写的。

我经常在 Excel 中这样做,它对于分析运营活动非常方便。

以下是每行上用于创建布林带的字段:

值可以是呼叫数、投诉率等

时间戳|价值|移动平均线|移动 STDEVP |下控制 |上层控制

简而言之,移动平均值和标准差 P 指向该系列中之前 8 个左右的值。给定时间点的下控制是 = 移动平均值 - 2*moving stdevP 上控制 = movingaverage + 2*moving stdevP

这可以很容易地在 excel 中为单个文件完成,但如果我能找到一种方法使 R work R 会更好地满足我的需求。希望自动化后也能更快、更可靠。

链接或提示将不胜感激。

Learning R language - I know how to do a moving average but I need to do more - but I am not a statistician - unfortunately all the docs seem to be written for statisticians.

I do this in excel a lot, it's really handy for analysis of operational activities.

Here are the fields on each row to make bollinger bands:

Value could be # of calls, complaint ratio, anything

TimeStamp | Value | Moving Average | Moving STDEVP | Lower Control | Upper Control

Briefly, the moving avg and the stdevP point to the prior 8 or so values in the series. Lower control at a given point in time is = moving average - 2*moving stdevP and upper control = moving average + 2*moving stdevP

This can easily be done in excel for a single file, but if I can find a way to make R work R will be better for my needs. Hopefully faster and more reliable when automated, too.

links or tips would be appreciated.

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

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

发布评论

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

评论(2

紫竹語嫣☆ 2024-09-25 21:08:09

您可以使用 zoo 包中的函数 rollapply() ,前提是您使用的是 Zoo 系列:

TimeSeries <- cumsum(rnorm(1000))
ZooSeries <- as.zoo(TimeSeries)

BollLines <- rollapply(ZooSeries,9,function(x){
    M <- mean(x)
    SD <- sd(x)
    c(M,M+SD*2,M-SD*2)
})

现在您必须记住 rollapply 使用居中框架,这意味着它采用当天左侧和右侧的值。与您采用 x 先验值的建议相比,这也更方便且更符合布林带的定义。

如果您不想转换为动物园,您也可以使用向量并编写自己的函数。我添加了一个基于 S3 的绘图函数,可以让您轻松绘制计算结果。使用这些函数,您可以执行以下操作:

TimeSeries <- cumsum(rnorm(1000))
X <- BollingerBands(TimeSeries,80)
plot(X,TimeSeries,type="l",main="An Example")

获取:

在此处输入图像描述

函数代码:

BollingerBands <- function(x,width){
  Start <- width +1
  Stop <- length(x)
  Trail <- rep(NA,ceiling(width/2))
  Tail <- rep(NA,floor(width/2))

  Lines <- sapply(Start:Stop,function(i){
    M <- mean(x[(i-width):i])
    SD <- sd(x[(i-width):i])
    c(M,M+2*SD,M-2*SD)
  })


  Lines <- apply(Lines,1,function(i)c(Trail,i,Tail))
  Out <- data.frame(Lines)
  names(Out) <- c("Mean","Upper","Lower")

  class(Out) <- c("BollingerBands",class(Out))

  Out
}

plot.BollingerBands <- function(x,data,lcol=c("red","blue","blue"),...){
    plot(data,...)

    for(i in 1:3){
      lines(x[,i],col=lcol[i])
    }
}

You could use the function rollapply() from the zoo package, providing you work with a zoo series :

TimeSeries <- cumsum(rnorm(1000))
ZooSeries <- as.zoo(TimeSeries)

BollLines <- rollapply(ZooSeries,9,function(x){
    M <- mean(x)
    SD <- sd(x)
    c(M,M+SD*2,M-SD*2)
})

Now you have to remember that rollapply uses a centered frame, meaning that it takes the values to the left and the right of the current day. This is also more convenient and more true to the definition of the Bollinger Band than your suggestion of taking x prior values.

If you don't want to convert to zoo, you can use the vectors as well and write your own function. I added an S3 based plotting function that allows you to easily plot the calculations as well. With these functions, you could do something like :

TimeSeries <- cumsum(rnorm(1000))
X <- BollingerBands(TimeSeries,80)
plot(X,TimeSeries,type="l",main="An Example")

to get :

enter image description here

The function codes :

BollingerBands <- function(x,width){
  Start <- width +1
  Stop <- length(x)
  Trail <- rep(NA,ceiling(width/2))
  Tail <- rep(NA,floor(width/2))

  Lines <- sapply(Start:Stop,function(i){
    M <- mean(x[(i-width):i])
    SD <- sd(x[(i-width):i])
    c(M,M+2*SD,M-2*SD)
  })


  Lines <- apply(Lines,1,function(i)c(Trail,i,Tail))
  Out <- data.frame(Lines)
  names(Out) <- c("Mean","Upper","Lower")

  class(Out) <- c("BollingerBands",class(Out))

  Out
}

plot.BollingerBands <- function(x,data,lcol=c("red","blue","blue"),...){
    plot(data,...)

    for(i in 1:3){
      lines(x[,i],col=lcol[i])
    }
}
Spring初心 2024-09-25 21:08:09

R Graph Gallery (65) 中有一个插图给出了代码既可以用于计算区间,也可以用于绘制股价。

六年后,2005 年的代码似乎仍然有效,并将给出 IBM 当前的股价以及几个月前的情况

>最明显的错误是带宽和成交量下图的宽度已缩小;可能还有另一个超过承保天数的情况。

There is an illustration in the R Graph Gallery (65) giving code both for calculating the bands and for plotting share prices.

The 2005 code still seems to work six years later and will give IBM's current share price and going back several months

IBM Bollinger Bands

The most obvious bug is the width of the bandwidth and volume lower charts which have been narrowed; there may be another over the number of days covered.

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