0
votes

I am using the [dowjones][1] dataset but I think maybe my date format is incorrect because when I run the zoo function to make the data time series I get the warning:

some methods for “zoo” objects do not work if the index entries in ‘order.by’ are not unique

My code:

dow = read.table('dow_jones_index.data', header=T, sep=',')
dowts = zoo(dow$close, as.Date(as.character(dow$date), format = "%m/%d/%Y"))

The dates look like this: 5/6/2011

Does my error have to do with using an incorrect date format? Or something else?

Thank you.

EDIT:

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

1
what is zoo method? - dikesh
"Package ”zoo” provides methods for dealing with totally ordered indexed observations.In other words it allows creating and manipulating time series. However the main objective of this package is handling ”Irregular Time Series” where the base package of R doesn’t allow in handling irregularly spaced observations. Therefore key feature is that the independence of particular index/date/time. Not only that this ”zoo” package is consistent with ”ts” and ”base” R." - hope288
I didn't want to provide my own definition since I'm new to using it, as well as dealing with time series data. Reference: people.stat.sfu.ca/~dac5/CompStat/CompStat2013/… - hope288
This is not an error... It's a warning !! - dikesh

1 Answers

1
votes

The problem as the warning message indicates is that your date values are not unique. This is because your data is in long format with multiple stocks. A timeseries has to be in a matrix like structure with each column representing a stock and each row a point in time. With dcast from the package reshape2 this straigthforward:

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"))