Last active
November 13, 2019 09:46
-
-
Save CnrLwlss/a79a3d039230390fed4622f8e67172cd to your computer and use it in GitHub Desktop.
How many replicate samples do we need to estimate the mean of a distribution? 2-panel plot, random output.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mu = 5 | |
stdev = 2 | |
N = 10000 | |
data = rnorm(N,mu,stdev) | |
pdf = function(x) dnorm(x,mu,stdev) | |
bestmu = function(N,x) sum(x[1:N])/N | |
op=par(mfrow=c(1,2)) | |
plot(density(data,from=min(data), to=max(data)),main = paste("Comparing density plot from",N,"\nsamples with true density"),lwd=2) | |
curve(pdf,from = min(data),to=max(data),add = TRUE,col="red",lwd=3) | |
estimates = sapply(1:N,bestmu,x = data) | |
plot(estimates,xlab="Number of measurements",ylab="Best estimate for mean",type="n") | |
abline(h = mu,col="red",lwd=3) | |
points(estimates,type="l",lwd=2) | |
par=op |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment