# [R] optim

From: Gerster Sarah <sgerster_at_student.ethz.ch>
Date: Wed 03 Jan 2007 - 10:22:33 GMT

Hi!

I'm trying to figure out how to use optim... I get some really strange results, so I guess I got something wrong.

I defined the following function which should be minimized: errorFunction <- function(localShifts,globalShift,fileName,experimentalPI,lambda) {
lambda <- 1/sqrt(147)

```  # error <- abs(errHuber(localShifts,globalShift,
#    "/home/sarah/Semesterarbeit/Sequences/R/R1593_filtered.data",3.48)) +
#    sum(abs(localShifts))*lambda

```

error <- sum(abs(localShifts))*lambda
error # return the error to be minimized }

Then I call optim:
par <- seq(length=9, from=0, by=0)
lambda <- 1/sqrt(147)

```        globalShift,
"/home/sarah/Semesterarbeit/Sequences/R/R1593_filtered.data",
experimentalPI=3.48, lambda = lambda)

```

The output is:
\$par

[1] 0.56350964 0.56350964 0.56350964 0.56350964 0.00000000 -0.29515957 [7] 0.00569937 0.32543297 0.18615880

\$value

[1] 0.2529198

\$counts

31 31

\$convergence

[1] 0

\$message

[1] "CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH"

Warning messages:
1: bounds can only be used with method L-BFGS-B in: optim(par, errorFunction, gr = NULL, method = "Nelder-Mead", 2: NAs introduced by coercion

If I change my "error-function" to
errorFunction <- function(localShifts,globalShift,fileName,experimentalPI,lambda) {
error <- sum(abs(localShifts*lambda))
error # return the error to be minimized }

or to:
errorFunction <- function(localShifts,globalShift,fileName,experimentalPI,lambda) {
error <- sum(abs(localShifts))/sqrt(147)   error # return the error to be minimized }

The output is:
\$par

[1] 6.018101e-20 6.018101e-20 6.018101e-20 6.018101e-20 0.000000e+00 [6] 5.176245e-21 -4.002183e-21 -8.254019e-20 3.231412e-21

\$value

[1] 2.768593e-20

\$counts

76 76

\$convergence

[1] 0

\$message

[1] "CONVERGENCE: NORM OF PROJECTED GRADIENT <= PGTOL"

Warning messages:
1: bounds can only be used with method L-BFGS-B in: optim(par, errorFunction, gr = NULL, method = "Nelder-Mead", 2: NAs introduced by coercion

• What is wrong with the first version?

Thanks for the help!

Sarah

R-help@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 Jan 03 21:28:55 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 03 Jan 2007 - 12:30:31 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.