R中的时间序列日期格式问题
Time Series date format issue in R
我正在使用 [dowjones][1] 数据集,但我认为我的日期格式可能不正确,因为当我 运行 zoo 函数制作数据时间序列时,我收到警告:
some methods for “zoo” objects do not work if the index entries in
‘order.by’ are not unique
我的代码:
dow = read.table('dow_jones_index.data', header=T, sep=',')
dowts = zoo(dow$close, as.Date(as.character(dow$date), format = "%m/%d/%Y"))
日期如下所示:2011 年 5 月 6 日
我的错误是否与使用不正确的日期格式有关?或者别的什么?
谢谢。
编辑:
hist(dowts, xlab='close change rate', prob=TRUE, main='Histogram',ylim=c(0,.07))
Error in hist.default(dowts, xlab = "close change rate", prob = TRUE,
: character(0)
In addition: Warning messages: 1: In zoo(rval[i],
index(x)[i]) : some methods for “zoo” objects do not work if the
index entries in ‘order.by’ are not unique 2: In
pretty.default(range(x), n = breaks, min.n = 1) : NAs introduced by
coercion [1]:
https://archive.ics.uci.edu/ml/datasets/Dow+Jones+Index
警告消息指出的问题是您的日期值不唯一。这是因为您的数据是包含多个股票的长格式。时间序列必须采用类似矩阵的结构,每一列代表一只股票,每一行代表一个时间点。使用包 reshape2
中的 dcast
这个直截了当的:
library(zoo)
library(reshape2)
dow <- read.table('dow_jones_index.data', header=T, sep=',', stringsAsFactors = FALSE)
# delete $ symbol and coerce to numeric
dow$close <- as.numeric(sub("\$", "",dow$close))
tmp <- dcast(dow, date~stock, value.var = "close")
dowts <- as.zoo(x = tmp[,-1], order.by = as.Date(tmp$date, format = "%m/%d/%Y"))
我正在使用 [dowjones][1] 数据集,但我认为我的日期格式可能不正确,因为当我 运行 zoo 函数制作数据时间序列时,我收到警告:
some methods for “zoo” objects do not work if the index entries in ‘order.by’ are not unique
我的代码:
dow = read.table('dow_jones_index.data', header=T, sep=',')
dowts = zoo(dow$close, as.Date(as.character(dow$date), format = "%m/%d/%Y"))
日期如下所示:2011 年 5 月 6 日
我的错误是否与使用不正确的日期格式有关?或者别的什么?
谢谢。
编辑:
hist(dowts, xlab='close change rate', prob=TRUE, main='Histogram',ylim=c(0,.07))
Error in hist.default(dowts, xlab = "close change rate", prob = TRUE, : character(0) In addition: Warning messages: 1: In zoo(rval[i], index(x)[i]) : some methods for “zoo” objects do not work if the index entries in ‘order.by’ are not unique 2: In pretty.default(range(x), n = breaks, min.n = 1) : NAs introduced by coercion [1]: https://archive.ics.uci.edu/ml/datasets/Dow+Jones+Index
警告消息指出的问题是您的日期值不唯一。这是因为您的数据是包含多个股票的长格式。时间序列必须采用类似矩阵的结构,每一列代表一只股票,每一行代表一个时间点。使用包 reshape2
中的 dcast
这个直截了当的:
library(zoo)
library(reshape2)
dow <- read.table('dow_jones_index.data', header=T, sep=',', stringsAsFactors = FALSE)
# delete $ symbol and coerce to numeric
dow$close <- as.numeric(sub("\$", "",dow$close))
tmp <- dcast(dow, date~stock, value.var = "close")
dowts <- as.zoo(x = tmp[,-1], order.by = as.Date(tmp$date, format = "%m/%d/%Y"))