If this is posted elsewhere I cannot find it. I need to perform multiple integration where some of the variables are in the bounds of the other variables. I was trying to use R2Cuba function but cannot set the upper and lower bounds. My code so far is : int <- function(y){ u2 = y[1] z2 = y[2] u1 =y[3] z1 = y[4] ff <- u1*(z1-u1)*u2*(z2-u2)*exp(-0.027*(12-z2)) return(ff) } cuhre(4,1,int,rel.tol=1e-3,lower=c(y[4],y[4],4,4),upper=c(12,y[4],12,y[2]),abs.tol1e-12,flags= list(verbose=2, final=0)) I know that code is wrong but it shows that variables are in both the upper and lower bounds. Thanks. Dustin Long UNC-Chapel Hill -- View this message in context: http://r.789695.n4.nabble.com/Integration-with-variable-bounds-tp3413606p3413606.html Sent from the R help mailing list archive at Nabble.com.
One useful trick is to use the indicator function in your integrand to define regions where the integrand is non-zero. int <- function(y){ u2 = y[1] z2 = y[2] u1 =y[3] z1 = y[4] reg.nonzero <- (u2 > z1 & u2 <= 12) & (z2 > z1 & z2 <= 12) & (u1 > 4 & u1 <= z2) & (u2 > 4 & u2 <= z2) ff <- ifelse (reg.nonzero, u1*(z1-u1)*u2*(z2-u2)*exp(-0.027*(12-z2)), 0) return(ff) } Ravi. ____________________________________________________________________ Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvaradhan at jhmi.edu ----- Original Message ----- From: Dmlong21 <dmlong at bios.unc.edu> Date: Tuesday, March 29, 2011 3:05 am Subject: [R] Integration with variable bounds To: r-help at r-project.org> If this is posted elsewhere I cannot find it. I need to perform multiple > integration where some of the variables are in the bounds of the other > variables. I was trying to use R2Cuba function but cannot set the > upper and > lower bounds. My code so far is : > > int <- function(y){ > u2 = y[1] > z2 = y[2] > u1 =y[3] > z1 = y[4] > > ff <- u1*(z1-u1)*u2*(z2-u2)*exp(-0.027*(12-z2)) > return(ff) > } > > > cuhre(4,1,int,rel.tol=1e-3,lower=c(y[4],y[4],4,4),upper=c(12,y[4],12,y[2]),abs.tol> 1e-12,flags= list(verbose=2, final=0)) > > > I know that code is wrong but it shows that variables are in both the > upper > and lower bounds. > Thanks. > > Dustin Long > UNC-Chapel Hill > > -- > View this message in context: > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > > PLEASE do read the posting guide > and provide commented, minimal, self-contained, reproducible code.
Thanks for the tip but all I get is 0 for the integral. Any other suggestions? int <- function(y){ u2 = y[1] z2 = y[2] u1 =y[3] z1 = y[4] reg.nonzero <- (u2 > z1 & u2 < z2) & (z2 > z1 & z2 < 12) & (u1 > 4 & u1 < z1) & (z1 > 4 & z1 < 12) ff <- ifelse (reg.nonzero, u1*(z1-u1)*u2*(z2-u2)*exp(-0.027*(12-z2)), 0) return(ff) } cuhre(4,1,int) -- View this message in context: http://r.789695.n4.nabble.com/Integration-with-variable-bounds-tp3413606p3415639.html Sent from the R help mailing list archive at Nabble.com.
You get 0 because you did not specify lower and upper bounds that define the hyper-rectangle; therefore, the default is used which is (0,1)^4. Specify the proper lower and upper bounds. Ravi. ____________________________________________________________________ Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvaradhan at jhmi.edu ----- Original Message ----- From: Dmlong21 <dmlong at bios.unc.edu> Date: Tuesday, March 29, 2011 3:11 pm Subject: Re: [R] Integration with variable bounds To: r-help at r-project.org> Thanks for the tip but all I get is 0 for the integral. Any other > suggestions? > > int <- function(y){ > u2 = y[1] > z2 = y[2] > u1 =y[3] > z1 = y[4] > > reg.nonzero <- (u2 > z1 & u2 < z2) & (z2 > z1 & z2 < 12) & (u1 > 4 & > u1 < > z1) & (z1 > 4 & z1 < 12) > > > ff <- ifelse (reg.nonzero, u1*(z1-u1)*u2*(z2-u2)*exp(-0.027*(12-z2)), > 0) > > > return(ff) > > > } > > > cuhre(4,1,int) > > -- > View this message in context: > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > > PLEASE do read the posting guide > and provide commented, minimal, self-contained, reproducible code.
Try this: require(R2Cuba) ans <- divonne(4,1,int, lower=c(4,4,4,4), upper=c(12,12,12,12), max.eval=1e06, rel.tol=1.e-04)> ansintegral: 15663.04 (+-1.6) nregions: 182; number of evaluations: 910834; probability: 1.478672e-09>I am not sure if this answer is correct. Ravi. ____________________________________________________________________ Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvaradhan at jhmi.edu ----- Original Message ----- From: Ravi Varadhan <rvaradhan at jhmi.edu> Date: Tuesday, March 29, 2011 9:13 pm Subject: Re: [R] Integration with variable bounds To: Dmlong21 <dmlong at bios.unc.edu> Cc: r-help at r-project.org> You get 0 because you did not specify lower and upper bounds that > define the hyper-rectangle; therefore, the default is used which is (0,1)^4. > > Specify the proper lower and upper bounds. > > Ravi. > > ____________________________________________________________________ > > Ravi Varadhan, Ph.D. > Assistant Professor, > Division of Geriatric Medicine and Gerontology > School of Medicine > Johns Hopkins University > > Ph. (410) 502-2619 > email: rvaradhan at jhmi.edu > > > ----- Original Message ----- > From: Dmlong21 <dmlong at bios.unc.edu> > Date: Tuesday, March 29, 2011 3:11 pm > Subject: Re: [R] Integration with variable bounds > To: r-help at r-project.org > > > > Thanks for the tip but all I get is 0 for the integral. Any other > > suggestions? > > > > int <- function(y){ > > u2 = y[1] > > z2 = y[2] > > u1 =y[3] > > z1 = y[4] > > > > reg.nonzero <- (u2 > z1 & u2 < z2) & (z2 > z1 & z2 < 12) & (u1 > 4 & > > > u1 < > > z1) & (z1 > 4 & z1 < 12) > > > > > > ff <- ifelse (reg.nonzero, > u1*(z1-u1)*u2*(z2-u2)*exp(-0.027*(12-z2)), > > 0) > > > > > > return(ff) > > > > > > } > > > > > > cuhre(4,1,int) > > > > -- > > View this message in context: > > Sent from the R help mailing list archive at Nabble.com. > > > > ______________________________________________ > > R-help at r-project.org mailing list > > > > PLEASE do read the posting guide > > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at r-project.org mailing list > > PLEASE do read the posting guide > and provide commented, minimal, self-contained, reproducible code.