[R] How to use fixed parameter in mle2() function from bbmle package?

Marc Girondot marc_grt at yahoo.fr
Sun Jun 8 20:59:32 CEST 2014

Dear list-members,

I discover recently bbmle package from an answer in r-list. It makes 
some analyses more easily to solve. However I have one problem using 
fixed parameter of mle2 and one question about parametrization of mle2.
I send the question directly to the maintainer of the package but I get 
no answer still.
If anyone has solution, it would be great. Thanks.

Marc Girondot

I make a very simplified version of the problem I have:
First, a version that works perfectly. It fits simply a Gaussian 
distribution from a set of data:
# generate data from Gaussian distribution
df <- data.frame(A=rnorm(50, 10, 2))

# the function that return log L
knorm <- function(x, mean, sd, log=FALSE) {
   l <- dnorm(x, mean, sd, log=TRUE)
     if (log) l else exp(l)

# fit data using mle2. All is ok
r <- mle2(A ~ knorm(mean=m, sd=s), data=df, start=list(m=10, s=2), 

Then I try to fix one parameter, here sd; and I have problem:
# does not work
r <- mle2(A ~ knorm(mean=m), fixed=list(sd=2), data=df, 
start=list(m=10), control=list(maxit=5000))

Probably I make an error but I cant find what is wrong.

Second, I try to implement parameters as a vector within the list. I 
don't know if it is possible. But at least with this try, it does not work:
knorm <- function(x, par, log=FALSE) {
   l <- dnorm(x, par["mean"], par["sd"], log=TRUE)
     if (log) l else exp(l)

# does not work
r <- mle2(A ~ knorm(par=p), data=df, start=list(p=structure(c(10, 12), 
.Names = c("mean", "sd"))), control=list(maxit=5000))

Is it not possible or I make a mistake ?

More information about the R-help mailing list