Reputation: 560
Homework.
I am new to R and statistics. I have a problem where is should implement a user defined function that takes degrees of freedom ("df") and a data set as arguments and returns the minus-log-likelihood. It is assmued that the data is chi-squared distrbuted with "df" degrees of freedom.
I know the minus-log-likelihood is defined as:
I will only apply this function to the same data set, so my function can have the signature: loglike <- function(df)
Edit: I followed the user shadows advice and tried to write the function:
loglike <- function(df) {
value <- sum(-log(dchisq(data, df)))
return(value)
}
Can this be right?
Upvotes: 0
Views: 237
Reputation: 3888
The log likelihood:
minusLogLike <- function(df, data) -sum(dchisq(data, df, log=TRUE))
Notice the use of log=TRUE
. A little example of estimating by MLE follows:
dat <- rchisq(100,5)
optim(2, minusLogLike, lower=1, upper=10, method="Brent", data=dat)
Upvotes: 1