#' Constant Mean Return Model
#'
#' This function calculates the normal returns using the constant-mean-return model.
#' Returns either the residuals or the constant means.
#' @param return.data should be a data frame containing dates (Date) in the first column and returns in the rest.
#' @param residuals Do you want the residuals? Defaults to TRUE. If FALSE the constan means are returned.
#' @keywords Keywords
#' @export
#' @examples
#' ConstantMeanReturn(return.data = ReturnData, residuals = TRUE)
#' ConstantMeanReturn(return.data = ReturnData, residuals = FALSE)
# This function calculates the normal returns using the constant-mean-return model
# Input:
# return.data should be a data frame containing dates (Date) in the first column and returns in the rest.
# Output:
# If residuals = TRUE the residuals from the constant-mean-return model are returned. If FALSE the constant mean is returned.
ConstantMeanReturn <- function(return.data, residuals = TRUE) {
# Set number of observations and assets in return.data and set date vector
observations <- nrow(return.data)
assets <- ncol(return.data) - 1
returns <- return.data[,-1]
# Construct result containers
constant.mean <- as.data.frame(setNames(replicate(assets, 1, simplify = F), colnames(returns)))
resids <- return.data
# If only one asset is included in the study
if(assets == 1){
constant.mean <- mean(returns, na.rm = TRUE)
if (residuals == TRUE) {
resids[,2] <- returns - constant.mean
result <- resids
} else {
result <- constant.mean
}
} else {
# If multiple assets are included in the study
for (i in 1:assets) {
constant.mean[i] <- mean(returns[,i], na.rm = TRUE)
if (residuals == TRUE) {
resids[,i+1] <- returns[,i] - as.numeric(constant.mean[i])
}
}
if (residuals == TRUE) {
result <- resids
} else {
result <- constant.mean
}
}
return(result)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.