Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

Is there an easier way to write a function like this to calculate the confidence interval?

$
0
0

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?


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>