Normal distribution R base

Hi,

How can I fill in the correct form the area?

Thanks

     f <- dnorm(x, Mean, Sd) # Density function
     qnorm(0.5, mean = 0, sd = 1) # 0
      x <- pnorm(-1.5, mean = 0, sd = 1) # 0.0668072
      qnorm(x, mean = 0, sd = 1) # -1.5
      
      Mean <- 0
      Sd <- 1
      # Area:
      lb <- -3 # Lower bound
      ub <- -1.5
      
      
                  x <- seq(-3, 3, length = 100) * Sd + Mean
                  x2 <- seq(min(x), ub, length = 0.2) # New Grid
                  y <- dnorm(x2, Mean, Sd) # Density
      
      # Plot Area:
      plot(x, f, type = "l", lwd = 2, col = "blue", ylab = "", xlab = "Weight", main = "")
      polygon(c(lb, x2, ub), c(0, y, 0), col = rgb(0, 0, 1, alpha = 0.5))

With that code, I obtain this:

How to fix it correctly?

What are you trying to do? Do you want to shade part of the area under the bell curve?

Hi, yes.

I´ve been studying dnorm(), pnorm(), qnorm() and rnorm() in this website, an in that website there´s a solution with a function()

I don´t want to use the function, I want to understand the process, for that reason, I need that help.

thanks,

Setting length = 0.2 in x2<-seq() is probably a mistake. Try changing that.

Here I added it but doesn´t work:

I´m so Sorry, this is the correct code, now you see the plot, that I sent at the beginning:

Mean <- 1000
Sd <- 10
x <- seq(-3, 3, length = 100) * Sd + Mean # X grid for non-standard normal distribution
f <- dnorm(x, Mean, Sd) # Density function
qnorm(0.5, mean = 0, sd = 1) # 0
x <- pnorm(-1.5, mean = 0, sd = 1) # 0.0668072
qnorm(x, mean = 0, sd = 1) # -1.5

Mean <- 0
Sd <- 1

lb <- -3 # Lower bound
ub <- -1.5

x <- seq(-3, 3, length = 100) * Sd + Mean
x2 <- seq(min(x), ub, length = 0.2) # New Grid
y <- dnorm(x2, Mean, Sd) # Density

plot(x, f, type = "l", lwd = 2, col = "blue", ylab = "", xlab = "Weight", main = "")
polygon(c(lb, x2, ub), c(0, y, 0), col = rgb(0, 0, 1, alpha = 0.5))

It must be a simple way, but i don´t know how to do that.
thanks,

Part of the problem is that you are changing Mean and Sd. You also still have the length of x2 set to 0.2.

Could you tell us a little more about which parts of the code you understand and which parts you don't?

1 Like

Now it works, with this code:

Sd <- 1
Mean <- 0 
x <- seq(-3, 3, length = 100) * Sd + Mean
x
lb <- -3
ub <- -1.5
x2 <- seq(lb, ub, length = 100) 
f <- dnorm(x, Mean, Sd) # Density function
y <- dnorm(x2, Mean, Sd) # Density


plot(x, f, type = "l", lwd = 2, col = "blue", ylab = "", xlab = "Weight", main = "")
polygon(c(lb, x2, ub), c(0, y, 0), col = rgb(0, 0, 1, alpha = 0.5))

Thanks for all the advices, now I have to practice more :blush:

Greetings,

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.