[R] Integration using nlsLM ?
Jeremy Clark
jeremyclarkbio at gmail.com
Wed Jun 3 11:53:05 CEST 2015
Dear All - I'm trying to integrate the following function via nlsLM - and I
think the problem is something to do with passing the correct arguments
(error is given below) - any help gratefully appreciated - many thanks.
library(minpack.lm)
dOM <- function(x, mu = 0, sigma = 1, log = FALSE)
{
if (any(sigma < 0)) stop(paste("sigma must be positive", "\n",
""))
loglik <- x*mu + log(sigma) + sigma*(1-exp(x*mu)) + log(mu)
if(log==FALSE) ft <- exp(loglik) else ft <- loglik
ft
}
vecA <- as.vector(seq(from = 1, to = 29, by = 1))
vecBy <- c(234, 255, 263, 276, 308, 329, 350, 345, 377, 391, 432, 474, 457,
483, 484, 510, 526, 492, 496, 481, 454, 447, 408, 379, 342, 284, 247, 207,
174)
dataframeleft <- data.frame(vecA, vecBy)
colnames(dataframeleft) <- c("vecA", "vecBy")
OMPD <- nlsLM(vecBy ~ alpha*dOM(vecA, mu, sigma), data = dataframeleft,
start = c(alpha = 59000, mu = 0.086, sigma = 0.0013), lower = c(0, 0, 0),
upper = c(Inf, Inf, Inf), control = nls.lm.control(maxiter = 1024, factor =
100, maxfev = 5000), trace = TRUE)
omfunction <- function(mygrid, object) {
predmyp1 <- predict(object = OMPD, x = mygrid, mu = mu, sigma = sigma)
predmyp <- as.vector(as.numeric(predmyp1))
return(predmyp)
}
omfunction(mygrid)
intom <- integrate(omfunction, object = OMPD, lower = 1, upper = 20)
## Error in integrate(omfunction, object = OMPD, lower = 1, upper = 20) :
## evaluation of function gave a result of wrong length
More information about the R-help
mailing list