创建用于 plot.xts 的季度 `xts` 时间序列对象
creating quarterly `xts` time-series object for use with plot.xts
我正在努力将一些数据转换为季度时间 xts
系列对象。首先,我的数据不是 适当的基于时间的对象 ,现在 as.yearqtr
的行为我无法理解。
我想要转换对象 df
以便我可以用 plot.xts
绘制它,这是我的最终目标,但我卡住了,如下所示。
df <- structure(list(yrQ = structure(1:7, .Label = c("2016-1", "2016-2",
"2016-3", "2016-4", "2016-5", "2016-6", "2016-7"), class = "factor"),
a = c(4.14, 2.83, 3.71, 4.15, 4.63, 4.91, 5.31), b = c(4.25,
3.5, 3.5, 3.5, 3.5, 3.5, 5)), .Names = c("yrQ", "a", "b"
), row.names = c(NA, 7L), class = "data.frame")
df
# yrQ a b
# 1 2016-1 4.14 4.25
# 2 2016-2 2.83 3.21
# 3 2016-3 3.71 3.21
# 4 2016-4 4.15 3.21
# 5 2016-5 4.63 3.21
# 6 2016-6 4.91 3.21
# 7 2016-7 5.31 5.00
# install.packages(c("xts"), dependencies = TRUE)
library(xts)
xts(df, order.by = df[,1])
# Error in xts(df, order.by = df[, 1]) :
# order.by requires an appropriate time-based object
df$yrQ <- as.yearqtr(df$yrQ)
df
# yrQ a b
# 1 2016 Q1 4.14 4.25
# 2 2016 Q2 2.83 3.21
# 3 2016 Q3 3.71 3.21
# 4 2016 Q4 4.15 3.21
# 5 NA QNA 4.63 3.21
# 6 NA QNA 4.91 3.21
# 7 NA QNA 5.31 5.00
您在 df 中的数据似乎是月度数据,因为它运行时间超过 4。
在那种情况下,我想要走的路是使用 as.yearmon
,然后从那里到 to.quarterly
。如果没有 OHLC 图表,它并没有真正绘制得那么好,所以我也查找了另一个选项。看看你怎么想的。
这是我的尝试:
require(xts)
df <- structure(list(yrQ = structure(1:7, .Label = c("2016-1", "2016-2",
"2016-3", "2016-4", "2016-5", "2016-6", "2016-7"), class = "factor"),
a = c(4.14, 2.83, 3.71, 4.15, 4.63, 4.91, 5.31), b = c(4.25,
3.5, 3.5, 3.5, 3.5, 3.5, 5)), .Names = c("yrQ", "a", "b"
), row.names = c(NA, 7L), class = "data.frame")
df
# using yearmon to create xts
myxts<- xts(df[,-1], order.by = as.yearmon(as.character(df[,1]) ))
myxts
#using to.quarterly .. cannot be used simultaneously with both columns
#guess that would need apply and Reduce
myxtsQ<- to.quarterly(myxts$a)
myxtsQ
plot(myxtsQ)
# require(quantmod)
# quantmod::chartSeries(myxtsQ)
# One option seems to be indexing
# though it seems XTS does not support recycling of boolean indices
#so we need to create a list of indices
indx<- rep( c(T,F,F), ceiling(nrow(myxts)/3 ))
indx
myxtsQ<- myxts[indx,]
plot.xts(myxtsQ)
我正在努力将一些数据转换为季度时间 xts
系列对象。首先,我的数据不是 适当的基于时间的对象 ,现在 as.yearqtr
的行为我无法理解。
我想要转换对象 df
以便我可以用 plot.xts
绘制它,这是我的最终目标,但我卡住了,如下所示。
df <- structure(list(yrQ = structure(1:7, .Label = c("2016-1", "2016-2",
"2016-3", "2016-4", "2016-5", "2016-6", "2016-7"), class = "factor"),
a = c(4.14, 2.83, 3.71, 4.15, 4.63, 4.91, 5.31), b = c(4.25,
3.5, 3.5, 3.5, 3.5, 3.5, 5)), .Names = c("yrQ", "a", "b"
), row.names = c(NA, 7L), class = "data.frame")
df
# yrQ a b
# 1 2016-1 4.14 4.25
# 2 2016-2 2.83 3.21
# 3 2016-3 3.71 3.21
# 4 2016-4 4.15 3.21
# 5 2016-5 4.63 3.21
# 6 2016-6 4.91 3.21
# 7 2016-7 5.31 5.00
# install.packages(c("xts"), dependencies = TRUE)
library(xts)
xts(df, order.by = df[,1])
# Error in xts(df, order.by = df[, 1]) :
# order.by requires an appropriate time-based object
df$yrQ <- as.yearqtr(df$yrQ)
df
# yrQ a b
# 1 2016 Q1 4.14 4.25
# 2 2016 Q2 2.83 3.21
# 3 2016 Q3 3.71 3.21
# 4 2016 Q4 4.15 3.21
# 5 NA QNA 4.63 3.21
# 6 NA QNA 4.91 3.21
# 7 NA QNA 5.31 5.00
您在 df 中的数据似乎是月度数据,因为它运行时间超过 4。
在那种情况下,我想要走的路是使用 as.yearmon
,然后从那里到 to.quarterly
。如果没有 OHLC 图表,它并没有真正绘制得那么好,所以我也查找了另一个选项。看看你怎么想的。
这是我的尝试:
require(xts)
df <- structure(list(yrQ = structure(1:7, .Label = c("2016-1", "2016-2",
"2016-3", "2016-4", "2016-5", "2016-6", "2016-7"), class = "factor"),
a = c(4.14, 2.83, 3.71, 4.15, 4.63, 4.91, 5.31), b = c(4.25,
3.5, 3.5, 3.5, 3.5, 3.5, 5)), .Names = c("yrQ", "a", "b"
), row.names = c(NA, 7L), class = "data.frame")
df
# using yearmon to create xts
myxts<- xts(df[,-1], order.by = as.yearmon(as.character(df[,1]) ))
myxts
#using to.quarterly .. cannot be used simultaneously with both columns
#guess that would need apply and Reduce
myxtsQ<- to.quarterly(myxts$a)
myxtsQ
plot(myxtsQ)
# require(quantmod)
# quantmod::chartSeries(myxtsQ)
# One option seems to be indexing
# though it seems XTS does not support recycling of boolean indices
#so we need to create a list of indices
indx<- rep( c(T,F,F), ceiling(nrow(myxts)/3 ))
indx
myxtsQ<- myxts[indx,]
plot.xts(myxtsQ)