Re: [R] Environmental oddity.
On Sun, 7 Nov 2021 13:11:10 -0500 Duncan Murdoch wrote: > I've submitted a bug report and patch: > > https://bugs.r-project.org/show_bug.cgi?id=18232 Thanks Duncan. It's good to know that the anomaly wasn't just a result of my doing something stupid. cheers, Rolf -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 __ R-help@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.
Re: [R] Environmental oddity.
I've submitted a bug report and patch: https://bugs.r-project.org/show_bug.cgi?id=18232 Duncan Murdoch On 07/11/2021 12:20 p.m., Duncan Murdoch wrote: Here's how to construct a similar deparsing error: # e1 and e2 are obviously different expressions e1 <- quote(5 * if (TRUE) 2 else 3/4) e2 <- quote(5 * (if (TRUE) 2 else 3)/4) # and they evaluate differently eval(e1) #> [1] 10 eval(e2) #> [1] 2.5 # We can make an equivalent version of e2 by messing around in it e3 <- e2 as.list(e3[[c(2,3)]]) #> [[1]] #> `(` #> #> [[2]] #> if (TRUE) 2 else 3 e3[[c(2,3)]] <- e3[[c(2,3,2)]] # Now e3 looks like e1 e1 #> 5 * if (TRUE) 2 else 3/4 e3 #> 5 * if (TRUE) 2 else 3/4 # But it doesn't evaluate that way eval(e1) #> [1] 10 eval(e3) #> [1] 2.5 Duncan Murdoch On 07/11/2021 6:27 a.m., Duncan Murdoch wrote: On 06/11/2021 11:32 p.m., Deepayan Sarkar wrote: On Sun, Nov 7, 2021 at 6:05 AM Rolf Turner 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 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 and typing "environment(d2)" gives The d2() function however gives an incorrect result: d1(1,0,3,TRUE) [1] -0.2962963 d2(1,0,3,TRUE) [1] -0.889 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 __ R-help@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.
Re: [R] Environmental oddity.
Here's how to construct a similar deparsing error: # e1 and e2 are obviously different expressions e1 <- quote(5 * if (TRUE) 2 else 3/4) e2 <- quote(5 * (if (TRUE) 2 else 3)/4) # and they evaluate differently eval(e1) #> [1] 10 eval(e2) #> [1] 2.5 # We can make an equivalent version of e2 by messing around in it e3 <- e2 as.list(e3[[c(2,3)]]) #> [[1]] #> `(` #> #> [[2]] #> if (TRUE) 2 else 3 e3[[c(2,3)]] <- e3[[c(2,3,2)]] # Now e3 looks like e1 e1 #> 5 * if (TRUE) 2 else 3/4 e3 #> 5 * if (TRUE) 2 else 3/4 # But it doesn't evaluate that way eval(e1) #> [1] 10 eval(e3) #> [1] 2.5 Duncan Murdoch On 07/11/2021 6:27 a.m., Duncan Murdoch wrote: On 06/11/2021 11:32 p.m., Deepayan Sarkar wrote: On Sun, Nov 7, 2021 at 6:05 AM Rolf Turner 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 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 and typing "environment(d2)" gives The d2() function however gives an incorrect result: d1(1,0,3,TRUE) [1] -0.2962963 d2(1,0,3,TRUE) [1] -0.889 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 __ R-help@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.
Re: [R] Environmental oddity.
On 06/11/2021 11:32 p.m., Deepayan Sarkar wrote: On Sun, Nov 7, 2021 at 6:05 AM Rolf Turner 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 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 and typing "environment(d2)" gives The d2() function however gives an incorrect result: d1(1,0,3,TRUE) [1] -0.2962963 d2(1,0,3,TRUE) [1] -0.889 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 __ R-help@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.
Re: [R] Environmental oddity --- reproducible example.
G'day Rolf, On Sun, 7 Nov 2021 19:33:40 +1300 Rolf Turner wrote: > 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.889 Fascinating: R> pryr::call_tree(body(d1)) R> pryr::call_tree(body(d2)) clearly show that the two functions have a different idea to what expression the final "/sd" is applied too (as an earlier poster suggested), but I have no idea why. Deriv() seems to return the correct function, but when it is displayed, the deparser(?) somehow omits a crucial pair of braces. Cheers, Berwin __ R-help@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.
Re: [R] Environmental oddity.
On Sun, 7 Nov 2021 09:02:36 +0530 Deepayan Sarkar 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 __ R-help@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.
Re: [R] Environmental oddity --- reproducible example.
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.889 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-8LC_COLLATE=en_GB.UTF-8 [5] LC_MONETARY=en_NZ.UTF-8LC_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.2pkgload_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.0sessioninfo_1.1.1 cli_2.5.0 [13] withr_2.4.2 ellipsis_0.3.2remotes_2.4.0 rprojroot_1.3-2 [17] lifecycle_1.0.0 crayon_1.3.4 processx_3.5.2purrr_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.0cachem_1.0.5 compiler_4.1.1 [29] desc_1.3.0backports_1.1.6 prettyunits_1.1.1 -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 d2 <- function (x, mean = 0, sd = 1, log = FALSE) (((x - mean)/sd)^2 - 1) * if (log) 1 else dnorm(x, mean, sd)/sd __ R-help@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.
Re: [R] Environmental oddity.
On Sun, Nov 7, 2021 at 6:05 AM Rolf Turner 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 > > > > 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 > > > > > and typing "environment(d2)" gives > > > > > The d2() function however gives an incorrect result: > > > d1(1,0,3,TRUE) > > [1] -0.2962963 > > d2(1,0,3,TRUE) > > [1] -0.889 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. 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@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. __ R-help@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.
Re: [R] Environmental oddity.
In general, the search for symbols for a function Z in a package Y will span only those namespaces that the package Y specifies. The search for symbols in a function whose parent environment is the global environment will start there, thereby opening the door to find masking versions of functions instead of the intended package function. Kind of hard to investigate your case from here. On November 6, 2021 5:35:08 PM PDT, Rolf Turner 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 >> > >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 > >> > >and typing "environment(d2)" gives > >> > >The d2() function however gives an incorrect result: > >> d1(1,0,3,TRUE) >> [1] -0.2962963 >> d2(1,0,3,TRUE) >> [1] -0.889 > >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". > >Can anyone give me any insight? Ta. > >cheers, > >Rolf Turner > -- Sent from my phone. Please excuse my brevity. __ R-help@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.
[R] Environmental oddity.
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 > 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 > and typing "environment(d2)" gives > The d2() function however gives an incorrect result: > d1(1,0,3,TRUE) > [1] -0.2962963 > d2(1,0,3,TRUE) > [1] -0.889 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". 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@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.