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")
Related
I am performing a meta-analysis of proportions using metaprop function. I am looking at the prevalence of heart fibrosis in people living with HIV.
#mri$lgehivn <- number in people with HIV with fibrosis
#mri$lgehivn <- total number of peopl with HIV who have had CMR
lge.prop <- metaprop(event = mri$lgehivn,
n = mri$hivnmri,
subset = c(1:11, 13:16),
studlab = paper,
data = mri,
method = "Inverse"
sm = "PLOGIT",
random = TRUE,
hakn = FALSE,
pscale = 100,
digits = 1)
I am then passing this into a forest plot:
forest.meta(lge.prop,
rightcols=FALSE,
leftcols=c("studlab", "event", "n", "effect", "ci"),
leftlabs = c("Study", "Cases", "Total", "Prevalence", "95% C.I."),
xlim= c(0,110),
smlab = c("Prevalence of LGE (%)"),
digits = 1,
colgap.left = 1)
This then gives me the following forest plot:
Forest plot of meta analysis
I am trying to remove the line that reports the "Common effect model" and only show the random effect model.
Does anyone know the code for this?
Thank you!
I do not know if it is still relevant but I think that this was introduced with a newer version of the meta package.
When manually installing version 4.15-1, the common effect model was removed automatically.
Please use the bellow code before making forest figure
lge.prop <- metaprop(event = mri$lgehivn,
n = mri$hivnmri,
subset = c(1:11, 13:16),
studlab = paper,
data = mri,
method = "Inverse"
sm = "PLOGIT",
random = TRUE,
hakn = FALSE,
pscale = 100,
digits = 1,
common=F)
I am trying to create an heatmap with a row annotation inclusive of p-values as reported in the example in the guide for the use of the ComplexHeatmap package (https://jokergoo.github.io/ComplexHeatmap-reference/book/heatmap-annotations.html#simple-annotation).
I tried to reproduce the example:
library(ComplexHeatmap)
library(circlize) # colorRamp2 function
set.seed(123)
pvalue = 10^-runif(10, min = 0, max = 3)
is_sig = pvalue < 0.01
pch = rep("*", 10)
pch[!is_sig] = NA
# color mapping for -log10(pvalue)
pvalue_col_fun = colorRamp2(c(0, 2, 3), c("green", "white", "red"))
ha = HeatmapAnnotation(
pvalue = anno_simple(-log10(pvalue), col = pvalue_col_fun, pch = pch),
annotation_name_side = "left")
ht = Heatmap(matrix(rnorm(100), 10), name = "mat", top_annotation = ha)
# now we generate two legends, one for the p-value
# see how we define the legend for pvalue
lgd_pvalue = Legend(title = "p-value", col = pvalue_col_fun, at = c(0, 1, 2, 3),
labels = c("1", "0.1", "0.01", "0.001"))
# and one for the significant p-values
lgd_sig = Legend(pch = "*", type = "points", labels = "< 0.01")
# these two self-defined legends are added to the plot by `annotation_legend_list`
draw(ht, annotation_legend_list = list(lgd_pvalue, lgd_sig))
but when I am creating the annotation ha I get the error
Error in anno_simple(-log10(pvalue), col = pvalue_col_fun, pch = pch) :
could not find function "anno_simple"
likely showing a possible problem with the package.
The version of the ComplexHeatmap package I am running is 1.20.0.
The R version is 3.5.1.
Could you please help me solving this problem?
Thanks
So I'm doing a meta-analysis using the meta.for package in R. I am preparing figures for publication in a scientific journal and i would like to add p-values to my forest plots but with scientific annotation formatted as x10-04 rather than standard
e-04
However the argument ilab in the forest function does not accept expression class objects but only vectors
Here is an example :
library(metafor)
data(dat.bcg)
## REM
res <- rma(ai = tpos, bi = tneg, ci = cpos, di = cneg, data = dat.bcg,
measure = "RR",
slab = paste(author, year, sep = ", "), method = "REML")
# MADE UP PVALUES
set.seed(513)
p.vals <- runif(nrow(dat.bcg), 1e-6,0.02)
# Format pvalues so only those bellow 0.01 are scientifically notated
p.vals <- ifelse(p.vals < 0.01,
format(p.vals,digits = 3,scientific = TRUE,trim = TRUE),
format(round(p.vals, 2), nsmall=2, trim=TRUE))
## Forest plot
forest(res, ilab = p.vals, ilab.xpos = 3, order = "obs", xlab = "Relative Risk")
I want the scientific notation of the p-values to be formatted as x10-04
All the answers to similar questions that i've seen suggest using expression() but that gives Error in cbind(ilab) : cannot create a matrix from type 'expression' which makes sense because the help file on forest specifies that the ilab argument should be a vector.
Any ideas on how I can either fix this or work around it?
A hacky solution would be to
forest.rma <- edit(forest.rma)
Go to line 574 and change
## line 574
text(ilab.xpos[l], rows, ilab[, l], pos = ilab.pos[l],
to
text(ilab.xpos[l], rows, parse(text = ilab[, l]), pos = ilab.pos[l],
fix your p-values and plot
p.vals <- gsub('e(.*)', '~x~10^{"\\1"}', p.vals)
forest(res, ilab = p.vals, ilab.xpos = 3, order = "obs", xlab = "Relative Risk")
I'm trying to study this protein interaction net. I need to do a plot of the degree distribution of the edges (last lines of the code), but I'm not able to make a trend line and the slope (last line of the code). Can someone help me please?
install.packages("igraph")
library("igraph")
tfile<-tempfile()
download.file("http://pdg.cnb.uam.es/pazos/tmp/Yeast_int.txt", tfile)
datosraw <- subset(read.delim(tfile, sep="\t", header=F, stringsAsFactors = F), !is.na(V3) & V3!="" & V3!="METHOD")
names(datosraw)<-c("orf1","orf2","method")
datos<-split(datosraw,datosraw$method)
df.y2h <- graph.data.frame(d = datos$Y2H[1:5125,c(1,2)], directed = FALSE)
deg.dist.df.y2h <- degree.distribution(df.y2h)
plot(deg.dist.df.y2h, xlab="k", ylab="P(k)", main="Y2H")
plot(deg.dist.df.y2h, xlab="log k", ylab="log P(k)", main="Y2H", log = "xy", asp)
abline(lm(deg.dist.df.y2h))
You are placing abline on the plot done on log10-log10 scale. Here's one way of doing it
Your code
library("igraph")
tfile<-tempfile()
download.file("http://pdg.cnb.uam.es/pazos/tmp/Yeast_int.txt", tfile)
datosraw <- subset(read.delim(tfile, sep="\t", header=F, stringsAsFactors = F), !is.na(V3) & V3!="" & V3!="METHOD")
names(datosraw)<-c("orf1","orf2","method")
datos<-split(datosraw,datosraw$method)
df.y2h <- graph.data.frame(d = datos$Y2H[1:5125,c(1,2)], directed = FALSE)
The rest can be wrapped in to a helper function
plot_degree_distribution = function(graph) {
# calculate degree
d = degree(graph, mode = "all")
dd = degree.distribution(graph, mode = "all", cumulative = FALSE)
degree = 1:max(d)
probability = dd[-1]
# delete blank values
nonzero.position = which(probability != 0)
probability = probability[nonzero.position]
degree = degree[nonzero.position]
# plot
plot(probability ~ degree, log = "xy", xlab = "Degree (log)", ylab = "Probability (log)",
col = 1, main = "Degree Distribution")
abline(lm(log10(probability) ~ log10(degree)))
}
plot_degree_distribution(df.y2h)
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.