Skip to content
Prev 389821 / 398503 Next

Environmental oddity.

On Sun, Nov 7, 2021 at 6:05 AM Rolf Turner <r.turner at auckland.ac.nz> wrote:
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)
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