Tyler Weaver Tyler Weaver - 2 months ago 20
R Question

More MLE troubles

I am in a student research position and am new to R. I have asked a question similar (posted here:MLE Issues). I have resolved the initial problem but i have encountered more problems with this function.

I am still using this function for trying to estimate theta[i],enter image description here
where each of the other variables is currently known.

Below is my code:

maxParam <- function(theta) {
logl <- sum(for (i in 1:length(doses)) {
sum(
for (j in 1:LITTERS.M) {
sum(
for (k in 0:(litterResponses[i,j]-1)) {
sum(log10(probabilityResponses[i] + k * theta[i]))
}
+
for (k in 0:(litterSizes[i,j]-litterResponses[i,j]-1)) {
sum(log10(1 - probabilityResponses[i] + k * theta[i]))
}
-
for (k in 0:(litterSizes[i,j] - 1)) {
sum(log10(1 + k * theta[i]))
}
)
}
)
})

return (-logl)
}

mle.fit <- mle(maxParam, start=list(theta=c(1,1,1,1,1,1)))
print(mle.fit)


The error i am being thrown is:


Error: argument "theta" is missing, with no default


I apologize if the error is silly, I have little knowledge of R.

Notes:
I am using a vector of (1,1,1,1,1,1) as a test for theta. It is not actual data. Doses is a vector of 6 that corresponds to dose levels of a serum. Litter Responses is a matrix that describes the responses to the serum per dose per litter. LitterSizes is a matrix that describes the size of a litter per dose per litter. LITTERS.M is the initial number of litters that came in contact with serum. ProbabilityResponses is a vector that describes the probability that a given mouse will be affected by the serum.

Answer

The function mle does not accept a vector of initial starting values. Each parameter to be found by optimisation needs to be passed as a scalar. It is sufficient to change the declaration of your function to:

maxParam <- function(theta_1 = 1, theta_2 = 1, etc) {
    theta <- unlist(as.list(environment()))

    ... # rest of function follows
  }

where etc means replace this with theta_3 = 1, theta_4 = 1 as necessary. The function mle can then be called with:

mle.fit <- mle(maxParam)