使用具有多个参数和索引的lapply/sapply减少函数内的嵌套循环(代码片段)

author author     2022-12-27     305

关键词:

我有两个观察向量,我想要计算哪些参数最适合这些观察。因此,我已经定义了似然函数。我正在努力从嵌套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”缺失,没有默认值

看起来这些参数并没有在我的代码中通过我的第一个函数。任何有关如何确保我的参数通过所有功能的提示将不胜感激!

答案

我设法解决了我的问题。

bud <- runif(10, 1.1,1.55)
deltakere <- round(runif(10,5,9))
r=0.5

foo_inner <- function(i, theta1, theta2, lambda)
    function(N)
    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)
    

foo_outer <- function(theta1, theta2, lambda)
            function(i)
            listeObs <- sapply(deltakere[i]:20000, foo_inner(i, theta1, theta2, lambda))
            return(sum(listeObs))
            

eqThree <- function(theta1, theta2, lambda)
    secondPart <- pbsapply(1:length(bud), foo_outer(theta1, theta2, lambda))

    LL <- -sum(log(secondPart))
    return(LL)


result_mle <- mle(minuslogl = eqThree, start=list(theta1 = 1,
                                                  theta2 = 2,
                                                  lambda = 7),
                  method="L-BFGS-B", lower=c(0.1,0.1,5),
                  nobs = length(bud))

在 R 中使用带有多个参数的匿名函数

】在R中使用带有多个参数的匿名函数【英文标题】:usinganonymousfunctionsinRwithmultiplearguments【发布时间】:2012-10-1517:27:41【问题描述】:我正在尝试在数据框中生成新变量,这些变量以数据框中的两个(或更多)其他变量为条件。... 查看详情

r语言apply函数详解及实战(lapply,sapply,vapply,tapply,mapply)

R语言apply函数详解及实战(lapply,sapply,vapply,tapply,mapply)目录R语言apply函数详解及实战(lapply,sapply,vapply,tapply,mapply)#dataframe仿真数据 查看详情

渲染具有多个索引的网格

...一个立方体。但是每条顶点数据都有自己的索引。我可以使用OpenGL/Direct3D渲染这个网格数据吗?【问题讨论】:【参考方案1】:在最一般的意义上,没有。OpenGL和Direc 查看详情

渲染具有多个索引的网格

...一个立方体。但是每条顶点数据都有自己的索引。我可以使用OpenGL/Direct3D渲染这个网格数据吗?【问题讨论】:【参考方案1】:在最一般的意义上,没有。OpenGL和Direc 查看详情

如何声明和定义具有多个可变长度数组的结构?

】如何声明和定义具有多个可变长度数组的结构?【英文标题】:howtodeclareanddefineastructurewithmultiplevariablelengtharrays?【发布时间】:2021-11-0114:40:03【问题描述】:问题描述:有3个宏(配置参数)被用作多个结构中数组的索引。现... 查看详情

如何在 Swift 中使用 Alamofire 与 Multipart 一起使用具有不同键和多种参数的多个图像

】如何在Swift中使用Alamofire与Multipart一起使用具有不同键和多种参数的多个图像【英文标题】:HowtoworkwithMultipartusingAlamofireinSwiftwithmultipleimageswithdifferentkeysandparameterswithmultiplekinds【发布时间】:2019-09-0311:10:34【问题描述】:如果... 查看详情

如何正确索引在具有多个连接的查询中使用的表

】如何正确索引在具有多个连接的查询中使用的表【英文标题】:Howtoproperlyindextablesusedinaquerywithmultiplejoins【发布时间】:2011-08-3017:02:57【问题描述】:我正在尝试确定为下面的查询编制索引的最佳方式。到目前为止,我已经在... 查看详情

致命错误:使用具有多个部分的 UITableView 时索引超出范围

】致命错误:使用具有多个部分的UITableView时索引超出范围【英文标题】:fatalerror:IndexoutofrangewhenusingUITableViewwithmultiplesections【发布时间】:2016-09-1823:38:47【问题描述】:我正在尝试使用UITableView从数组中删除项目,但是UITableView... 查看详情

javascript:多个持久索引变量(代码片段)

我是编程世界的新手,在使用JS制作照片库方面遇到了一些麻烦。因此,缩略图调用具有适当图像的模态,该图像通过索引作为参数传递。我使用了一些JQuery只是为了在没有循环的情况下在所有缩略图上附加处理程序。第一个Moda... 查看详情

我们如何使用具有多个参数的laravelurl?(代码片段)

目前,我正在尝试通过这种方式使用具有多个参数的URL。例如,我将使用以下模式在URL中传递员工ID,组ID和员工的当前状态。mydomain.com/en/employee/benefit-status/1-1-enroll我定义的路线如下:Route::get('lang/employee/benefit-status/employeeID_Gou... 查看详情

如何创建具有一个索引键列和多个值列的字典

】如何创建具有一个索引键列和多个值列的字典【英文标题】:howtocreateadictionarywithoneindexkeycolumnandmultiplevaluecolumns【发布时间】:2019-10-3000:14:21【问题描述】:我有一个包含3列A、B、C的数据框df。我希望A列作为索引和键,B和C列... 查看详情

合并具有非唯一索引的多个数据帧

】合并具有非唯一索引的多个数据帧【英文标题】:Mergingmultipledataframeswithnonuniqueindexes【发布时间】:2015-06-2118:19:58【问题描述】:给定两个具有非唯一索引和多维列的DF:ars:arsenalarsenalarsenalarsenalNaNB3SKBXBY2015-04-15NaNNaNNaN26.02015-... 查看详情

Pandas:具有多个索引的滚动总和(即面板数据)

】Pandas:具有多个索引的滚动总和(即面板数据)【英文标题】:Pandas:Rollingsumwithmultipleindexes(i.e.paneldata)【发布时间】:2016-12-2003:54:27【问题描述】:我有一个具有多个索引的数据框,并希望创建一些数据的滚动总和,但针对索... 查看详情

导致具有多个参数的过滤列表的自定义操作?

...义操作按钮指向过滤后的奏鸣曲列表(课程),但我需要使用两个参数(公司和外部)。以前我一直在使用这种方法:SonataAdminActionButtontoPreFilteredList,但我无法 查看详情

如何在多个实例的python上找到特定字符的索引?(代码片段)

...三个参数之后找到第二个参数的实例。我在想我会在这里使用切片或范围,但我不知道用户输入的第一 查看详情

返回具有多个输入参数的函数的值以在同一类中具有多个参数的另一个函数中使用?

...数的函数的值以在同一类中具有多个参数的另一个函数中使用?【英文标题】:ReturningthevalueofafunctionwithmultipleinputparameterstouseinanotherfunctionwithmultipleparametersinsidethesameClass?【发布时间】:2018-10-2823:31:00【问题描述】:现在我知道... 查看详情

索引匹配具有多个条件的唯一值?

】索引匹配具有多个条件的唯一值?【英文标题】:Indexmatchuniquevalueswithmultiplecriteria?【发布时间】:2016-07-1919:49:06【问题描述】:我在一张纸上有以下用户列表,以及他们的年龄和运动兴趣:A18FootballB19RugbyC18FootballD50DanceD21RugbyA... 查看详情

具有多个条件的布尔索引[重复]

】具有多个条件的布尔索引[重复]【英文标题】:BooleanIndexingwithmultipleconditions[duplicate]【发布时间】:2016-04-0412:57:09【问题描述】:我有一个PandasDF,我需要在其中过滤一些包含特征“a”和特征“b”的值==0的行。为了检查这些值... 查看详情