Skip to content

Instantly share code, notes, and snippets.

@cdriveraus
Created October 30, 2016 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdriveraus/24d288230918c9117229e6a04e6ac23a to your computer and use it in GitHub Desktop.
Save cdriveraus/24d288230918c9117229e6a04e6ac23a to your computer and use it in GitHub Desktop.
Does a 95% confidence interval contain the true parameter 95% of the time?
b=.8
n=100
nruns=10000
out<-rep(NA,nruns)
for(i in 1:nruns){
x=rnorm(n,3,2)
y=b*x + rnorm(n, 0, 3)
fit=lm(y~x)
summ=summary(fit)
upper=summ$coefficients[2,1]+2*summ$coefficients[2,2]
lower=summ$coefficients[2,1]-2*summ$coefficients[2,2]
out[i]<- b < upper & b > lower
}
sum(out)/nruns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment