更多的MLE麻烦 [英] More MLE troubles
问题描述
我处于学生研究职位,并且是R的新手.我曾问过类似的问题(发布在这里:
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.
我仍在使用此函数尝试估算theta [i], 当前每个其他变量都已知的位置.
I am still using this function for trying to estimate theta[i], where each of the other variables is currently known.
下面是我的代码:
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)
我被抛出的错误是:
错误:缺少参数"theta",没有默认值
Error: argument "theta" is missing, with no default
如果错误很傻,我对此表示歉意,对R的了解却很少.
I apologize if the error is silly, I have little knowledge of R.
注释: 我正在使用(1,1,1,1,1,1,1)的向量作为theta的检验.它不是实际数据.剂量是6的向量,对应于血清的剂量水平.凋落物响应是一个矩阵,描述了每窝每剂对血清的响应. LitterSizes是一个矩阵,描述每窝每剂的一窝产仔数. LITTERS.M是与血清接触的垫料的初始数量. ProbabilityResponses是一个向量,它描述了给定小鼠受到血清影响的可能性.
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.
推荐答案
函数mle
不接受初始起始值的向量.要通过优化找到的每个参数都需要作为标量传递.将函数的声明更改为:
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
}
其中,etc
表示根据需要将其替换为theta_3 = 1
,theta_4 = 1
.函数mle
然后可以通过以下方式调用:
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)
这篇关于更多的MLE麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!