简体   繁体   中英

Implementing a function in R that computes minus-log-likelihood

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM