如何将数据帧转换为时间序列?

How to convert dataframe into time series?

我有一个 csv 文件,其中有 2 个股票收盘价(每日)

Dates   Bajaj_close Hero_close
3/14/2013   1854.8  1669.1
3/15/2013   1850.3  1684.45
3/18/2013   1812.1  1690.5
3/19/2013   1835.9  1645.6
3/20/2013   1840    1651.15
3/21/2013   1755.3  1623.3
3/22/2013   1820.65 1659.6
3/25/2013   1802.5  1617.7
3/26/2013   1801.25 1571.85
3/28/2013   1799.55 1542

我想将以上数据转换成时间序列格式。 (开始日期是 3/14/2013 结束日期是 3/13/2015) 我试过了,但它给了我一些奇怪的输出

values <- bajaj_hero[, -1]  (excluded first column i.e date in real dataset)
bajaj_hero_timeseries <- ts(values,start=c(2013,1),end=c(2015,3),frequency=365)

输出为:

           Bajaj_close Hero_close
2013.000     1854.80    1669.10
2013.003     1850.30    1684.45
2013.005     1812.10    1690.50
2013.008     1835.90    1645.60
2013.011     1840.00    1651.15
2013.014     1755.30    1623.30
2013.016     1820.65    1659.60
2013.019     1802.50    1617.70
2013.022     1801.25    1571.85

输入。 我们将从问题中显示的输入文本开始,因为问题没有提供 csv 输入:

Lines <- "Dates   Bajaj_close Hero_close
3/14/2013   1854.8  1669.1
3/15/2013   1850.3  1684.45
3/18/2013   1812.1  1690.5
3/19/2013   1835.9  1645.6
3/20/2013   1840    1651.15
3/21/2013   1755.3  1623.3
3/22/2013   1820.65 1659.6
3/25/2013   1802.5  1617.7
3/26/2013   1801.25 1571.85
3/28/2013   1799.55 1542"

zoo. "ts" class 系列通常不代表日期索引,但我们可以创建一个动物园系列(参见 zoo package ):

library(zoo)
z <- read.zoo(text = Lines, header = TRUE, format = "%m/%d/%Y")

或者,如果您已经将其读入数据框 DF,则可以将其转换为 zoo,如下面第二行所示:

DF <- read.table(text = Lines, header = TRUE)
z <- read.zoo(DF, format = "%m/%d/%Y")

在上述任一情况下 z 都是具有 "Date" class 时间索引的动物园系列。还可以创建动物园系列 zz,它使用 1、2、3... 作为时间索引:

zz <- z
time(zz) <- seq_along(time(zz))

ts. 这些中的任何一个都可以转换为 "ts" class 系列:

as.ts(z)
as.ts(zz)

第一个有一个时间索引,它是自大纪元(1970 年 1 月 1 日)以来的天数,并且将有缺失天数的 NA,第二个将有 1、2、3 ... 作为时间索引,没有 NA。

月度系列。 通常 "ts" 系列用于月度、季度或年度系列。因此,如果我们要将输入汇总为几个月,我们可以合理地将其表示为 "ts" 系列:

z.m <- as.zooreg(aggregate(z, as.yearmon, mean), freq = 12)
as.ts(z.m)

R 有多种表示时间序列的方法。由于您处理的是股票的每日价格,因此您可能希望考虑金融市场在周末和工作假期休市,以便交易日和日历日不同。但是,您可能需要根据交易日和日历日处理时间序列。例如,每日 returns 是根据连续的每日收盘价计算的,无论是否有周末介入。但您可能还想进行基于日历的报告,例如每周价格汇总。由于这些原因,xts 包(zoo 的扩展)通常与 R 中的财务数据一起使用。下面是一个如何将其与您的数据一起使用的示例。

假设您示例中显示的数据在数据帧 df 中

  library(xts)
  stocks <- xts(df[,-1], order.by=as.Date(df[,1], "%m/%d/%Y"))
#
#  daily returns
#
   returns <- diff(stocks, arithmetic=FALSE ) - 1
#
#  weekly open, high, low, close reports
#
   to.weekly(stocks$Hero_close, name="Hero")

给出输出

           Hero.Open Hero.High Hero.Low Hero.Close
2013-03-15    1669.1   1684.45   1669.1    1684.45
2013-03-22    1690.5   1690.50   1623.3    1659.60
2013-03-28    1617.7   1617.70   1542.0    1542.00

借助库 fpp,您可以轻松创建具有日期格式的时间序列: time_ser=ts(data,frequency=4,start=c(1954,2))

这里我们从 1954 年第二季度开始,频率为四分之一。

查看此问题:Converting data.frame to xts order.by requires an appropriate time-based object,建议查看 order.by、

的参数

Currently acceptable classes include: ‘Date’, ‘POSIXct’, ‘timeDate’, as well as ‘yearmon’ and ‘yearqtr’ where the index values remain unique.

并进一步建议使用 order.by = as.POSIXct,

进行显式转换
df$Date <- as.POSIXct(strptime(df$Date,format),tz="UTC")
xts(df[, -1], order.by=as.POSIXct(df$Date))

你的格式在别处指定的地方,

format <- "%m/%d/%Y" #see strptime for details

聚会晚了,但 tsbox 包旨在执行这样的转换。要将您的数据转换为 ts-object,您可以执行以下操作:

dta <- data.frame(
  Dates = c("3/14/2013", "3/15/2013", "3/18/2013", "3/19/2013"),
  Bajaj_close = c(1854.8, 1850.3, 1812.1, 1835.9),
  Hero_close = c(1669.1, 1684.45, 1690.5, 1645.6)
)

dta
#>       Dates Bajaj_close Hero_close
#> 1 3/14/2013      1854.8    1669.10
#> 2 3/15/2013      1850.3    1684.45
#> 3 3/18/2013      1812.1    1690.50
#> 4 3/19/2013      1835.9    1645.60

library(tsbox)
ts_ts(ts_long(dta))
#> Time Series:
#> Start = 2013.1971293045 
#> End = 2013.21081883954 
#> Frequency = 365.2425 
#>          Bajaj_close Hero_close
#> 2013.197      1854.8    1669.10
#> 2013.200      1850.3    1684.45
#> 2013.203          NA         NA
#> 2013.205          NA         NA
#> 2013.208      1812.1    1690.50
#> 2013.211      1835.9    1645.60

它会自动解析日期、检测频率并在周末显式显示缺失值。使用ts_<class>,您可以将数据转换为任何其他时间序列class。