r - Using `bbmle:mle2` with vector parameters (already works using `optim`) -


i having trouble using bbmle:mle2 function when trying regression. illustrate problem, have come toy example.

we define minus log-likelihood poisson distribution (or custom distribution):

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

in code above, beta parameter vector estimate, z model/design matrix , x variable of interest.

i generate random data work with:

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

i can use optim regression. here intercept model:

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

here intercept + age model:

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

the way large number of different models can assessed quite easy, 1 has specify model matrix. how can made work mle2 function bbmle package?

i can it, if beta one-dimensional:

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

but if beta vector, run problems:

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

i cannot syntax right , cannot find examples in documentation or vignettes me. problem surely beta vector now. documentation suggests using vecpar = t argument way forward "compatibility optim". tips appreciated.

also, there way pass z , x arguments in log-likelihood function in more elegant way mle2 have done in optim?

i think main problem need provide start atomic vector (not 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 know can implement poisson regression more in bbmle formula interface , parameters argument:

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

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -