Sorting xts data to look like panel data in R

安稳与你 提交于 2019-12-11 02:55:48

问题


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"))

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.

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).


回答1:


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

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.

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
})

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 ""

And you can access the attributes via xtsAttributes:

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



回答2:


An efficient way to achieve this goal is to covert the Panel Data (long format) into wide format using 'reshape2' package. After performing the estimations, convert it back to long format or panel data format. Here is an example:

library(foreign)
library(reshape2)
dd <- read.dta("DDA.dta") // DDA.dta is Stata data; keep only date, id and variable of interest (i.e. three columns in total)
wdd<-dcast(dd, datadate~gvkey) // gvkey is the id
require(PerformanceAnalytics)
wddxts <- xts(wdd[,-1],order.by=as.Date(wdd$datadate,format= "%Y-%m-%d"))

ssd60A<-rollapply(wddxts,width=60,SemiDeviation,by.column=TRUE,fill=NA) // e.g of rolling window calculation
ssd60A.df<-as.data.frame(ssd60A.xts) // convert dataframe to xts
ssd60A.df$datadate=rownames(ssd60A.df) // insert time index
lssd60A.df<-melt(ssd60A.df, id.vars=c('datadate'),var='gvkey') // convert back to panel format
write.dta(lssd60A.df,"ssd60A.dta",convert.factors = "string") // export as Stata file

Then simply merge it with the master database to perform some regression.



来源:https://stackoverflow.com/questions/21393866/sorting-xts-data-to-look-like-panel-data-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!