The purpose of this project is to examine how student study habits, class attendance, and earlier grading-period scores relate to final performance in high school mathematics. This analysis is intended for secondary school teachers, academic advisors, and school counselors who want to identify students who may need extra academic support before the end of the school year. Specifically, this project addresses two main research questions: 1) How do weekly study time and class absences relate to students’ final mathematics grades? and 2) How strongly do first- and second-period grades predict final course performance?
To answer these questions, I am using the Student Performance dataset
(student-mat.csv), which was collected during the 2005-2006
school year from two public secondary schools in Portugal (Gabriel
Pereira and Mousinho da Silveira) using official school grade reports
and student questionnaires (Cortez & Silva, 2008). The full dataset
includes 395 students and 33 variables covering student demographics,
family background, school habits, and grades. The dataset is publicly
available from the UCI Machine Learning Repository (https://archive.ics.uci.edu/dataset/320/student+performance).
The download contains two files, one for mathematics (student-mat.csv,
395 students) and one for Portuguese language (student-por.csv, 649
students). This project uses only the mathematics file.For this
analysis, I focused on five variables directly related to my research
questions: first-period grade (G1), second-period grade
(G2), final course grade (G3), weekly study
time (studytime), and total school absences
(absences). All three grade variables are measured on a 0
to 20 scale, while studytime is recorded across four weekly
time brackets.
# 1. Load the tidyverse package
library(tidyverse)
# 2. Load the dataset (using delim = ";" because this file uses semicolons)
student_data <- read_delim("student-mat.csv", delim = ";")
# 3. Check the size of the full dataset
dim(student_data)
## [1] 395 33
# 4. Select the columns needed and convert studytime into a labeled factor
student_subset <- student_data %>%
select(G1, G2, G3, studytime, absences) %>%
mutate(
studytime = factor(
studytime,
levels = c(1, 2, 3, 4),
labels = c("<2 hours", "2-5 hours", "5-10 hours", ">10 hours")
)
)
# 5. Inspect the cleaned subset
glimpse(student_subset)
## Rows: 395
## Columns: 5
## $ G1 <dbl> 5, 5, 7, 15, 6, 15, 12, 6, 16, 14, 10, 10, 14, 10, 14, 14, 1…
## $ G2 <dbl> 6, 5, 8, 14, 10, 15, 12, 5, 18, 15, 8, 12, 14, 10, 16, 14, 1…
## $ G3 <dbl> 6, 6, 10, 15, 10, 15, 11, 6, 19, 15, 9, 12, 14, 11, 16, 14, …
## $ studytime <fct> 2-5 hours, 2-5 hours, 2-5 hours, 5-10 hours, 2-5 hours, 2-5 …
## $ absences <dbl> 6, 4, 10, 2, 4, 10, 0, 6, 0, 0, 0, 4, 2, 2, 0, 4, 6, 4, 16, …
Before creating summary statistics or visualizations, I performed
several preprocessing steps to prepare the dataset for analysis. First,
because the CSV file uses semicolons rather than commas to separate
columns, I specified delim = ";" when importing the file so
that all 33 columns read in properly. Next, I narrowed the data down to
the five variables of interest so the dataset would be easier to work
with and less cluttered. I also transformed studytime from
a numeric code (1 through 4) into a labeled
factor ("<2 hours", "2-5 hours",
"5-10 hours", and ">10 hours"). This
transformation was necessary so that R and ggplot2 treat
study time as four distinct categories rather than a continuous
numerical scale. Finally, I checked for missing values and duplicate
rows across the dataset, confirming that there are 0 missing values in
my subset and 0 duplicate student records.
# 1. Check for missing values (NAs) in the subset
colSums(is.na(student_subset))
## G1 G2 G3 studytime absences
## 0 0 0 0 0
# 2. Check for duplicate rows across the full dataset
sum(duplicated(student_data))
## [1] 0
# 3. Check students who scored 0 on the final exam (G3 == 0)
student_subset %>%
filter(G3 == 0) %>%
summarize(
count = n(),
min_G1 = min(G1),
max_G1 = max(G1),
max_absences = max(absences)
)
## # A tibble: 1 × 4
## count min_G1 max_G1 max_absences
## <int> <dbl> <dbl> <dbl>
## 1 38 4 12 0
# 4. Check the distribution and highest values of absences
summary(student_subset$absences)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.000 0.000 4.000 5.709 8.000 75.000
student_subset %>%
filter(absences > 20) %>%
arrange(desc(absences))
## # A tibble: 15 × 5
## G1 G2 G3 studytime absences
## <dbl> <dbl> <dbl> <fct> <dbl>
## 1 10 9 9 2-5 hours 75
## 2 9 9 8 2-5 hours 56
## 3 11 12 11 2-5 hours 54
## 4 13 11 11 5-10 hours 40
## 5 8 9 8 <2 hours 38
## 6 8 8 8 <2 hours 30
## 7 10 9 9 5-10 hours 28
## 8 7 6 6 2-5 hours 26
## 9 7 10 11 2-5 hours 25
## 10 18 18 18 <2 hours 24
## 11 13 13 13 2-5 hours 23
## 12 6 6 4 2-5 hours 22
## 13 9 9 9 <2 hours 22
## 14 13 10 11 2-5 hours 22
## 15 17 18 18 2-5 hours 21
During the wrangling process, I also checked for unusual values in
the grade and absence columns to decide whether any rows should be
excluded. Filtering for students with a final grade of 0
(G3 == 0) showed 38 students in this category. All 38 of
these students had non-zero scores in the first grading period
(G1 scores ranging from 4 to 12, including several students
with passing scores of 10 or higher), and every single one of them had 0
recorded absences. Rather than representing students who attended every
class and scored zero points, this pattern likely indicates students who
dropped the course or did not take the final exam. Similarly, checking
the distribution of absences showed a median of 4 absences,
but 15 students had more than 20 absences, reaching a maximum of 75.
Looking at those 15 students individually revealed that high absences do
not always lead to failing grades, as two students with 21 and 24
absences still earned final grades of 18. I chose not to remove the
G3 == 0 group or the high-absence outliers during data
cleaning because excluding them would hide important patterns in the
student population, so I kept all 395 observations and will account for
these cases when interpreting my charts.
To answer both research questions, I calculated summary statistics, pass rates (G3 >= 10), and correlation values across the full dataset (N = 395) as well as the subset of active exam-takers (G3 > 0, n = 357). Separating the 38 students with a final grade of 0 helps prevent likely course dropouts from skewing the attendance and grade patterns.
# Flag G3 == 0 records for dual-group comparison
student_analysis <- student_subset %>%
mutate(
completion_status = if_else(G3 == 0, "G3 = 0 (n = 38)", "G3 > 0 (n = 357)"),
absence_bracket = cut(
absences,
breaks = c(-Inf, 0, 5, 15, Inf),
labels = c("0 absences", "1-5 absences", "6-15 absences", ">15 absences")
)
)
# Summary 1: Final grade by weekly study time (All vs. G3 > 0)
studytime_summary <- student_analysis %>%
group_by(studytime) %>%
summarize(
n_all = n(),
mean_G3_all = round(mean(G3), 2),
median_G3_all = median(G3),
pass_rate_all = round(mean(G3 >= 10) * 100, 1),
n_active = sum(G3 > 0),
mean_G3_active = round(mean(G3[G3 > 0]), 2),
median_G3_active = median(G3[G3 > 0]),
.groups = "drop"
)
knitr::kable(studytime_summary, caption = "Final Grade (G3) Summary by Weekly Study Time")
| studytime | n_all | mean_G3_all | median_G3_all | pass_rate_all | n_active | mean_G3_active | median_G3_active |
|---|---|---|---|---|---|---|---|
| <2 hours | 105 | 10.05 | 10 | 64.8 | 92 | 11.47 | 11.0 |
| 2-5 hours | 198 | 10.17 | 11 | 64.6 | 182 | 11.07 | 11.0 |
| 5-10 hours | 65 | 11.40 | 12 | 75.4 | 59 | 12.56 | 12.0 |
| >10 hours | 27 | 11.26 | 12 | 74.1 | 24 | 12.67 | 12.5 |
# Summary 2: Final grade by absence bracket
absence_summary <- student_analysis %>%
group_by(absence_bracket) %>%
summarize(
n_all = n(),
mean_G3_all = round(mean(G3), 2),
n_active = sum(G3 > 0),
mean_G3_active = round(mean(G3[G3 > 0]), 2),
median_G3_active = median(G3[G3 > 0]),
.groups = "drop"
)
knitr::kable(absence_summary, caption = "Final Grade (G3) Summary by Absence Bracket")
| absence_bracket | n_all | mean_G3_all | n_active | mean_G3_active | median_G3_active |
|---|---|---|---|---|---|
| 0 absences | 115 | 8.44 | 77 | 12.61 | 12 |
| 1-5 absences | 134 | 11.65 | 134 | 11.65 | 11 |
| 6-15 absences | 113 | 11.07 | 113 | 11.07 | 11 |
| >15 absences | 33 | 10.03 | 33 | 10.03 | 10 |
# Summary 3: Correlations with Final Grade (G3)
# Uses Pearson's r for continuous variables and Spearman's rho for ordinal studytime
active_students <- student_analysis %>% filter(G3 > 0)
cor_table <- tibble(
Predictor = c(
"G1 (First Period, Pearson r)",
"G2 (Second Period, Pearson r)",
"Absences (Pearson r)",
"Study Time (1-4 Bracket, Spearman rho)"
),
r_All_395 = round(c(
cor(student_analysis$G1, student_analysis$G3, method = "pearson"),
cor(student_analysis$G2, student_analysis$G3, method = "pearson"),
cor(student_analysis$absences, student_analysis$G3, method = "pearson"),
cor(as.numeric(student_analysis$studytime), student_analysis$G3, method = "spearman")
), 3),
r_Active_357 = round(c(
cor(active_students$G1, active_students$G3, method = "pearson"),
cor(active_students$G2, active_students$G3, method = "pearson"),
cor(active_students$absences, active_students$G3, method = "pearson"),
cor(as.numeric(active_students$studytime), active_students$G3, method = "spearman")
), 3),
R2_Active_357 = c(
round(cor(active_students$G1, active_students$G3)^2, 3),
round(cor(active_students$G2, active_students$G3)^2, 3),
round(cor(active_students$absences, active_students$G3)^2, 3),
NA
)
)
knitr::kable(
cor_table,
caption = "Correlations and Variance Explained (R²) with Final Grade (G3)"
)
| Predictor | r_All_395 | r_Active_357 | R2_Active_357 |
|---|---|---|---|
| G1 (First Period, Pearson r) | 0.801 | 0.892 | 0.795 |
| G2 (Second Period, Pearson r) | 0.905 | 0.966 | 0.932 |
| Absences (Pearson r) | 0.034 | -0.213 | 0.045 |
| Study Time (1-4 Bracket, Spearman rho) | 0.105 | 0.110 | NA |
Looking at the summary tables, separating the 38 zero-grade students makes a noticeable difference in how the data behaves. For weekly study time, students who studied less than 5 hours per week had similar overall pass rates (64.8% for under 2 hours and 64.6% for 2 to 5 hours) and similar active average grades (11.47 and 11.07). Once study time reached 5 to 10 hours or more than 10 hours per week, overall pass rates increased to 75.4% and 74.1%, and the average active grade rose above 12.5. Even with that increase, the overall correlation between study time and final grade remains fairly weak (rho = 0.105 for all students and rho = 0.110 for active students), showing that higher study hours alone do not guarantee higher grades.
Absences show an interesting shift once the G3 = 0 group is separated. Across all 395 students, absences appear to have almost no correlation with final grades (r = 0.034) because all 38 zero-grade students had 0 recorded absences, which pulled the average for the 0-absence group down to 8.44. Among the 357 active exam-takers, however, absences have a weak-to-moderate negative correlation with final grades (r = -0.213). In that active group, the average final grade drops steadily as absences increase, moving from 12.61 for students with 0 absences down to 10.03 for students with more than 15 absences.
In contrast to study time and absences, earlier grading-period scores show a very strong relationship with final performance. Among active students, first-period grades (G1) have a strong positive correlation with final grades (r = 0.892, R-squared = 0.795), and second-period grades (G2) are even stronger (r = 0.966, R-squared = 0.932). This indicates that a student’s performance earlier in the school year aligns closely with where they finish at the end of the course.
ggplot(student_analysis, aes(x = studytime, y = G3)) +
geom_boxplot(fill = "lightgray", outlier.shape = NA, width = 0.5) +
geom_jitter(
aes(color = completion_status),
width = 0.18,
height = 0,
alpha = 0.7,
size = 2
) +
geom_hline(yintercept = 10, linetype = "dashed", color = "darkgray") +
scale_color_manual(values = c("G3 = 0 (n = 38)" = "red", "G3 > 0 (n = 357)" = "blue")) +
labs(
title = "Final Mathematics Grade (G3) by Weekly Study Time",
subtitle = "Dashed line marks the passing threshold (10/20); median G3 rises from 10 (<2 hrs) to 12 (5+ hrs)",
x = "Weekly Study Time",
y = "Final Grade (G3, 0-20 Scale)",
color = "Student Group"
) +
theme_minimal(base_size = 12) +
theme(legend.position = "bottom")
ggplot(student_analysis, aes(x = absences, y = G3)) +
geom_point(aes(color = completion_status), alpha = 0.7, size = 2.2) +
geom_smooth(
data = active_students,
method = "lm",
color = "darkblue",
fill = "lightgray",
linewidth = 1.1
) +
geom_hline(yintercept = 10, linetype = "dashed", color = "darkgray") +
scale_color_manual(values = c("G3 = 0 (n = 38)" = "red", "G3 > 0 (n = 357)" = "blue")) +
labs(
title = "Final Mathematics Grade (G3) vs. Total School Absences",
subtitle = "Trend line fitted to active exam-takers (G3 > 0, r = -0.21); all 38 zero-grade students had 0 absences",
x = "Total School Absences",
y = "Final Grade (G3, 0-20 Scale)",
color = "Student Group"
) +
theme_minimal(base_size = 12) +
theme(legend.position = "bottom")
student_long <- student_analysis %>%
pivot_longer(cols = c(G1, G2), names_to = "Period", values_to = "Period_Grade") %>%
mutate(
Period = recode(
Period,
"G1" = "First-Period Grade (G1: r = 0.89)",
"G2" = "Second-Period Grade (G2: r = 0.97)"
)
)
ggplot(student_long, aes(x = Period_Grade, y = G3)) +
geom_abline(slope = 1, intercept = 0, linetype = "dotted", color = "darkgray") +
geom_point(aes(color = completion_status), alpha = 0.65, size = 2) +
geom_smooth(
data = student_long %>% filter(G3 > 0),
method = "lm",
color = "darkblue",
fill = "lightgray",
linewidth = 1
) +
facet_wrap(~Period) +
scale_color_manual(values = c("G3 = 0 (n = 38)" = "red", "G3 > 0 (n = 357)" = "blue")) +
labs(
title = "Earlier Grading-Period Scores Strongly Predict Final Grade (G3)",
subtitle = "Dotted diagonal shows 1:1 grade consistency; linear fit shown for active exam-takers (G3 > 0)",
x = "Grading-Period Score (0-20 Scale)",
y = "Final Grade (G3, 0-20 Scale)",
color = "Student Group"
) +
theme_minimal(base_size = 12) +
theme(legend.position = "bottom")
These three charts reinforce the patterns found in the summary tables. In the first chart, the median final grade moves up from 10 for students studying less than 2 hours per week to 12 for students studying 5 or more hours, but the boxes overlap heavily across all four brackets. Students who received a final grade of 0 also appear in every study-time group, which further shows that study time by itself does not separate passing and failing students.
The second chart highlights why separating the G3 = 0 group is important when looking at attendance. All 38 zero-grade students sit directly on the vertical line at 0 absences, while the active exam-takers follow a gradual downward trend line as absences increase. At the same time, the scatter points show that a few students with more than 20 absences still earned high final grades, meaning high absences increase the risk of lower grades without guaranteeing failure.
The third chart compares first- and second-period grades against final grades. By the second grading period (G2), active students cluster very tightly along the dotted diagonal line, showing strong consistency between second-period scores and final exam results. Meanwhile, the 38 zero-grade students sit flat along the bottom of both panels even though many of them earned passing scores in G1 and G2, supporting the idea that these students did not complete the final exam rather than failing based on coursework.
Overall, this analysis shows that prior grading-period scores are much stronger indicators of final mathematics performance than self-reported weekly study time or total absences. To answer my first research question, weekly study time has only a slight positive relationship with final grades, though students studying at least 5 hours per week tend to have higher pass rates and slightly higher median scores. Class absences have a negative relationship with final grades among active exam-takers, with average scores declining steadily once students miss more than 5 classes. To answer my second research question, first- and second-period grades strongly predict final course performance, especially by the second grading period.
Based on these findings, teachers, academic advisors, and school counselors can take a few practical steps to support students before the end of the year:
Several methodological limitations should be kept in mind when interpreting these findings. Because this dataset is observational, the analyses show associations between variables rather than proving cause and effect. In addition, weekly study time was self-reported by students in broad brackets rather than logged directly, and the data were collected from two Portuguese public secondary schools during the 2005–2006 academic year, meaning patterns may differ across other school systems, cohorts, or subjects.
From an ethical and legal standpoint, this project uses a publicly
available, de-identified dataset in which direct student identifiers
(such as names and student ID numbers) were removed prior to release,
aligning with student privacy principles. Even when working with
anonymized educational records, practitioners should avoid using
early-warning metrics, such as low G1 scores or rising
absences, to label or track students punitively. Instead, these
indicators should be used ethically to allocate supportive resources
(such as tutoring or counselor check-ins), and schools should verify
administrative enrollment status before acting on G3 = 0
records that may reflect course withdrawals or excused exam
absences.
I acknowledge Paulo Cortez and Alice Silva for making the Student
Performance dataset publicly available through the UCI Machine Learning
Repository, as well as the open-source R and tidyverse
developer communities.