简体   繁体   English

使用“PerformanceAnalytics”包来计算性能度量

[英]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_listxts_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.calculatelapply来估计每月的回报,并将生成的输出作为附加变量保存在我的原始数据集中用于回归分析吗? 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.

相关问题 使用PerformanceAnalytics包下标超出范围 - Subscript out of bound using the PerformanceAnalytics package PerformanceAnalytics + data.frame:使用包函数时的格式问题 - PerformanceAnalytics + data.frame: Formatting issue when using package functions 使用PerformanceAnalytics套件计算风险价值 - Calculating Value at Risk with performanceanalytics package 使用textplot()进行文本对齐(PerformanceAnalytics软件包) - text alignment with textplot() (PerformanceAnalytics package) 使用PerformanceAnalytics的问题 - Issues using PerformanceAnalytics 如何使用 PerformanceAnalytics 计算具有 NA 的等权重投资组合回报? - How do I calculate equal weighted portfolio returns with NA using PerformanceAnalytics? 是否有一个R包可以使用clogit或bife为条件(固定效应)逻辑模型计算伪R平方度量? - Is there an R-package to calculate pseudo R-squared measures for conditional (fixed effects) logistic models using clogit or bife? R使用PerformanceAnalytics包绘制“ xy.coords(x,y)中的错误” - R plotting “Error in xy.coords(x, y)” using PerformanceAnalytics package 从PerformanceAnalytics包中保存图表对象 - Save chart object from performanceanalytics package R包'performanceanalytics'优化器中的最大资产数量 - Maximum number of assets in R package 'performanceanalytics' optimizer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM