Sample Data
# Load necessary libraries
set.seed(42)
n <- 50
numeric_variable <- rnorm(n, mean = 100, sd = 15)
paired_sample1 <- rnorm(n, mean = 100, sd = 10)
paired_sample2 <- rnorm(n, mean = 95, sd = 10)
group1 <- rnorm(n, mean = 100, sd = 15)
group2 <- rnorm(n, mean = 95, sd = 15)
categorical_variable <- rbinom(n, size = 1, prob = 0.4)
group1_binary <- rbinom(n, size = 1, prob = 0.3)
group2_binary <- rbinom(n, size = 1, prob = 0.5)
# Create data frame
data <- data.frame(
numeric_variable,
paired_sample1,
paired_sample2,
group1,
group2,
categorical_variable,
group1_binary,
group2_binary
)
head(data)
## numeric_variable paired_sample1 paired_sample2 group1 group2
## 1 120.56438 103.21925 107.00965 99.38952 64.98606
## 2 91.52953 92.16161 105.44751 76.72683 100.00666
## 3 105.44693 115.75728 84.96791 117.50754 112.56988
## 4 109.49294 106.42899 113.48482 95.89531 125.89309
## 5 106.06402 100.89761 88.33227 92.98232 74.34708
## 6 98.40813 102.76551 96.05514 81.42622 77.73717
## categorical_variable group1_binary group2_binary
## 1 0 1 0
## 2 0 0 0
## 3 0 0 1
## 4 1 0 0
## 5 0 0 1
## 6 1 0 0
1. One-Population Mean Test (Unknown Sigma)
one_population_mean_test <- t.test(data$numeric_variable, mu = 100)
print(one_population_mean_test)
##
## One Sample t-test
##
## data: data$numeric_variable
## t = -0.21906, df = 49, p-value = 0.8275
## alternative hypothesis: true mean is not equal to 100
## 95 percent confidence interval:
## 94.55623 104.37361
## sample estimates:
## mean of x
## 99.46492
2. Two-Population Mean Paired Test (Unknown Sigma)
two_population_paired_test <- t.test(data$paired_sample1, data$paired_sample2, paired = TRUE)
print(two_population_paired_test)
##
## Paired t-test
##
## data: data$paired_sample1 and data$paired_sample2
## t = 4.0495, df = 49, p-value = 0.0001823
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## 3.787905 11.251145
## sample estimates:
## mean difference
## 7.519525
3. Two-Population Mean Independent Test (Unknown Sigma)
two_population_independent_test <- t.test(data$group1, data$group2, var.equal = FALSE)
print(two_population_independent_test)
##
## Welch Two Sample t-test
##
## data: data$group1 and data$group2
## t = 1.6081, df = 96.828, p-value = 0.1111
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -1.059931 10.110245
## sample estimates:
## mean of x mean of y
## 99.64426 95.11910
4. One-Population Proportion Test
successes <- sum(data$categorical_variable)
n_total <- nrow(data)
prop_test_one <- prop.test(successes, n_total, p = 0.5, alternative = "two.sided")
print(prop_test_one)
##
## 1-sample proportions test with continuity correction
##
## data: successes out of n_total, null probability 0.5
## X-squared = 4.5, df = 1, p-value = 0.03389
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
## 0.2159464 0.4885541
## sample estimates:
## p
## 0.34
5. Two-Population Proportion Test
successes_group <- c(sum(data$group1_binary), sum(data$group2_binary))
sizes_group <- c(length(data$group1_binary), length(data$group2_binary))
prop_test_two <- prop.test(successes_group, sizes_group, alternative = "two.sided")
print(prop_test_two)
##
## 2-sample test for equality of proportions with continuity correction
##
## data: successes_group out of sizes_group
## X-squared = 2.0798, df = 1, p-value = 0.1493
## alternative hypothesis: two.sided
## 95 percent confidence interval:
## -0.36766591 0.04766591
## sample estimates:
## prop 1 prop 2
## 0.30 0.46