Making a standard normal distribution in R

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

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

x

I need the x axis to be labeled at the mean and at points three standard deviations above and below the mean. How can I add these labels?

回答1:

The easiest (but not general) way is to restrict the limits of the x axis. The +/- 1:3 sigma will be labeled as such, and the mean will be labeled as 0 - indicating 0 deviations from the mean.

plot(x,y, type = "l", lwd = 2, xlim = c(-3.5,3.5))

Another option is to use more specific labels:

plot(x,y, type = "l", lwd = 2, axes = FALSE, xlab = "", ylab = "") axis(1, at = -3:3, labels = c("-3s", "-2s", "-1s", "mean", "1s", "2s", "3s"))


回答2:

Using David's code, you could skip creating x and just use curve() on the dnorm function:

curve(dnorm, -3.5, 3.5, lwd=2, axes = FALSE, xlab = "", ylab = "") axis(1, at = -3:3, labels = c("-3s", "-2s", "-1s", "mean", "1s", "2s", "3s"))

But this doesn't use the given code anymore. If this is a homework assignment please tag it as such.


Small tip, use consistently either or = with spaces around them, it will make your life much easier. For example:

x 


回答3:

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.



回答4:

If you like hard way of doing something without using R built in function or you want to do this outside R, you can use the following formula.

x


回答5:

In general case, for example: Normal(2, 1)

f 

This is a very general, f can be defined freely, with any given parameters, for example:

f 


回答6:

I particularly love Lattice for this goal. It easily implements graphical information such as specific areas under a curve, the one you usually require when dealing with probabilities problems such as find P(a

library(lattice)  e4a =1 & x=1 & x

In this example I make the area between z = 1 and z = 1.5 stand out. You can move easily this parameters according to your problem.

Axis labels are automatic.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!