Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R xts按月滚动计算第2部分_R_Xts_Apply_Quantmod - Fatal编程技术网

R xts按月滚动计算第2部分

R xts按月滚动计算第2部分,r,xts,apply,quantmod,R,Xts,Apply,Quantmod,我想在月末用历史法计算VaR。我的时间序列将从2000年初开始到现在。计算应该从2005年开始,以获得足够的数据。有一篇类似的帖子,我试图修改我案例的代码。每月末的风险值应使用过去的数据 这是我的代码(这里从2012年开始,因为否则需要很长时间): 这似乎适用于不重叠的句点…在代码中,lappy中的函数不使用其参数i: 您正在计算相同的东西(期间内每天的VaR) 一遍又一遍 此外,VaR的默认方法是modified: 您需要指定method=“historical” 如果您想根据当月的每日收益计

我想在月末用历史法计算VaR。我的时间序列将从2000年初开始到现在。计算应该从2005年开始,以获得足够的数据。有一篇类似的帖子,我试图修改我案例的代码。每月末的风险值应使用过去的数据

这是我的代码(这里从2012年开始,因为否则需要很长时间):


这似乎适用于不重叠的句点…

在代码中,
lappy
中的函数不使用其参数
i
: 您正在计算相同的东西(期间内每天的VaR) 一遍又一遍

此外,
VaR
的默认方法是
modified
: 您需要指定
method=“historical”

如果您想根据当月的每日收益计算风险价值, 您建议使用“使用”
apply.“每月”
实际有效:

apply.monthly(ldr_sp500, VaR, method="historical")
如果您想要一个扩展窗口:

library(quantmod)
library(PerformanceAnalytics)
getSymbols("^GSPC", from = "2012-01-01" )
x <- Return.calculate( Ad(GSPC), method = "log" )
idx <- index(x)[endpoints(x, 'months')]
result <- sapply( idx,
  function(i) VaR( x[paste0("/",i)], method = "historical" )
)
xts( result, idx )
库(quantmod)
库(性能分析)
getSymbols(“^GSPC”,from=“2012-01-01”)

在代码中,
lappy
中的函数不使用其参数
i
: 您正在计算相同的东西(期间内每天的VaR) 一遍又一遍

此外,
VaR
的默认方法是
modified
: 您需要指定
method=“historical”

如果您想根据当月的每日收益计算风险价值, 您建议使用“使用”
apply.“每月”
实际有效:

apply.monthly(ldr_sp500, VaR, method="historical")
如果您想要一个扩展窗口:

library(quantmod)
library(PerformanceAnalytics)
getSymbols("^GSPC", from = "2012-01-01" )
x <- Return.calculate( Ad(GSPC), method = "log" )
idx <- index(x)[endpoints(x, 'months')]
result <- sapply( idx,
  function(i) VaR( x[paste0("/",i)], method = "historical" )
)
xts( result, idx )
库(quantmod)
库(性能分析)
getSymbols(“^GSPC”,from=“2012-01-01”)
x