Error in R-script: error in abs (alpha) non-numeric argument to mathematical function -
i trying reproduce results book "financial risk modelling , portfolio optimisation r" , error can't seem head around. following error in copposterior function:
error in abs(alpha) : non-numeric argument mathematical function
is able see why error?
the error following script:
library(urca) library(vars) library(fmultivar) ## loading data set , converting zoo data(eustockmarkets) assets <- as.zoo(eustockmarkets) ## aggregating month-end series assetsm <- aggregate(assets, as.yearmon, tail, 1) head(assetsm) ## applying unit root tests sub-sample assetsmsub <- window(assetsm, start = start(assetsm), end = "jun 1996") ## levels adf <- lapply(assetsmsub, ur.df, type = "drift", selectlags = "aic") ers <- lapply(assetsmsub, ur.ers) ## differences dadf <- lapply(diff(assetsmsub), ur.df, selectlags = "aic") ders <- lapply(diff(assetsmsub), ur.ers) ## vecm vec <- ca.jo(assetsmsub, ecdet = "none", spec = "transitory") summary(vec) ## index of time stamps in test (extending window) idx <- index(assetsm)[-c(1:60)] anames <- colnames(assetsm) nassets <- ncol(assetsm) ## function return expectations f1 <- function(x, ci, percent = true){ data <- window(assetsm, start = start(assetsm), end = x) lobs <- t(tail(data, 1)) vec <- ca.jo(data, ecdet = "none", spec = "transitory") m <- vec2var(vec, r = 1) fcst <- predict(m, n.ahead = 1, ci = ci) lu <- matrix(unlist(fcst$fcst), ncol = 4, byrow = true)[, c(2, 3)] re <- rep(0, nassets) pview <- lu[, 1] > lobs nview <- lu[, 2] < lobs re[pview] <- (lu[pview, 1] / lobs[pview, 1] - 1) re[nview] <- (lu[nview, 1] / lobs[nview, 1] - 1) names(re) <- anames if(percent) re <- re * 100 return(re) } returnest <- lapply(idx, f1, ci = 0.5) qv <- zoo(matrix(unlist(returnest), ncol = nassets, byrow = true), idx) colnames(qv) <- anames tail(qv) library(blcop) library(fportfolio) ## computing returns , ew-benchmark returns r <- (assetsm / lag(assetsm, k = -1) -1.0) * 100 ## prior distribution ## fitting of skewed student's t distribution mstfit <- mvfit(r, method = "st") mu <- c(mstfit@fit[["beta"]]) s <- mstfit@fit[["omega"]] skew <- c(mstfit@fit[["alpha"]]) df <- mstfit@fit[["df"]] copprior <- mvdistribution("mvst", dim = nassets, mu = mu, omega = s, alpha = skew, df = df) ## pick matrix , view distributions last forecast retestcop <- returnest[[27]] retestcop pcop <- matrix(0, ncol = nassets, nrow = 3) colnames(pcop) <- anames pcop[1, anames[1]] <- 1 pcop[2, anames[2]] <- 1 pcop[3, anames[4]] <- 1 sds <- apply(r, 2, sd) retviews <- list(distribution("norm", mean = retestcop[1], sd = sds[1]), distribution("norm", mean = retestcop[2], sd = sds[2]), distribution("norm", mean = retestcop[4], sd = sds[4]) ) copviews <- copviews(pick = pcop, viewdist = retviews, confidences = rep(0.5, 3), assetnames = anames) ## simulation of posterior numsim <- 10000 coppost <- copposterior(copprior, copviews, numsimulations = numsim) print(copprior) print(copviews) slotnames(coppost)
look @ structure of mstfit:
str(mstfit)
you can see if want estimated alpha value, need access via:
mstfit@fit$estimated[['alpha']] rather than
mstfit@fit[['alpha']]
Comments
Post a Comment