我有一个xts对象列表(几个国家/地区的年度时间序列)。现在,我想将整个列表转换为单个数据框来运行面板数据回归。我认为有一种简单的方法可以让我对每个列表对象使用名称 (xts时间序列)&以及每个 xts对象的时间信息作为一起唯一标识每个面板数据观察的变量。
我希望我能够清楚地说出这个问题。以下是我到目前为止的情况:
library(data.table)
lstData <- Map(as.data.frame, data)
Data <- rbindlist(lstData)
不幸的是,这个简单的转换不允许我将国家标识符和时间序列信息保存为标识符,如果我想进行面板数据分析,则渲染无效。
答案 0 :(得分:1)
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
答案 1 :(得分:1)
我会尝试这个(虽然ldply
解决方案可能更快)。
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))
收率:
> 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