Reputation: 145
I would like to know how to do the maximum likelihood estimation in R when fitting parameters are given in an array. This is needed when the number of parameters is large. So basically, to fit a normal distribution to the data x, I would like to do something like the following
LL <- function(param_array) {
R = dnorm(x, param_array[1], param_array[2])
-sum(log(R))
}
mle(LL, start = list(param_array = c(1,1)))
(Instead of this original code in the first section of http://www.r-bloggers.com/fitting-a-model-by-maximum-likelihood/) If I ran the code above I will get an error
Error in dnorm(x, param_array[1], param_array[2]) : argument "param_array" is missing, with no default
Could anyone let me know how to achieve what I want in the correct way?
Upvotes: 4
Views: 704
Reputation: 73405
stats4::mle
is not a long function, you can inspect it in your R console:
> stats4::mle
Note how start
is handled:
start <- sapply(start, eval.parent)
nm <- names(start)
case 1
If you do:
LL <- function(mu, sigma) {
R = dnorm(x, mu, sigma)
-sum(log(R))
}
mle(LL, start = list(mu = 1, sigma = 1))
you get:
nm
#[1] "mu" "sigma"
Also,
formalArgs(LL)
#[1] "mu" "sigma"
case 2
If you do:
LL <- function(param_array) {
R = dnorm(x, param_array[1], param_array[2])
-sum(log(R))
}
mle(LL, start = list(param_array = c(1,1)))
you get
nm
#[1] NULL
but
formalArgs(LL)
#[1] param_array
The problem
The evaluation of function LL
inside stats::mle
is by matching nm
to the formal arguments of LL
. In case 1, there is no difficulty in matching, but in case 2 you get no match, thus you will fail to evaluate LL
.
So what do people do if they have like 50 parameters? Do they type them in by hand?
Isn't this a bogus argument, after a careful reflection? If you really have 50 parameters, does using an array really save your effort?
First, inside your function LL
, you have to specify param_array[1]
, param_array[2]
, ..., param_array[50]
, i.e., you still need to manually input 50 parameters into the right positions. While when specifying start
, you still need to type in a length-50 vector element by element, right? Isn't this the same amount of work, compared with not using an array but a list?
Upvotes: 3