Both sides previous revisionPrevious revision | Next revisionBoth sides next revision |
sampling [2018/03/13 16:48] – [Sample statistics] hkimscil | sampling [2018/03/13 16:49] – [Sample statistics] hkimscil |
---|
| |
var_ <- new.env() | var_ <- new.env() |
n<-20 ## Sample n individuals at a time | n<-20 ## Sample n individuals at a time |
p_mean<-0 ## Population mean | p_mean<-0 ## Population mean |
p_sd<-1 ## Population standard deviation | p_sd<-1 ## Population standard deviation |
N<-500 ## Number of times the experiment (sampling) is replicated | N<-500 ## Number of times the experiment (sampling) is replicated |
| |
pdf('SE.pdf') | pdf('SE.pdf') |
| |
for(i in 1:N) ## do the experiment N times | for(i in 1:N) ## do the experiment N times |
{ | { |
smp<-rnorm(n,p_mean,p_sd) ## sample n data points from the population | smp<-rnorm(n,p_mean,p_sd) ## sample n data points from the population |
| |
var_$x_bar<-c(var_$x_bar,mean(smp)) ## keep track of the mean (x_bar) from each sample | var_$x_bar<-c(var_$x_bar,mean(smp)) ## keep track of the mean (x_bar) from each sample |
| |
| hist(var_$x_bar,probability=TRUE,col="red",xlim=c(-4,4),xlab="x / x_bar",main="",ylim=c(0,2.2)) |
| # Plot a histogram of x_bar values |
| |
hist(var_$x_bar,probability=TRUE,col="red",xlim=c(-4,4),xlab="x / x_bar",main="",ylim=c(0,2.2)) # Plot a histogram of x_bar values | |
points(mean(smp),0,pch=19,cex=1.5,col='black') | points(mean(smp),0,pch=19,cex=1.5,col='black') |
curve(dnorm(x,p_mean,p_sd/sqrt(n)),lwd=3,add=TRUE) | curve(dnorm(x,p_mean,p_sd/sqrt(n)),lwd=3,add=TRUE) |
text(2.5,1.5,labels=paste('standard deviation of\nsample means = ',round(sd(var_$x_bar),2),sep='') ) | text(2.5,1.5,labels=paste('standard deviation of\nsample means = ',round(sd(var_$x_bar),2),sep='') ) |
| |
curve(dnorm(x,p_mean,p_sd),main="",ylab="",xlim=c(-4,4),xlab="X",col="blue",lwd=3,add=TRUE) ## Plot the sample | curve(dnorm(x,p_mean,p_sd),main="",ylab="",xlim=c(-4,4),xlab="X",col="blue",lwd=3,add=TRUE) |
| ## Plot the sample |
| |
text(2.5,0.5,labels=paste('# of means drawn = ',i,sep='')) | text(2.5,0.5,labels=paste('# of means drawn = ',i,sep='')) |
dev.off() | dev.off() |
</code> | </code> |
| {{SE.pdf}} |
| |
* Variation See, [[:Variance]]: 225.0584138 (15^2) | * Variation See, [[:Variance]]: 225.0584138 (15^2) |