R 中的时间序列数据,日期问题
TIme series data in R, problems with dates
Date T1V T2V T3V T1MV T2MV T3MV
1997-12-31 2.631202 2.201695 -0.660092 -0.77492483 0.282662305 4.66506798
1998-01-30 2.193793 3.763458 5.565432 3.50711734 2.874381814 5.14118430
1998-02-27 5.173496 8.727646 6.333820 2.59892279 8.363146480 9.27289259
这是我在 R 中使用的 table。它更大。数据按月计算,直到 2014.The 不同的列只是不同投资组合的 return 日期。如果我想将它用作时间序列数据,我总是会出错。我下载了 PerformanceAnalytics 包。例如,对于它给我的 SharpeRatio 函数。
> SharpeRatio(T1V)
Error in checkData(R) :
The data cannot be converted into a time series. If you are trying to passin names from a data object with one column, you should use the form 'data[rows, columns, drop = FALSE]'. Rownames should have standard date formats, such as '1985-03-15'.
当您查看 table 中的日期列时,您会发现日期格式正是这种格式。
我尝试了一百件事。它也不会让我只用点来绘制图表。
非常感谢任何帮助。
> dput(FactorR[1:5,])
structure(list(Date = structure(1:5, .Label = c("1997-12-31",
"1998-01-30", "1998-02-27", "1998-03-31", "1998-04-30", "1998-05-29",
"1998-06-30", "1998-07-31", "1998-08-31", "1998-09-30", "1998-10-30",
"1998-11-30", "1998-12-31", "1999-01-29", "1999-02-26", "1999-03-31",
"1999-04-30", "1999-05-31", "1999-06-30", "1999-07-30", "1999-08-31",
"1999-09-30", "1999-10-29", "1999-11-30", "1999-12-31", "2000-01-31",
"2000-02-29", "2000-03-31", "2000-04-28", "2000-05-31", "2000-06-30",
"2000-07-31", "2000-08-31", "2000-09-29", "2000-10-31", "2000-11-30",
"2000-12-29", "2001-01-31", "2001-02-28", "2001-03-30", "2001-04-30",
.
.
.
, class = "factor"),
T1V = c(2.631202, 2.193793, 5.173496, 8.033864, 1.369065),
T2V = c(2.201695, 3.763458, 8.727646, 11.375482, 3.097196
), T3V = c(-0.660092, 5.565432, 6.33382, 20.608638, 4.022475
), T1MV = c(-0.774924835, 3.507117337, 2.598922792, 16.26945887,
4.544096701), T2MV = c(0.282662305, 2.874381814, 8.36314648,
12.7091841, 1.078742371), T3MV = c(4.665067984, 5.141184302,
9.27289259, 10.62133318, 2.791853987), T1BTM = c(0.617378168,
3.498582776, 3.332624722, 8.802164975, 1.366229683), T2BTM = c(1.101407825,
5.578394125, 8.910685728, 20.05317039, 1.258609942), T3BTM = c(2.454019461,
2.445706552, 7.991651412, 10.79096755, 5.464002646), T1MOM = c(2.99986853,
4.982808153, 8.657010689, 10.60637296, 4.44333707), T2MOM = c(0.011102554,
3.184165606, 7.55229158, 11.9341773, 0.328377299), T3MOM = c(1.161834369,
3.355709694, 4.025659592, 17.12665788, 3.55822744), Rm = c(1.390935,
3.840895, 6.744987, 13.262647, 2.753486), SMB = c(-5.439992819,
-1.634066965, -6.673969798, 5.648125694, 1.752242715), HML = c(-1.836641293,
1.052876225, -4.65902669, -1.988802574, -4.097772963), MOM = c(1.838034161,
1.62709846, 4.631351096, -6.520284921, 0.885109629)), .Names = c("Date",
"T1V", "T2V", "T3V", "T1MV", "T2MV", "T3MV", "T1BTM", "T2BTM",
"T3BTM", "T1MOM", "T2MOM", "T3MOM", "Rm", "SMB", "HML", "MOM"
), row.names = c(NA, 5L), class = "data.frame")
有两处错误:
- 您的
Date
列不包含日期,但 factor
s.
SharpeRatio
不知道如何将您的 data.frame
转换为时间序列对象。
通过手动进行转换,我们可以指定将哪一列用作时间索引并即时将其转换为 Date
:
library(PerformanceAnalytics)
FactorR_xts <- xts(x = FactorR[, -1], # use all columns except for first column (date) as data
order.by = as.Date(FactorR$Date) # Convert Date column from factor to Date and use as time index
)
SharpeRatio(FactorR_xts)
Date T1V T2V T3V T1MV T2MV T3MV
1997-12-31 2.631202 2.201695 -0.660092 -0.77492483 0.282662305 4.66506798
1998-01-30 2.193793 3.763458 5.565432 3.50711734 2.874381814 5.14118430
1998-02-27 5.173496 8.727646 6.333820 2.59892279 8.363146480 9.27289259
这是我在 R 中使用的 table。它更大。数据按月计算,直到 2014.The 不同的列只是不同投资组合的 return 日期。如果我想将它用作时间序列数据,我总是会出错。我下载了 PerformanceAnalytics 包。例如,对于它给我的 SharpeRatio 函数。
> SharpeRatio(T1V)
Error in checkData(R) :
The data cannot be converted into a time series. If you are trying to passin names from a data object with one column, you should use the form 'data[rows, columns, drop = FALSE]'. Rownames should have standard date formats, such as '1985-03-15'.
当您查看 table 中的日期列时,您会发现日期格式正是这种格式。 我尝试了一百件事。它也不会让我只用点来绘制图表。
非常感谢任何帮助。
> dput(FactorR[1:5,])
structure(list(Date = structure(1:5, .Label = c("1997-12-31",
"1998-01-30", "1998-02-27", "1998-03-31", "1998-04-30", "1998-05-29",
"1998-06-30", "1998-07-31", "1998-08-31", "1998-09-30", "1998-10-30",
"1998-11-30", "1998-12-31", "1999-01-29", "1999-02-26", "1999-03-31",
"1999-04-30", "1999-05-31", "1999-06-30", "1999-07-30", "1999-08-31",
"1999-09-30", "1999-10-29", "1999-11-30", "1999-12-31", "2000-01-31",
"2000-02-29", "2000-03-31", "2000-04-28", "2000-05-31", "2000-06-30",
"2000-07-31", "2000-08-31", "2000-09-29", "2000-10-31", "2000-11-30",
"2000-12-29", "2001-01-31", "2001-02-28", "2001-03-30", "2001-04-30",
.
.
.
, class = "factor"),
T1V = c(2.631202, 2.193793, 5.173496, 8.033864, 1.369065),
T2V = c(2.201695, 3.763458, 8.727646, 11.375482, 3.097196
), T3V = c(-0.660092, 5.565432, 6.33382, 20.608638, 4.022475
), T1MV = c(-0.774924835, 3.507117337, 2.598922792, 16.26945887,
4.544096701), T2MV = c(0.282662305, 2.874381814, 8.36314648,
12.7091841, 1.078742371), T3MV = c(4.665067984, 5.141184302,
9.27289259, 10.62133318, 2.791853987), T1BTM = c(0.617378168,
3.498582776, 3.332624722, 8.802164975, 1.366229683), T2BTM = c(1.101407825,
5.578394125, 8.910685728, 20.05317039, 1.258609942), T3BTM = c(2.454019461,
2.445706552, 7.991651412, 10.79096755, 5.464002646), T1MOM = c(2.99986853,
4.982808153, 8.657010689, 10.60637296, 4.44333707), T2MOM = c(0.011102554,
3.184165606, 7.55229158, 11.9341773, 0.328377299), T3MOM = c(1.161834369,
3.355709694, 4.025659592, 17.12665788, 3.55822744), Rm = c(1.390935,
3.840895, 6.744987, 13.262647, 2.753486), SMB = c(-5.439992819,
-1.634066965, -6.673969798, 5.648125694, 1.752242715), HML = c(-1.836641293,
1.052876225, -4.65902669, -1.988802574, -4.097772963), MOM = c(1.838034161,
1.62709846, 4.631351096, -6.520284921, 0.885109629)), .Names = c("Date",
"T1V", "T2V", "T3V", "T1MV", "T2MV", "T3MV", "T1BTM", "T2BTM",
"T3BTM", "T1MOM", "T2MOM", "T3MOM", "Rm", "SMB", "HML", "MOM"
), row.names = c(NA, 5L), class = "data.frame")
有两处错误:
- 您的
Date
列不包含日期,但factor
s. SharpeRatio
不知道如何将您的data.frame
转换为时间序列对象。
通过手动进行转换,我们可以指定将哪一列用作时间索引并即时将其转换为 Date
:
library(PerformanceAnalytics)
FactorR_xts <- xts(x = FactorR[, -1], # use all columns except for first column (date) as data
order.by = as.Date(FactorR$Date) # Convert Date column from factor to Date and use as time index
)
SharpeRatio(FactorR_xts)