I am trying to use the adapt function in the package adapt. To make sure I am using it correctly, I am trying a toy example that should yield a result of 2/3. Suppose the function is f(x,y) = x*y^2 and I want to integrate over f as Int_0^1 Int_0^2 x*y^2 dxdy Where the limits of integration for y are 0 to 1 and the limits for x are 0 to 2. So, I tried ff <- function(xy) x*y^2 adapt(2, lo = c(0,0), up = c(1,2), fun = ff) And this produces> adapt(2, lo = c(0,0), up = c(1,2), fun = ff)value relerr minpts lenwrk ifail 2 7.38275e-08 165 73 0 Can anyone offer insight into my error? Harold> sessionInfo()R version 2.9.0 (2009-04-17) i386-pc-mingw32 locale: LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] adapt_1.0-4 loaded via a namespace (and not attached): [1] tools_2.9.0
Harold, Try this: ff <- function(x) x[1]*x[2]^2 adapt(2, lo = c(0,0), up = c(1,2), fun = ff) The answer should be 4/3, since the first variable is integrated from 0 to 1 and the second variable (second-degree) is integrated from 0 to 2. The answer is 1/3, if you do, adapt(2, lo = c(0,0), up = c(2,1), fun = 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: "Doran, Harold" <HDoran at air.org> Date: Thursday, October 8, 2009 1:14 pm Subject: [R] Adapt function To: r-help at r-project.org> I am trying to use the adapt function in the package adapt. To make sure > I am using it correctly, I am trying a toy example that should yield a > result of 2/3. > > Suppose the function is f(x,y) = x*y^2 and I want to integrate over f > as > > Int_0^1 Int_0^2 x*y^2 dxdy > > Where the limits of integration for y are 0 to 1 and the limits for x > are 0 to 2. So, I tried > > ff <- function(xy) x*y^2 > adapt(2, lo = c(0,0), up = c(1,2), fun = ff) > > And this produces > > > adapt(2, lo = c(0,0), up = c(1,2), fun = ff) > value relerr minpts lenwrk ifail > 2 7.38275e-08 165 73 0 > > Can anyone offer insight into my error? > > Harold > > > > > sessionInfo() > R version 2.9.0 (2009-04-17) > i386-pc-mingw32 > > locale: > LC_COLLATE=English_United States.1252;LC_CTYPE=English_United > States.1252;LC_MONETARY=English_United > States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252 > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > > other attached packages: > [1] adapt_1.0-4 > > loaded via a namespace (and not attached): > [1] tools_2.9.0 > > ______________________________________________ > R-help at r-project.org mailing list > > PLEASE do read the posting guide > and provide commented, minimal, self-contained, reproducible code.
Doran, Harold wrote:> > I am trying to use the adapt function in the package adapt. To make sure > I am using it correctly, I am trying a toy example that should yield a > result of 2/3. > > Suppose the function is f(x,y) = x*y^2 and I want to integrate over f as > > Int_0^1 Int_0^2 x*y^2 dxdy > > Where the limits of integration for y are 0 to 1 and the limits for x > are 0 to 2. So, I tried > > ff <- function(xy) x*y^2 > adapt(2, lo = c(0,0), up = c(1,2), fun = ff) > > And this produces > >> adapt(2, lo = c(0,0), up = c(1,2), fun = ff) > value relerr minpts lenwrk ifail > 2 7.38275e-08 165 73 0 > > Can anyone offer insight into my error? > > Harold >adapt() wants a function whose first argument is a vector of parameters (and you had your limits switched, I think).> ff <- function(p) p[1]*p[2]^2 > adapt(2,lo=c(0,0),up=c(2,1),fun=ff)value relerr minpts lenwrk ifail 0.6666667 7.142849e-08 165 73 0 -- View this message in context: http://www.nabble.com/Adapt-function-tp25808121p25808382.html Sent from the R help mailing list archive at Nabble.com.