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(AmesHousing)
library(boot)
library(broom)
library(lindia)
my_data <- read.csv("/Users/shemaaeltayeb/Desktop/STATISTIC IN R /student_data.csv")
In my dataset, the column that seems most appropriate to be the response variable would be G3: Final grade (Numeric). This column represents the students’ final grades, which is a key indicator of their academic performance and outcomes. Analyzing the factors of these final grades could provide valuable insights into the students’ performance and the impact of various attributes and circumstances on their academic achievements.
a categorical column that might influence the students’ final grades (the response variable, G3) could be :
1- The ‘school’ column indicates the name of the school attended by the students and has two categories: ‘GP’ (Gabriel Pereira) and ‘MS’ (Mousinho da Silveira). The student attends specific scool may have an impact on their academic performance. Different schools may have varying teaching methods, resources, and overall learning environments, which could influence students’ grades.
2- ‘address’ is another categorical column in the dataset that could be an explanatory variable. The ‘address’ column represents the type of student’s home address and has two categories: ‘U’ (urban) and ‘R’ (rural). This variable could potentially influence students’ academic performance. Students living in urban and rural areas might have different access to educational resources, extracurricular activities, and support systems, which could impact their grades. For example, urban areas typically have better access to libraries, tutoring centers, and cultural activities, which might positively affect students’ performance. On the other hand, students in rural areas might face challenges related to transportation and access to educational facilities.
** Null Hypothesis (H0): There is no significant difference in the mean final grades (G3) among students from different address: ‘U’ for urban and ‘R’ for rural.
# Perform one-way ANOVA for 'address'
anova_result <- aov(G3 ~ address, data=my_data)
# Print ANOVA summary
summary(anova_result)
## Df Sum Sq Mean Sq F value Pr(>F)
## address 1 92 92.49 4.445 0.0356 *
## Residuals 393 8177 20.81
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The output represents the results of the ANOVA test conducted on the ‘address’ variable, which compares the mean final grades (G3) between students from urban and rural areas.
Df (Degrees of Freedom): there are 1 degree of freedom for the ‘address’ variable, indicating there are two categories (‘U’ for urban and ‘R’ for rural).
Sum Sq (Sum of Squares): The total sum of squares is 92, indicating the total variability in G3 scores.
Mean Sq (Mean Sum of Squares): Mean sum of squares is the average variance between groups (address) and within groups (residuals). For the ‘address’ variable, it is 92.49.
F value (F-statistic): The F-statistic is a ratio of the variances between the groups and within the groups. A larger F value suggests that the means of the groups are more different relative to the variability within each group.
Pr(>F) (p-value): This is the p-value associated with the F-statistic. The p-value (Pr(>F)) is 0.0356, which is less than 0.05. Therefore, we reject the null hypothesis at the 0.05 significance level. This means there is enough evidence to suggest that there is a significant difference in the mean final grades between students from urban and rural areas.
In summary, based on ANOVA test result, we concluded that there is a statistically significant difference in the mean final grades of students between urban and rural areas.
** Null Hypothesis (H0): There is no significant difference in the mean final grades (G3) among students from different schools (school: ‘GP’ - Gabriel Pereira and ‘MS’ - Mousinho da Silveira).
# Perform one-way ANOVA for 'School'
anova_result <- aov(G3 ~ school, data=my_data)
# Print ANOVA summary
summary(anova_result)
## Df Sum Sq Mean Sq F value Pr(>F)
## school 1 17 16.76 0.798 0.372
## Residuals 393 8253 21.00
ANOVA test conducted on the ‘school’ variable:
Df (Degrees of Freedom): there are two schools (‘GP’ and ‘MS’), so there is one degree of freedom associated with ‘school’.
Sum Sq (Sum of Squares): The total sum of squares for ‘school’ is 17.
Mean Sq (Mean Sum of Squares):For the ‘school’ variable, it is 16.76.
F value (F-statistic): F-value is 0.798, which is calculated by dividing the mean square between groups by the mean square within groups.
Pr(>F) (p-value): This is the p-value associated with the F-statistic. It indicates the probability of observing the data given that the null hypothesis (no difference in mean final grades between schools) is true. In this result, the p-value is 0.372.
The p-value (0.372) is greater than the typical significance level of 0.05. In hypothesis testing, if the p-value is greater than the significance level, you fail to reject the null hypothesis. In this result, it means that there is no significant difference in the mean final grades between the two schools (‘GP’ and ‘MS’). The F-statistic of 0.798 also supports this, as it is close to 1, indicating that the variance between the schools is not significantly different from the variance within the schools.
In summary, based on ANOVA test, there is no significant difference in the mean final grades between students from different schools (‘GP’ and ‘MS’).
‘Mjob’ (Mother’s job) and ‘Fjob’ (Father’s job) are interesting variables to explore using an F-test (ANOVA). These columns provide information about the parents’ occupations, which could potentially influence students’ academic performance and choices.
step 1 : Null Hypothesis: There is no significant difference in the mean final grades (G3) among students whose Father have different types of jobs (e.g., ‘teacher’, ‘healthcare’, ‘services’, ‘at_home’, etc.).
# Create a boxplot for G3 grouped by consolidated Fjob categories
ggplot(my_data, aes(y = G3, x = Fjob)) +
geom_boxplot() +
labs(x = "Father's Job Groups",
y = "Final Grades (G3)") +
theme_minimal()
my_data |>
group_by(Fjob) |>
summarise(mean_Final_Grade=mean(G3),
sd_Final_Grade=sd(G3))
## # A tibble: 5 × 3
## Fjob mean_Final_Grade sd_Final_Grade
## <chr> <dbl> <dbl>
## 1 at_home 10.2 5.32
## 2 health 11.6 3.24
## 3 other 10.2 4.51
## 4 services 10.3 4.48
## 5 teacher 12.0 5.49
Step 2, Perform ANOVA test
# Perform ANOVA test
anova_result <- aov(G3 ~ Fjob, data = my_data)
# Summarize ANOVA results
summary(anova_result)
## Df Sum Sq Mean Sq F value Pr(>F)
## Fjob 4 109 27.26 1.303 0.268
## Residuals 390 8161 20.93
Based on the ANOVA output so we can interpret the results:
Df: Degrees of freedom : In this case, there are 4 degrees of freedom for ‘Fjob’, indicating there are 5 categories (father’s job types) in total.
Sum Sq: Sum of squares : It measures the total variability in the data. For ‘Fjob’, it is 109, indicating the total variability explained by the father’s job categories.
Mean Sq: Mean square : For ‘Fjob’, it is 27.26. For residuals, it is 20.93.
F value: The test statistic for ANOVA. It compares the variance between groups to the variance within groups. In this case, the F value is 1.303.
Pr(>F) : Since the p-value is greater than 0.05, that means we do not have enough evidence to reject the null hypothesis.
In summary, the ANOVA test did not find a significant difference in mean final grades based on father’s job categories in the data.
Step 3, will d pairwise t-tests to compare each group (rows) to each other group (columns).
pairwise.t.test(my_data$G3, my_data$Fjob, p.adjust.method = "bonferroni")
##
## Pairwise comparisons using t tests with pooled SD
##
## data: my_data$G3 and my_data$Fjob
##
## at_home health other services
## health 1.00 - - -
## other 1.00 1.00 - -
## services 1.00 1.00 1.00 -
## teacher 1.00 1.00 0.51 0.81
##
## P value adjustment method: bonferroni
From the result:
For the ‘teacher’ category: There is no significant difference in mean final grades between ‘teacher’ and any of the other categories.
For the other categories (‘at_home’, ‘health’, ‘other’, ‘services’): There is no significant difference in mean final grades between these categories.
In summary, based on the Bonferroni-adjusted pairwise comparisons, none of the father’s job categories (‘at_home’, ‘health’, ‘other’, ‘services’, ‘teacher’) have statistically significant differences in mean final grades compared to each other in the dataset.
The “failures” column represents the number of past class failures for each student, which is an ordered integer variable, this column to explore its relationship with the response variable, “G3,” to determine if there is a linear trend or any correlation between the number of past failures and the final grade.
# Build a linear regression model using failures as the predictor and G3 as the response
model <- lm(G3 ~ failures, data = my_data)
# Print the summary of the linear regression model
summary(model)
##
## Call:
## lm(formula = G3 ~ failures, data = my_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.1572 -2.1572 -0.1572 2.8428 9.0632
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 11.1572 0.2361 47.26 < 2e-16 ***
## failures -2.2204 0.2899 -7.66 1.47e-13 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.279 on 393 degrees of freedom
## Multiple R-squared: 0.1299, Adjusted R-squared: 0.1277
## F-statistic: 58.67 on 1 and 393 DF, p-value: 1.466e-13
The intercept represents when a student has 0 past failures, the predicted final grade is 11.1572.
The coefficient for the “failures” variable is -2.2204. This indicates that for every additional past failure a student has, their final grade (G3) is expected to decrease by approximately 2.2204 points.
The negative coefficient for “failures” suggests that as the number of past failures increases, the final grade tends to decrease.
Low p-values (p < 0.001). This means that the relationship between past failures and final grade is unlikely to be due to random chance.
The R-squared value of 0.1299 indicates that approximately 12.99% of the variance in the final grades can be explained by the number of past failures.
In this case, the F-statistic is 58.67 with a very low p-value (p < 0.001), indicating the model is significant.
Overall, the model suggests that the number of past failures is a statistically significant predictor of the final grade, and an increase in past failures is associated with a decrease in the final grade on average.
my_data|>
ggplot(mapping = aes(x = failures, y = G3)) +
geom_point(size = 2) +
geom_smooth(method = "lm", se = FALSE, color = 'darkblue') +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
For every additional failure a student has, their final grade is expected to decrease by approximately 2.22 points.
Recommendation:
Based on this model, that means the number of failures has a significant negative impact on students’ final grades. To improve final grades, efforts should be focused on reducing the number of failures. This might involve additional support, tutoring, or interventions for students who are struggling academically.
# Including 'failures', 'famsup', 'activities', and their interaction term
model <- lm(G3 ~ failures * famsup * activities , data = my_data)
# Summary of the updated model
summary(model)
##
## Call:
## lm(formula = G3 ~ failures * famsup * activities, data = my_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -12.2959 -2.1826 0.5622 2.8174 9.6701
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 12.295874 0.559590 21.973 < 2e-16 ***
## failures -2.113257 0.545239 -3.876 0.000125 ***
## famsupyes -1.858033 0.701451 -2.649 0.008407 **
## activitiesyes -1.409583 0.768019 -1.835 0.067221 .
## failures:famsupyes 0.005283 0.750591 0.007 0.994388
## failures:activitiesyes -0.847227 0.930201 -0.911 0.362968
## famsupyes:activitiesyes 2.401580 0.970636 2.474 0.013780 *
## failures:famsupyes:activitiesyes 0.616058 1.208090 0.510 0.610382
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.25 on 387 degrees of freedom
## Multiple R-squared: 0.1549, Adjusted R-squared: 0.1396
## F-statistic: 10.13 on 7 and 387 DF, p-value: 1.167e-11
In regression model, it included the variables ‘failures,’ ‘family support’ (famsup), and ‘extracurricular activities’ (activities) along with their interactions.
To interpret the results:
Intercept (12.295874): When all predictor variables are at 0, the expected final grade (G3) is approximately 12.3. Failures (-2.113257): For every additional failure, the final grade is expected to decrease by approximately 2.11 points. This effect is statistically significant (p-value < 0.05).
Family Support (famsupyes, -1.858033): If a student receives family support, their final grade is expected to decrease by approximately 1.86 points, but this effect is statistically significant (p-value < 0.05).
Extracurricular Activities (activitiesyes, -1.409583): If a student participates in extracurricular activities, their final grade is expected to decrease by approximately 1.41 points. However, this effect is marginally significant (p-value = 0.067), suggesting a weak relationship.
Interactions: The interaction between failures and family support is not statistically significant, indicating that the combined effect of failures and family support doesn’t significantly impact final grades.
The interaction between failures and extracurricular activities is not statistically significant, indicating that the combined effect of failures and participation in activities doesn’t significantly impact final grades.
The interaction between family support and extracurricular activities is statistically significant. If a student receives family support and participates in activities, their final grade is expected to increase by approximately 2.40 points. This suggests a positive synergistic effect on grades for students with family support who also participate in extracurricular activities.
The triple interaction is not statistically significant, indicating that the combined effect of failures, family support, and extracurricular activities doesn’t significantly impact final grades.
Failures and family support have significant individual effects on final grades.
Participation in extracurricular activities has a marginally significant negative effect on final grades.
There is a positive effect on grades for students with family support who also participate in extracurricular activities.