R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

##Question 1
##Comparing two independent sample means,taken fromt/wopopulationwithunknownvariance. The following data shows the heights of the individuals of two different countries with unknown population variances. Is there any significant difference between the average heights of the two groups.
# A: 175, 168, 168, 190, 156, 181, 182, 175, 174, 179 
# B: 185, 169, 173, 173, 188, 186, 175, 174, 179, 180
group_A <- c(175, 168, 168, 190, 156, 181, 182, 175, 174, 179)
group_B <- c(185, 169, 173, 173, 188, 186, 175, 174, 179, 180)
alpha <- 0.05
mean_A <- mean(group_A)
mean_B <- mean(group_B)
sd_A <- sd(group_A)
sd_B <- sd(group_B)
n_A <- length(group_A)
n_B <- length(group_B)
pooled_sd <- sqrt(((n_A - 1) * sd_A^2 + (n_B - 1) * sd_B^2) / (n_A + n_B - 2))
t_statistic <- abs((mean_A - mean_B) / (pooled_sd * sqrt(1/n_A + 1/n_B)))
df <- n_A + n_B - 2
critical_value <- qt(1 - alpha / 2, df)
cat("Sample Mean of Group A:", mean_A, "\n")
## Sample Mean of Group A: 174.8
## Sample Mean of Group A: 174.8
cat("Sample Mean of Group B:", mean_B, "\n")
## Sample Mean of Group B: 178.2
## Sample Mean of Group B: 178.2
cat("T-Statistic:", t_statistic, "\n")
## T-Statistic: 0.947373
## T-Statistic: 0.947373
cat("Critical Value (two-tailed):", critical_value, "\n")
## Critical Value (two-tailed): 2.100922
## Critical Value (two-tailed): 2.100922
if (t_statistic > critical_value) {
  cat("Reject the null hypothesis: There is a significant difference between the average heights of the two groups.\n")
} else {
  cat("Fail to reject the null hypothesis: There is no significant difference between the average heights of the two groups.\n")
}
## Fail to reject the null hypothesis: There is no significant difference between the average heights of the two groups.
##Question 2
##Suppose X ~ N(μ, 36). For n = 25 samples of X, the observed mean is 6.2. What conclusion would you reach if the null hypothesis assumes µ = 4 against an alternative hypothesis µ≠4 at a significance level of alpha = 0.05? What if the null hypothesis assumes μ = 8 against an alternative hypothesis µ < 8?
# Set the variables
n <- 25  # Number of samples
x_bar <- 6.2  # Observed mean
mu_null <- 4  # Null hypothesis mean
mu_alt1 <- 8  # Alternative hypothesis mean for μ ≠ 8
mu_alt2 <- 8  # Alternative hypothesis mean for μ < 8
sigma_sq <- 36  # Variance

# Calculate the standard error (SE) using the provided variance
SE <- sqrt(sigma_sq / n)

# Calculate the z-score for the observed mean under the null hypothesis (μ = 4)
z_score_null <- (x_bar - mu_null) / SE

# Calculate the p-value for the two-sided test (μ ≠ 4)
p_value_null <- 2 * (1 - pnorm(abs(z_score_null)))

# Calculate the z-score for the observed mean under the null hypothesis (μ = 8)
z_score_alt <- (x_bar - mu_alt2) / SE

# Calculate the p-value for the one-sided test (μ < 8)
p_value_alt <- pnorm(z_score_alt)

# Determine conclusions based on the p-values and significance level
alpha <- 0.05  # Significance level

cat("For μ = 4 (two-sided test):\n")
## For μ = 4 (two-sided test):
## For μ = 4 (two-sided test):
cat("Z-score:", z_score_null, "\n")
## Z-score: 1.833333
## Z-score: 1.833333
cat("P-value:", p_value_null, "\n")
## P-value: 0.06675302
## P-value: 0.06675302
if (p_value_null < alpha) {
  cat("Reject the null hypothesis (μ = 4) at the 0.05 significance level.\n")
} else {
  cat("Fail to reject the null hypothesis (μ = 4) at the 0.05 significance level.\n")
}
## Fail to reject the null hypothesis (μ = 4) at the 0.05 significance level.
## Fail to reject the null hypothesis (μ = 4) at the 0.05 significance level.
cat("\nFor μ < 8 (one-sided test):\n")
## 
## For μ < 8 (one-sided test):
## 
## For μ < 8 (one-sided test):
cat("Z-score:", z_score_alt, "\n")
## Z-score: -1.5
## Z-score: -1.5
cat("P-value:", p_value_alt, "\n")
## P-value: 0.0668072
## P-value: 0.0668072
if (p_value_alt < alpha) {
  cat("Reject the null hypothesis (μ = 8) at the 0.05 significance level.\n")
} else {
  cat("Fail to reject the null hypothesis (μ = 8) at the 0.05 significance level.\n")
}
## Fail to reject the null hypothesis (μ = 8) at the 0.05 significance level.
## Fail to reject the null hypothesis (μ = 8) at the 0.05 significance level.
##Question 3
##Suppose that 10 volunteers have taken an intelligence test; here are the results obtained;
#Scores: 65, 78, 88, 55, 48, 95, 66, 57, 79, 81.
#The average score of the entire population is 75 in the entire test. Is there any significant difference(with a significance level of  0.05) between the sample and population means, assuming that the variance of the population is not known
# Define the sample scores
sample_scores <- c(65, 78, 88, 55, 48, 95, 66, 57, 79, 81)

# Population mean
pop_mean <- 75

# Calculate sample mean
sample_mean <- mean(sample_scores)

# Calculate sample size
sample_size <- length(sample_scores)

# Calculate sample standard deviation
sample_sd <- sd(sample_scores)

# Perform one-sample t-test
t_stat <- (sample_mean - pop_mean) / (sample_sd / sqrt(sample_size))

# Degrees of freedom
df <- sample_size - 1

# Calculate critical t-value for a significance level of 0.05
critical_t <- qt(0.975, df)

# Calculate p-value
p_value <- 2 * pt(-abs(t_stat), df)

# Print results
cat("Sample Mean:", sample_mean, "\n")
## Sample Mean: 71.2
## Sample Mean: 71.2
cat("Sample Standard Deviation:", sample_sd, "\n")
## Sample Standard Deviation: 15.34637
## Sample Standard Deviation: 15.34637
cat("t-statistic:", t_stat, "\n")
## t-statistic: -0.7830291
## t-statistic: -0.7830291
cat("Degrees of Freedom:", df, "\n")
## Degrees of Freedom: 9
## Degrees of Freedom: 9
cat("Critical t-value:", critical_t, "\n")
## Critical t-value: 2.262157
## Critical t-value: 2.262157
cat("p-value:", p_value, "\n")
## p-value: 0.4537205
## p-value: 0.4537205
# Determine significance
if (abs(t_stat) > critical_t) {
  cat("Reject null hypothesis: There is a significant difference between sample and population means.\n")
} else {
  cat("Fail to reject null hypothesis: There is no significant difference between sample and population means.\n")
}
## Fail to reject null hypothesis: There is no significant difference between sample and population means.
## Fail to reject null hypothesis: There is no significant difference between sample and population means.