How to plot a normal distribution by labeling specific parts of the x-axis?

后端 未结 7 1856
轻奢々
轻奢々 2020-12-03 00:01

I am using the following code to create a standard normal distribution in R:

x <- seq(-4, 4, length=200)
y <- dnorm(x, mean=0, sd=1)
plot(x, y, type=\"         


        
7条回答
  •  我在风中等你
    2020-12-03 00:11

    An extremely inefficient and unusual, but beautiful solution, which works based on the ideas of Monte Carlo simulation, is this:

    1. simulate many draws (or samples) from a given distribution (say the normal).
    2. plot the density of these draws using rnorm. The rnorm function takes as arguments (A,B,C) and returns a vector of A samples from a normal distribution centered at B, with standard deviation C.

    Thus to take a sample of size 50,000 from a standard normal (i.e, a normal with mean 0 and standard deviation 1), and plot its density, we do the following:

    x = rnorm(50000,0,1)
    
    plot(density(x))
    

    As the number of draws goes to infinity this will converge in distribution to the normal. To illustrate this, see the image below which shows from left to right and top to bottom 5000,50000,500000, and 5 million samples.

提交回复
热议问题