Hi r-users,
I really need help in fitting the t-copula. I try to reproduce the example
given by Jun Yan in “Enjoy the joy of copula” but I’m not sure how to correct
the error based on the error message. I tried so many ways but still could not
get it working.
loglik.marg <- function(b, x) sum(dgamma(x, shape = b[1], scale = b[2], log =
TRUE))
ctrl <- list(fnscale = -1)
#dat <- stn_pos[,1:2] ## observed data
myCop.t <- ellipCopula(family = "t", param = 0.5,dim = 2, dispstr =
"un", df = 8)
myCop.t
myMvd <- mvdc(copula = myCop.t, margins = c("gamma",
"gamma"), paramMargins = list(list(shape = 1.5, scale = 38),
list(shape = 1.7, scale = 50)))
myMvd
n <- 200
dat <- rmvdc(myMvd, n)
mm <- apply(dat, 2, mean)
vv <- apply(dat, 2, var)
rho <- rcorr(dat,type="spearman")[[1]]; round(rho,2)
rbind(mm,vv)
b1.0 <- c(mm[1]^2/vv[1], vv[1]/mm[1])
b2.0 <- c(mm[2]^2/vv[2], vv[2]/mm[2])
a.0 <- sin(cor(dat[, 1], dat[, 2], method = "kendall") * pi/2)
start <- c(b1.0, b2.0, a.0)
b1hat <- optim(c(1,5), fn = loglik.marg, x = dat[, 1], control = ctrl)$par
b2hat <- optim(c(1,5), fn = loglik.marg, x = dat[, 2], control = ctrl)$par
udat <- cbind(pgamma(dat[, 1], shape = b1hat[1], scale =
b1hat[2]),pgamma(dat[, 2], shape = b2hat[1], scale = b2hat[2]))
> fit.ifl <- fitCopula(myCop.t, udat, method="mpl",
c(1,5,1,5,8),estimate.variance=TRUE)
Error in fitCopula.ml(data, copula, start, lower, upper, optim.control, :
The length of start and copula parameters do not match.
> fit.ifl <- fitCopula(udat, myMvd@copula, c(1,5,1,5,8))
Error in fitCopula(udat, myMvd@copula, c(1, 5, 1, 5, 8)) :
Implemented methods are: ml, mpl, itau, and irho.
In addition: Warning messages:
1: In if (method == "ml") fit <- fitCopula.ml(data, copula, start,
:
the condition has length > 1 and only the first element will be used
2: In if (method == "mpl") fit <- fitCopula.mpl(copula, data,
start, :
the condition has length > 1 and only the first element will be used
3: In if (method == "itau") fit <- fitCopula.itau(copula, data,
estimate.variance) else if (method == :
the condition has length > 1 and only the first element will be used
4: In if (method == "irho") fit <- fitCopula.irho(copula, data,
estimate.variance) else stop("Implemented methods are: ml, mpl, itau, and
irho.") :
the condition has length > 1 and only the first element will be used
> fit.ifl <- fitCopula(udat, myMvd@copula, a.0) ## this given the paper
Error in fitCopula(udat, myMvd@copula, a.0) :
Implemented methods are: ml, mpl, itau, and irho.
Thank you so much for your help.
[[alternative HTML version deleted]]