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

时间:2021-07-31 03:37:22

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. The distribution of sample means should be approximately normal.

我试图绘制正常概率密度函数的1000个样本值的平均值,每个样本值来自40个大小的指数分布。样本均值的分布应近似正常。

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: R使用数据集创建正态分布图

结果:

Approach #2 for plotting:

方法#2用于绘图:

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

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

结果:

However what I want is something similar to this graph:

但是我想要的是类似于这个图:

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

3 个解决方案

#1


1  

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

I do it with ggplot2

我是用ggplot2做的

#2


0  

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")

#3


0  

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(这里添加到同一个图中):

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) 

#1


1  

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

I do it with ggplot2

我是用ggplot2做的

#2


0  

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")

#3


0  

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(这里添加到同一个图中):

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)