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.