【发布时间】:2019-07-19 08:12:04
【问题描述】:
我有两个观察向量,我想计算最适合这些观察的参数。因此,我定义了似然函数。我正在努力从嵌套的 for 循环到使用 sapply。我的目标是返回一个 (50x1) 观察“芽”的概率向量。
> bud
[1] 1.72 1.12 1.01 1.14 1.56 1.04 1.00 1.39 1.38
[10] 0.99 1.66 1.55 2.26 0.86 1.45 0.90 1.58 1.09
[19] 0.97 0.98 1.16 1.20 1.17 1.59 1.00 0.92 1.24
[28] 0.98 1.29 1.36 1.80 1.42 1.67 1.13 0.76 0.93
[37] 1.03 1.29 1.09 1.64 1.27 1.14 1.60 1.00 0.91
[46] 1.40 1.15 2.03 1.34 1.37
> deltakere
[1] 8 5 7 6 9 5 5 9 9 6 5 8 8 6 6 5 7 6 7 7 9 6 6 8
[25] 8 6 6 5 9 9 6 7 9 5 5 6 6 8 7 9 8 6 9 8 5 8 8 6
[49] 9 7
当前代码(虽然运行缓慢):
# first equation in my likelihood function
eqOne <- function(i,theta1, theta2, lambda){
vektorN = (1:100)
loopVerdi = 0
if(deltakere[i]>=2){
for (N in deltakere[i]:20000) {
density = ((N)*(N-1)*(pWEI2(bud[i], theta1, theta2))^(N-2) *
pWEI2(bud[i], theta1, theta2, lower.tail = FALSE) *
dWEI2(bud[i], theta1, theta2)) /
(1-(pWEI2(r, theta1, theta2))^N)
ngittN =
dbinom(deltakere[i], size = N,
prob = pWEI2(r, theta1, theta2, lower.tail = FALSE))
sN =
dpois(N, lambda)
loopVerdi = loopVerdi + (density * ngittN * sN)}
return(loopVerdi)}}
# here is the second equation in the likelihood estimate.
eqTwo <- function(theta1, theta2, lambda) {
firstPart <- 1:length(bud)
for (i in 1:length(bud)){
firstPart[i] <- eqOne(i,theta1, theta2, lambda)}
return(firstPart)}
Vector <- eqTwo(1,2,7)
> Vector
[1] 0.071126958 0.174198273 0.125381705 0.202950594
[5] 0.116409283 0.156245797 0.143071864 0.156875579
[9] 0.157670857 0.139149430 0.052085368 0.138248017
[13] 0.001884680 0.073326986 0.161989881 0.103352207
[17] 0.125124465 0.186201434 0.103156346 0.108636007
[21] 0.116075468 0.214339615 0.209921459 0.121433257
[25] 0.084230719 0.102485943 0.216127393 0.135710730
[29] 0.153834163 0.158552633 0.036673600 0.194744214
[33] 0.079498794 0.175511921 0.049722001 0.107646750
[37] 0.159465393 0.198467993 0.169062724 0.089375280
[41] 0.196173239 0.202950594 0.102923165 0.084230719
[45] 0.107521392 0.190136547 0.159243477 0.008274964
[49] 0.158457042 0.210361176
此代码生成一个 (50x1) 向量,该向量具有观察“芽”值的概率,这正是我想要的。
这是我从 for 循环到 sapply 方法的尝试
funDeltakere <- function(i, theta1, theta2, lambda){
function(N, theta1, theta2, lambda){
density = ((N) *(N-1)*(pWEI2(bud[i], theta1, theta2))^(N-2) *
pWEI2(bud[i], theta1, theta2, lower.tail = FALSE) *
dWEI2(bud[i], theta1, theta2)) /
(1-(pWEI2(r, theta1, theta2))^N)
ngittN =
dbinom(deltakere[i], size = N,
prob = pWEI2(r, theta1, theta2, lower.tail = FALSE))
sN = dpois(N, lambda)
return(density * ngittN * sN)}}
eqOne <- function(i,theta1, theta2, lambda){
if(deltakere[i]>=2){
loopVerdi <- sapply(deltakere[i]:20000, funDeltakere(i, theta1, theta2, lambda))
return(loopVerdi)}}
eqTwo <- function(theta1, theta2, lambda) {
firstPart <- 1:length(bud)
for (i in 1:length(bud)){
firstPart[i] <- eqOne(i,theta1, theta2, lambda)
}
return(firstPart)
}
Vector <- eqTwo(1,2,7)
pWEI2(bud[i], theta1, theta2) 中的错误:参数“theta1”是 缺失,没有默认值
似乎参数并没有一直通过我的代码中的第一个函数。任何有关如何确保我的参数通过所有函数的提示都将不胜感激!
【问题讨论】:
-
仅出现代码损坏的问题并不是特别有用。您需要用自然语言指定问题,
-
如果您使这个问题完全可重现,我们将更容易为您提供帮助。查看这篇文章的指导方针:stackoverflow.com/questions/5963269/… 您也可以使用 reprex-package 来确保您的答案是可重现的:reprex.tidyverse.org
-
我试图让它可重现,并解释了我想要的代码。
标签: r function for-loop lapply