Skip to content
Prev 31100 / 63424 Next

optim() example in relist() help page

I think the optim() example in the Details section of relist()'s help 
page is not totally correct. In particular, in the current form it is 
not taken into account that vcov should be a symmetric matrix and only 
the parameters in the lower (or upper) triangular part should be optimized.

A possible fix is:

ipar <- list(mean = c(0, 1), vcov = c(1, 1, 0))
initial.param <- as.relistable(ipar)

invvech <- function (x) {
     d <- 0.5 * (sqrt(8 * length(x) + 1) - 1)
     out <- matrix(0, d, d)
     out[lower.tri(out, TRUE)] <- x
     ind <- upper.tri(out)
     out[ind] <- t(out)[ind]
     out
}

ll <- function (param.vector) {
     param <- relist(param.vector, skeleton = ipar)
     param$vcov <- invvech(param$vcov)
     -sum(mvdnorm(x, mean = param$mean,
	vcov = param$vcov, log = TRUE))
}

optim(unlist(initial.param), ll)


or another example

Best,
Dimitris

p.s.,

 > R.Version()
$platform
[1] "i386-pc-mingw32"

$arch
[1] "i386"

$os
[1] "mingw32"

$system
[1] "i386, mingw32"

$status
[1] ""

$major
[1] "2"

$minor
[1] "8.1"

$year
[1] "2008"

$month
[1] "12"

$day
[1] "22"

$`svn rev`
[1] "47281"

$language
[1] "R"

$version.string
[1] "R version 2.8.1 (2008-12-22)"