title: Estimation of rolling Value at Risk (VaR) using R
body: <p>I need to perform rolling VaR estimation of daily stock returns. At first I did the following:</p>
  
  <pre><code>library(PerformanceAnalytics)
  data(edhec)
  sample&lt;-edhec[,1:5]
  var605&lt;-rollapply(as.zoo(sample),width=60,FUN=function(x) VaR(R=x,p=.95,method="modified",invert=T),by.column=TRUE,fill=NA)
  </code></pre>
  
  <p>It performs the computation and returns a zoo object but gives a series of warnings as follows:</p>
  
  <pre><code>VaR calculation produces unreliable result (inverse risk) for column: 1 : -0.00030977098532231 
  </code></pre>
  
  <p>Then, I tried the same with sample of my data as follows:</p>
  
  <pre><code>library(foreign)
  sample2 &lt;- read.dta("sample2.dta")
  sample2.xts &lt;- xts(sample2[,-1],order.by=as.Date(sample2$datadate,format= "%Y-%m-%d"))
  any(is.na(sample2.xts))
  var605&lt;-rollapply(as.zoo(sample2.xts),width=60,FUN=function(x) VaR(R=x,p=.95,method="modified",invert=T),by.column=TRUE,fill=NA)
  </code></pre>
  
  <p>But is does not return any zoo object and gives the following warnings and error:</p>
  
  <pre><code>VaR calculation produces unreliable result (inverse risk) for column: 1 : -0.0077322590200255
  Error in if (eval(tmp &lt; 0)) { : missing value where TRUE/FALSE needed
  Called from: top level
  </code></pre>
  
  <p>From an earlier post (<a href="https://stackoverflow.com/questions/25045612/using-rollapply-function-for-var-calculation-using-r">Using rollapply function for VaR calculation using R</a>) I understand that rolling estimation cannot be performed if complete rolling window is missing, but in my data (sample2.dta) there are no missing values.</p>
  
  <p>sample2.dta can be downloaded from <a href="https://drive.google.com/file/d/0B8usDJAPeV85WDdDQTFEbGQwaUU/edit?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/0B8usDJAPeV85WDdDQTFEbGQwaUU/edit?usp=sharing</a></p>
  
  <p>Can anyone please help me to resolve and understand this issue?</p>