Implementation of DCC-GARCH - r

I have some issues implementing the DCC-GARCH in R. When I run the following code in R, I always get the same error message that says:
Error in UseMethod("convergence") :
no applicable method for 'convergence' applied to an object of class "try-error"
Unfortunately I have no idea how to fix this...
install.packages("fGarch")
install.packages("rugarch")
install.packages("rmgarch")
library(fGarch)
library(rmgarch)
library(rugarch)
library(tseries)
library(zoo)
#Daten runterladen
ibm <- get.hist.quote(instrument = "DB", start = "2005-11-21",
quote = "AdjClose")
sys<- get.hist.quote(instrument = "^STOXX50E", start = "2005-11-21",
quote = "AdjClose")
#Returns
retibm<-diff(log(ibm))
retsys<-diff(log(sys))
# 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
MSFT.GSPC.ret = merge(retsys,retibm)
plot(MSFT.GSPC.ret)
dcc.fit = dccfit(dcc.garch11.spec, data = MSFT.GSPC.ret)
I wasn't sure if this subforum was the right one, but it seemed more appropiate than the quantitative finance forum. If it is the wrong one, I apologize.

The problem is caused by a somewhat nonstandard behaviour of merge. When merging by column names, we have all = FALSE by default. However, when merging by row names, as in this case, it seems that we have all = TRUE and, hence, MSFT.GSPC.ret contains NA values.
So, using either
MSFT.GSPC.ret <- merge(retsys, retibm, all = FALSE)
or
dcc.fit <- dccfit(dcc.garch11.spec, data = na.omit(MSFT.GSPC.ret))
solves the problem.

Related

importance ranking: error must be an object of class xgb.Booster

I ran a xgboost regression forecast (also tried to complete it with the xgb.Booster.complete). When trying to get the xgb.importance, I get the error massage
Error in xgboost::xgb.importance(case_xgbm) : model: must be an
object of class xgb.Booster
However, when verifying, R says it is an "xgb.Booster" class.
Any idea what is going on?
library(xgboost)
library(caret)
somedata <- MASS::Boston
indexes = createDataPartition(somedata$medv, p = .85, list = F) #medv is the y
train = somedata[indexes, ]
test = somedata[-indexes, ]
train_x = data.matrix(train[, -13])
train_y = train[,13]
xgb_train = xgb.DMatrix(data = train_x, label = train_y)
xgbc = xgboost(data = xgb_train, max.depth = 2, nrounds = 50)
class(xgbc)
xgboost::xgb.importance(xgbc)
xgbc2 = xgb.Booster.complete(xgbc, saveraw = TRUE)
class(xgbc2)
xgboost::xgb.importance(xgbc2)
try
xgboost::xgb.importance(model=xgbc)
this worked for me

R: Can I pass the weight parameter into the params = list() in LightGBM

Recently, I am learning the LightGBM package and want to tune the parameters of it.
I want to try all the parameters which can be tuned in the LightGBM.
One question is: when I build the model using the function: lightgbm(data, label = NULL, weight = NULL, params = list(), nrounds = 10, verbose = 1), can I put the weight and nrounds and many other parameters into a list object and feed to the params argument?
The following code is what I used:
# input data for lgb.Dataset()
data_lgb <- lgb.Dataset(
data = X_tr,
label = y_tr
)
# can I put all parameters to be tuned into this list?
params_list <- list(weight = NULL, nrounds = 20, verbose = 1, learning_rate = 0.1)
# build lightgbm model using only: data_lgb and params_list
lgb_model <- lightgbm(data_lgb, params = params_list)
Can I do this using the above code?
I ask because I have a large training data set (2 million rows and 700 features). If I put the lgb.Dataset() into the lightgbm such as lightgbm(data = lgb.Dataset(data = X_tr,label = y_tr), params = params_list), then It takes time for multiple model building. Therefore, I first get the dataset used for lightgbm and for each model, the dataset is constant, what I did can only focus on the different parameters.
However, I am not sure, in total, how many parameters can be put into the params_list? Such as can the weight parameter be in the params_list? When I look the help ?lightgbm, I notice that the weight parameter and many other parameters are out side of the params_list.
Can you help me figure out: in total which parameters can be put into the params_list? That is the final model is built only using the data argument and params argument (other parameters are put into the params list object) as shown above, is that feasible?
Thank you.
Lightgbm has many params which you can tune. Please read the documentation.
I am pasting some part from one of my model script which shows the process. Should be a good hint for you.
nthread <- as.integer(future::availableCores())
seed <- 1000
EARLY_STOPPING <- 50
nrounds <- 1000
param <- list(objective = "regression"
metric = "rmse",
max_depth = 3,
num_leaves = 5,
learning_rate = 0.1,
nthread = nthread,
bagging_fraction = 0.7,
feature_fraction = 0.7,
bagging_freq = 5,
bagging_seed = seed,
verbosity = -1,
min_data_in_leaf = 5)
dtrain <- lgb.Dataset(data = as.matrix(train_X),
label = train_y)
dval <- lgb.Dataset(data = as.matrix(val_X),
label = val_y)
valids <- list(val = dval)
bst <- lgb.train(param,
data = dtrain,
nrounds = nrounds,
data_random_seed = seed,
early_stopping_rounds = EARLY_STOPPING,
valids = valids)

R package mlr Multilabel Text Classification: how to classify new data

I found this code in a tutorial about multilabel classification with package mlr.
library("mlr")
yeast = getTaskData(yeast.task)
labels = colnames(yeast)[1:14]
yeast.task = makeMultilabelTask(id = "multi", data = yeast, target = labels)
lrn.br = makeLearner("classif.rpart", predict.type = "prob")
lrn.br = makeMultilabelBinaryRelevanceWrapper(lrn.br)
mod = train(lrn.br, yeast.task, subset = 1:1500, weights = rep(1/1500, 1500))
pred = predict(mod, task = yeast.task, subset = 1:10)
pred = predict(mod, newdata = yeast[1501:1600,])
I understand the structure of the dataset yeast, but I do not understand how to use the code when I have new data which I want to classify because then I wouldn´t have any TRUE or FALSE values for the labels. Actually I would have some training data with the same structure as yeast but for my new data the columns 1:14 would be missing.
Am I missunderstanding something? If not: How can I use the code correctly?
Edit:
Here´s a sample code how I would use the code:
library("tm")
train.data = data.frame("id" = c(1,1,2,3,4,4), "text" = c("Monday is nice weather.", "Monday is nice weather.", "Dogs are cute.", "It is very rainy.", "My teacher is angry.", "My teacher is angry."), "label" = c("label1", "label2", "label3", "label1", "label4", "label5"))
test.data = data.frame("id" = c(5,6), "text" = c("Next Monday I will meet my teacher.", "Dogs do not like rain."))
train.data$text = as.character(train.data$text)
train.data$id = as.character(train.data$id)
train.data$label = as.character(train.data$label)
test.data$text = as.character(test.data$text)
test.data$id = as.character(test.data$id)
### Bring training data into structure
train.data$label = make.names(train.data$label)
labels = unique(train.data$label)
# DocumentTermMatrix for all texts
texts = unique(c(train.data$text, test.data$text))
docs <- Corpus(VectorSource(unique(texts)))
terms <-DocumentTermMatrix(docs)
m <- as.data.frame(as.matrix(terms))
# Logical columns for labels
test = data.frame("id" = train.data$id, "topic"=train.data$label)
test2 = as.data.frame(unclass(table(test)))
test2[,c(1:ncol(test2))] = as.logical(unlist(test2[,c(1:ncol(test2))]))
rownames(test2) = unique(test$id)
# Bind columns from dtm
termsDf = cbind(test2, m[1:nrow(test2),])
names(termsDf) = make.names(names(termsDf))
### Create Multilabel Task
classify.task = makeMultilabelTask(id = "multi", data = termsDf, target = labels)
### Now the model
lrn.br = makeLearner("classif.rpart", predict.type = "prob")
lrn.br = makeMultilabelBinaryRelevanceWrapper(lrn.br)
mod = train(lrn.br, classify.task)
### How can I predict for test.data?
So, the problem is that I do not have any labels for test.data because that is what I would actually like to compute?
Edit2:
When I simply use
names(m) = make.names(names(m))
pred = predict(mod, newdata = m[(nrow(termsDf)+1):(nrow(termsDf)+nrow(test.data)),])
the result is for both texts the same and really not that I would expect.

Forecasting densities of exchange rates in R

This is my first post here so I am not quite sure how to frame a question here but I will try my best
I am trying to forecast densities of daily exchange rates, I have chosen EUR/USD as my currency pair that I'd like to forecast. I am using GARCH models to do the forecast. I have done my coding using "rugarch" package. The code looks like this
> ex1 <- as.xts(DEXUSEU) #DEXUSEU is the daily data of exchange rates
> ex2 <- ex1[!is.na(ex1)]
> lex2 <- 100*log((ex2[2:Nd,])/(ex2[1:(Nd-1),])) #taking log differences, it
has 4496 observations
> model1=ugarchspec (variance.model = list(model = "sGARCH", garchOrder = c(1,
1), submodel = NULL, external.regressors = NULL, variance.targeting =
FALSE),mean.model = list(armaOrder = c(0, 0), include.mean = TRUE,
archm = FALSE, archpow = 1, arfima = FALSE, external.regressors = NULL,
archex = FALSE),distribution.model = "std") #specifying garch model with
student t distribution
> modelfit1=ugarchfit(model,data=lex2,out.sample = 2000) #fitting model
> modelroll1=ugarchroll (
model1, data=lex2, n.ahead = 1, forecast.length = 2000,
n.start = NULL, refit.every = 50, refit.window = c("rolling"),
window.size = NULL, solver = "hybrid", fit.control = list(),
solver.control = list(), calculate.VaR = TRUE, VaR.alpha = 0.01,
cluster = NULL, keep.coef = TRUE) #doing rolling window forecast
> plot(modelroll1,which=1)
Thats how the density forecast looks like, and i am quite sure that something is wrong here, it shouldn't look like this:
Can anybody please help and tell me what I did wrong. I can provide additional data/information if needed. I am just not sure what else to provide as this is my first post here. Any kind of help would be very much appreciated.

Unable to generate plot from dccfit (R)

R version: 3.4.2
I'm using rugarch and mgarch to spec and fit model with DCC to my data. The model is generated successfully, however I'm unable to generate the plots. Here's a snippet of my code:
library(rugarch)
library(rmgarch)
da=read.table("d-msft3dx0113.txt",header=T)
MSFT.ret = da[,3]
GSPC.ret = da[,6]
MSFT.GSPC.ret = cbind(MSFT.ret,GSPC.ret)
garch11.spec = ugarchspec(mean.model = list(armaOrder = c(0,0)),
variance.model = list(garchOrder = c(1,1),
model = "sGARCH"),
distribution.model = "norm")
dcc.garch11.spec = dccspec(uspec = multispec( replicate(2, garch11.spec) ),
dccOrder = c(1,1),
distribution = "mvnorm")
dcc.fit = dccfit(dcc.garch11.spec, data = MSFT.GSPC.ret)
dcc.fcst = dccforecast(dcc.fit, n.ahead=100)
plot(dcc.fcst)
When I call for plot, I get this error:
plot(dcc.fcst)
Make a plot selection (or 0 to exit):
Conditional Mean Forecast (vs realized returns)
Conditional Sigma Forecast (vs realized |returns|)
Conditional Covariance Forecast
Conditional Correlation Forecast
EW Portfolio Plot with forecast conditional density VaR limits
Selection: 1
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
I then give it a new plot area:
plot.new()
plot(dcc.fcst)
Which gives me this unhelpful plot:
Selection1Plot
I have the same question, too. I don't know why plot(dcc.fic) cannot work. So I do it manually to extract the correlation and covariance. rcov and rcor are two functions to extract what we need.
plot(rcov(dcc.fit)[1,2,], type = "l", col = "blue",
main = "Conditional Covariance", xlab = "Time",
ylab = "Covariance")
plot(rcor(dcc.fit)[1,2,], type = "l", col = "purple",
main = "Conditional Correlation", xlab = "Time",
ylab = "Correlation")

Resources