Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing complex time-series in R

I have a dataframe with several columns:

  • state
  • county
  • year

Then x, y, and z, where x, y, and z are observations unique to the triplet listed above. I am looking for a sane way to store this in a time series and xts will not let me since there are multiple observations for each time index. I have looked at the hts package, but am having trouble figuring out how to get my data into it from the dataframe.

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

like image 299
James Howard Avatar asked May 02 '11 01:05

James Howard


1 Answers

One option is to reshape your data so you have a column for every State-County combination. This allows you to construct an 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 could work with a list of xts objects, each time making sure that you have the correct format as asked by xts. Same goes for any of the other timeseries packages. A possible solution would be :

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

Which would allow something like :

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

to select a specific time series. You can use all xts options on that. You can loop through the counties, states and years to construct plots like this one :

enter image description here

Code for 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
like image 126
Joris Meys Avatar answered Oct 22 '22 21:10

Joris Meys