I am looking for help with getting a volatility function to work with my dataframe. In the function below, I'm just trying to get price daily log returns for each security (each column in my data is a different security's prices over time), and then calculate an annualized vol.
volcalc= function (x) {
returns=log(x)-log(lag(x))
vol=sd(returns)*sqrt(252)
return(vol)
}
Then I run it with the function below, but it returns a 1*ncol numeric vector of only NAs.
testlag=apply(dataexample,2,volcalc)
My dataframe has NAs galore (it includes all assets over the entire time period, even if they weren't around at the time), and one clear problem is that my function is ignoring the NAs. But when I tried to add various na.rm=TRUE to the function, it did not work at all.
Below is an example dataset, where the columns x and y are different securities, with each row representing a day.
structure(list(x = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
NA, NA), y = c(3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, NA, NA, NA, NA
)), .Names = c("x", "y"), row.names = c(NA, 12L), class = "data.frame")
My question is: How do I either incorporate the NAs in the function or get around this problem in a different way by rewriting the function? Thank you for your help!