Plot Curve Function

Dear,

Why doesn't the red curve overlap the green curve? See the code below:

set.seed(1L)
x <- rnorm(n = 1e3L, mean = 200, sd = 30)
hist(x, probability = TRUE, ylim = c(0, 0.015))
curve(dnorm(x = x, mean = 200, sd = 30), col = "black", lty = 1, lwd = 2, add = TRUE) # OK
curve(dnorm(x = x, mean = 199.6506, sd = 31.04748), col = "green", lty = 1, lwd = 2, add = TRUE) # OK
curve(dnorm(x = x, mean = mean(x), sd = sd(x)), col = "red", lty = 1, lwd = 2, add = TRUE) # ?


Best regards,
prdm0

x within the curve function is not being interpreted as expected. Calculating mean(x) and sd(x) outside of curve() works.

set.seed(1L)
x <- rnorm(n = 1e3L, mean = 200, sd = 30)
hist(x, probability = TRUE, ylim = c(0, 0.015))
curve(dnorm(x = x, mean = 200, sd = 30), col = "black", lty = 1, lwd = 2, add = TRUE) # OK
curve(dnorm(x = x, mean = 199.6506, sd = 31.04748), col = "green", lty = 1, lwd = 4, add = TRUE) # OK
z <- mean(x) 
zsd = sd(x)
curve(dnorm(x = x, mean = z, sd = zsd), col = "red", lty = 1, lwd = 2, add = TRUE) # ?

Created on 2019-08-01 by the reprex package (v0.2.1)

Trying to calculate within curve() but using a symbol other than x leads to an error.

Y <- x
curve(dnorm(x = Y, mean = mean(Y), sd = sd(Y)), col = "red", lty = 1, lwd = 2, add = TRUE) # ?
#> Error in curve(dnorm(x = Y, mean = mean(Y), sd = sd(Y)), col = "red", : 'expr' must be a function, or a call or an expression containing 'x'

Dear @FJCC

yes, curve() works by saving to an object and then passing the object to curve(). I had realized this and it also works using the line() command. What struck me as odd is that it does not work with the operation being performed within the curve() function, but the scope rules are being followed.

My interest is to understand why it doesn't work by doing the operation inside curve(). I found that curious.

Best regards,
prdm0.

Best source is probably the curve() docs here.

A post on https://thomasleeper.com/Rcourse/Tutorials/curve.html

The implementation of function in R can be seen in the mirror here:

2 Likes

Just adding a note here that if you use plot.function (which calls curve itself) in place of the last curve call in the following way, you'll get the expected output:

plot.function(x = function(t) dnorm(x = t, mean = mean(x = x), sd = sd(x = x)),
              to = 100.0, # min(.Last.value$x)
              from = 320.0, # max(.Last.value$x)
              n = 101, # not necessary
              add = TRUE,
              col = "red")

I don't know the answer, and will try to dig into this over the weekend.

Edit

I think the trick is to use xname to avoid confusion, as follows.

set.seed(seed = 1L)

x <- rnorm(n = 1e3L,
           mean = 200,
           sd = 30)

hist(x = x,
     probability = TRUE,
     ylim = c(0, 0.015))

curve(expr = dnorm(x = x, mean = 200, sd = 30),
      col = "black",
      lty = 1,
      lwd = 2,
      add = TRUE)

curve(expr = dnorm(x = x,
                   mean = 199.6506,
                   sd = 31.04748),
      col = "green",
      lty = 1,
      lwd = 2,
      add = TRUE)

# plot.function(x = function(t) dnorm(x = t,
#                                     mean = mean(x = x),
#                                     sd = sd(x = x)),
#               from = min(.Last.value$x),
#               to = max(.Last.value$x),
#               col = "red",
#               lwd = 2,
#               add = TRUE)

curve(expr = dnorm(x = t, 
                   mean = mean(x = x),
                   sd = sd(x = x)),
      add = TRUE,
      xname = "t",
      col = "red",
      lwd = 2)

Created on 2019-08-02 by the reprex package (v0.3.0)

Does this help :thinking: ?

Hi @Yarnabrina , thanks for the info. My problem is not practical. I am studying the code of the curve() function to understand what happens. My question is why does curve() work differently than I thought. I want to understand the curve() philosophy and why it works like that.

Best regards,
prdm0

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.