如何计算R中的二重积分
这是我的r代码,用于计算每种情况下的beta值,非常简单如何计算R中的二重积分,r,regression,integration,R,Regression,Integration,这是我的r代码,用于计算每种情况下的beta值,非常简单 data =data.frame( "t" = seq(0, 1, 0.001) ) B3t <- function(t){ t**3 - 1.6*t**2 +0.76*t+1 } B2t <- function(t){ ifelse(t >= 0 & t < 0.342, ((t-0.5)^2-0.025), ifelse( data$t >
data =data.frame(
"t" = seq(0, 1, 0.001)
)
B3t <- function(t){
t**3 - 1.6*t**2 +0.76*t+1
}
B2t <- function(t){
ifelse(t >= 0 & t < 0.342,
((t-0.5)^2-0.025),
ifelse( data$t >= 0.342 & data$t <= 0.658,
0,
ifelse(t > 0.658 & t <= 1,
(-(t-0.5)^2+0.025),
0
)))
}
B1t <- function(t){
0
}
X1t <- function(t){
a0 = rnorm(1)
a1 = rnorm(1)
a2 = rnorm(1)
a3 = rnorm(1)
return(a0 + a1*t + a2*(t^2) + a3*(t^3))
}
X2t <- function(t){
a0 = rnorm(1)
a1 = rnorm(1)
a2 = rnorm(1)
a3 = rnorm(1)
a4 = rnorm(1)
return(a0 + a1 * sin(2*pi*t) + a2 * cos(2*pi*t) + a3 * sin(4*pi*t) + a4 * cos(4*pi*t))
}
data=data.frame(
“t”=序号(0,1,0.001)
)
B3t=0.342&data$t0.658&t以下是我将如何做到这一点(对于beta和X的一个版本)。请注意,二重积分只是两个相互嵌套的单积分。参数n
定义了我用于估计积分期望值的随机样本数
beta <- function(t){
return(t*t*t-1.6*t*t+0.76*t+1)
}
myX <- function(a,t){
pt <- c(1,t,t*t,t*t*t)
return(sum(a*pt))
}
## computes the expectation by averaging over n samples
myE <- function(n,s,t){
samp <- sapply(seq(n),function(x){
a <- rnorm(4)
myX(a,s)*myX(a,t)})
return(mean(samp,na.rm=T))
}
## funtion inside the first integral
myIntegrand1 <- function(s,t,n){
return(beta(s)*myE(n,s,t))
}
## function inside the second integral
myIntegrand2 <- function(t,n){
v <- integrate(myIntegrand1,0,1,t=t,n=n)
return(beta(t)*v$value)
}
## computes sigma
mySig <- function(n){
v <- integrate(myIntegrand2,0,1,n=n)
return( 0.25*v$value)
}
## tests various values of n (number of samples drawn to compute the expectation)
sapply(seq(3),function(x)
c("100"=mySig(100),"1000"=mySig(1000),"10000"=mySig(10000)))
## output shows you the level of precision you may expect:
## [,1] [,2] [,3]
## 100 48.61876 47.85445 58.2094
## 1000 52.95681 50.61860 50.61702
## 10000 54.88292 53.02073 54.48635
beta您可以使用函数rnorm()
生成标准正态分布。此外,integral
函数与函数和限制一起工作。您不需要计算函数的值,只需定义它:B3t@jenesaisquoi我按照指示创建了函数。不知道下一步该做什么。@Rohit知道双积分函数中的s
是什么吗?是否与t
相同?对于双重集成,您可以在网站上查看该主题的其他问题,如: