简体   繁体   English

R使用数据集创建正态分布图

[英]R Creating Normal Distribution Plot using dataset

Im new to R. 我是R的新手。

Im trying to plot normal probability density function for the mean of 1000 sample values that are from exponential distributions of size 40 each. 我试图绘制1000个样本值的平均值的正态概率密度函数,这些样本值分别来自大小为40的指数分布。 The distribution of sample means should be approximately normal. 样本均值的分布应近似正态分布。

Problem that Im having is with how the plot is rendered, see below: 我有关于如何绘制情节的问题,请参见下文:

Here is my "R" code: 这是我的“ R”代码:

#allocate list size to store means
meanOfSampleMeansVector <- numeric(1000)
#for 1000 iterations create 40 exponential random variable with variance of 0.2 units
for (i in 1:1000 ){ 
sample <- rexp(n=40,0.2) 
#get mean of sample
meanOfSample <- mean(sample) 
#set the mean in list 
meanOfSampleMeansVector[i] <- meanOfSample
}

generate normal probability density function 生成正态概率密度函数

propDensity=dnorm(meanOfSampleMeansVector,mean(meanOfSampleMeansVector),sd(meanOfSampleMeansVector))

Approach #1 for plotting: 绘制方法#1:

plot(meanOfSampleMeansVector,propDensity, xlab="x value", type="l",
  ylab="Density", main="Sample Means of Exponential Distribution",col="red")

Result: 结果: 在此处输入图片说明

Approach #2 for plotting: 绘图方法2:

plot(meanOfSampleMeansVector,propDensity, xlab="x value",
  ylab="Density", main="Sample Means of Exponential Distribution",col="red")

Result: 结果: 在此处输入图片说明

However what I want is something similar to this graph: 但是我想要的是与此图类似的东西:

在此处输入图片说明

require(ggplot2)
qplot(meanOfSampleMeansVector,propDensity,geom="line")+
  xlab("x value")+ylab("Density")+
  ggtitle("Sample Means of Exponential Distribution")

I do it with ggplot2 我用ggplot2

Base graphics can do this just as well: 基本图形也可以做到这一点:

xval <- seq(min(meanOfSampleMeansVector), max(meanOfSampleMeansVector), length=200)
propDensity=dnorm(xval, mean(meanOfSampleMeansVector), sd(meanOfSampleMeansVector))
plot(xval,propDensity, xlab="x value", type="l",
      ylab="Density", main="Sample Means of Exponential Distribution",col="red")

The problem in approach #1 is simply that the sample isn't sorted: 方法1的问题很简单,就是样本未排序:

S<-sort(meanOfSampleMeansVector)
propDensity=dnorm(S,mean(S),sd(S))
plot(S,propDensity, xlab="x value", type="l",
  ylab="Density", main="Sample Means of Exponential Distribution",col="red")

But I strongly recommend, that you take a look at density() instead if you want to plot the estimated pdf (here added to the same plot): 但我强烈建议,如果要绘制估计的pdf(此处添加到同一图),请改用density() ):

lines(density(meanOfSampleMeansVector),col=1)

or maybe just use a normal quantile plot if you want to verify the CLT: 或者,如果您要验证CLT,则可能仅使用正常的分位数图:

qqnorm(S)
qqline(S) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM