[英]Using 'PerformanceAnalytics' package to calculate Performance Measures
I need to use 'PerformanceAnalytics' package of R and to use this package, I understand that I need to convert the data into xts data, which is actually a panel data.我需要使用R的'PerformanceAnalytics'包并使用这个包,我知道我需要将数据转换为xts数据,这实际上是一个面板数据。 Following this forum's suggestion I have done the 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
and xts_list
.它给了我RNOM_list
和xts_list
。
After this, can some please help me to estimate the monthly returns using the function Return.calculate
and lapply
and save the output generated as an addition variable in my original data-set for regression analysis?在此之后,有人可以帮助我使用函数Return.calculate
和lapply
来估计每月的回报,并将生成的输出作为附加变量保存在我的原始数据集中用于回归分析吗? Subsequently, I also need to estimate VaR, ES and semi-sd.随后,我还需要估计 VaR、ES 和 semi-sd。
The data can be downloaded here .数据可以在这里下载。 Note, prccm
is the monthly closing price in the data and gvkey
is the firm ID.注意, prccm
是数据中的月收盘价, gvkey
是公司 ID。
An efficient way to achieve this goal is to covert the Panel Data (long format) into wide format using 'reshape2' package.实现此目标的有效方法是使用“reshape2”包将面板数据(长格式)转换为宽格式。 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.然后只需将其与主数据库合并以执行一些回归。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.