将带有日期列的数据框转换为时间序列

时间:2012-01-04 19:04:25

标签: r xts

我有一个包含以下数据的数据框:

>PRICE
         DATE  CLOSE
1    20070103 54.700
2    20070104 54.770
3    20070105 55.120
4    20070108 54.870
5    20070109 54.860
6    20070110 54.270
7    20070111 54.770
8    20070112 55.360
9    20070115 55.760
...

如您所见,我的DATE列代表日期(yyyyMMdd),而我的CLOSE列代表价格。

我现在必须从PerformanceAnalytics包计算CalmarRatio。

我是R的新手,所以我无法理解所有内容,但是从我用google搜索的那一刻起,我看到该函数的R参数需要是一个类似时间序列的对象。

有没有什么方法可以将我的数组转换为时间序列对象,因为一个句点中的每个日期可能都没有数据(仅适用于我指定的数据)?

5 个答案:

答案 0 :(得分:49)

您的DATE列可能代表一个日期,但它实际上是字符,因子,整数或数字向量。

首先,您需要将DATE列转换为Date对象。然后,您可以从CLOSE data.frame的DATEPRICE列创建xts对象。最后,您可以使用xts对象来计算回报和Calmar比率。

PRICE <- structure(list(
  DATE = c(20070103L, 20070104L, 20070105L, 20070108L, 20070109L,
           20070110L, 20070111L, 20070112L, 20070115L),
  CLOSE = c(54.7, 54.77, 55.12, 54.87, 54.86, 54.27, 54.77, 55.36, 55.76)),
  .Names = c("DATE", "CLOSE"), class = "data.frame",
  row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9"))

library(PerformanceAnalytics)  # loads/attaches xts
# Convert DATE to Date class
PRICE$DATE <- as.Date(as.character(PRICE$DATE),format="%Y%m%d")
# create xts object
x <- xts(PRICE$CLOSE,PRICE$DATE)
CalmarRatio(Return.calculate(x))
#                  [,1]
# Calmar Ratio 52.82026

答案 1 :(得分:15)

大多数人发现使用时间序列课是一个很大的痛苦。您应该考虑使用package zoo中的zoo类。它不会抱怨错过的时间,只会重复。 PerformanceAnalytics函数几乎肯定会期待'zoo'或它的后代类'xts'。

pricez <- read.zoo(text="   DATE  CLOSE
 1    20070103 54.700
 2    20070104 54.770
 3    20070105 55.120
 4    20070108 54.870
 5    20070109 54.860
 6    20070110 54.270
 7    20070111 54.770
 8    20070112 55.360
 9    20070115 55.760
 ")
 index(pricez) <- as.Date(as.character(index(pricez)), format="%Y%m%d")
 pricez
2007-01-03 2007-01-04 2007-01-05 2007-01-08 2007-01-09 2007-01-10 2007-01-11 2007-01-12 2007-01-15 
     54.70      54.77      55.12      54.87      54.86      54.27      54.77      55.36      55.76 

答案 2 :(得分:2)

另一种解决方案是使用tidyquant包,它允许财务包的功能(包括时间序列功能)与数据帧一起使用。以下示例显示了如何获得多个资产的Calmar Ratio。 tidyquant vignettes详细介绍了如何使用该软件包。


library(tidyquant)
# Get prices
price_tbl <- c("FB", "AMZN", "NFLX", "GOOG") %>%
    tq_get(get  = "stock.prices",
           from = "2010-01-01",
           to   = "2016-12-31")
price_tbl
#> # A tibble: 6,449 × 8
#>    symbol       date  open  high   low close    volume adjusted
#>     <chr>     <date> <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
#> 1      FB 2012-05-18 42.05 45.00 38.00 38.23 573576400    38.23
#> 2      FB 2012-05-21 36.53 36.66 33.00 34.03 168192700    34.03
#> 3      FB 2012-05-22 32.61 33.59 30.94 31.00 101786600    31.00
#> 4      FB 2012-05-23 31.37 32.50 31.36 32.00  73600000    32.00
#> 5      FB 2012-05-24 32.95 33.21 31.77 33.03  50237200    33.03
#> 6      FB 2012-05-25 32.90 32.95 31.11 31.91  37149800    31.91
#> 7      FB 2012-05-29 31.48 31.69 28.65 28.84  78063400    28.84
#> 8      FB 2012-05-30 28.70 29.55 27.86 28.19  57267900    28.19
#> 9      FB 2012-05-31 28.55 29.67 26.83 29.60 111639200    29.60
#> 10     FB 2012-06-01 28.89 29.15 27.39 27.72  41855500    27.72
#> # ... with 6,439 more rows

# Convert to period returns
return_tbl <- price_tbl %>%
    group_by(symbol) %>%
    tq_transmute(ohlc_fun   = Ad, 
                 mutate_fun = periodReturn,
                 period     = "daily")
return_tbl
#> Source: local data frame [6,449 x 3]
#> Groups: symbol [4]
#> 
#>    symbol       date daily.returns
#>     <chr>     <date>         <dbl>
#> 1      FB 2012-05-18    0.00000000
#> 2      FB 2012-05-21   -0.10986139
#> 3      FB 2012-05-22   -0.08903906
#> 4      FB 2012-05-23    0.03225806
#> 5      FB 2012-05-24    0.03218747
#> 6      FB 2012-05-25   -0.03390854
#> 7      FB 2012-05-29   -0.09620809
#> 8      FB 2012-05-30   -0.02253811
#> 9      FB 2012-05-31    0.05001770
#> 10     FB 2012-06-01   -0.06351355
#> # ... with 6,439 more rows

# Calculate performance
return_tbl %>%
    tq_performance(Ra = daily.returns,
                   performance_fun = CalmarRatio)
#> Source: local data frame [4 x 2]
#> Groups: symbol [4]
#> 
#>   symbol CalmarRatio
#>    <chr>       <dbl>
#> 1     FB  0.50283172
#> 2   AMZN  0.91504597
#> 3   NFLX  0.14444744
#> 4   GOOG  0.05068483

答案 3 :(得分:0)

是否要将数据帧(或任何时间序列)转换为xts或zoo对象(如上述答案),或转换为任何其他时间序列(例如ts对象),{{ 3}}软件包使强制转换变得容易:

PRICE <- structure(list(
  DATE = c(20070103L, 20070104L, 20070105L, 20070108L, 20070109L,
           20070110L, 20070111L, 20070112L, 20070115L),
  CLOSE = c(54.7, 54.77, 55.12, 54.87, 54.86, 54.27, 54.77, 55.36, 55.76)),
  .Names = c("DATE", "CLOSE"), class = "data.frame",
  row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9"))

library(tsbox)

ts_xts(PRICE)
#> [time]: 'DATE' [value]: 'CLOSE'
#> Loading required namespace: xts
#> Registered S3 method overwritten by 'xts':
#>   method     from
#>   as.zoo.xts zoo
#>            CLOSE
#> 2007-01-03 54.70
#> 2007-01-04 54.77
#> 2007-01-05 55.12
#> 2007-01-08 54.87
#> 2007-01-09 54.86
#> 2007-01-10 54.27
#> 2007-01-11 54.77
#> 2007-01-12 55.36
#> 2007-01-15 55.76

ts_ts(PRICE)
#> [time]: 'DATE' [value]: 'CLOSE'
#> Time Series:
#> Start = 2007.00547581401 
#> End = 2007.0383306981 
#> Frequency = 365.2425 
#>  [1] 54.70 54.77 55.12    NA    NA 54.87 54.86 54.27 54.77 55.36    NA
#> [12]    NA 55.76

答案 4 :(得分:0)

此答案基于@Joshua_Ulrich的答案,它根据内置的airquality数据集创建了一个时间序列,该数据集包含“ 1973年5月至1973年9月纽约的每日空气质量测量”。

> head(airquality,3)
  Ozone Solar.R Wind Temp Month Day
1    41     190  7.4   67     5   1
2    36     118  8.0   72     5   2
3    12     149 12.6   74     5   3

将月和日转换为“日期”类的向量

airqualitydate = as.Date(sprintf("1973%02.0f%02.0f", airquality$Month, airquality$Day), 
                         format="%Y%m%d")

创建时间序列对象

ts_airquality <- xts(airquality, airqualitydate)
head(ts_airquality, 3)
           Ozone Solar.R Wind Temp Month Day
1973-05-01    41     190  7.4   67     5   1
1973-05-02    36     118  8.0   72     5   2
1973-05-03    12     149 12.6   74     5   3

图解说明plot.xts()函数的不同输出。 (与plot(airquality)相比)

plot(ts_airquality$Ozone, main="Ozone (ppb)")
lines(ts_airquality$Temp, on=NA, main="Temperature (degrees F)")

enter image description here

请注意,基本的R ts()方法最适合于季度或年度数据。 如in an answer to "starting a daily time series in R"所述:

“时间序列对象不适用于创建每日时间序列。我建议您使用动物园图书馆。”

特别是xts包是zoo的扩展。

相关问题