[R] Get all X iterations in optim output when controls(trace=6)

Greg Snow Greg.Snow at imail.org
Mon Jun 2 23:06:00 CEST 2008


Here is an alternative approach to saving the itterations:

testfunc <- function(data) {
        ms.mat <- matrix(nrow=0, ncol=2)

        f1 <- function(ms, data){
                ms.mat <<- rbind(ms.mat, ms)
                -sum( dnorm(data, mean=ms[1], sd=ms[2], log=TRUE) )
        }

        out1 <- optim( c(0,1), f1, data=data )

        out1$ms.mat <- ms.mat

        out1
}

testdat <- rnorm(100, mean=10, sd=3)

testout <- testfunc(testdat)


And here is one way to look at the different steps/iterations/calls (assuming you have the TeachingDemos package installed):

plotfunc <- function(data, obj, step=1){
        tmp.m <- obj$ms.mat[step,1]
        tmp.s <- obj$ms.mat[step,2]
        hist(data, freq=FALSE,
                main=paste('mean=',round(tmp.m,5),'  sd=',round(tmp.s,5)))
        xx <- seq(min(data), max(data), length.out=250)
        lines(xx, dnorm(xx, mean=tmp.m, sd=tmp.s ) )
}

TeachingDemos::tkexamp(plotfunc(testdat, testout),
        list(step=list('spinbox',init=1,from=1,to=nrow(testout$ms.mat))))


Hope this helps,


--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
(801) 408-8111



> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Cleber
> Nogueira Borges
> Sent: Friday, May 30, 2008 5:45 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Get all X iterations in optim output when
> controls(trace=6)
>
>
> Hi,
> I would like to get all X iterations in optim output in matrix form.
> I know about the follow approach:
>
> sink("reportOptim")
> optim( ......., control=list( trace=6,..........) )
> sink()
> all_iterOptim <- readLines("reportOptim")
> unlink("reportOptim")
> all_iterOptim <- all_iterOptim[ grep( '^X', all_iterOptim ) ]
> ### TODO: the rest !!! :-)
>
> But it is very silly code when the output is large...
> Somebody can help me?
>
> Cleber
>
>
> _______________________________________________________
>
> Experimente já e veja as novidades.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list