[R] Optimization Question
Ravi Varadhan
rvaradhan at jhmi.edu
Mon Feb 14 17:44:19 CET 2011
Hi Gabriel,
I played with your problem a little bit. I tried both "optimx" and also the
Nelder-Mead function from my recent package "dfoptim". The log-likelihood
is minimized at -Inf. Therefore, I think that either there is a mistake in
the way your likelihood is coded, or that your model is flawed. Please
check your code carefully.
require(optimx)
require(dfoptim) # contains an improved Nelder-Mead algorithm
test <- read.csv("h:/test/gabrielsen_optim_test.csv", header=TRUE)
fn <- function(p) {
ftemp <- 263*log(sqrt(2*pi)*sd(test$A)) + sum(log(abs(c(test$A[-1],
1))^p[3])) + (sum(((test$A-p[1]+(p[2]+1)*c(test$A[-1],
1)))^2)/sum(sd(test$A)*(abs(c(test$A[-1], 1))^p[3])^2))/2
if (is.nan(ftemp)) ftemp <- 1.0e11 #
return(ftemp)
}
out <- optimx(fn, p = c(0.1, 0.1, 2.5), lower=c(-Inf, -Inf, 0),
hessian=TRUE, control=list(all.methods=TRUE))
out1 <- optimx(fn, p = c(0.1, 0.1, 2.5), lower=c(-Inf, -Inf, 0),
upper=rep(10,3), hessian=TRUE, control=list(all.methods=TRUE))
out1 <- optimx(fn, p = c(0.1, 0.1, 2.5), lower=c(-Inf, -Inf, 0),
upper=rep(20,3), hessian=TRUE, control=list(all.methods=TRUE))
out.nm <- nmk(fn, par = c(0.1, 0.1, 2.5),control=list(trace=TRUE))
Hope this helps,
Ravi.
-------------------------------------------------------
Ravi Varadhan, Ph.D.
Assistant Professor,
Division of Geriatric Medicine and Gerontology School of Medicine Johns
Hopkins University
Ph. (410) 502-2619
email: rvaradhan at jhmi.edu
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Ravi Varadhan
Sent: Monday, February 14, 2011 10:20 AM
To: 'Gabrielsen, Alexandros'; r-help at r-project.org
Subject: Re: [R] Optimization Question
Your function is non-smooth and nasty looking. You might want to set the
function value to a large positive number if an illegal arithmetic operation
is performed and `NaN' is returned.
fn <- function(p) {
ftemp <- 263*log(sqrt(2*pi)*sd(test$A))+ sum(log(abs(c(test$A[-1],
1))^p[3])) + (sum(((test$A-p[1]+(p[2]+1)*c(test$A[-1],
1)))^2)/sum(sd(test$A)*(abs(c(test$A[-1], 1))^p[3])^2))/2 if (is.nan(ftemp))
ftemp <- 1.0e11 #
return(ftemp)
}
Another option is to specify bounds on the parameters. This will help avoid
illegal arithmetic.
If you still unsuccessful after these changes, you might want to try
optimizing using "optimx" package, as it will try various optimization
tools. Hopefully, some of them will be successful.
If you send the data test$A, we might be able to help you better.
Hope this helps,
Ravi.
-------------------------------------------------------
Ravi Varadhan, Ph.D.
Assistant Professor,
Division of Geriatric Medicine and Gerontology School of Medicine Johns
Hopkins University
Ph. (410) 502-2619
email: rvaradhan at jhmi.edu
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Gabrielsen, Alexandros
Sent: Monday, February 14, 2011 7:17 AM
To: r-help at r-project.org
Subject: [R] Optimization Question
Hi all,
This is my first optimization code and am receiving the following error for
the following function:
fn <- function(p) {
+263*log(sqrt(2*pi)*sd(test$A))+ sum(log(abs(c(test$A[-1], 1))^p[3])) +
(sum(((test$A-p[1]+(p[2]+1)*c(test$A[-1],
1)))^2)/sum(sd(test$A)*(abs(c(test$A[-1], 1))^p[3])^2))/2
}
out <- optim(fn, p = c(0.1, 0.1, 2.5), method="BFGS", hessian=TRUE)
Error in optim(fn, p = c(0.1, 0.1, 2.5), method = "BFGS", hessian = TRUE) :
non-finite finite-difference value [3]
Have tried multiple initial values however, the error remains tha same.
Running R 2.12.1
Many Thanks!
[[alternative HTML version deleted]]
______________________________________________
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.
______________________________________________
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