On Sun, Nov 7, 2021 at 6:05 AM Rolf Turner <r.turner at auckland.ac.nz> wrote:> > > I have two functions which appear to differ only in their environments. > They look like: > > > d1 > > function (x, mean = 0, sd = 1, log = FALSE) > > (((x - mean)/sd)^2 - 1) * if (log) 1 else dnorm(x, mean, sd)/sd > > <environment: namespace:stats> > > and > > > d2 > > function (x, mean = 0, sd = 1, log = FALSE) > > (((x - mean)/sd)^2 - 1) * if (log) 1 else dnorm(x, mean, sd)/sd > > Typing "environment(d1)" gives > > > <environment: namespace:stats> > > and typing "environment(d2)" gives > > > <environment: R_GlobalEnv> > > The d2() function however gives an incorrect result: > > > d1(1,0,3,TRUE) > > [1] -0.2962963 > > d2(1,0,3,TRUE) > > [1] -0.8888889It can't be as simple as that. I get the same result (as your d2) with the following: d <- function (x, mean = 0, sd = 1, log = FALSE) { (((x - mean)/sd)^2 - 1) * if (log) 1 else dnorm(x, mean, sd) / sd } d(1, 0, 3, TRUE) environment(d) environment(d) <- as.environment("package:stats") d(1, 0, 3, TRUE)> In d2() the result of the if() statement does not get divided > by the final "sd" whereas in d1() it does (which is the desired/correct > result). > > Of course the code is ridiculously kludgy (it was produced by "symbolic > differentiation"). That's not the point. I'm just curious (idly?) as > to *why* the association of the namespace:stats environment with d1() > causes it to "do the right thing".This sounds like a difference in precedence. The expression if (log) 1 else dnorm(x, mean, sd) / sd is apparently being interpreted differently as d1: (if (log) 1 else dnorm(x, mean, sd)) / sd d2: if (log) 1 else (dnorm(x, mean, sd)) / sd) It's unclear how environments could affect this, so it would be very helpful to have a reproducible example. Best, -Deepayan> Can anyone give me any insight? Ta. > > cheers, > > Rolf Turner > > -- > Honorary Research Fellow > Department of Statistics > University of Auckland > Phone: +64-9-373-7599 ext. 88276 > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
library(Deriv) d1 <- Deriv(dnorm,"sd") source("d2.txt") # d2.txt is attached d1(1,0,3,TRUE) # [1] -0.2962963 d2(1,0,3,TRUE) # [1] -0.8888889 cheers, Rolf P.S.:> sessionInfo()R version 4.1.1 (2021-08-10) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 20.04.3 LTS Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/atlas/libblas.so.3.10.3 LAPACK: /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3 locale: [1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_NZ.UTF-8 LC_COLLATE=en_GB.UTF-8 [5] LC_MONETARY=en_NZ.UTF-8 LC_MESSAGES=en_GB.UTF-8 [7] LC_PAPER=en_NZ.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_NZ.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] Deriv_4.1.3 brev_0.0-7 loaded via a namespace (and not attached): [1] magrittr_1.5 usethis_2.0.1 devtools_2.4.2 pkgload_1.2.1 [5] colorspace_1.4-1 R6_2.4.1 rlang_0.4.11 fastmap_1.0.1 [9] tools_4.1.1 pkgbuild_1.2.0 sessioninfo_1.1.1 cli_2.5.0 [13] withr_2.4.2 ellipsis_0.3.2 remotes_2.4.0 rprojroot_1.3-2 [17] lifecycle_1.0.0 crayon_1.3.4 processx_3.5.2 purrr_0.3.4 [21] callr_3.7.0 fs_1.5.0 ps_1.6.0 testthat_3.0.3 [25] memoise_2.0.0 glue_1.4.0 cachem_1.0.5 compiler_4.1.1 [29] desc_1.3.0 backports_1.1.6 prettyunits_1.1.1 -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: d2.txt URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20211107/62c97da8/attachment.txt>
On Sun, 7 Nov 2021 09:02:36 +0530 Deepayan Sarkar <deepayan.sarkar at gmail.com> wrote:> This sounds like a difference in precedence. The expression > > if (log) 1 else dnorm(x, mean, sd) / sd > > is apparently being interpreted differently as > > d1: (if (log) 1 else dnorm(x, mean, sd)) / sd > d2: if (log) 1 else (dnorm(x, mean, sd)) / sd) > > It's unclear how environments could affect this, so it would be very > helpful to have a reproducible example.This seems to be caused by the deparser producing the same source text for different expressions: ( x <- expression(`/`(`*`(a, if (b) c else d), e)) ) # expression(a * if (b) c else d/e) ( y <- expression(a * if (b) c else d/e) ) # expression(a * if (b) c else d/e) all.equal(x, y) # [1] TRUE The expressions *seem* to be the same, but: as.list(x[[1]]) # [[1]] # `/` # # [[2]] # a * if (b) c else d # # [[3]] # e as.list(y[[1]]) # [[1]] # `*` # # [[2]] # a # # [[3]] # if (b) c else d/e Perhaps it could be possible to make the deparser output extra parentheses at the cost of slightly uglier output in cases when they are not needed. all.equal.language uses deparse(), so it will behave correctly when the deparse() output is fixed. In the original example, as.list(body(d1)) and as.list(body(d2)) should show different results, too. -- Best regards, Ivan
On 06/11/2021 11:32 p.m., Deepayan Sarkar wrote:> On Sun, Nov 7, 2021 at 6:05 AM Rolf Turner <r.turner at auckland.ac.nz> wrote: >> >> >> I have two functions which appear to differ only in their environments. >> They look like: >> >>> d1 >>> function (x, mean = 0, sd = 1, log = FALSE) >>> (((x - mean)/sd)^2 - 1) * if (log) 1 else dnorm(x, mean, sd)/sd >>> <environment: namespace:stats> >> >> and >> >>> d2 >>> function (x, mean = 0, sd = 1, log = FALSE) >>> (((x - mean)/sd)^2 - 1) * if (log) 1 else dnorm(x, mean, sd)/sd >> >> Typing "environment(d1)" gives >> >>> <environment: namespace:stats> >> >> and typing "environment(d2)" gives >> >>> <environment: R_GlobalEnv> >> >> The d2() function however gives an incorrect result: >> >>> d1(1,0,3,TRUE) >>> [1] -0.2962963 >>> d2(1,0,3,TRUE) >>> [1] -0.8888889 > > It can't be as simple as that. I get the same result (as your d2) with > the following: > > d <- function (x, mean = 0, sd = 1, log = FALSE) { > (((x - mean)/sd)^2 - 1) * if (log) 1 else dnorm(x, mean, sd) / sd > } > d(1, 0, 3, TRUE) > environment(d) > environment(d) <- as.environment("package:stats") > d(1, 0, 3, TRUE) > >> In d2() the result of the if() statement does not get divided >> by the final "sd" whereas in d1() it does (which is the desired/correct >> result). >> >> Of course the code is ridiculously kludgy (it was produced by "symbolic >> differentiation"). That's not the point. I'm just curious (idly?) as >> to *why* the association of the namespace:stats environment with d1() >> causes it to "do the right thing". > > This sounds like a difference in precedence. The expression > > if (log) 1 else dnorm(x, mean, sd) / sd > > is apparently being interpreted differently as > > d1: (if (log) 1 else dnorm(x, mean, sd)) / sd > d2: if (log) 1 else (dnorm(x, mean, sd)) / sd) > > It's unclear how environments could affect this, so it would be very > helpful to have a reproducible example. >Rolf said these were automatically produced functions. Those don't always deparse properly, because manipulating expressions can produce things that can never be produced by the parser. I'm not sure this happened in this case. You'd need to examine the parse trees of d1 and d2 to see. There's also a possibility that the srcref attached to them is lying, and we're not seeing the deparsed versions of the functions. Printing removeSource(d1) and removeSource(d2) should reveal that. Duncan Murdoch