![](/img/trans.png)
[英]Time series in R: How do I calculate percent change from a fixed year for multiple time series variables in R?
[英]How do I calculate a monthly rate of change from a daily time series in R?
我開始用R弄濕自己,我對時間序列概念不熟悉。 任何人都可以指出我正確的方向來計算每月的%變化,基於每日數據點? 我想要在每個月的第一個和最后一個數據點之間進行更改。 例如:
tseries數據:
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
理想情況下,我能夠從上個月的端點計算到當月的端點,但我認為按月分區更容易作為起點。 我正在看包裹動物園和xts,但我仍然卡住了。 任何接受者? 謝謝...
這是使用plyr
和ddply
完成它的一種方法。 我按順序使用ddply,首先獲取每個月的第一行和最后一行,然后再次計算每月返回。 (也許使用xts或動物園可能會更容易,我不確定。)
#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
希望有所幫助。
這是另一種方法(使用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*
這是一個非常古老的線程,但作為參考,這里有一個使用與@Ram相同數據的data.table
解決方案:
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
函數的data.table::month
:
library(data.table)
setDT(df)[ , diff(Value) / Value[1], by= .(month(Date))]
這將產生相對於每個月的第一個記錄日的變化。 如果首選相對於最后一天的更改,則應將中間的表達式更改為diff(Value) / Vale[2]
。
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時間序列包,這會容易一些。 fmt
和ret
來自上方:
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 <- read.zoo(DF, format = fmt)
,如果第一列是"Date"
類,則省略format
arg。
如果需要"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"
TTR包中的ROC功能將執行此操作。 如果您只查看月度行為,則可以首先使用to.monthly或endpoints()( 從每日時間序列到R xts對象中的每周時間序列 )。
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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.