While writing a little function to calculate the 99% confidence interval of some sample,
I get a different answer than the R t.test formula. Here's my function:
confinterval <- function(x,alpha){
alpha = 0.01
df = length(x)-1
pos_confinterval = mean(x) + qt((1 - alpha/2),df)*(var(x)/length(x))
neg_confinterval = mean(x) - qt((1 - alpha/2),df)*(var(x)/length(x))
cut_points <- c(neg_confinterval,pos_confinterval)
return(cut_points)
}
confinterval(data) gives me the following vector of cutpoints (4.469488 4.598704) While : t.test(data,conf.level=.995) yields (4.064382 5.003810)
Is there an easier way to get a function like this to calculates confidence intervals?