简体   繁体   English

在R中实现一个计算负对数可能性的函数

[英]Implementing a function in R that computes minus-log-likelihood

Homework. 家庭作业。

I am new to R and statistics. 我是R和统计新手。 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. 我有一个问题,是应该实现一个以自由度(“ df”)和数据集为参数并返回减对数似然的用户定义函数。 It is assmued that the data is chi-squared distrbuted with "df" degrees of freedom. 可以肯定的是,数据以卡方分布,自由度为“ df”。

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) 我只会将此函数应用于相同的数据集,因此我的函数可以具有以下签名: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 . 注意使用log=TRUE A little example of estimating by MLE follows: 通过MLE估算的一个小例子如下:

dat <- rchisq(100,5)
optim(2, minusLogLike, lower=1, upper=10, method="Brent", data=dat)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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