Skip to content

Instantly share code, notes, and snippets.

@CnrLwlss CnrLwlss/estimate_mu.R
Last active Nov 13, 2019

What would you like to do?
How many replicate samples do we need to estimate the mean of a distribution? 2-panel plot, random output.
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
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.