Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a standard normal distribution in R [duplicate]

Possible Duplicate:
Making a standard normal distribution in R

Using R, draw a standard normal distribution. Label the mean and 3 standard deviations above and below the (10) mean. Include an informative title and labels on the x and y axes.

This is a homework problem. I'm not sure how to get going with the code. How should I get started?

like image 848
Stats Rookie Avatar asked May 10 '12 23:05

Stats Rookie


People also ask

How do you generate a normal random variable in R?

A standard normal distribution is the type of distribution that has mean equals to zero with standard deviation 1. If we want to generate standard normal random numbers then rnorm function of R can be used but need to pass the mean = 0 and standard deviation = 1 inside this function.

How do I add a normal distribution curve to a histogram in R?

A basic histogram can be created with the hist function. In order to add a normal curve or the density line you will need to create a density histogram setting prob = TRUE as argument.

What is Rnorm function in R?

rnorm is the R function that simulates random variates having a specified normal distribution. As with pnorm , qnorm , and dnorm , optional arguments specify the mean and standard deviation of the distribution.


2 Answers

I am pretty sure this is a duplicate. Anyway, have a look at the following piece of code

x <- seq(5, 15, length=1000)
y <- dnorm(x, mean=10, sd=3)
plot(x, y, type="l", lwd=1)

I'm sure you can work the rest out yourself, for the title you might want to look for something called main= and y-axis labels are also up to you.

If you want to see more of the tails of the distribution, why don't you try playing with the seq(5, 15, ) section? Finally, if you want to know more about what dnorm is doing I suggest you look here

like image 60
user1317221_G Avatar answered Sep 30 '22 18:09

user1317221_G


By the way, instead of generating the x and y coordinates yourself, you can also use the curve() function, which is intended to draw curves corresponding to a function (such as the density of a standard normal function).

see

help(curve)

and its examples.

And if you want to add som text to properly label the mean and standard deviations, you can use the text() function (see also plotmath, for annotations with mathematical symbols) .

see

help(text)
help(plotmath)
like image 41
Matthieu Dubois Avatar answered Sep 30 '22 16:09

Matthieu Dubois