计算函数的积分,该功能是R中其他两个积分函数的乘法
我有以下数据。数据有2组(g),每个组有2个个人(N_I)。
rm(list=ls()); set.seed(1234)
G=2 ; # Suppose 2 groups
n_i<-2 # There are two individuals per group
nTot<-4 # In total we have 4 individuals
z_ij<-runif(nTot, 0, 5)
T_ij<- runif(nTot, 5, 10)
Data<-round(data.frame(id=rep(1:nTot), group=rep(1:G, rep(2,G) ),z_ij, T_ij),1)
Data
id group z_ij T_ij
1 1 1 0.6 9.3
2 2 1 3.1 8.2
3 3 2 3.0 5.0
4 4 2 3.1 6.2
对于每个单个(行),我都有以下函数f(x,y)= xy \ exp(x+y+z [id])
其中集成为x < /代码> IS
tij [id]
。
我想要R中的代码,可以计算以下积分
组1:
组2组2
我的尝试会产生错误Integrate中的错误
error
aS.numeric(split(split) (sapply(1:ntot,:函数评估给出了错误的lengt
的结果
GroupInt<-rep(NA,G)
for(i in 1:G) ## s<-3
{
GroupInt[i]<- integrate( function(y)
as.numeric( sapply( split( sapply( 1:nTot, function(id) integrate(function(x)
x*y*exp( x+y+Data$z_ij[id] ), 0, Data$T_ij[id] )$value ), Data$group),prod) )[i] ,
0 , 5 )$value
}
I have the following data. The data has 2 groups (G) each with 2 individuals (n_i).
rm(list=ls()); set.seed(1234)
G=2 ; # Suppose 2 groups
n_i<-2 # There are two individuals per group
nTot<-4 # In total we have 4 individuals
z_ij<-runif(nTot, 0, 5)
T_ij<- runif(nTot, 5, 10)
Data<-round(data.frame(id=rep(1:nTot), group=rep(1:G, rep(2,G) ),z_ij, T_ij),1)
Data
id group z_ij T_ij
1 1 1 0.6 9.3
2 2 1 3.1 8.2
3 3 2 3.0 5.0
4 4 2 3.1 6.2
For every individual(row), I have the following function f(x,y)= xy\exp(x+y+z[id])
where upper limit in integrating for x
is Tij[id]
.
I would like a code in R that can compute the following integral
Group 1:
Group 2
My attempt which yields an error Error in integrate(function(y) as.numeric(sapply(split(sapply(1:nTot, : evaluation of function gave a result of wrong lengt
GroupInt<-rep(NA,G)
for(i in 1:G) ## s<-3
{
GroupInt[i]<- integrate( function(y)
as.numeric( sapply( split( sapply( 1:nTot, function(id) integrate(function(x)
x*y*exp( x+y+Data$z_ij[id] ), 0, Data$T_ij[id] )$value ), Data$group),prod) )[i] ,
0 , 5 )$value
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)