Skip to content

Instantly share code, notes, and snippets.

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

Embed
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
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
You can’t perform that action at this time.