Use bbmle with vector arguments: mle2 (already run with optim)


Brix

bbmle:mle2I'm having some trouble using the function when trying to do a regression . To illustrate my problem, I came up with a toy example.

We define negative log-likelihood for a Poisson distribution (or any custom distribution):

LL <- function(beta, z, x){
  -sum(stats::dpois(x, lambda = exp(z %*% beta), log = TRUE))
}

In the code above, betais the parameter vector I want to estimate the zmodel/design matrix, xor the variable I'm interested in.

Then I generate some random data to use:

set.seed(2)
age <- round(exp(rnorm(5000, mean = 2.37, sd = 0.78) - 1))
claim <- rpois(5000, lambda = 0.07

optimI can do regression analysis easily . This is the interceptonly model:

z1 <- model.matrix(claim ~ 1)
optim(par = 0, fn = LL, z = z1, x = claim)

Here is the intercept + agemodel:

z2 <- model.matrix(claim ~ age)
optim(par = c(0, 0), fn = LL, z = z2, x = claim)

The way to evaluate a large number of different models is as simple as specifying the model matrix. How can I make it work with the mle2functions in the package bbmle?

I can do, if beta1D:

mle2(minuslogl = function(beta){ LL(beta = beta, z = z1, x = claim) },
  start = list(beta = 0))

But if it beta's a vector, then I have a problem:

mle2(
  minuslogl = function(beta){ LL(beta = beta, z = z2, x = claim) },
  start = list(beta = c(0, 0)),
  vecpar = T,
  parnames = colnames(z2)
  )

I can't get the syntax right and I can't find any examples in the documentation or vignette to help me. The problem must be that it betais now a vector. The documentation recommends using a vecpar = Tparameter that is "and optim" compatible . Any hints would be greatly appreciated.

Also, is there a way to pass the zsum parameter in a more elegant way like I do in the log likelihood function ?xmle2optim

Ben Bock

I think the main problem is that you need to provide startan atomic vector (not a list).

 library(bbmle)
 LL2 <- function(beta) {
     LL(beta, z = z2, x = claim)
 }
 parnames(LL2) <- colnames(z2)
 mle2(
   minuslogl = LL2 ,
     start = setNames(c(0,0),colnames(z2)),
     vecpar = TRUE
  )

It might be helpful to know that something like Poisson regression can be implemented more easily bbmlewith the formula interface and parametersparameters :

mle2(claim~dpois(exp(loglambda)),     ## use log link/exp inverse-link
     data=data.frame(claim,age),      ## need to specify as data frame
     parameters=list(loglambda~age),  ## linear model for loglambda
     start=list(loglambda=0))         ## start values for *intercept*

Related


Use bbmle with vector arguments: mle2 (already run with optim)

Brix bbmle:mle2I'm having some trouble using the function when trying to do a regression . To illustrate my problem, I came up with a toy example. We define negative log-likelihood for a Poisson distribution (or any custom distribution): LL <- function(beta, z

Use bbmle with vector arguments: mle2 (already run with optim)

Brix bbmle:mle2I'm having some trouble using the function when trying to do a regression . To illustrate my problem, I came up with a toy example. We define negative log-likelihood for a Poisson distribution (or any custom distribution): LL <- function(beta, z

Use bbmle with vector arguments: mle2 (already run with optim)

Brix bbmle:mle2I'm having some trouble using the function when trying to do a regression . To illustrate my problem, I came up with a toy example. We define negative log-likelihood for a Poisson distribution (or any custom distribution): LL <- function(beta, z

Use bbmle with vector arguments: mle2 (already run with optim)

Brix bbmle:mle2I'm having some trouble using the function when trying to do a regression . To illustrate my problem, I came up with a toy example. We define negative log-likelihood for a Poisson distribution (or any custom distribution): LL <- function(beta, z

Error running MLE2 function (BBMLE)

tension mle2()I get the following error when running a function from a package bbmlein R : Some parameters are on the boundary: Hessian-based variance-covariance calculations may be unreliable I'm trying to understand if this is due to a problem with my data o

Error running MLE2 function (BBMLE)

tension mle2()I get the following error when running a function from a package bbmlein R : Some parameters are on the boundary: Hessian-based variance-covariance calculations may be unreliable I'm trying to understand if this is due to a problem with my data o

Error running MLE2 function (BBMLE)

tension mle2()I get the following error when running a function from a package bbmlein R : Some parameters are on the boundary: Hessian-based variance-covariance calculations may be unreliable I'm trying to understand if this is due to a problem with my data o

Error running MLE2 function (BBMLE)

tension mle2()I get the following error when running a function from a package bbmlein R : Some parameters are on the boundary: Hessian-based variance-covariance calculations may be unreliable I'm trying to understand if this is due to a problem with my data o

Error running MLE2 function (BBMLE)

tension mle2()I get the following error when running a function from a package bbmlein R : Some parameters are on the boundary: Hessian-based variance-covariance calculations may be unreliable I'm trying to understand if this is due to a problem with my data o

Error running MLE2 function (BBMLE)

tension mle2()I get the following error when running a function from a package bbmlein R : Some parameters are on the boundary: Hessian-based variance-covariance calculations may be unreliable I'm trying to understand if this is due to a problem with my data o

Gaussian mixture modeling with mle2/optim

CodeGuy I have mle2developed a mockup here to demonstrate the problem. x1I generate a sum of values from two separate Gaussian distributions x2, combine them together in form x=c(x1,x2), and then create an MLE that attempts to reclassify xthe values as belongi

Use the mle2 function

username I want to find the parameters of MLE epsilonand muin a pattern like this: $$ X \ sim \ frac {1} {mu1} e ^ {-x / mu1} + \ frac {1} {mu2} e ^ [-x / mu2} $$ I'm trying to do this using a mle2function from a package bbmlelike this: library(Renext) library

Use the mle2 function

username I want to find the parameters of MLE epsilonand muin a pattern like this: $$ X \ sim \ frac {1} {mu1} e ^ {-x / mu1} + \ frac {1} {mu2} e ^ [-x / mu2} $$ I'm trying to do this using a mle2function from a package bbmlelike this: library(Renext) library

Use the mle2 function

username I want to find the parameters of MLE epsilonand muin a pattern like this: $$ X \ sim \ frac {1} {mu1} e ^ {-x / mu1} + \ frac {1} {mu2} e ^ [-x / mu2} $$ I'm trying to do this using a mle2function from a package bbmlelike this: library(Renext) library

Use the mle2 function

username I want to find the parameters of MLE epsilonand muin a pattern like this: $$ X \ sim \ frac {1} {mu1} e ^ {-x / mu1} + \ frac {1} {mu2} e ^ [-x / mu2} $$ I'm trying to do this using a mle2function from a package bbmlelike this: library(Renext) library

Use the mle2 function

username I want to find the parameters of MLE epsilonand muin a pattern like this: $$ X \ sim \ frac {1} {mu1} e ^ {-x / mu1} + \ frac {1} {mu2} e ^ [-x / mu2} $$ I'm trying to do this using a mle2function from a package bbmlelike this: library(Renext) library

Use the mle2 function

username I want to find the parameters of MLE epsilonand muin a pattern like this: $$ X \ sim \ frac {1} {mu1} e ^ {-x / mu1} + \ frac {1} {mu2} e ^ [-x / mu2} $$ I'm trying to do this using a mle2function from a package bbmlelike this: library(Renext) library

Problems implementing MLE estimation in the bbmle package (for R)

will I am trying to validate the MLEs for $\alpha$ , $\beta$ and $\lambda$ obtained for the Logistic-Lomax distribution by Zubair et al. in their paper titled A Study of Logistic-Lomax Distribution when using Dataset 1 . The paper uses the following code to do

Use the names of vector arguments in functions

with stars I'm writing a function that passes in a vector of values, and I want to use those values for calculations inside the function. However, I also want to return a meaningful error message using the names of the vectors I pass, but can't figure out how

Use "Shell" to run an executable with arguments

Leonardo Joksan The problem I was plaguing the game used to be directly in vb, but it's no longer tested and works in start.bat, see code: @echo off start main.exe gt550 exit Wondering the following, how can I run this command in a shell? Tried and Failed

Use "Shell" to run an executable with arguments

Leonardo Joksan The problem I was plaguing the game used to be directly in vb, but it's no longer tested and works in start.bat, see code: @echo off start main.exe gt550 exit Wondering the following, how can I run this command in a shell? Tried and Failed

mle2 on Weibull samples

Siegfried I want to generate mles for Weibull shape and scale parameters using the mle2 function. I have written the following code but I am getting an error: So which component is NULL and should I change to a number? Are there any other issues with the code

mle2 on Weibull samples

Siegfried I want to generate mles for Weibull shape and scale parameters using the mle2 function. I have written the following code but I am getting an error: So which component is NULL and should I change to a number? Are there any other issues with the code

mle2 on Weibull samples

Siegfried I want to generate mles for Weibull shape and scale parameters using the mle2 function. I have written the following code but I am getting an error: So which component is NULL and should I change to a number? Are there any other issues with the code

mle2 on Weibull samples

Siegfried I want to generate mles for Weibull shape and scale parameters using the mle2 function. I have written the following code but I am getting an error: So which component is NULL and should I change to a number? Are there any other issues with the code

mle2 on Weibull samples

Siegfried I want to generate mles for Weibull shape and scale parameters using the mle2 function. I have written the following code but I am getting an error: So which component is NULL and should I change to a number? Are there any other issues with the code