在 R 中生成新的时滞变量

发布于 2024-09-12 11:28:58 字数 744 浏览 0 评论 0原文

我正在使用 R 中的 Transform() 函数创建一个包含新变量的时间序列对象,但找不到正确的函数来计算今天和昨天之间变量 C 的差异。

这是我到目前为止所得到的:

                 O       H       L       C  Typical Range 
2010-07-23 1092.17 1103.73 1087.88 1102.66 1098.090 15.85 
2010-07-26 1102.89 1115.01 1101.30 1115.01 1110.440 13.71  
2010-07-27 1117.36 1120.95 1109.78 1113.84 1114.857 11.17  
2010-07-28 1112.84 1114.66 1103.11 1106.13 1107.967 11.55  
2010-07-29 1108.07 1115.90 1092.82 1101.53 1103.417 23.08  
2010-07-30 1098.44 1106.44 1088.01 1101.60 1098.683 18.43

下一行将添加以下函数:

SPX <- transform(SPX, Return = (C - C(yesterday) ) / C(yesterday)))

显然,C(昨天)是不正确的。我尝试过 lag()、diff() 但没有找到正确的组合。

额外问题:如何让典型变量只显示给第 100 个人?

I'm creating a time-series object with new variables using the transform() function in R and cannot find the proper function to calculate the difference in variable C between today and yesterday.

This is what I've got so far:

                 O       H       L       C  Typical Range 
2010-07-23 1092.17 1103.73 1087.88 1102.66 1098.090 15.85 
2010-07-26 1102.89 1115.01 1101.30 1115.01 1110.440 13.71  
2010-07-27 1117.36 1120.95 1109.78 1113.84 1114.857 11.17  
2010-07-28 1112.84 1114.66 1103.11 1106.13 1107.967 11.55  
2010-07-29 1108.07 1115.90 1092.82 1101.53 1103.417 23.08  
2010-07-30 1098.44 1106.44 1088.01 1101.60 1098.683 18.43

The next row will be added with the following function:

SPX <- transform(SPX, Return = (C - C(yesterday) ) / C(yesterday)))

Obviously, C(yesterday) is incorrect. I've tried lag(), diff() and haven't found the correct combination.

Bonus question: How do you get the Typical variable to show only to the hundreth?

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

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

发布评论

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

评论(1

月亮是我掰弯的 2024-09-19 11:28:59

如果您想计算今天和昨天之间 C 的差异,正确的函数是 diff。

> SPX$Return <- diff(SPX$C)
> SPX
                 O       H       L       C  Typical Range Return
2010-07-23 1092.17 1103.73 1087.88 1102.66 1098.090 15.85     NA
2010-07-26 1102.89 1115.01 1101.30 1115.01 1110.440 13.71  12.35
2010-07-27 1117.36 1120.95 1109.78 1113.84 1114.857 11.17  -1.17
2010-07-28 1112.84 1114.66 1103.11 1106.13 1107.967 11.55  -7.71
2010-07-29 1108.07 1115.90 1092.82 1101.53 1103.417 23.08  -4.60
2010-07-30 1098.44 1106.44 1088.01 1101.60 1098.683 18.43   0.07

但看起来您想计算变化率,您可以使用 TTR 中的 ROC 函数来完成。

> SPX$Return <- ROC(SPX$C)
> SPX
                 O       H       L       C  Typical Range        Return
2010-07-23 1092.17 1103.73 1087.88 1102.66 1098.090 15.85            NA
2010-07-26 1102.89 1115.01 1101.30 1115.01 1110.440 13.71  1.113793e-02
2010-07-27 1117.36 1120.95 1109.78 1113.84 1114.857 11.17 -1.049869e-03
2010-07-28 1112.84 1114.66 1103.11 1106.13 1107.967 11.55 -6.946068e-03
2010-07-29 1108.07 1115.90 1092.82 1101.53 1103.417 23.08 -4.167314e-03
2010-07-30 1098.44 1106.44 1088.01 1101.60 1098.683 18.43  6.354596e-05

The correct function is diff if you're trying to calculate the difference of C between today and yesterday.

> SPX$Return <- diff(SPX$C)
> SPX
                 O       H       L       C  Typical Range Return
2010-07-23 1092.17 1103.73 1087.88 1102.66 1098.090 15.85     NA
2010-07-26 1102.89 1115.01 1101.30 1115.01 1110.440 13.71  12.35
2010-07-27 1117.36 1120.95 1109.78 1113.84 1114.857 11.17  -1.17
2010-07-28 1112.84 1114.66 1103.11 1106.13 1107.967 11.55  -7.71
2010-07-29 1108.07 1115.90 1092.82 1101.53 1103.417 23.08  -4.60
2010-07-30 1098.44 1106.44 1088.01 1101.60 1098.683 18.43   0.07

But it looks like you want to calculate the rate of change instead, which you can do with the ROC function from TTR.

> SPX$Return <- ROC(SPX$C)
> SPX
                 O       H       L       C  Typical Range        Return
2010-07-23 1092.17 1103.73 1087.88 1102.66 1098.090 15.85            NA
2010-07-26 1102.89 1115.01 1101.30 1115.01 1110.440 13.71  1.113793e-02
2010-07-27 1117.36 1120.95 1109.78 1113.84 1114.857 11.17 -1.049869e-03
2010-07-28 1112.84 1114.66 1103.11 1106.13 1107.967 11.55 -6.946068e-03
2010-07-29 1108.07 1115.90 1092.82 1101.53 1103.417 23.08 -4.167314e-03
2010-07-30 1098.44 1106.44 1088.01 1101.60 1098.683 18.43  6.354596e-05
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文