r - Using 'PerformanceAnalytics' package to calcuate Performance Measures -
i need use 'performanceanalytics' package of r , use package, understand need convert data xts data, panel data. following forum's suggestion have done following:
library(foreign) rnom <- read.dta("return panel without missing.dta") rnom_list<-split(rnom,rnom$gvkey) xts_list<-lapply(rnom_list,function(x) {out<-xts(x[,-1],order.by=as.date(x$datadate,format="%d/%m/%y")) })
it gives me rnom_list
, xts_list
.
after this, can please me estimate monthly returns using function return.calculate
, lapply
, save output generated addition variable in original data-set regression analysis? subsequently, need estimate var, es , semi-sd.
the data can downloaded here. note, prccm
monthly closing price in data , gvkey
firm id.
an efficient way achieve goal covert panel data (long format) wide format using 'reshape2' package. after performing estimations, convert long format or panel data format. here example:
library(foreign) library(reshape2) dd <- read.dta("dda.dta") // dda.dta stata data; keep date, id , variable of interest (i.e. 3 columns in total) wdd<-dcast(dd, datadate~gvkey) // gvkey 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 xts ssd60a.df$datadate=rownames(ssd60a.df) // insert time index lssd60a.df<-melt(ssd60a.df, id.vars=c('datadate'),var='gvkey') // convert panel format write.dta(lssd60a.df,"ssd60a.dta",convert.factors = "string") // export stata file
then merge master database perform regression.
Comments
Post a Comment