I'm trying to convert a bunch of Stata commands to their R equivalents, and I'm struggling with how R does handle confidence intervals for inferential statistics of single variables.
In Stata, I can use
ci variable to calculate normal confidence intervals,
ci variable, b to calculate binomial intervals, and
ci variable, p to calculate the intervals assuming the variable is distributed as Poisson.
R seems slightly more complicated, though (
confint() only works with models…). It appears that I have to run the respective tests to get the confidence intervals (
poisson.test). I'm fine doing that, but these functions seem to be less generalized than Stata's
For example, to calculate a normal 95% confidence interval I use
set.seed(1234) x <- rnorm(100) t.test(x) # 95 percent confidence interval: # -0.35605755 0.04253406
However, it seems to be a lot more complicated to calcuate binomial or Poisson intervals. For example, if I have a column of binary data (yes/no; 1/0) like
x below, Stata appears to convert it into count data automatically when running
ci x, b. In R, I have to convert it to count data on my own with
set.seed(1234) x <- sample(0:1, 100, replace=TRUE) # lots of 0s and 1s binom.test(sum(x), length(x)) # 95 percent confidence interval: # 0.3503202 0.5527198
Is manually feeding in the sum and the length of the variable the official R-esque way of calculating the confidence interval assuming a binomial distribution, or is there a better way?
Likewise, what's the most R-esque way to calculate confidence intervals for a variable assumed to follow a Poisson distribution--the equivalent of
ci x, p:
set.seed(1234) x <- rpois(100, 5) # random Poisson distribution # ... magic R voodoo ... # Confidence interval!
So, I guess in summary, what's the best R equivalent for Stata's
ci x, b, and
ci x, p commands for single variables?