exracon
exracon

Reputation: 23

How can I calculate confidence interval for a mean in R not using confint

I am trying to work on a problem where I try generate random exponential and uniform distributions and sample from them. Then I calculate the confidence interval of the linear model on them using confint(); however, I don't know how to get the correct confidence interval levels that I got from confint() using mean, sd and qt.

Here is what I have so far:

x <- rexp(30, rate=1); x
confint(lm(x~1))
summary(lm(x~1))$coefficients
mean(x)
sd(x)
x.std.error <- sd(x)/sqrt(30)

I'm also not sure, how to do this using the runif command so if I can get help with that it would be really helpful

Upvotes: 1

Views: 2662

Answers (1)

Aaron Montgomery
Aaron Montgomery

Reputation: 1387

The result of confint in this context is just the ordinary classical 95% confidence interval for a population mean. The interval is centered around the sample mean (mean(x)), and the margin of error is the standard error you found (x.std.error) with a multiplier that comes from the t-distribution (qt(0.975, 29)). (This gives the 97.5th percentile of the t-distribution with 29 degrees of freedom; in this context, "degrees of freedom" can be regarded as one less than the sample size.)

To recover the confidence interval provided by confint(lm(x~1)), you can use:

mean(x) - qt(0.975, 29) * x.std.error
mean(x) + qt(0.975, 29) * x.std.error

or equivalently, and perhaps more intuitively:

mean(x) + qt(0.025, 29) * x.std.error   # qt(0.025, 29) = -qt(0.975, 29)
mean(x) + qt(0.975, 29) * x.std.error

I'm not quite sure what you mean when you say that you're not sure how to do this using runif, but presumably it's the same basic process as what you did, but replacing the first line with runif(30, 10, 15) for 30 variables uniformly distributed on the interval [10, 15] (as an example).

Upvotes: 2

Related Questions