jeudi 18 janvier 2018

Plotting a histogram with random numbers/variables?

I am trying to solve this problem. Let x = x1 + ... + x20, the sum of 20 independent Uniform(0,1) random variables. In R, create 1,000 simulations of x and plot their histogram. On the histogram, overlay a graph of the normal density function with the same mean as x. Comment on any differences between the histogram and the curve.

To plot a histogram in R, I am going to build on the following code:

library(ggplot2)

df <- data.frame( x = rnorm(1000) )

ggplot(df, aes(x)) + geom_histogram(aes(y=..density..))

In ggplot, I cannot plot the normal density as a function. But I can easily simulate lots of values drawn from a normal distribution (say 1,000) and plot these data as a geom_density(). The main challenge I have is to figure out how to use two ggplot layers, each with its own data.frame: The first layer uses the data from x the second uses data from the normal distribution. Thanks.




Aucun commentaire:

Enregistrer un commentaire