#install.packages("effsize")
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggthemes)
library(ggrepel)
library(effsize)
library(pwrss)              
## 
## Attaching package: 'pwrss'
## 
## The following object is masked from 'package:stats':
## 
##     power.t.test
my_data <- read.csv("/Users/shemaaeltayeb/Desktop/STATISTIC IN R /student_data.csv")

PART 1:

After having exploring your dataset over the past few weeks, you should already have some questions.

Here are some questions from the student data set:

• Question: Does the reason for choosing the school (reason) have an impact on student performance (e.g., G3)?

• Question: Are there significant differences in student performance (G3) based on the type of address (address: urban or rural)?

• Question: How does the guardian type (guardian) influence students’ daily alcohol consumption (Dalc)?

• Question: How do students’ daily alcohol consumption (Dalc) patterns change during the school year? Are there any significant trends?

• Question: Can we identify patterns in student absences (absences) based on the reason for school absence (reason)? Are there specific reasons that correlate with higher absence rates?

• Question: Do students’ grades in Math (G1 and G2) have any predictive power in determining their final grade (G3)? Can we identify a pattern of improvement or decline?

• Question: Do gender differences in academic performance vary based on other factors like parental education or family relationships?

• Question: Does the study time, absences, previous failures, and previous grades (G1 and G2) relate to a student’s final grade (G3)?

PART 2 : Devise at least two different null hypotheses based on two different aspects (e.g., columns) of your data. For each hypothesis:

Come up with an alpha level, power level, and minimum effect size, and explain why you chose each value.

Determine if you have enough data to perform a Neyman-Pearson hypothesis test. If you do, perform one and interpret results. If not, explain why.

Perform a Fisher’s style test for significance, and interpret the p-value.

So, technically, you have two hypothesis tests for each hypothesis, equating two four total tests.

First, for this question I will choose two null hypotheses related to the final grade (G3) and other factors: Mother education (Medu), Father education (Fedu), Family relationship quality (famrel) and Health (health).

Null Hypothesis 1: Impact of Mother’s Education (Medu) on Student Performance

Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their mother’s education level (Medu).

Alternative Hypothesis (H1): There is a significant difference in the final grades (G3) of students based on their mother’s education level (Medu).

Null Hypothesis 2: Impact of Father’s Education (Fedu) on Student Performance

Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their father’s education level (Fedu).

Alternative Hypothesis (H1): There is a significant difference in the final grades (G3) of students based on their father’s education level (Fedu).

Null Hypothesis 3: Impact of Family Relationship Quality (famrel) on Student Performance

Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their family relationship quality (famrel).

Alternative Hypothesis (H1): There is a significant difference in the final grades (G3) of students based on their family relationship quality (famrel).

Null Hypothesis 4: Impact of Health (health) on Student Performance

Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their health status (health).

Alternative Hypothesis (H1): There is a significant difference in the final grades (G3) of students based on their health status (health).

These hypotheses aim to explore the influence of parental education, family relationship quality, and health status on students’ final grades. The null hypotheses state that there is no effect, while the alternative hypotheses suggest there is a significant impact.

Come up with an alpha level, power level, and minimum effect size, and explain why you chose each value.

For Null Hypothesis 1:

Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their mother’s education level (Medu).

Alternative Hypothesis (H1): There is a significant difference in the final grades (G3) of students based on their mother’s education level (Medu).

For this hypothesis, will set alpha level (α), power level (1 - β), and minimum effect size:

Alpha Level (α = 0.05): Choose alpha level of 0.05. This means we are willing to accept a 5% chance of making a Type I error (incorrectly rejecting a true null hypothesis).Because it allows for some flexibility in detecting meaningful effects.

Power Level (1 - β = 0.80): Choose alpha level of 0.05. This means we are willing to accept a 5% chance of making a Type I error (incorrectly rejecting a true null hypothesis).Because ensures a good balance between detecting real effects and avoiding false negatives (Type II errors).

Minimum Effect Size (1-point difference in final grades): A 1-point difference in final grades represents an effect size of 1, which is more reasonable. To be focusing on detecting a smaller but still significant difference between groups.

count_G3 <- my_data %>%
  group_by(Medu) %>%
  summarize(count_G3 = n()) %>%
  arrange(Medu)

count_G3
## # A tibble: 5 × 2
##    Medu count_G3
##   <int>    <int>
## 1     0        3
## 2     1       59
## 3     2      103
## 4     3       99
## 5     4      131

For Null Hypothesis 4:

Null Hypothesis (H0):

There is no significant difference in the final grades (G3) of students based on their health status (health).

Alternative Hypothesis (H1): There is a significant difference in the final grades (G3) of students based on their health status (health).

For this hypothesis, will set alpha level (α), power level (1 - β), and minimum effect size:

Alpha Level (α): Choose alpha level of 0.05. This means we are willing to accept a 5% chance of making a Type I error (incorrectly rejecting a true null hypothesis).Because it allows for some flexibility in detecting meaningful effects.

Power Level (1 - β): Choose a power level of 0.80, indicating that an 80% chance of detecting a real effect if it exists. So, set 1 - β = 0.80. Because when we have a high power will help to avoid Type II errors (failing to detect a real effect).

Minimum Effect Size (1-point difference in final grades): A 1-point difference in final grades represents an effect size of 1, which is more reasonable. To be focusing on detecting a smaller but still significant difference between groups.

count_G3 <- my_data %>%
  group_by(health) %>%
  summarize(count_G3 = n()) %>%
  arrange(health)

count_G3
## # A tibble: 5 × 2
##   health count_G3
##    <int>    <int>
## 1      1       47
## 2      2       45
## 3      3       91
## 4      4       66
## 5      5      146

Determine if you have enough data to perform a Neyman-Pearson hypothesis test. If you do, perform one and interpret results. If not, explain why.

To determine if I have enough data to perform a Neyman-Pearson hypothesis test.I will do those steps:

  1. Define the Hypotheses.

  2. Choose Significance Level (Alpha).

3.Choose Power Level (1 - Beta).

  1. Determine the Effect Size.

  2. Calculate Required Sample Size.

  3. Compare Calculated Sample Size with Actual Sample Size.

  4. Perform the Hypothesis Test If you their is a enough data, proceed with performing the Neyman-Pearson hypothesis test using the appropriate statistical test ( for example, t-test), and interpret the results cautiously.

#install.packages("pwr")
#install.packages("pwr2")
library(pwr)
library(pwr2)

To find the sample size For Null Hypothesis 1 and Null Hypothesis 4 :

  # Set the parameters:
alpha <- 0.05
power <- 0.80
effect_size <- 1  # 1-point difference

# Calculate required sample size:
sample_size <- pwr.t.test(d = effect_size, sig.level = alpha, power = power, type = "two.sample")$n

# Print the sample size:
print(sample_size)
## [1] 16.71472

With the calculated sample size of 17, if we compared it with actual sample size (395) is a good chance of detecting small differences between groups(G3 and Medu).

The t. test for Null Hypothesis 1:

# Subset the data for different levels of Medu
G3_medu_0 <- my_data$G3[my_data$Medu == 0]
G3_medu_1 <- my_data$G3[my_data$Medu == 1]
G3_medu_2 <- my_data$G3[my_data$Medu == 2]
G3_medu_3 <- my_data$G3[my_data$Medu == 3]
G3_medu_4 <- my_data$G3[my_data$Medu == 4]

# Perform Welch's Two Sample t-tests
t_test_result_0_1 <- t.test(G3_medu_0, G3_medu_1)
t_test_result_0_2 <- t.test(G3_medu_0, G3_medu_2)
t_test_result_0_3 <- t.test(G3_medu_0, G3_medu_3)
t_test_result_0_4 <- t.test(G3_medu_0, G3_medu_4)

t_test_result_1_2 <- t.test(G3_medu_1, G3_medu_2)
t_test_result_1_3 <- t.test(G3_medu_1, G3_medu_3)
t_test_result_1_4 <- t.test(G3_medu_1, G3_medu_4)

t_test_result_2_3 <- t.test(G3_medu_2, G3_medu_3)
t_test_result_2_4 <- t.test(G3_medu_2, G3_medu_4)

t_test_result_3_4 <- t.test(G3_medu_3, G3_medu_4)

# Print the t-test results
print(t_test_result_0_1)
## 
##  Welch Two Sample t-test
## 
## data:  G3_medu_0 and G3_medu_1
## t = 2.0787, df = 2.3354, p-value = 0.1546
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -3.498781 12.142848
## sample estimates:
## mean of x mean of y 
## 13.000000  8.677966
print(t_test_result_0_2)
## 
##  Welch Two Sample t-test
## 
## data:  G3_medu_0 and G3_medu_2
## t = 1.5948, df = 2.214, p-value = 0.2398
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -4.785819 11.329508
## sample estimates:
## mean of x mean of y 
## 13.000000  9.728155
print(t_test_result_0_3)
## 
##  Welch Two Sample t-test
## 
## data:  G3_medu_0 and G3_medu_3
## t = 1.3135, df = 2.2216, p-value = 0.3082
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -5.344491 10.738431
## sample estimates:
## mean of x mean of y 
##  13.00000  10.30303
print(t_test_result_0_4)
## 
##  Welch Two Sample t-test
## 
## data:  G3_medu_0 and G3_medu_4
## t = 0.60785, df = 2.1414, p-value = 0.6015
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -6.985689  9.458972
## sample estimates:
## mean of x mean of y 
##  13.00000  11.76336
print(t_test_result_1_2)
## 
##  Welch Two Sample t-test
## 
## data:  G3_medu_1 and G3_medu_2
## t = -1.4404, df = 127.03, p-value = 0.1522
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -2.4929011  0.3925226
## sample estimates:
## mean of x mean of y 
##  8.677966  9.728155
print(t_test_result_1_3)
## 
##  Welch Two Sample t-test
## 
## data:  G3_medu_1 and G3_medu_3
## t = -2.2139, df = 127.71, p-value = 0.02861
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -3.0775011 -0.1726273
## sample estimates:
## mean of x mean of y 
##  8.677966 10.303030
print(t_test_result_1_4)
## 
##  Welch Two Sample t-test
## 
## data:  G3_medu_1 and G3_medu_4
## t = -4.5398, df = 109.63, p-value = 1.451e-05
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -4.432321 -1.738464
## sample estimates:
## mean of x mean of y 
##  8.677966 11.763359
print(t_test_result_2_3)
## 
##  Welch Two Sample t-test
## 
## data:  G3_medu_2 and G3_medu_3
## t = -0.88223, df = 199.73, p-value = 0.3787
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -1.8598074  0.7100575
## sample estimates:
## mean of x mean of y 
##  9.728155 10.303030
print(t_test_result_2_4)
## 
##  Welch Two Sample t-test
## 
## data:  G3_medu_2 and G3_medu_4
## t = -3.4514, df = 210.04, p-value = 0.0006741
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -3.197630 -0.872777
## sample estimates:
## mean of x mean of y 
##  9.728155 11.763359
print(t_test_result_3_4)
## 
##  Welch Two Sample t-test
## 
## data:  G3_medu_3 and G3_medu_4
## t = -2.4511, df = 201.77, p-value = 0.01509
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -2.6350832 -0.2855737
## sample estimates:
## mean of x mean of y 
##  10.30303  11.76336

For the first t-test (G3_medu_0 vs. G3_medu_1) with a p-value of 0.1546, since 0.1546 > 0.05, that means fail to reject the null hypothesis.

For the second t-test (G3_medu_0 vs. G3_medu_2) with a p-value of 0.2398, since 0.2398 > 0.05, that means fail to reject the null hypothesis.

For the third t-test (G3_medu_0 vs. G3_medu_3) with a p-value of 0.3082, since 0.3082 > 0.05, that means fail to reject the null hypothesis.

For the fourth t-test (G3_medu_0 vs. G3_medu_4) with a p-value of 0.6015, since 0.6015 > 0.05, that means fail to reject the null hypothesis.

For the fifth t-test (G3_medu_1 vs. G3_medu_2) with a p-value of 0.1522, since 0.1522 > 0.05, that means fail to reject the null hypothesis.

For the sixth t-test (G3_medu_1 vs. G3_medu_3) with a p-value of 0.02861, since 0.02861 ≤ 0.05, that means reject the null hypothesis.

For the seventh t-test (G3_medu_1 vs. G3_medu_4) with a p-value of 1.451e-05, since 1.451e-05 ≤ 0.05, that means reject the null hypothesis.

For the eighth t-test (G3_medu_2 vs. G3_medu_3) with a p-value of 0.3787, since 0.3787 > 0.05, that means fail to reject the null hypothesis.

For the ninth t-test (G3_medu_2 vs. G3_medu_4) with a p-value of 0.0006741, since 0.0006741 ≤ 0.05, that means reject the null hypothesis.

For the tenth t-test (G3_medu_3 vs. G3_medu_4) with a p-value of 0.01509, since 0.01509 ≤ 0.05, that means reject the null hypothesis.

In summary, will reject the null hypothesis and conclude a significant difference in means for the comparisons between G3_medu_1 vs. G3_medu_3, G3_medu_1 vs. G3_medu_4, G3_medu_2 vs. G3_medu_4, and G3_medu_3 vs. G3_medu_4. For the other comparisons, there is not enough evidence to conclude a significant difference in means.

The t. test for the Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their health status (health).

# Subset the data for different health groups
health_1 <- my_data$G3[my_data$health == 1]
health_2 <- my_data$G3[my_data$health == 2]
health_3 <- my_data$G3[my_data$health == 3]
health_4 <- my_data$G3[my_data$health == 4]

# Perform pairwise t-tests
t_test_1_2 <- t.test(health_1, health_2)
t_test_1_3 <- t.test(health_1, health_3)
t_test_1_4 <- t.test(health_1, health_4)
t_test_2_3 <- t.test(health_2, health_3)
t_test_2_4 <- t.test(health_2, health_4)
t_test_3_4 <- t.test(health_3, health_4)

# Print the t-test results
print(t_test_1_2)
## 
##  Welch Two Sample t-test
## 
## data:  health_1 and health_2
## t = 1.5918, df = 83.788, p-value = 0.1152
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.4113903  3.7116267
## sample estimates:
## mean of x mean of y 
##  11.87234  10.22222
print(t_test_1_3)
## 
##  Welch Two Sample t-test
## 
## data:  health_1 and health_3
## t = 2.4125, df = 89.914, p-value = 0.01787
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  0.3285218 3.3941810
## sample estimates:
## mean of x mean of y 
##  11.87234  10.01099
print(t_test_1_4)
## 
##  Welch Two Sample t-test
## 
## data:  health_1 and health_4
## t = 2.0228, df = 105.35, p-value = 0.04563
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  0.03495424 3.49760539
## sample estimates:
## mean of x mean of y 
##  11.87234  10.10606
print(t_test_2_3)
## 
##  Welch Two Sample t-test
## 
## data:  health_2 and health_3
## t = 0.22726, df = 69.999, p-value = 0.8209
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -1.642533  2.064999
## sample estimates:
## mean of x mean of y 
##  10.22222  10.01099
print(t_test_2_4)
## 
##  Welch Two Sample t-test
## 
## data:  health_2 and health_4
## t = 0.1144, df = 86.864, p-value = 0.9092
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -1.902167  2.134490
## sample estimates:
## mean of x mean of y 
##  10.22222  10.10606
print(t_test_3_4)
## 
##  Welch Two Sample t-test
## 
## data:  health_3 and health_4
## t = -0.12798, df = 126.91, p-value = 0.8984
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -1.565022  1.374879
## sample estimates:
## mean of x mean of y 
##  10.01099  10.10606

Interpret the results:

For the first t-test (health_1 vs. health_2) with a p-value of 0.1152, since 0.1152 > 0.05, that means fail to reject the null hypothesis.

For the second t-test (health_1 vs. health_3) with a p-value of 0.01787, since 0.01787 ≤ 0.05, that means reject the null hypothesis.

For the third t-test (health_1 vs. health_4) with a p-value of 0.04563, since 0.04563 ≤ 0.05, that means reject the null hypothesis.

Perform a Fisher’s style test for significance, and interpret the p-value for the Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their health status (health).

# Create a contingency table for G3 and health
contingency_table <- table(my_data$G3, my_data$health)

# Perform Fisher's exact test with simulation
fisher_test_result <- fisher.test(contingency_table, simulate.p.value = TRUE, B = 10000)

# Print the test result
print(fisher_test_result)
## 
##  Fisher's Exact Test for Count Data with simulated p-value (based on
##  10000 replicates)
## 
## data:  contingency_table
## p-value = 0.5301
## alternative hypothesis: two.sided

The output indicates the results of the Fisher’s exact test:

The p-value of 0.529 indicates that under the assumption that there is no true association between G3 and health.

Since this p-value (0.529) is greater than the commonly used significance level of 0.05, would fail to reject the null hypothesis. This means there is not enough evidence to conclude that there is a significant association between students’ final grades (G3) and their health status.

Perform a Fisher’s style test for significance, and interpret the p-value for the Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their mother eduction (Medu).

# Create a contingency table
contingency_table <- table(my_data$G3, my_data$Medu)

# Perform Fisher's exact test with simulation
fisher_test_result <- fisher.test(contingency_table, simulate.p.value = TRUE, B = 10000)

# Print the test result
print(fisher_test_result)
## 
##  Fisher's Exact Test for Count Data with simulated p-value (based on
##  10000 replicates)
## 
## data:  contingency_table
## p-value = 0.1392
## alternative hypothesis: two.sided

The output indicates the results of the Fisher’s exact test:

P-Value: The p-value associated with the test is 0.147.

Null Hypothesis (H0): The null hypothesis states that there is no significant difference in the final grades of students based on their mother’s education level.

Alternative Hypothesis: The alternative hypothesis (two-sided) suggests that there is a significant difference in the final grades of students based on their mother’s education level.

Interpretation: Since the p-value (0.147) is greater than the common significance level of 0.05, that means there is not enough evidence to reject the null hypothesis.

Therefore, based on this test, states that there is no significant difference in the final grades (G3) of students based on their mother’s education level (Medu).

Part 3: Build two visualizations that best illustrate the results from the two pairs of hypothesis tests, one for each null hypothesis.

# Box plot
ggplot(my_data, aes(x = as.factor(Medu), y = G3)) +
  geom_boxplot() +
  xlab("Mother Education Level") +
  ylab("Final Grades (G3)") +
  ggtitle("Distribution of Final Grades by Mother Education Level")

Based on the box plot, it appears that there is no substantial difference in the final grades (G3) of students based on their mother’s education level (Medu). For instance, in both the low and high levels of education, there are students with high G3 final grades, reaching up to 15 points. This suggests that maternal education level may not be a significant factor influencing students’ final grades.

# Box plot
ggplot(my_data, aes(x = as.factor(health), y = G3)) +
  geom_boxplot() +
  xlab("Health Status") +
  ylab("Final Grades (G3)") +
  ggtitle("Distribution of Final Grades by Health Status")

The box plot indicates that there is no significant variation in students’ final grades (G3) based on their health statuses. Both low and high health status categories exhibit students with high G3 scores, reaching up to 13 points, even in the case of minimum health status (1). This implies that health status may not play a significant role in determining students’ final grades.

Second, I will choose two null hypotheses based on the final grade (G3) and other factors: study time, absences, previous failures, and previous grades.

Null Hypothesis 1: Impact of Study Time (studytime) on Student Performance

Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their study time (studytime).

Alternative Hypothesis (H1): There is a significant difference in the final grades (G3) of students based on their study time (studytime).

Null Hypothesis 2: Impact of Absences (absences) on Student Performance

Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their number of absences (absences).

Alternative Hypothesis (H1): There is a significant difference in the final grades (G3) of students based on their number of absences (absences).

Null Hypothesis 3: Impact of Previous Failures (failures) on Student Performance

Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their number of previous failures (failures).

Alternative Hypothesis (H1): There is a significant difference in the final grades (G3) of students based on their number of previous failures (failures).

Null Hypothesis 4: Impact of Previous Grades (G1 and G2) on Student Performance

Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their previous grades (G1 and G2).

Alternative Hypothesis (H1): There is a significant difference in the final grades (G3) of students based on their previous grades (G1 and G2).

These hypotheses aim to explore the influence of study time, absences, previous failures, and previous grades on students’ final grades. The null hypotheses state that there is no effect, while the alternative hypotheses suggest there is a significant impact.

Come up with an alpha level, power level, and minimum effect size, and explain why you chose each value:

For Null Hypothesis 1:

Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their study time (studytime).

Alternative Hypothesis (H1): There is a significant difference in the final grades (G3) of students based on their study time (studytime).

Alpha Level (α): will choose alpha level of 0.05, because that means willing to accept a 5% chance of making a Type I error (incorrectly rejecting a true null hypothesis).

Power Level (1 - β): will choose a power level of 0.80, beacuse it is indicating an 80% chance of detecting a real effect. Will set 1 - β = 0.80.

Minimum Effect Size: a 1-hour increase in study time per week is significant for student performance, then the minimum effect size is 1 hours. Because that means a 1-hour increase in study time is considered practically significant and worth detecting.

For Null Hypothesis 4:

Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their previous grades (G1 and G2).

Alternative Hypothesis (H1): There is a significant difference in the final grades (G3) of students based on their previous grades (G1 and G2).

Alpha Level (α): will choose α = 0.05.The alpha level (α) represents the probability of making a Type I error, which is the chance of incorrectly rejecting a true null hypothesis.When Setting α = 0.05 indicates a 5% risk of making this error.

Power Level (1 - β): will choose 1 - β = 0.80. A power level of 0.80 means there is an 80% probability of detecting a true effect. This value provides a good balance between detecting the effects and the risk of Type II errors.

Minimum Effect Size: a 1-point increase in the average of G1 and G2 is significant, then the minimum effect size is 5 points.It determined that a 1-point increase in the previous grades (G1 and G2) is meaningful and relevant to research question.

count_G3 <- my_data %>%
  group_by(studytime) %>%
  summarize(count_G3 = n()) %>%
  arrange(studytime)

count_G3
## # A tibble: 4 × 2
##   studytime count_G3
##       <int>    <int>
## 1         1      105
## 2         2      198
## 3         3       65
## 4         4       27
count_G3 <- my_data %>%
  group_by(failures) %>%
  summarize(count_G3 = n()) %>%
  arrange(failures)

count_G3
## # A tibble: 4 × 2
##   failures count_G3
##      <int>    <int>
## 1        0      312
## 2        1       50
## 3        2       17
## 4        3       16

For Null Hypothesis 1: Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their study time (studytime).

Determine if you have enough data to perform a Neyman-Pearson hypothesis test. If you do, perform one and interpret results. If not, explain why.

# Subset the data for different levels of studytime:
G3_studytime_1 <- my_data$G3[my_data$studytime == 1]
G3_studytime_2 <- my_data$G3[my_data$studytime == 2]
G3_studytime_3 <- my_data$G3[my_data$studytime == 3]
G3_studytime_4 <- my_data$G3[my_data$studytime == 4]

# Perform Welch's Two Sample t-tests for study time groups
t_test_result_1_2 <- t.test(G3_studytime_1, G3_studytime_2)
t_test_result_1_3 <- t.test(G3_studytime_1, G3_studytime_3)
t_test_result_1_4 <- t.test(G3_studytime_1, G3_studytime_4)

t_test_result_2_3 <- t.test(G3_studytime_2, G3_studytime_3)
t_test_result_2_4 <- t.test(G3_studytime_2, G3_studytime_4)

t_test_result_3_4 <- t.test(G3_studytime_3, G3_studytime_4)

# Print the t-test results
print(t_test_result_1_2)
## 
##  Welch Two Sample t-test
## 
## data:  G3_studytime_1 and G3_studytime_2
## t = -0.21809, df = 184.82, p-value = 0.8276
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -1.2467170  0.9985207
## sample estimates:
## mean of x mean of y 
##  10.04762  10.17172
print(t_test_result_1_3)
## 
##  Welch Two Sample t-test
## 
## data:  G3_studytime_1 and G3_studytime_3
## t = -1.799, df = 142.58, p-value = 0.07413
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -2.8383682  0.1336063
## sample estimates:
## mean of x mean of y 
##  10.04762  11.40000
print(t_test_result_1_4)
## 
##  Welch Two Sample t-test
## 
## data:  G3_studytime_1 and G3_studytime_4
## t = -1.0764, df = 38.615, p-value = 0.2884
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -3.489111  1.065831
## sample estimates:
## mean of x mean of y 
##  10.04762  11.25926
print(t_test_result_2_3)
## 
##  Welch Two Sample t-test
## 
## data:  G3_studytime_2 and G3_studytime_3
## t = -1.8931, df = 101.02, p-value = 0.06121
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -2.51539929  0.05883363
## sample estimates:
## mean of x mean of y 
##  10.17172  11.40000
print(t_test_result_2_4)
## 
##  Welch Two Sample t-test
## 
## data:  G3_studytime_2 and G3_studytime_4
## t = -1.0263, df = 30.688, p-value = 0.3128
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -3.249608  1.074524
## sample estimates:
## mean of x mean of y 
##  10.17172  11.25926
print(t_test_result_3_4)
## 
##  Welch Two Sample t-test
## 
## data:  G3_studytime_3 and G3_studytime_4
## t = 0.1205, df = 43.524, p-value = 0.9046
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -2.213899  2.495380
## sample estimates:
## mean of x mean of y 
##  11.40000  11.25926

G3_studytime_1 vs. G3_studytime_2: t = -0.21809 p-value = 0.8276 The p-value (0.8276) is higher than the significance level (usually 0.05), indicating no significant difference in final grades between these study time categories.

G3_studytime_1 vs. G3_studytime_3: t = -1.799 p-value = 0.07413 The p-value (0.07413) is slightly above 0.05, suggesting weak evidence against the null hypothesis.

G3_studytime_1 vs. G3_studytime_4: t = -1.0764 p-value = 0.2884 The p-value (0.2884) is higher than 0.05, indicating no significant difference in final grades between these study time categories.

G3_studytime_2 vs. G3_studytime_3: t = -1.8931 p-value = 0.06121 The p-value (0.06121) is slightly above 0.05, suggesting weak evidence against the null hypothesis.

G3_studytime_2 vs. G3_studytime_4: t = -1.0263 p-value = 0.3128 The p-value (0.3128) is higher than 0.05, indicating no significant difference in final grades between these study time categories.

G3_studytime_3 vs. G3_studytime_4: t = 0.1205 p-value = 0.9046 The p-value (0.9046) is much higher than 0.05, indicating no significant difference in final grades between these study time categories.

In summary, based on these t-tests and the chosen significance level, there is only weak evidence against the null hypothesis in the comparison between G3_studytime_1 and G3_studytime_3, as well as G3_studytime_2 and G3_studytime_3, in all there is no significant difference in final grades between the study time categories.

For Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their number of previous failures (failures).

Determine if you have enough data to perform a Neyman-Pearson hypothesis test. If you do, perform one and interpret results. If not, explain why.

# Subset the data for different levels of failures:
G3_failures_0 <- my_data$G3[my_data$failures == 0]
G3_failures_1 <- my_data$G3[my_data$failures == 1]
G3_failures_2 <- my_data$G3[my_data$failures == 2]
G3_failures_3 <- my_data$G3[my_data$failures == 3]

# Perform Welch's Two Sample t-tests for failures groups
t_test_result_0_1 <- t.test(G3_failures_0, G3_failures_1)
t_test_result_0_2 <- t.test(G3_failures_0, G3_failures_2)
t_test_result_0_3 <- t.test(G3_failures_0, G3_failures_3)

t_test_result_1_2 <- t.test(G3_failures_1, G3_failures_2)
t_test_result_1_3 <- t.test(G3_failures_1, G3_failures_3)
t_test_result_2_3 <- t.test(G3_failures_2, G3_failures_3)

# Print the t-test results
print(t_test_result_0_1)
## 
##  Welch Two Sample t-test
## 
## data:  G3_failures_0 and G3_failures_1
## t = 4.4332, df = 61.924, p-value = 3.869e-05
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  1.720375 4.546035
## sample estimates:
## mean of x mean of y 
##  11.25321   8.12000
print(t_test_result_0_2)
## 
##  Welch Two Sample t-test
## 
## data:  G3_failures_0 and G3_failures_2
## t = 4.1894, df = 17.318, p-value = 0.0005937
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  2.494398 7.541424
## sample estimates:
## mean of x mean of y 
## 11.253205  6.235294
print(t_test_result_0_3)
## 
##  Welch Two Sample t-test
## 
## data:  G3_failures_0 and G3_failures_3
## t = 5.1824, df = 16.56, p-value = 8.14e-05
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  3.295258 7.836152
## sample estimates:
## mean of x mean of y 
##  11.25321   5.68750
print(t_test_result_1_2)
## 
##  Welch Two Sample t-test
## 
## data:  G3_failures_1 and G3_failures_2
## t = 1.396, df = 27.042, p-value = 0.1741
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.8852026  4.6546144
## sample estimates:
## mean of x mean of y 
##  8.120000  6.235294
print(t_test_result_1_3)
## 
##  Welch Two Sample t-test
## 
## data:  G3_failures_1 and G3_failures_3
## t = 1.9592, df = 28.171, p-value = 0.06005
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.110001  4.975001
## sample estimates:
## mean of x mean of y 
##    8.1200    5.6875
print(t_test_result_2_3)
## 
##  Welch Two Sample t-test
## 
## data:  G3_failures_2 and G3_failures_3
## t = 0.34809, df = 30.796, p-value = 0.7301
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -2.662660  3.758249
## sample estimates:
## mean of x mean of y 
##  6.235294  5.687500

Based on the provided Welch Two Sample t-tests for different levels of failures (failures 0, 1, 2, and 3) and their corresponding final grades (G3).

** interpret the results for each comparison:

G3_failures_0 vs. G3_failures_1: t = 4.4332, p-value = 3.869e-05 The p-value is significantly less than 0.05, indicating strong evidence against the null hypothesis. We reject the null hypothesis and conclude that there is a significant difference in mean final grades between students with 0 failures and students with 1 failure.

G3_failures_0 vs. G3_failures_2: t = 4.1894, p-value = 0.0005937

The p-value is significantly less than 0.05, indicating strong evidence against the null hypothesis. We reject the null hypothesis and conclude that there is a significant difference in mean final grades between students with 0 failures and students with 2 failures.

G3_failures_0 vs. G3_failures_3: t = 5.1824, p-value = 8.14e-05

The p-value is significantly less than 0.05, indicating strong evidence against the null hypothesis. We reject the null hypothesis and conclude that there is a significant difference in mean final grades between students with 0 failures and students with 3 failures.

G3_failures_1 vs. G3_failures_2: t = 1.396, p-value = 0.1741 The p-value is higher than 0.05, indicating lack of strong evidence against the null hypothesis. We do not reject the null hypothesis, suggesting no significant difference in mean final grades between students with 1 failure and students with 2 failures.

G3_failures_1 vs. G3_failures_3: t = 1.9592, p-value = 0.06005 The p-value is slightly higher than 0.05 ,it might suggest a potential difference in mean finalgrades between students with 1 failure and students with 3 failures, but it’s not strongly significant.

G3_failures_2 vs. G3_failures_3: t = 0.34809, p-value = 0.7301 The p-value is significantly higher than 0.05, indicating lack of evidence against the null hypothesis. We do not reject the null hypothesis, suggesting no significant difference in mean final grades between students with 2 failures and students with 3 failures.

In summary, based on the provided t-tests, there is strong evidence to suggest significant differences in mean final grades between students with 0 failures and those with 1, 2, or 3 failures. The comparison between students with 1 failure and 3 failures shows a result that is close to the significance threshold.

Perform a Fisher’s style test for significance, and interpret the p-value.For Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their number of previous failures (failures).

# Create a contingency table
contingency_table <- table(my_data$G3, my_data$studytime)

# Perform Fisher's exact test with simulation
fisher_test_result <- fisher.test(contingency_table, simulate.p.value = TRUE, B = 10000)

# Print the test result
print(fisher_test_result)
## 
##  Fisher's Exact Test for Count Data with simulated p-value (based on
##  10000 replicates)
## 
## data:  contingency_table
## p-value = 0.303
## alternative hypothesis: two.sided

Output Interpretation:

p-value = 0.2964:suggests there is no true association between ‘G3’ and ‘study time’.

In summary, the result suggests that there is no significant association between the final grades (G3) and the different levels of study time.

Perform a Fisher’s style test for significance, and interpret the p-value. For Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their number of previous failures (failures).

# Create a contingency table
contingency_table <- table(my_data$G3, my_data$failures)

# Perform Fisher's exact test with simulation
fisher_test_result <- fisher.test(contingency_table, simulate.p.value = TRUE, B = 10000)

# Print the test result
print(fisher_test_result)
## 
##  Fisher's Exact Test for Count Data with simulated p-value (based on
##  10000 replicates)
## 
## data:  contingency_table
## p-value = 9.999e-05
## alternative hypothesis: two.sided

Output Interpretation:

p-value = 9.999e-05: In this case, the p-value is extremely small (9.999e-05 or 0.00009999). This small p-value indicates that there is very strong evidence against the null hypothesis.

In summary, the test results suggest that there is a significant relationship between the final grades of students and the number of failures they have. Students with different numbers of failures tend to have significantly different final grades, based on the data analysis.

Part 3: Build two visualizations that best illustrate the results from the two pairs of hypothesis tests, one for each null hypothesis.

Visualization for Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their number of previous failures (failures).

# Box plot for G3 and failures
ggplot(my_data, aes(x = as.factor(failures), y = G3)) +
  geom_boxplot() +
  xlab("Number of Failures") +
  ylab("Final Grades (G3)") +
  ggtitle("Distribution of Final Grades by Number of Failures")

The box plot reveals a clear and significant relationship between students’ final grades and the number of failures they have. Students with no failures consistently achieve higher final grades, whereas those with more failures tend to attain lower final grades.

Visualization for Null Hypothesis (H0): There is no significant difference in the final grades (G3) of students based on their study time (studytime).

# Box plot based on G3 and studytime
ggplot(my_data, aes(x = as.factor(studytime), y = G3)) +
  geom_boxplot() +
  xlab("Study Time") +
  ylab("Final Grades (G3)") +
  ggtitle("Distribution of Final Grades by Study Time")

The box plot shows no significant association between final grades (G3) and the different levels of study time. The results are very similar across students with varying study hours, indicating that the time spent studying does not strongly correlate with their final grade