Hi everyone, I saw this problem dealt with here: http://markmail.org/search/list:r-project?q=integrate#query:list%3Ar-project%20integrate+page:1+mid:qczmyzr676pgmaaw+state:results but no one answered that request that I can tell. I'm having the same problem. I'm having problems passing arguments to functions that I'd like to integrate. I could swear this worked in the past, but I can't get it to work now. I'm running R on Windows. Here's my toy code to reproduce the problem: R.Version()$version.string # [1] "R version 2.8.1 (2008-12-22)" f=function(x,const) 2^x+const f2=Vectorize(f,c("x")) testval=2 f2(1:5,testval) # [1] 4 6 10 18 34 # The vectorized function seems to work right. integrate(f2,.5,1,c=2) # Works. Returns # 1.845111 with absolute error < 2.0e-14 integrate(f=f2,.5,1,const=testval) # Doesn't work. Returns # Error in eval(expr, envir, enclos) : # ..1 used in an incorrect context, no ... to look in I understand that I don't have to Vectorize this function for it to work, but the more complicated functions I'm using require Vectorization. Why can't I pass the argument "const" as a variable, instead of a value? I don't understand this error. Thanks in advance, Richard Morey -- Richard D. Morey Assistant Professor Psychometrics and Statistics Rijksuniversiteit Groningen / University of Groningen
On 09/04/2009 8:07 AM, Richard Morey wrote:> Hi everyone, > > I saw this problem dealt with here: > http://markmail.org/search/list:r-project?q=integrate#query:list%3Ar-project%20integrate+page:1+mid:qczmyzr676pgmaaw+state:results > > but no one answered that request that I can tell. I'm having the same > problem. I'm having problems passing arguments to functions that I'd > like to integrate. I could swear this worked in the past, but I can't > get it to work now. I'm running R on Windows. > > Here's my toy code to reproduce the problem: > > > R.Version()$version.string > # [1] "R version 2.8.1 (2008-12-22)" > > f=function(x,const) > 2^x+const > > f2=Vectorize(f,c("x")) > > testval=2 > > f2(1:5,testval) > # [1] 4 6 10 18 34 > # The vectorized function seems to work right. > > integrate(f2,.5,1,c=2) > # Works. Returns > # 1.845111 with absolute error < 2.0e-14 > > integrate(f=f2,.5,1,const=testval) > # Doesn't work. Returns > # Error in eval(expr, envir, enclos) : > # ..1 used in an incorrect context, no ... to look in > > I understand that I don't have to Vectorize this function for it to > work, but the more complicated functions I'm using require Vectorization. > > Why can't I pass the argument "const" as a variable, instead of a value? > I don't understand this error.I'm not sure what's happening exactly; both Vectorize and integrate do tricky things with evaluation frames, and it looks as though they are not interacting well. As a workaround, I'd suggest using a single argument function f instead: > makef <- function(const) { + return(function(x) 2^x + const) + } makef(testval) will capture the value of testval, so we don't need to pass it, and then things work: > f <- makef(testval) > f2 <- Vectorize(f) > integrate(f=f2, 0.5, 1) 1.845111 with absolute error < 2.0e-14 Duncan Murdoch
Richard, Your function f() is already vectorized, and it works well (see below). Therefore, you don't need to create f2(). f = function(x, const) 2^x + const testval = 2> integrate(f, 0.5, 1, const=testval)1.845111 with absolute error < 2.0e-14 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: Richard Morey <R.D.Morey at rug.nl> Date: Thursday, April 9, 2009 8:10 am Subject: [R] problems with integrate ... arguments To: r-help at r-project.org> Hi everyone, > > I saw this problem dealt with here: > > > but no one answered that request that I can tell. I'm having the same > problem. I'm having problems passing arguments to functions that I'd > like to integrate. I could swear this worked in the past, but I can't > get it to work now. I'm running R on Windows. > > Here's my toy code to reproduce the problem: > > > R.Version()$version.string > # [1] "R version 2.8.1 (2008-12-22)" > > f=function(x,const) > 2^x+const > > f2=Vectorize(f,c("x")) > > testval=2 > > f2(1:5,testval) > # [1] 4 6 10 18 34 > # The vectorized function seems to work right. > > integrate(f2,.5,1,c=2) > # Works. Returns > # 1.845111 with absolute error < 2.0e-14 > > integrate(f=f2,.5,1,const=testval) > # Doesn't work. Returns > # Error in eval(expr, envir, enclos) : > # ..1 used in an incorrect context, no ... to look in > > I understand that I don't have to Vectorize this function for it to > work, but the more complicated functions I'm using require Vectorization. > > Why can't I pass the argument "const" as a variable, instead of a value? > I don't understand this error. > > Thanks in advance, > Richard Morey > > -- > Richard D. Morey > Assistant Professor > Psychometrics and Statistics > Rijksuniversiteit Groningen / University of Groningen > > ______________________________________________ > R-help at r-project.org mailing list > > PLEASE do read the posting guide > and provide commented, minimal, self-contained, reproducible code.
Richard, I didn't read the last part of your email that you "do" want to Vectorize your function. Sorry about that. Here is a solution using "sapply" to vectorize your function. f <- function(x, const) 2^x + const f2 <- function(x, ...) sapply(x, function(x) f(x, ...) )> integrate(f2, 0.5, 1, const=testval)1.845111 with absolute error < 2.0e-14 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: Richard Morey <R.D.Morey at rug.nl> Date: Thursday, April 9, 2009 8:10 am Subject: [R] problems with integrate ... arguments To: r-help at r-project.org> Hi everyone, > > I saw this problem dealt with here: > > > but no one answered that request that I can tell. I'm having the same > problem. I'm having problems passing arguments to functions that I'd > like to integrate. I could swear this worked in the past, but I can't > get it to work now. I'm running R on Windows. > > Here's my toy code to reproduce the problem: > > > R.Version()$version.string > # [1] "R version 2.8.1 (2008-12-22)" > > f=function(x,const) > 2^x+const > > f2=Vectorize(f,c("x")) > > testval=2 > > f2(1:5,testval) > # [1] 4 6 10 18 34 > # The vectorized function seems to work right. > > integrate(f2,.5,1,c=2) > # Works. Returns > # 1.845111 with absolute error < 2.0e-14 > > integrate(f=f2,.5,1,const=testval) > # Doesn't work. Returns > # Error in eval(expr, envir, enclos) : > # ..1 used in an incorrect context, no ... to look in > > I understand that I don't have to Vectorize this function for it to > work, but the more complicated functions I'm using require Vectorization. > > Why can't I pass the argument "const" as a variable, instead of a value? > I don't understand this error. > > Thanks in advance, > Richard Morey > > -- > Richard D. Morey > Assistant Professor > Psychometrics and Statistics > Rijksuniversiteit Groningen / University of Groningen > > ______________________________________________ > R-help at r-project.org mailing list > > PLEASE do read the posting guide > and provide commented, minimal, self-contained, reproducible code.
On 09/04/2009 8:07 AM, Richard Morey wrote:> Hi everyone, > > I saw this problem dealt with here: > http://markmail.org/search/list:r-project?q=integrate#query:list%3Ar-project%20integrate+page:1+mid:qczmyzr676pgmaaw+state:results > > but no one answered that request that I can tell. I'm having the same > problem. I'm having problems passing arguments to functions that I'd > like to integrate. I could swear this worked in the past, but I can't > get it to work now. I'm running R on Windows.This appears to be a bug in the handling of ..n arguments (which are supposed to extract component n from ...). I will fix the bug in R-devel, and if nothing goes wrong there, will backport the fix into R-patched. It's too late to put a change like this into 2.9.0, but it should be fixed in the next release. Duncan Murdoch> > Here's my toy code to reproduce the problem: > > > R.Version()$version.string > # [1] "R version 2.8.1 (2008-12-22)" > > f=function(x,const) > 2^x+const > > f2=Vectorize(f,c("x")) > > testval=2 > > f2(1:5,testval) > # [1] 4 6 10 18 34 > # The vectorized function seems to work right. > > integrate(f2,.5,1,c=2) > # Works. Returns > # 1.845111 with absolute error < 2.0e-14 > > integrate(f=f2,.5,1,const=testval) > # Doesn't work. Returns > # Error in eval(expr, envir, enclos) : > # ..1 used in an incorrect context, no ... to look in > > I understand that I don't have to Vectorize this function for it to > work, but the more complicated functions I'm using require Vectorization. > > Why can't I pass the argument "const" as a variable, instead of a value? > I don't understand this error. > > Thanks in advance, > Richard Morey >