Gerrit Eichner

2008-May-23 11:53 UTC

### [R] Evaluation "conflict" in combination of replicate() and rexp()as variable inside a function

Dear userRs, "playing around" with combinations of replicate() and random number generating functions inside a self-defined "wrapper" function I encounterd a puzzling behaviour. The following are intentionally simple (and rather nonsense-) examples to isolate the relevant aspects. Please, note the seemingly "inconsistent" behaviour for the second call of rdistr() (with distr = rexp) for which I have not found any explanation (yet):> rdistr <- function( distr, ...) replicate( 1, distr( n = 1, ...))> rdistr( distr = rnorm)[1] -0.8889223> rdistr( distr = rexp)[1] NaN Warning message: In distr(n = 1, ...) : NAs produced> rdistr( distr = runif)[1] 0.8444856 Trying to discover the reason for this observation, I looked into the code of replicate() and realized that this seems to be an issue of evaluation schemes (and maybe also of variable scoping), but I didn''t delve deeper into it, yet. Can anybody more competent enlight me or give me a hint where to search, please? Thank you! Best regards -- Gerrit PS:> sessionInfo()R version 2.6.2 (2008-02-08) x86_64-unknown-linux-gnu locale: LC_CTYPE=de_DE.UTF-8;LC_NUMERIC=C;LC_TIME=de_DE.UTF-8;LC_COLLATE=de_DE.UTF-8;LC_MONETARY=de_DE.UTF-8;LC_MESSAGES=de_DE.UTF-8;LC_PAPER=de_DE.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=de_DE.UTF-8;LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] rcompgen_0.1-17 --------------------------------------------------------------------- AR Dr. Gerrit Eichner Mathematical Institute gerrit.eichner at math.uni-giessen.de Justus-Liebig-University Giessen Tel: +49-(0)641-99-32104 Arndtstr. 2, 35392 Giessen, Germany Fax: +49-(0)641-99-32029 http://www.math.uni-giessen.de

Prof Brian Ripley

2008-May-24 16:02 UTC

### [R] Evaluation "conflict" in combination of replicate() and rexp()as variable inside a function

Don''t use replicate() like this. You have ''...'' in your *expression*, and it really should only be used in the body of a function. But if you want to understand why> debug(rexp) > rdistr <- function(distr, ...) replicate(1, distr(n = 1, ...)) > rdistr(rexp)debugging in: distr(n = 1, ...) debug: .Internal(rexp(n, 1/rate)) Browse[1]> match.call() distr(n = 1, rate = ..1) Browse[1]> rate [1] 0 The point being that in the context that ... is evaluated, it does not refer to your function arguments. The behaviour is completely consistent: you called rnorm(n=1,0), rexp(n=1, 0) and runif(n=1, 0). replicate() is conveneint at the top level, but in programming you should manage replication yourself without the non-standard evaluation that use of eval.parent() (inside replicate()) implies. However, if you must rdistr <- function(distr, ...) { d <- function() distr(n=1, ...) replicate(1, d()) } will work. On Fri, 23 May 2008, Gerrit Eichner wrote:> Dear userRs, > > "playing around" with combinations of replicate() and random number > generating functions inside a self-defined "wrapper" function I encounterd a > puzzling behaviour. > > The following are intentionally simple (and rather nonsense-) examples to > isolate the relevant aspects. Please, note the seemingly "inconsistent" > behaviour for the second call of rdistr() (with distr = rexp) for which I > have not found any explanation (yet): > >> rdistr <- function( distr, ...) replicate( 1, distr( n = 1, ...)) > >> rdistr( distr = rnorm) > [1] -0.8889223 > >> rdistr( distr = rexp) > [1] NaN > Warning message: > In distr(n = 1, ...) : NAs produced > >> rdistr( distr = runif) > [1] 0.8444856 > > > Trying to discover the reason for this observation, I looked into the code of > replicate() and realized that this seems to be an issue of evaluation schemes > (and maybe also of variable scoping), but I didn''t delve deeper into it, yet. > Can anybody more competent enlight me or give me a hint where to search, > please? Thank you! > > Best regards -- Gerrit > > PS: > >> sessionInfo() > R version 2.6.2 (2008-02-08) > x86_64-unknown-linux-gnu > > locale: > LC_CTYPE=de_DE.UTF-8;LC_NUMERIC=C;LC_TIME=de_DE.UTF-8;LC_COLLATE=de_DE.UTF-8;LC_MONETARY=de_DE.UTF-8;LC_MESSAGES=de_DE.UTF-8;LC_PAPER=de_DE.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=de_DE.UTF-8;LC_IDENTIFICATION=C > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > loaded via a namespace (and not attached): > [1] rcompgen_0.1-17 > > > --------------------------------------------------------------------- > AR Dr. Gerrit Eichner Mathematical Institute > gerrit.eichner at math.uni-giessen.de Justus-Liebig-University Giessen > Tel: +49-(0)641-99-32104 Arndtstr. 2, 35392 Giessen, Germany > Fax: +49-(0)641-99-32029 http://www.math.uni-giessen.de > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595

### Maybe Matching Threads

- Matrix package problem: dsyMatrix %*% vector gives error
- effects & lme4: error since original data frame notfoundWASeffects: error when original data frame is missing
- Problem with regression line
- effects & lme4: error since original data frame not found WASeffects: error when original data frame is missing
- effects: error when original data frame is missing