简体   繁体   中英

R: dccfit error: length of 'dimnames' [2] not equal to array extent

Im trying to run the dccfit function for some stock returns in order to model the covariance matrix.

In the model documentation its recommended to use a an xts object. I found an example of how to run the function with a data frame under the bellow link and the example works. But when I try to run it with my own data set I do not get it to work. http://www.unstarched.net/2013/01/03/the-garch-dcc-model-and-2-stage-dccmvt-estimation/ https://faculty.washington.edu/ezivot/econ589/DCCgarchPowerpoint.pdf

I have tried to change the dimnames and the rownames without result

library(zoo)
library(rugarch)
library(xts)
library(rmgarch)

getSymbols(Symbols = c("^GSPC", "SPN"), 
       env = parent.frame(),
       reload.Symbols = FALSE,
       verbose = FALSE,
       warnings = TRUE,
       src = "yahoo",
       symbol.lookup = TRUE,
       auto.assign = getOption('getSymbols.auto.assign',TRUE),
       from = "1927-01-01",
       to = "2018-04-28")

GSPC_dataframe <- data.frame(date=index(GSPC), coredata(GSPC))

## SPECIFYING DCC GARCH
# univariate normal GARCH(1,1) for each series
garch11.spec = ugarchspec(mean.model = list(armaOrder = c(0,0)),variance.model = list(garchOrder = c(1,1),model = "sGARCH"),distribution.model = "norm")
# dcc specification - GARCH(1,1) for conditional correlations
dcc.garch11.spec = dccspec(uspec = multispec( replicate(2, garch11.spec) ),dccOrder = c(1,1),distribution = "mvnorm")
dcc.garch11.spec

## ESTIMATE DCC GARCH data needs to be a dataframe
# I have tried to see if idderent options work
dcc.fit = dccfit(dcc.garch11.spec, data = GSPC$LogReturn)
dcc.fit = dccfit(dcc.garch11.spec, data = GSPC_dataframe$LogReturn)
dcc.fit = dccfit(dcc.garch11.spec, data = GSPC)
dcc.fit = dccfit(dcc.garch11.spec, data = GSPC_dataframe)

I have tried to run the function with both an its object and a data frame and I'm getting the same error:

"Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent"

Grateful for any help or direction!

I'm not sure what a 'dccfit function' is, and it seems like your method is unnecessarily complex. Anyway, if you want a basic correlation matrix and covariance matrix, the script below will do it for you.

library(quantmod)
library(tidyverse)
library(lubridate)

symbols <- c("AAPL", "MSFT", "GOOG")
getSymbols(symbols)
stocks <- data.frame(as.xts(merge(AAPL, MSFT, GOOG)))
stocks$date <- row.names(stocks)
row.names(stocks) <- NULL

# head(stocks)

jan31 <- ymd("2018-01-31")
days_to_keep <- jan31 %m+% months(0:11)
# days_to_keep

res <- stocks %>%
  select(ends_with("Close"), date) %>%
  mutate(date = ymd(date)) %>%
  filter(date %in% days_to_keep)

# correlation matrix:
res %>% select(-date) %>% cor()

#            AAPL.Close MSFT.Close GOOG.Close
# AAPL.Close  1.0000000  0.9198357  0.9313001
# MSFT.Close  0.9198357  1.0000000  0.9103830
# GOOG.Close  0.9313001  0.9103830  1.0000000

# Or more visually...
res %>% select(-date) %>% pairs()


# covariance matrix:
res %>% select(-date) %>% cov()

Instead of this line:

GSPC_dataframe <- data.frame(date=index(GSPC), coredata(GSPC))

You should extract the closing prices from both datasets and merge them into one xts time series:

data <- cbind(Cl(GSPC), Cl(SPN))

Than you can run the dccfit like this:

dcc.fit = dccfit(dcc.garch11.spec, data)

A look at your data with head(GSPC) or head(SPN) will give you some idea why this was not working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM