Storage of complex time series in R

I have a data block with several columns:

  • state
  • county
  • year

Then x, y and z, where x, y and z are the observations that are unique in the triplet mentioned above. I am looking for a reasonable way to keep this in a time series, and xts will not allow me, since there are several observations for each time index. I looked at the hts package, but it's hard for me to figure out how to get the data from it from the data framework.

(Yes, I asked the same question on Quora, and I was asked to bring it here!)

+6
source share
1 answer

One option is to change your data so that you have a column for each State-County combination. This allows you to build the xts matrix:

require(reshape) Opt1 <- as.data.frame(cast(Data, Date ~ county + State, value="Val")) rownames(Opt1) <- Opt1$Date Opt1$Date <- NULL as.xts(Opt1) 

Alternatively, you can work with the list of xts objects, each time checking that you have the correct format specified by xts. The same goes for any other package package. Possible Solution:

 Opt2 <- with(Data, by(Data,list(county,State,year), function(x){ rownames(x) <- x$Date x <- x["Val"] as.xts(x) } ) ) 

Which will allow something like:

 Opt2[["d","b","2012"]] 

to select a specific time series. You can use all xts options. You can scroll through counties, states, and years to build graphs like this:

enter image description here

Code for the plot:

 counties <- dimnames(Opt2)[[1]] states <- dimnames(Opt2)[[2]] years <- dimnames(Opt2)[[3]] op <- par(mfrow=c(3,6)) apply( expand.grid(counties,states,years),1, function(i){ plot(Opt2[[i[1],i[2],i[3]]],main=paste(i,collapse="-")) invisible() } ) par(op) 

Test data:

 Data <- data.frame( State = rep(letters[1:3],each=90), county = rep(letters[4:6],90), Date = rep(seq(as.Date("2011-01-01"),by="month",length.out=30),each=3), Val = runif(270) ) Data$year <- as.POSIXlt(Data$Date)$year + 1900 
+7
source

Source: https://habr.com/ru/post/887084/


All Articles