Question

I am an statistics student and R beginner (understatement of the year) trying to generate multiple confidence intervals for randomly generated samples of a normal distribution as part of an assignment.

I used the function

data <- replicate(25, rnorm(20, 50, 6))

to generate 25 samples of size n=20 from a N(50, 6^2) distribution (in a double matrix).

My question is, how do I find a 95% confidence interval for each sample of this distribution? I know that I can use colMeans(data) and sd(data) to find the sample mean and sample standard deviation for each sample, but I am having a brain fart trying to think of a function that can generate the confidence intervals for all columns in the double matrix (data).

As of now, my (extremely crude) solution consists of creating the functions

left <- function (x,y){x-(qnorm(0.975)*y/sqrt(20))}
right <- function (x,y){x+(qnorm(0.975)*y/sqrt(20))}

left(colMeans(data), sd(data)
right(colMeans(data), sd(data)

to generate 2 vectors of left and right bounds. Please let me know if there is a better way I can do this.

Was it helpful?

Solution

I suppose you could use the t.test() function. It returns the mean and the 95% confidence interval for a given vector of numbers.

# Create your data
data <- replicate(25, rnorm(20, 50, 6))
data <- as.data.frame(data)

After you make your data, you could apply the t.test() function to all columns using the lapply() function.

# Apply the t.test function and save the results
results <- lapply(data, t.test)

If you only want to see the confidence interval or mean returned, you can call them using the dollar sign operator. For example, for column one of your original data frame, you could type the following:

# Check 95% CI for sample one
results[[1]]$conf.int[1:2]

You could come up with a more eloquent way of saving these data to a results data frame. Remember, you can always see what individual bits of information you can yank from an object by using the str() command. For example:

# Example
example <- t.test(data[,1])
str(example)

Hope this helps. Try this link for more information: Using R to find Confidence Intervals

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top