[Rd] optim() example in relist() help page

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Mon Jan 19 14:55:35 CET 2009


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)"

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014



More information about the R-devel mailing list