MLE issues in R

牧云@^-^@ 提交于 2019-12-24 12:33:13

问题


I am new to R and taught myself what I know of R based on the other languages i know. I am in a student research position currently and must use R to find the maximum likelihood estimate of the given likelihood function:

Where g, m_i, x_ij, n_ij, and mu_i are known. I have to maximize theta_i, but i am not sure how since i am mostly self taught. I do know that i should have six estimated values of theta, however. I have tried doing research online about using mle but I am not far into Statistics to understand what the websites are talking about. Any help in figuring out what i am doing wrong would be greatly appreciated. I am unsure how to attach excel files, so i apologize not being able to include data tables.

In trying to teach myself this and work with the professor we receive this error:

Error in do.call("minuslogl", l) : could not find function "minuslogl"

Below is the code i have done up until this point:


library(stats4)
#####################################################################
#Liklihood Model ####CHECK
###################################################################
BB <- function(LITTERS, responses, fetuses, mu, theta) {
  total <- 0

  #1
  for (i in 1:6) {
    firstSum <- 0

    #2
    for (j in 1:LITTERS) {
      secondSum <- 0

      #log(mu[i] + kTheta[i])
      insideFirst <- 0
      for (k in 0:(responses[i,j] - 1)) 
      {
        insideFirst <- insideFirst + log10(mu[i] + k * theta[i])

      }

      #log(1-mu[i] + kTheta[i])
      insideSecond <- 0
      for (k in 0:(fetuses[i,j] - responses[i,j] - 1)) 
      {
        insideSecond <- insideSecond + log10(1 - mu[i] + k * theta[i])
      }

      #log(1 + kTheta[i])
      insideThird <- 0
      for (k in 0:(fetuses[i,j] - 1)) 
      {
        insideThird <- insideThird + log10(1 + k * theta[i])
      }

      secondSum <- insideFirst + insideSecond - insideThird
      firstSum <- firstSum + secondSum
    }
    total <- total + firstSum
  }
  return (total)
}
###################################################################
#Number of litters
LITTERS.M <- 25

doses <- c(0, 30, 45, 60, 75, 90)

  #Retrieves the litter sizes (fetuses)
  litterSize.dose0 <- get.Litter.Sizes(dose0, LITTERS.M)
  litterSize.dose30 <- get.Litter.Sizes(dose30, LITTERS.M)
  litterSize.dose45 <- get.Litter.Sizes(dose45, LITTERS.M)
  litterSize.dose60 <- get.Litter.Sizes(dose60, LITTERS.M)
  litterSize.dose75 <- get.Litter.Sizes(dose75, LITTERS.M)
  litterSize.dose90 <- get.Litter.Sizes(dose90, LITTERS.M)
  litterSize <- c(litterSize.dose0, litterSize.dose30, litterSize.dose45, litterSize.dose60, litterSize.dose75, litterSize.dose90)
  litterSizes <- matrix(litterSize, nrow = 6, ncol = LITTERS.M)

  #Start of Linear Regression for AB By first estimating AB
  estimate.dose0 <- get.estimate.AB(dose0)
  estimate.dose30 <- get.estimate.AB(dose30)
  estimate.dose45 <- get.estimate.AB(dose45)
  estimate.dose60 <- get.estimate.AB(dose60)
  estimate.dose75 <- get.estimate.AB(dose75)
  estimate.dose90 <- get.estimate.AB(dose90)

  rProbR <- c(estimate.dose0, estimate.dose30, estimate.dose45, estimate.dose60,
             estimate.dose75, estimate.dose90)

  ab <- c(get.Log.Estimate(estimate.dose0), get.Log.Estimate(estimate.dose30), get.Log.Estimate(estimate.dose45),
         get.Log.Estimate(estimate.dose60), get.Log.Estimate(estimate.dose75), get.Log.Estimate(estimate.dose90))

  #Fit to Linear Regression
  toFit <- data.frame(rProbR, ab)

  linearRegression <- lm(ab ~ rProbR, data=toFit)
  #Get Coefficients of linear regression of AB
  AApproximation = linearRegression$coefficients[1]
  BApproximation = linearRegression$coefficients[2]


  #Get probability response for each dose group (P(D[i]))
  probabilityResponse.dose0 <- get.Probability.Response.Logistic(AApproximation + BApproximation * 0)
  probabilityResponse.dose30 <- get.Probability.Response.Logistic(AApproximation + BApproximation * 30)
  probabilityResponse.dose45 <- get.Probability.Response.Logistic(AApproximation + BApproximation * 45)
  probabilityResponse.dose60 <- get.Probability.Response.Logistic(AApproximation + BApproximation * 60)
  probabilityResponse.dose75 <- get.Probability.Response.Logistic(AApproximation + BApproximation* 75)
  probabilityResponse.dose90 <- get.Probability.Response.Logistic(AApproximation + BApproximation * 90)
  probabilityResponses <- c(probabilityResponse.dose0, probabilityResponse.dose30, probabilityResponse.dose45, probabilityResponse.dose60, probabilityResponse.dose75, probabilityResponse.dose90)

  #Generate number of responses for each litter (Responses)
  litterResponses.dose0 <- rbinom(LITTERS.M, litterSize.dose0, probabilityResponse.dose0)
  litterResponses.dose30 <- rbinom(LITTERS.M, litterSize.dose30, probabilityResponse.dose30)
  litterResponses.dose45 <- rbinom(LITTERS.M, litterSize.dose45, probabilityResponse.dose45)
  litterResponses.dose60 <- rbinom(LITTERS.M, litterSize.dose60, probabilityResponse.dose60)
  litterResponses.dose75 <- rbinom(LITTERS.M, litterSize.dose75, probabilityResponse.dose75)
  litterResponses.dose90 <- rbinom(LITTERS.M, litterSize.dose90, probabilityResponse.dose90)
  litterResponse <- c(litterResponses.dose0, litterResponses.dose30, litterResponses.dose45, litterResponses.dose60, litterResponses.dose75, litterResponses.dose90)
  litterResponses <- matrix(litterResponse, 6, LITTERS.M)


  backgroundResponseProb <- get.Probability.Response.Logistic(AApproximation + BApproximation * 0)
  backgroundResponseProb <- backgroundResponseProb + .001


  mle(BB(LITTERS.M, litterResponses, litterSizes, probabilityResponse, theta=0))

回答1:


I haven't gone through the entire code (you should try to provide minimal reproducible examples), but the error you're getting is caused by the fact that you're not using the mle function correctly.

The first argument to the mle function should be another function that takes candidate parameters as arguments, and returns the negative log-likelihood of the data as a function of these parameters. The second argument to the mle function is a named list of starting parameters. Look at ?mle for more details.

Here's a minimal example for fitting normally distributed data:

library(stats4)
y <- rnorm(100, 5, 3)  ## Example data
mllNorm <- function(mean, log.sd) {-sum(dnorm(y, mean, exp(log.sd), log=TRUE))}  ## Minus Gaussian log-likelihood
mle.fit <- mle(mllNorm, start=list(mean=1, log.sd=1))  ## MLE
print(mle.fit)

As a more general tip, I highly recommend the maxLik package for MLE. It offers a more flexible interface, prettier output, and more optimization options.



来源:https://stackoverflow.com/questions/33756745/mle-issues-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!