对XTS数据进行排序以使其看起来像R中的面板数据 [英] Sorting xts data to look like panel data in R

查看:173
本文介绍了对XTS数据进行排序以使其看起来像R中的面板数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用R的'PerformanceAnalytics'程序包并使用此程序包,它需要我将数据转换为xts数据.可以从以下链接下载数据: https://drive.google. com/file/d/0B8usDJAPeV85elBmWXFwaXB4WUE/edit?usp = sharing .因此,我通过使用以下命令创建了xts数据:

I need to use 'PerformanceAnalytics' package of R and to use this package, it requires me to convert the data into xts data. The data can be downloaded from this link: https://drive.google.com/file/d/0B8usDJAPeV85elBmWXFwaXB4WUE/edit?usp=sharing . Hence, I have created an xts data by using the following commands:

data<-read.csv('monthly.csv')
dataxts <- xts(data[,-1],order.by=as.Date(data$datadate,format="%d/%m/%Y"))

但是,这样做之后,它将失去面板数据结构.我试图对xts数据进行排序以使其以面板数据形式返回,但失败了.

But after doing this, it looses the panel data structure. I tried to sort the xts data to get it back in panel data form but failed.

任何人都可以帮助我重组xts数据以使其看起来像面板数据.我需要按公司ID(gvkey)和数据(datadate)对其进行排序.

Can anyone please help me to reorganize the xts data to look like a panel data. I need to sort them by firm id (gvkey) and data(datadate).

推荐答案

xts对象仅按时间索引排序.它们不能用其他任何东西排序.

xts objects are sorted by time index only. They cannot be sorted by anything else.

我鼓励您通过gvkey将data.frame拆分为一个列表.然后将每个列表元素转换为xts,并删除随时间变化的列,并将其存储为xtsAttributes.您可能还需要考虑使用yearmon类,因为您要处理每月数据.

I would encourage you to split your data.frame into a list, by gvkey. Then convert each list element to xts and remove the columns that do not vary across time, storing them as xtsAttributes. You might also want to consider using the yearmon class, since you're dealing with monthly data.

由于您不能在xts对象中混合类型,因此您将不得不确定如何编码非数值,随时间变化的值.

You will have to determine how you want to encode non-numeric, time-varying values, since you cannot mix types in xts objects.

Data <- read.csv('monthly.csv', nrow=1000, as.is=TRUE)
DataList <- split(Data, Data$gvkey)
xtsList <- lapply(DataList, function(x) {
  attrCol <- c("iid","tic","cusip","conm","exchg","secstat","tpci",
    "cik","fic","conml","costat","idbflag","dldte")
  numCol <- c("ajexm","ajpm","cshtrm","prccm","prchm","prclm",
    "trfm", "trt1m", "rawpm", "rawxm", "cmth", "cshom", "cyear")
  toEncode <- c("isalrt","curcdm")
  y <- xts(x[,numCol], as.Date(x$datadate,format="%d/%m/%Y"))
  xtsAttributes(y) <- as.list(x[1,attrCol])
  y
})

每个列表元素现在都是xts对象,并且更加紧凑,因为您无需重复完全冗余的数据.而且,您可以通过lapply和朋友轻松地对每个gvkey进行分析.

Each list element is now an xts object, and is much more compact, since you do not repeat completely redundant data. And you can easily run analysis on each gvkey via lapply and friends.

> str(xtsList[["1004"]])
An ‘xts’ object on 1983-01-31/2012-12-31 containing:
  Data: num [1:360, 1:13] 3.38 3.38 3.38 3.38 3.38 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:13] "ajexm" "ajpm" "cshtrm" "prccm" ...
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
List of 13
 $ iid    : int 1
 $ tic    : chr "AIR"
 $ cusip  : int 361105
 $ conm   : chr "AAR CORP"
 $ exchg  : int 11
 $ secstat: chr "A"
 $ tpci   : chr "0"
 $ cik    : int 1750
 $ fic    : chr "USA"
 $ conml  : chr "AAR Corp"
 $ costat : chr "A"
 $ idbflag: chr "D"
 $ dldte  : chr ""

您可以通过xtsAttributes访问属性:

> xtsAttributes(xtsList[["1004"]])$fic
[1] "USA"
> xtsAttributes(xtsList[["1004"]])$tic
[1] "AIR"

这篇关于对XTS数据进行排序以使其看起来像R中的面板数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆