Confidence Interval
Problem Statement : Study and perform foundations for statistical inference to find answers regarding confidence intervals. (Calculating standard error of the mean, finding the t-score, calculating margin of error and constructing the confidence interval.)
Code:
First, let's start by loading a dataset that we can work with. For the purpose of this exercise, we will use the "mtcars" dataset which is a built-in dataset in R.
data(mtcars)
Now, let's assume that we want to calculate the confidence interval for the mean miles per gallon (mpg) of cars in the dataset. We can start by calculating the mean and standard error of the mean using the following commands:
n <- length(mtcars$mpg) # sample size
xbar <- mean(mtcars$mpg) # sample mean
s <- sd(mtcars$mpg) # sample standard deviation
sem <- s / sqrt(n) # standard error of the mean
Next, we need to find the t-score for a given confidence level and degrees of freedom. Let's assume we want to calculate the 95% confidence interval, which corresponds to a two-tailed test with a significance level of 0.05 and 30 degrees of freedom (n-1). We can use the following command to find the t-score:
t <- qt(0.025, df = n-1)
We can now calculate the margin of error using the standard error of the mean and the t-score:
moe <- t * sem
Finally, we can construct the confidence interval by adding and subtracting the margin of error to the sample mean:
ci <- c(xbar - moe, xbar + moe)
We can print the confidence interval using the following command:
cat("The 95% confidence interval for the mean mpg is [", round(ci[1], 2), ", ", round(ci[2], 2), "].")
TASK:
Repeat Above steps for iris Dataset"
Assume that we want to calculate the confidence interval for the mean sepal length of the "setosa" species in the dataset. We can start by subsetting the dataset to include only the "setosa" species and then calculating the mean and standard error of the mean.
hint:
setosa <- subset(iris, Species == "setosa")
n <- length(setosa$Sepal.Length) # sample size
xbar <- mean(setosa$_________) # sample mean
s <- sd(setosa$__________) # sample standard deviation
sem <- ___________ # standard error of the mean
Comments
Post a Comment