how to put external regressors in DCC model in R studio

Hello, i have four variables in a csv format file (A,B,C,D), i can run dcc model in r without external regressors but now i want to put two variables (C,D), into the mean equation and also both of them in variance equation and other two variables (A,B) are dependent variables. Now kindly guide me how can i make a command for DCC model while using rmgarch package in r studio. i shall be grateful to you, please answer my query...

spec <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1),submodel = NULL, external.regressors = matrix(cbind(stocka$PAK,stocka$IND),ncol = 2), variance.targeting = FALSE), mean.model = list(armaOrder = c(0, 0), external.regressors = matrix(cbind(stocka$PAK,stocka$IND),ncol = 2),distribution.model = "norm", start.pars = list(), fixed.pars = list()))
spec2 = multispec(replicate(4, spec))
cl1 = makePSOCKcluster(4)
multft = multifit(uspec, stocka, cluster = cl1)
multft
specmv = dccspec(uspec = uspec, dccOrder = c(1, 1), (external.regressors = stocka$PAK,stocka$IND), ncol = 2)), distribution = 'mvnorm')
fitmv = dccfit(specmv, data = stocka, fit.control = list(eval.se = TRUE), fit = multf, cluster = cl)
fitmv
i have tried this model as i said i want to put two variables (stocka$PAK and stocka$IND) into mean and as well as variance equation of multivariate garch. my all command are working at the multft stage, as you can see results of multft command below but i am unable to set the dccspec command denoted by "specmv" in the above commands and got the following error. now help me to set dccspec command and further commands for complete results. I shall be thankful to you.

multft results
----------------------------

  • GARCH Multi-Fit        *
    

----------------------------
No. Assets :4
GARCH Multi-Spec Type : Equal
GARCH Model Spec

Model : sGARCH
Exogenous Regressors in variance equation: 2

Mean Equation :
Include Mean : 1
AR(FI)MA Model : (0,d,0)
GARCH-in-Mean : FALSE
Exogenous Regressors in mean equation: 2
Conditional Distribution: norm

GARCH Model Fit

Optimal Parameters:
WTI IND PAK BAN
mu 3505.89032 0.000 0.00000 217.65067
mxreg1 3.74979 0.000 1.00000 0.66725
mxreg2 2.05562 1.000 0.00000 0.20710
omega 20042.42826 2670.472 86.44499 60.16874
alpha1 0.82333 0.050 0.05000 0.49192
beta1 0.00000 0.900 0.90000 0.47664
vxreg1 0.00000 0.000 0.00000 0.00000
vxreg2 0.00000 0.000 0.00000 0.01040
Log-Lik -16089.42721 -13584.784 -9701.77805 -11366.07593
as you can see here two external regressors are shown in mean and as well as in variance equation but i am unable to get them in dcc.
dccspec error given by R studio.
Error: unexpected ',' in "specmv = dccspec(uspec = uspec, dccOrder = c(1, 1), (external.regressors = stocka$PAK,"
i also tried to change this command like this

specmv = dccspec(uspec = uspec, dccOrder = c(1, 1), (external.regressors = matrix(cbind(stocka$PAK,stocka$IND), ncol = 2)), distribution = 'mvnorm')
Warning message:
In if (VAR) { :
the condition has length > 1 and only the first element will be used
fitmv = dccfit(specmv, data = stocka, fit.control = list(eval.se = TRUE), fit = multf, cluster = cl)
Error in .dccfit(spec = spec, data = data, out.sample = out.sample, solver = solver, :

The user supplied fit object has a non-null mean specification but VAR already chosen for mean filtration!!!
so this is my problem now help me please to overcome this. i am new but passionate to learn R. kindly help me...

This question is also asked here, though it's put on hold currently.

I don't know about DCC model, but a quick google search shows a somewhat similar question, which may be useful:

http://r.789695.n4.nabble.com/Fitting-a-DCC-GARCH-with-more-than-one-external-regressor-per-single-process-using-rmgarch-tp4675275.html

Thanks for your reply Yarnabrina, i have already searched not only this link but also many other links after getting tired I do this post, anyhow I appreciate your help.

Okay. In that case, I think it'll be better if you provide more specifics of your problem, and share what you have tried so far. A reprex will be most useful to show the problems you're facing.

There's an argument external.regressors in the dccspec function, which Allows for a matrix of common pre-lagged external regressors for the VAR option, as per the documentation, though no example is provided. If you are using this function, maybe if you show the problems you're facing, people with more expertise on this field will be able to help you.

Good Luck!

1 Like
spec <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1),submodel = NULL, external.regressors = matrix(cbind(stocka$PAK,stocka$IND),ncol = 2), variance.targeting = FALSE), mean.model = list(armaOrder = c(0, 0), external.regressors = matrix(cbind(stocka$PAK,stocka$IND),ncol = 2),distribution.model = "norm", start.pars = list(), fixed.pars = list()))   
spec2 = multispec(replicate(4, spec))
cl1 = makePSOCKcluster(4)
multft = multifit(uspec, stocka, cluster = cl1)
multft
specmv = dccspec(uspec = uspec, dccOrder = c(1, 1), (external.regressors = stocka$PAK,stocka$IND), ncol = 2)), distribution = 'mvnorm')
fitmv = dccfit(specmv, data = stocka, fit.control = list(eval.se = TRUE), fit = multf, cluster = cl)
fitmv

i have tried this model as i said i want to put two variables (stocka$PAK and stocka$IND) into mean and as well as variance equation of multivariate garch. my all command are working at the multft stage, as you can see results of multft command below but i am unable to set the dccspec command denoted by "specmv" in the above commands and got the following error. now help me to set dccspec command and further commands for complete results. I shall be thankful to you.

multft results
*----------------------------*
*     GARCH Multi-Fit        *
*----------------------------*
No. Assets :4
GARCH Multi-Spec Type : Equal
GARCH Model Spec
--------------------------
Model : sGARCH
Exogenous Regressors in variance equation:  2 

Mean Equation :
Include Mean :  1
AR(FI)MA Model : (0,d,0)
GARCH-in-Mean :  FALSE
Exogenous Regressors in mean equation:  2
Conditional Distribution:  norm 

GARCH Model Fit
--------------------------
Optimal Parameters:
                 WTI        IND         PAK          BAN
mu        3505.89032      0.000     0.00000    217.65067
mxreg1       3.74979      0.000     1.00000      0.66725
mxreg2       2.05562      1.000     0.00000      0.20710
omega    20042.42826   2670.472    86.44499     60.16874
alpha1       0.82333      0.050     0.05000      0.49192 
beta1        0.00000      0.900     0.90000      0.47664
vxreg1       0.00000      0.000     0.00000      0.00000
vxreg2       0.00000      0.000     0.00000      0.01040
Log-Lik -16089.42721 -13584.784 -9701.77805 -11366.07593
as you can see here two external regressors are shown in mean and as well as in variance equation but i am unable to get them in dcc.
dccspec error given by R studio.
Error: unexpected ',' in "specmv = dccspec(uspec = uspec, dccOrder = c(1, 1), (external.regressors = stocka$PAK,"

i also tried to change this command like this

> specmv = dccspec(uspec = uspec, dccOrder = c(1, 1), (external.regressors = matrix(cbind(stocka$PAK,stocka$IND), ncol = 2)), distribution = 'mvnorm')
**Warning message**:
In if (VAR) { :
  the condition has length > 1 and only the first element will be used
> fitmv = dccfit(specmv, data = stocka, fit.control = list(eval.se = TRUE), fit = multf, cluster = cl)
Error in .dccfit(spec = spec, data = data, out.sample = out.sample, solver = solver,  : 
  
The user supplied fit object has a non-null mean specification but VAR already chosen for mean filtration!!!

so this is my problem now help me please to overcome this. i am new but passionate to learn R. kindly help me...

We can't now for sure since you are not providing sample data, so please try to make a proper reproducible example by adding some sample data.

You didn't really need to post the same code twice. I edited your post (the one before Andres' post) to make the code more readable. I hope you will not mind. For future posts, please place your code in between a pair of ```.

You're getting error on this one for obvious reasons. external.regressors takes a matrix, and you supplied two vectors separated by a comma. Because the use of ,, R will think it as a different argument, and the ) afterwards cancelled that possibility.

I can't find any problem with your 2nd implementation, though simply cbind(stocka$PAK,stocka$IND) would have worked for (external.regressors = matrix(cbind(stocka$PAK,stocka$IND), ncol = 2)). Since by default VAR = FALSE, I'm confused why you're getting the warning the condition has length &gt; 1 and only the first element will be used.

As Andres has said, providing some sample data may help others to figure out a solution. If you don't know how, kindly go through the link I shared earlier for reprex and use the datapasta package.

thanks for your attention Yarnabrina and andresrcs, now I tried a new xts data downloaded directly in R as you can see my commands, but I got the same error with that data...
my codes
" startDate=as.Date("2007-01-03")
endDate=as.Date("2019-02-28")
getSymbols("IBM", from = startDate,to=endDate)
getSymbols("GOOG", from =startDate, to=endDate)
getSymbols("BP", from =startDate, to=endDate)
getSymbols("T", from =startDate, to=endDate)
head(IBM)
str(IBM)
chartSeries(IBM)
rIBM<-dailyReturn(IBM)
rBP<-dailyReturn(BP)
rGOOG<-dailyReturn(GOOG)
rt<-dailyReturn(T)
chartSeries(rIBM)
rx<-data.frame(rIBM,rBP,rGOOG,rt)
names(rx)[1]<-"rIBM"
names(rx)[2]<-"rBP"
names(rx)[3]<-"GOOG"
names(rx)[4]<-"rt"
nspec <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1),submodel = NULL, external.regressors = matrix(cbind(rIBM,rBP),ncol = 2), variance.targeting = FALSE), mean.model = list(armaOrder = c(0, 0), external.regressors = matrix(cbind(rIBM,rBP),ncol = 2),distribution.model = "norm", start.pars = list(), fixed.pars = list()))
nspec2 = multispec(replicate(4, nspec))
ncl1 = makePSOCKcluster(4)
nmultft = multifit(nspec2, rx, cluster = ncl1)
nspecmv = dccspec(uspec = nspec2, dccOrder = c(1, 1), (external.regressors = matrix(cbind(rIBM,rBP), ncol = 2)), distribution = 'mvnorm')
Warning message:
In if (VAR) { :
the condition has length > 1 and only the first element will be used

nfitmv = dccfit(nspecmv, data = rx, fit.control = list(eval.se = TRUE), fit = multf, cluster = cl)
Error in .dccfit(spec = spec, data = data, out.sample = out.sample, solver = solver, :

The user supplied fit object has a non-null mean specification but VAR already chosen for mean filtration!!! "
i run dcc garch with one external regressor on the same data and it works but on multiple regressors it gives me warning and error

There are several problems with your code.

  1. This is not a reprex. You have not included the calls to the libraries. Please go through the following link. A reproducible example helps others to help you.
  1. As I said earlier, you don't need matrix(cbind(rIBM,rBP), ncol = 2). If cbind(rIBM, rBP) is good enough, why will you do the extra typing? Moreover, you confused with the brackets and your code read the distribution.model, start.pars and fixed.pars under mean.model, which is not the case.

  2. names(rx) <- c("rIBM", "rBP", "GOOG", "rt") would have been enough.

  3. Please format your code before uploading. It helps.

I modified your code and got the following. I don't have knowledge on DCC models, so can't interpret the results or can't say whether it's right or wrong. You'll have to judge that yourself.

library(quantmod)
#> Loading required package: xts
#> Loading required package: zoo
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
#> Loading required package: TTR
#> Version 0.4-0 included new data defaults. See ?getSymbols.
library(rmgarch)
#> Loading required package: rugarch
#> Loading required package: parallel
#> 
#> Attaching package: 'rugarch'
#> The following object is masked from 'package:stats':
#> 
#>     sigma
#> 
#> Attaching package: 'rmgarch'
#> The following objects are masked from 'package:xts':
#> 
#>     first, last
startDate <- as.Date("2007-01-03")
endDate <- as.Date("2019-02-28")
getSymbols("IBM", from = startDate, to = endDate)
#> 'getSymbols' currently uses auto.assign=TRUE by default, but will
#> use auto.assign=FALSE in 0.5-0. You will still be able to use
#> 'loadSymbols' to automatically load data. getOption("getSymbols.env")
#> and getOption("getSymbols.auto.assign") will still be checked for
#> alternate defaults.
#> 
#> This message is shown once per session and may be disabled by setting 
#> options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
#> 
#> WARNING: There have been significant changes to Yahoo Finance data.
#> Please see the Warning section of '?getSymbols.yahoo' for details.
#> 
#> This message is shown once per session and may be disabled by setting
#> options("getSymbols.yahoo.warning"=FALSE).
#> [1] "IBM"
getSymbols("GOOG", from = startDate, to = endDate)
#> [1] "GOOG"
getSymbols("BP", from = startDate, to = endDate)
#> [1] "BP"
getSymbols("T", from = startDate, to = endDate)
#> [1] "T"
head(IBM)
#>            IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume IBM.Adjusted
#> 2007-01-03    97.18    98.40   96.26     97.27    9196800     70.11556
#> 2007-01-04    97.25    98.79   96.88     98.31   10524500     70.86525
#> 2007-01-05    97.60    97.95   96.91     97.42    7221300     70.22369
#> 2007-01-08    98.50    99.50   98.35     98.90   10340000     71.29050
#> 2007-01-09    99.08   100.33   99.07    100.07   11108200     72.13392
#> 2007-01-10    98.50    99.05   97.93     98.89    8744800     71.28333
str(IBM)
#> An 'xts' object on 2007-01-03/2019-02-27 containing:
#>   Data: num [1:3059, 1:6] 97.2 97.2 97.6 98.5 99.1 ...
#>  - attr(*, "dimnames")=List of 2
#>   ..$ : NULL
#>   ..$ : chr [1:6] "IBM.Open" "IBM.High" "IBM.Low" "IBM.Close" ...
#>   Indexed by objects of class: [Date] TZ: UTC
#>   xts Attributes:  
#> List of 2
#>  $ src    : chr "yahoo"
#>  $ updated: POSIXct[1:1], format: "2019-03-12 18:45:37"
chartSeries(IBM)

rIBM <- dailyReturn(IBM)
rBP <- dailyReturn(BP)
rGOOG <- dailyReturn(GOOG)
rt <- dailyReturn(T)
chartSeries(rIBM)

rx <- data.frame(rIBM, rBP, rGOOG, rt)
names(rx) <- c("rIBM", "rBP", "GOOG", "rt")
nspec <- ugarchspec(variance.model = list(model = "sGARCH",
                                          garchOrder = c(1, 1),
                                          submodel = NULL,
                                          external.regressors = matrix(cbind(rIBM, rBP), ncol = 2),
                                          variance.targeting = FALSE),
                    mean.model = list(armaOrder = c(0, 0),
                                      external.regressors = cbind(rIBM, rBP)),
                    distribution.model = "norm",
                    start.pars = list(),
                    fixed.pars = list())
nspec2 <- multispec(replicate(4, nspec))
ncl1 <- makePSOCKcluster(4)
nmultft <- multifit(nspec2, rx, cluster = ncl1)
(nspecmv <- dccspec(uspec = nspec2,
                    dccOrder = c(1, 1),
                    external.regressors = cbind(rIBM, rBP),
                    distribution = 'mvnorm'))
#> 
#> *------------------------------*
#> *       DCC GARCH Spec         *
#> *------------------------------*
#> Model          :  DCC(1,1)
#> Estimation     :  2-step
#> Distribution   :  mvnorm
#> No. Parameters :  40
#> No. Series     :  4

Hope this helps.

I am very thankful to you for my help and my problem has been solved. Thanks to both of you.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.