# Load the mtcars dataset
data(mtcars)

# Question 1: Calculate mean and standard deviation for 'qsec'
qsec_mean =mean(mtcars$qsec)
qsec_std =sd(mtcars$qsec)

# Question 2: Standard error of 'qsec'
qsec_se =qsec_std / sqrt(length(mtcars$qsec))

# Question 3: 95% confidence interval for the mean of 'qsec' using t-distribution
confidence_level_95 =0.95
df_95 =length(mtcars$qsec) - 1
t_value_95 =qt((1 + confidence_level_95) / 2, df_95)
qsec_ci_95 =c(qsec_mean - t_value_95 * qsec_se, qsec_mean + t_value_95 * qsec_se)

# Question 4: 99% one-sided confidence interval (lower limit) for the mean of 'qsec' using t-distribution
confidence_level_99 =0.99
t_value_99_lower =qt(1 - confidence_level_99, df_95)
qsec_lower_ci_99 =qsec_mean + t_value_99_lower * qsec_se

# Question 5: 99% confidence interval for the mean of 'qsec' using normal distribution
z_value_99 =qnorm((1 + confidence_level_99) / 2)
qsec_ci_99_normal =c(qsec_mean - z_value_99 * qsec_se, qsec_mean + z_value_99 * qsec_se)

# Display results
cat("Mean of qsec:", qsec_mean, "\n")
## Mean of qsec: 17.84875
cat("Std Dev of qsec:", qsec_std, "\n")
## Std Dev of qsec: 1.786943
cat("Standard Error of qsec:", qsec_se, "\n")
## Standard Error of qsec: 0.3158899
cat("95% CI for mean of qsec (t-dist):", qsec_ci_95, "\n")
## 95% CI for mean of qsec (t-dist): 17.20449 18.49301
cat("99% Lower Limit CI for mean of qsec (t-dist):", qsec_lower_ci_99, "\n")
## 99% Lower Limit CI for mean of qsec (t-dist): 17.07393
cat("99% CI for mean of qsec (normal dist):", qsec_ci_99_normal, "\n")
## 99% CI for mean of qsec (normal dist): 17.03507 18.66243
# Given values
sample_size =25
sample_mean =22.3
sample_std =5
population_mean =mean(mtcars$mpg)  # Calculate the mean mpg from the mtcars dataset

# 6(a) 95% confidence interval for the standard deviation (using Chi-square distribution)
df_chi =sample_size - 1  # degrees of freedom
chi2_lower =qchisq((1 - 0.95) / 2, df_chi)
chi2_upper =qchisq((1 + 0.95) / 2, df_chi)

ci_std_lower =sqrt((df_chi * sample_std^2) / chi2_upper)
ci_std_upper =sqrt((df_chi * sample_std^2) / chi2_lower)
ci_std_95 =c(ci_std_lower, ci_std_upper)

# 6(b) Hypothesis testing
# Null Hypothesis: H0: μ = 20.09 (two-sided test)

# Perform t-test
t_stat =(sample_mean - population_mean) / (sample_std / sqrt(sample_size))
p_value =2 * (1 - pt(abs(t_stat), df=sample_size - 1))

# Confidence Interval for the sample mean at 95%
t_value_95 =qt(0.975, sample_size - 1)
ci_mean_lower =sample_mean - t_value_95 * (sample_std / sqrt(sample_size))
ci_mean_upper =sample_mean + t_value_95 * (sample_std / sqrt(sample_size))
ci_mean_95 =c(ci_mean_lower, ci_mean_upper)

# Display results
cat("95% Confidence Interval for standard deviation:", ci_std_95, "\n")
## 95% Confidence Interval for standard deviation: 3.904142 6.955761
cat("Null Hypothesis: H0: μ = 20.09 (two-sided test)\n")
## Null Hypothesis: H0: μ = 20.09 (two-sided test)
cat("t-statistic:", t_stat, "\n")
## t-statistic: 2.209375
cat("p-value:", p_value, "\n")
## p-value: 0.03693856
cat("95% Confidence Interval for sample mean:", ci_mean_95, "\n")
## 95% Confidence Interval for sample mean: 20.2361 24.3639
cat("Is 20.09 within the 95% CI for sample mean:", population_mean >= ci_mean_95[1] & population_mean <= ci_mean_95[2], "\n")
## Is 20.09 within the 95% CI for sample mean: FALSE