R: Error in estimating mle using MaxLik() package


Jasmine Helen

My purpose is to find the maximum likelihood estimator using Newton Raphson's algorithm and compare this solution with glm(). So I try to use maxLik() in R. The result is an error, I have not used this package before, please fix this error, thank you! !

d <- read.delim("http://dnett.github.io/S510/Disease.txt")
    
d$disease=as.factor(d$disease)
d$ses=as.factor(d$ses)
d$sector=as.factor(d$sector)
str(d)

y<-as.numeric(as.character(d$disease))
x1<-as.numeric(as.character(d$age))
x2<-as.numeric(as.character(d$sector))
x3<-as.numeric(as.character(d$ses))

oreduced <- glm(disease ~ age + sector, family=binomial(link = logit), data=d)
summary(oreduced)
    
    
nlldbin=function(param){
  eta<-param[1]+param[2]*x1+param[3]*(x2==2)
  p<-1/(1+exp(-eta))
  -sum(y*log(p)+(1-y)*log(1-p),na.rm=TRUE)
}
est_nr<-maxLik(nlldbin,start=c(0.01,0.01,0.01))
summary(est_nr)

The result is

Iteration 1 
Parameter:
[1]   9841290 377928533   4325584
Gradient (first 30 components):
     [,1] [,2] [,3]
[1,]  NaN  NaN  NaN
Error in maxNRCompute(fn = function (theta, fnOrig, gradOrig = NULL, hessOrig = NULL,  : 
  NA in gradient
user 20650

We are trying to maximize the log-likelihood, but your function is minimizing due to applying a negative number to the sum. So just remove the negation, giving:

nlldbin <- function(param){
  eta <- param[1] + param[2]*x1 + param[3]*(x2==2)
  p <- 1/(1+exp(-eta))  
  sum(y*log(p) + (1-y) * log(1-p), na.rm=TRUE)
}

This is different from other optimizers, for example optim, which usually minimize by default. That's why you negate the sum, as you did.

ps you can use the built-in function to write your function, which might be a bit more stable (and less typing):

nlldbin2 <- function(param){
  eta <- cbind(1, x1, x2 == 2) %*% param
  p <- plogis(eta)
  sum(dbinom(y, 1, p, log=TRUE))
}

Related


R: Estimating Carma(2,1) parameters (using yuima package)

Tobond (This was also posted on Quant.SX, but I'm not sure if this is a better place) I'm very new to R, especially yuimapackages, so I'm hoping someone can help me. I have some data (daily prices) and want to fit a Carma(2,1) model by estimating parameters. S

Maximum Likelihood Estimation - MLE - in R using the plm package

Lucca coding I have unbalanced panel data and I want to fit this type of regression: Pr(y=1|xB) = G(xB+a) where "y" is a binary variable, "x" is a vector of explanatory variables, and "B" is my coefficient. I want to implement a random effects model with maxi

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

Simulated Maximum Likelihood in R, MaxLik

timbo1988 I am trying to estimate a model by simulating maximum likelihood via the MaxLik package in R. Unfortunately, as the amount of data increases, I have serious performance issues. Can anyone give advice on the following questions: Is there a way to spee

Error in estimating CI for GLMM using confint()

tao I have a set of GLMMs fitted with binary response variables and a set of continuous variables, and I want to get confidence intervals for each model. I've been using the confint()function (95% of the profileway) and the method works fine if I apply it to a

Error in estimating CI for GLMM using confint()

tao I have a set of GLMMs fitted with binary response variables and a set of continuous variables, and I want to get confidence intervals for each model. I've been using the confint()function (95% of the profileway) and the method works fine if I apply it to a

MLE error in R: initial value in 'vmmin' is not finite

BiXiC Say I have 2 data.frameobjects: df1 <- data.frame(x = 1:100) df1$y <- 20 + 0.3 * df1$x + rnorm(100) df2 <- data.frame(x = 1:200000) df2$y <- 20 + 0.3 * df2$x + rnorm(200000) I want to do MLE. With df1everything working fine: LL1 <- function(a, b, mu, si

Estimating Rolling Value at Risk (VaR) using R

Jairaj Gupta I need to perform a rolling VaR estimate of daily stock returns. First, I did the following: library(PerformanceAnalytics) data(edhec) sample<-edhec[,1:5] var605<-rollapply(as.zoo(sample),width=60,FUN=function(x) VaR(R=x,p=.95,method="modified",in

Estimating Rolling Value at Risk (VaR) using R

Jairaj Gupta I need to perform a rolling VaR estimate of daily stock returns. First, I did the following: library(PerformanceAnalytics) data(edhec) sample<-edhec[,1:5] var605<-rollapply(as.zoo(sample),width=60,FUN=function(x) VaR(R=x,p=.95,method="modified",in

Estimating Rolling Value at Risk (VaR) using R

Jairaj Gupta I need to perform a rolling VaR estimate of daily stock returns. First, I did the following: library(PerformanceAnalytics) data(edhec) sample<-edhec[,1:5] var605<-rollapply(as.zoo(sample),width=60,FUN=function(x) VaR(R=x,p=.95,method="modified",in

Estimating Rolling Value at Risk (VaR) using R

Jairaj Gupta I need to perform a rolling VaR estimate of daily stock returns. First, I did the following: library(PerformanceAnalytics) data(edhec) sample<-edhec[,1:5] var605<-rollapply(as.zoo(sample),width=60,FUN=function(x) VaR(R=x,p=.95,method="modified",in

Implementing Gaussian Mixture MLE using optim() in R

User 2007598 I am trying to implement MLE for mixture of Gaussians in R using optim() using R's native dataset (Geyser from MASS). My code is as follows. The problem is that optim works fine, but returns the original parameters I passed to it, and also says it

Implementing Gaussian Mixture MLE using optim() in R

User 2007598 I am trying to implement MLE for mixture of Gaussians in R using optim() using R's native dataset (Geyser from MASS). My code is as follows. The problem is that optim works fine, but returns the original parameters I passed to it, and also says it

Implementing Gaussian Mixture MLE using optim() in R

User 2007598 I am trying to implement MLE for mixture of Gaussians in R using optim() using R's native dataset (Geyser from MASS). My code is as follows. The problem is that optim works fine, but returns the original parameters I passed to it, and also says it

Error using dplyr package in R

Mohan Raj I am using the following code to extract a summary of data about column x by calculating the values in column x from dataset unique_data and sorting them in descending order. unique_data %>% group_by(x) %>% arrange(desc(count(x))) However, when I ex

Error in R using Neuronet package

How many I am running a script to estimate a neural network in R using the package Neuronet . I am using Linux OS and the script is as follows: # useful libraries library("XLConnect") library("neuralnet") # data loading; DATA = loadWorkbook("/home/quant/Des

Error using R magick package

User 3357059 I'm trying to save a jpeg image in png format using the magick package in R and I'm running into an error. Here is the error I get when using this code: library(magick) testPic <- "https://upload.wikimedia.org/wikipedia/commons/thumb/4/42/Preside

Error using dplyr package in R

Mohan Raj I am using the following code to extract a summary of data about column x by calculating the values in column x from dataset unique_data and sorting them in descending order. unique_data %>% group_by(x) %>% arrange(desc(count(x))) However, when I ex

Error using dplyr package in R

Mohan Raj I am using the following code to extract a summary of data about column x by calculating the values in column x from dataset unique_data and sorting them in descending order. unique_data %>% group_by(x) %>% arrange(desc(count(x))) However, when I ex

Error in R using Neuronet package

How many I am running a script to estimate a neural network in R using the package Neuronet . I am using Linux OS and the script is as follows: # useful libraries library("XLConnect") library("neuralnet") # data loading; DATA = loadWorkbook("/home/quant/Des

Error using dplyr package in R

Mohan Raj I am using the following code to extract a summary of data about column x by calculating the values in column x from dataset unique_data and sorting them in descending order. unique_data %>% group_by(x) %>% arrange(desc(count(x))) However, when I ex