Re: [R] Help with optim

From: Bojanowski, M.J. (Michal) <M.J.Bojanowski_at_uu.nl>
Date: Wed, 30 May 2007 02:25:35 +0200

Hi,

Unfortunately I don't think it is possible to do exactly what you want, but:

If the numbers reported by 'optim' to the console are enough for you, then consider using 'capture.output'. Below I used the example from 'optim' help page, because I could not use yours directly.

hth,

Michal

# -b-e-g-i-n---R---c-o-d-e-

# this is from the 'optim' example

fr <- function(x) { ## Rosenbrock Banana function

    x1 <- x[1]
    x2 <- x[2]
    100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}

# and now optim-ize capturing the output to 'out' and the results to 'o'
out <- capture.output(

    o <- optim( c(-1.2, 1), fr, method="BFGS",

        control=c(REPORT=1, trace=1))
    )

# 'out' is a character vector storing every line as a separate element
out

# 'o' is returned by optim

o

# to get a grip on the values you could use for example 'strsplit' and then
# extract neccessary info

optimout <- function(out)
{

    # split by spaces
    l <- strsplit(out, " ")
    # just return the numbers
    rval <- sapply(l[-length(l)], function(x) x[length(x)] )     as.numeric(rval)
}

x <- optimout(out)
x
plot(x)

# -e-n-d---R---c-o-d-e-

-----Wiadomość oryginalna-----
Od: r-help-bounces_at_stat.math.ethz.ch w imieniu Anup Nandialath Wysłano: Wt 2007-05-29 08:33
Do: r-help_at_stat.math.ethz.ch
Temat: [R] Help with optim  

Dear Friends,

I'm using the optim command to maximize a likelihood function. My optim command is as follows

estim.out <- optim(beta, loglike, X=Xmain, Y=Y, hessian=T, method="BFGS", control=c(fnscale=-1, trace=1, REPORT=1))

Setting the report=1, gives me the likelihood function value (if i'm correct) at each step. The output from running this is as follows

initial  value 3501.558347 
iter   2 value 3247.277071
iter   3 value 3180.679307
iter   4 value 3157.201356
iter   5 value 3156.579887
iter   6 value 3017.715292
iter   7 value 2993.349538
iter   8 value 2987.181782
iter   9 value 2986.672719
iter  10 value 2986.658620
iter  11 value 2986.658266
iter  12 value 2986.658219
iter  13 value 2986.658156
iter  13 value 2986.658156
iter  13 value 2986.658135

final value 2986.658135
converged

I just wanted to know if there was any way I could get the value of each iteration into an object. At present it is dumped on the screen. But is there a way to get hold of these values through an object??

Thanks in advance

sincerely

Anup  



The fish are biting.

        [[alternative HTML version deleted]]



R-help_at_stat.math.ethz.ch 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.

        [[alternative HTML version deleted]]



R-help_at_stat.math.ethz.ch 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. Received on Wed 30 May 2007 - 00:34:58 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 30 May 2007 - 01:31:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.