【问题标题】:How do I calculate a monthly rate of change from a daily time series in R?如何从 R 中的每日时间序列计算每月变化率?
【发布时间】:2013-07-17 19:27:19
【问题描述】:

我开始接触 R,而且我对时间序列概念是全新的。谁能指出我正确的方向来根据每日数据点计算每月的百分比变化?我想要每个月的第一个和最后一个数据点之间的变化。例如:

系列数据:

1/1/2000 10.00
...
1/31/2000 10.10
2/1/2000 10.20
...
2/28/2000 11.00

我正在寻找表单的返回数据框:

1/31/2000 .01
2/28/2000 .0784

理想情况下,我可以从上个月的端点计算到本月的端点,但我认为按月分区作为起点更容易。我正在查看软件包 zoo 和 xts,但仍然卡住了。有接盘侠吗?谢谢...

【问题讨论】:

  • 你试过什么?您是否浏览了动物园和 xts 小插曲以寻求答案?你搜索过 [xts] 标签吗?
  • 现在正在解析它们..

标签: r time-series xts zoo


【解决方案1】:

1) 没有包试试这个:

DF <- read.table(text = Lines)

fmt <- "%m/%d/%Y"
ym <- format(as.Date(DF$V1, format = fmt), "%Y-%m")

ret <- function(x) diff(range(x))/x[1]
ag <- aggregate(V2 ~ ym, DF, ret)

给予:

> ag
       ym         V2
1 2000-01 0.01000000
2 2000-02 0.07843137

如果需要,我们可以将其转换为 "ts" 类。假设没有缺失月份:

ts(ag$V2, start = 2000, freq = 12)

给予:

            Jan        Feb
2000 0.01000000 0.07843137

2) 如果使用 zoo 或 xts 时间序列包,会容易一些。 fmtret 来自上方:

library(zoo)
z <- read.zoo(text = Lines, format = fmt)
z.ret <- aggregate(z, as.yearmon, ret)

给予:

> z.ret
  Jan 2000   Feb 2000 
0.01000000 0.07843137 

如果您已经有一个data.frame DF,那么read.zoo 语句可以替换为z &lt;- read.zoo(DF, format = fmt),或者如果第一列属于"Date" 类,则省略format 参数。

如果需要"ts" 类,则使用as.ts(z.ret)

注意:输入Lines是:

Lines <- "1/1/2000 10.00
1/31/2000 10.10
2/1/2000 10.20
2/28/2000 11.00"

【讨论】:

    【解决方案2】:

    这是一个相当老的线程,但作为参考,这里有一个data.table 解决方案,使用与@Ram 相同的数据:

    structure(list(Date = structure(c(10957, 10987, 10988, 11015, 11017, 11047, 11087, 11099, 11112, 11141, 11168, 11319, 11321), class = "Date"), Value = c(10, 10.1, 10.2, 11, 10, 24.1, 510, 522, 604, 10.1, 7.2, 11, 3)), .Names = c("Date", "Value"), row.names = c(NA, -13L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x00000000001b0788>)
    

    它本质上是一个使用data.table::month 函数的单行代码:

    library(data.table)
    
    setDT(df)[ , diff(Value) / Value[1], by= .(month(Date))]
    

    这将产生相对于每个月的第一个记录日的变化。如果首选相对于最后天的更改,则中间的表达式应更改为diff(Value) / Vale[2]

    【讨论】:

      【解决方案3】:

      这是另一种方法(使用 quantmod 包):

      这会根据 AAPL 的每日价格计算每月回报。

      *library(quantmod)     # load the quantmod package
      getSymbols("AAPL")     # download daily price for stock AAPL
      monthlyReturn = periodReturn(AAPL,period="monthly")
      monthlyReturn2014 = periodReturn(AAPL,period="monthly",subset='2014:') # for 2014*
      

      【讨论】:

        【解决方案4】:

        TTR 包中的 ROC 函数将执行此操作。如果您只想查看每月行为,则可以先使用 to.monthly 或 endpoints() (From daily time series to weekly time series in R xts object)。

        library(TTR)
        # data.monthly <- to.monthly( data, indexAt='periodEnd' ) # if OHLC data
        # OR
        data.monthly <- data[ endpoints(data, on="months", k=1), ]
        data.roc <- ROC(data.monthly, n = 1, type = "discrete")
        

        【讨论】:

          【解决方案5】:

          这是使用plyrddply 的一种方法。 我按顺序使用 ddply,首先获取每个月的第一行和最后一行,然后再次计算 monthlyReturn。 (也许使用 xts 或 zoo 可能更容易,我不确定。)

          #Using plyr and the data in df
          df$Date <- as.POSIXlt(as.Date(df$Date, "%m/%d/%Y"))
          df$Month <- (df$Date$mon + 1) #0 = January
          
          sdf <- df[,-1] #drop the Date Column, ddply doesn't like it
          
          library("plyr")
          #this function is called with 2 row data frames
          monthlyReturn<- function(df) {
            (df$Value[2] - df$Value[1])/(df$Value[1])  
          }
          
          adf <- ddply(sdf, .(Month), function(x) x[c(1, nrow(x)), ]) #get first and last values for each Month   
          mon.returns <- ddply(adf, .(Month), monthlyReturn)
          

          这是我用来测试的数据:

          > df
                   Date Value
          1    1/1/2000  10.0
          2   1/31/2000  10.1
          3    2/1/2000  10.2
          4   2/28/2000  11.0
          5    3/1/2000  10.0
          6   3/31/2000  24.1
          7   5/10/2000 510.0
          8   5/22/2000 522.0
          9   6/04/2000 604.0
          10  7/03/2000  10.1
          11  7/30/2000   7.2
          12 12/28/2000  11.0
          13 12/30/2000   3.0
          
          > mon.returns
            Month          V1
          1     1  0.01000000
          2     2  0.07843137
          3     3  1.41000000
          4     5  0.02352941
          5     6  0.00000000
          6     7 -0.28712871
          7    12 -0.72727273
          

          希望对您有所帮助。

          【讨论】:

          • 拉姆,谢谢。 Zoo 和 xts 是要走的路,但这让我开始思考结构。
          猜你喜欢
          • 2021-08-07
          • 1970-01-01
          • 2015-11-13
          • 1970-01-01
          • 1970-01-01
          • 2021-02-13
          • 2021-09-28
          • 1970-01-01
          • 2015-04-14
          相关资源
          最近更新 更多