Pergunta

I have a list of xts objects (yearly time-series for several countries). Now, I'd like to convert the whole list into a single data frame to run a panel data regression. I would think there is a simple way that allows me to use the name for each list object (xts time-series) & and the time information for each xts object as variables that together uniquely identify each panel data observation.

I hope I stated the problem clearly enough. Here is what I have so far:

library(data.table)
lstData <- Map(as.data.frame, data)
Data <- rbindlist(lstData)

unfortunately, this simple conversion doesn't allow me to keep the country identifier and time-series information as identifiers, which renders is useless if I want to do panel data analysis.

Foi útil?

Solução

I would try this (although the ldply solution may be faster).

library(xts)
A <- xts(read.zoo(data.frame(day=as.Date("2001-05-25") + 1:10, x=rnorm(10), y=rnorm(10))))
B <- xts(read.zoo(data.frame(day=as.Date("2001-05-25") + 1:10, x=rnorm(10), y=rnorm(10))))
C <- list(US=A, CAN=B)
D <- do.call(merge.zoo, C)
E <- data.frame(day=index(D), coredata(D))
reshape(E, direction="long", idvar="day", timevar="country", varying=2:ncol(E))

Yields:

> reshape(E, direction="long", idvar="day", timevar="country", varying=2:ncol(E))
                      day country           x           y
2001-05-26.US  2001-05-26      US -1.14792688 -0.70425857
2001-05-27.US  2001-05-27      US  0.42892010 -0.62678907
2001-05-28.US  2001-05-28      US  1.20302730 -0.88504965
2001-05-29.US  2001-05-29      US  0.14411623  0.62155740
2001-05-30.US  2001-05-30      US -0.30979083 -1.63573976
2001-05-31.US  2001-05-31      US -0.53765221 -0.94028377
2001-06-01.US  2001-06-01      US  0.21273968  0.39703515
2001-06-02.US  2001-06-02      US -0.45567642  0.28003478
2001-06-03.US  2001-06-03      US -0.52659903 -1.05184085
2001-06-04.US  2001-06-04      US  0.23540896 -1.52234888
2001-05-26.CAN 2001-05-26     CAN  0.27341723 -0.29382874
2001-05-27.CAN 2001-05-27     CAN  0.08398618  0.88950783
2001-05-28.CAN 2001-05-28     CAN  0.24333694  0.60005146
2001-05-29.CAN 2001-05-29     CAN  0.82480254 -0.77898367
2001-05-30.CAN 2001-05-30     CAN -0.18744699 -1.14777217
2001-05-31.CAN 2001-05-31     CAN  0.98918900 -0.04893292
2001-06-01.CAN 2001-06-01     CAN -0.27379800 -1.23558134
2001-06-02.CAN 2001-06-02     CAN -0.88556293  2.34522201
2001-06-03.CAN 2001-06-03     CAN -0.68985258 -0.37681843
2001-06-04.CAN 2001-06-04     CAN  0.11916878 -2.39336976

Outras dicas

library(xts)
library(plyr)

# make up the data since none proived

data(sample_matrix)
data <- list(country1=as.xts(sample_matrix, descr='my new xts object'),
             country2=as.xts(sample_matrix, descr='my new xts object'),
             country3=as.xts(sample_matrix, descr='my new xts object'),
             country4=as.xts(sample_matrix, descr='my new xts object'))


Data <- ldply(data)

# examine new data frame

str(Data)
'data.frame':   720 obs. of  5 variables:
 $ .id  : chr  "country1" "country1" "country1" "country1" ...
 $ Open : num  50 50.2 50.4 50.4 50.2 ...
 $ High : num  50.1 50.4 50.4 50.4 50.2 ...
 $ Low  : num  50 50.2 50.3 50.2 50.1 ...
 $ Close: num  50.1 50.4 50.3 50.3 50.2 ...

head(Data)
       .id     Open     High      Low    Close
1 country1 50.03978 50.11778 49.95041 50.11778
2 country1 50.23050 50.42188 50.23050 50.39767
3 country1 50.42096 50.42096 50.26414 50.33236
4 country1 50.37347 50.37347 50.22103 50.33459
5 country1 50.24433 50.24433 50.11121 50.18112
6 country1 50.13211 50.21561 49.99185 49.99185

tail(Data)
         .id     Open     High      Low    Close
715 country4 47.20471 47.42772 47.13405 47.42772
716 country4 47.44300 47.61611 47.44300 47.61611
717 country4 47.62323 47.71673 47.60015 47.62769
718 country4 47.67604 47.70460 47.57241 47.60716
719 country4 47.63629 47.77563 47.61733 47.66471
720 country4 47.67468 47.94127 47.67468 47.76719
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top