Research question: How are gender, parents’ education, lunch type, and test preparation related to students’ math scores?
The Students Performance in Exams dataset has 1,000 students and eight variables. Each row represents one student. The data includes gender, race/ethnicity, parents’ education, lunch type, test preparation, and math, reading, and writing scores. I got this dataset from Blackboard. My response variable is math score, which is quantitative because it is a number. My four predictor variables are categorical because they are groups.
I chose multiple linear regression because I want to see how several factors are related to math scores. Gender lets me compare male and female students. Parents’ education and lunch type may help explain differences in students’ backgrounds. Test preparation shows whether students completed a preparation course. I will also check the five assumptions of my model to see if it is a good fit for the data.
First, I will check the size of the dataset, missing values, duplicate rows, and basic statistics. Then I will keep only the variables I need, remove any rows with missing values, and turn the groups into factors. I will use a histogram to show how math scores are spread out and boxplots to compare the groups.
library(readr)
library(dplyr)
library(ggplot2)
# Keep the CSV in the same folder as this R Markdown file.
path <- if (file.exists("StudentsPerformance.csv")) {
"StudentsPerformance.csv"
} else {
"StudentsPerformance.csv"
}
students <- read_csv(path, show_col_types = FALSE)
dim(students)
## [1] 1000 8
summary(students)
## gender race/ethnicity parental level of education lunch
## Length :1000 Length :1000 Length :1000 Length :1000
## N.unique : 2 N.unique : 5 N.unique : 6 N.unique : 2
## N.blank : 0 N.blank : 0 N.blank : 0 N.blank : 0
## Min.nchar: 4 Min.nchar: 7 Min.nchar: 11 Min.nchar: 8
## Max.nchar: 6 Max.nchar: 7 Max.nchar: 18 Max.nchar: 12
##
## test preparation course math score reading score writing score
## Length :1000 Min. : 0.00 Min. : 17.00 Min. : 10.00
## N.unique : 2 1st Qu.: 57.00 1st Qu.: 59.00 1st Qu.: 57.75
## N.blank : 0 Median : 66.00 Median : 70.00 Median : 69.00
## Min.nchar: 4 Mean : 66.09 Mean : 69.17 Mean : 68.05
## Max.nchar: 9 3rd Qu.: 77.00 3rd Qu.: 79.00 3rd Qu.: 79.00
## Max. :100.00 Max. :100.00 Max. :100.00
colSums(is.na(students))
## gender race/ethnicity
## 0 0
## parental level of education lunch
## 0 0
## test preparation course math score
## 0 0
## reading score writing score
## 0 0
sum(duplicated(students))
## [1] 0
clean <- students %>%
select(math = `math score`, gender,
education = `parental level of education`, lunch,
prep = `test preparation course`) %>%
filter(complete.cases(.)) %>%
mutate(gender = factor(gender), education = factor(education),
lunch = factor(lunch), prep = factor(prep))
# Explicit reference categories for interpretation.
clean <- clean %>% mutate(
gender = relevel(gender, ref = "female"),
education = relevel(education, ref = "associate's degree"),
lunch = relevel(lunch, ref = "free/reduced"),
prep = relevel(prep, ref = "completed"))
clean %>% group_by(prep) %>% summarise(n = n(),
mean_math = mean(math), sd_math = sd(math))
## # A tibble: 2 × 4
## prep n mean_math sd_math
## <fct> <int> <dbl> <dbl>
## 1 completed 358 69.7 14.4
## 2 none 642 64.1 15.2
ggplot(clean, aes(x = math)) +
geom_histogram(binwidth = 5, fill = "steelblue", color = "white") +
labs(title = "Distribution of Math Scores", x = "Math score", y = "Number of students") +
theme_minimal()
for (v in c("gender", "education", "lunch", "prep")) {
print(ggplot(clean, aes(x = .data[[v]], y = math, fill = .data[[v]])) +
geom_boxplot() + coord_flip() + theme_minimal() +
theme(legend.position = "none") +
labs(title = paste("Math Scores by", v), x = v, y = "Math score"))
}
All 1,000 students were kept in the dataset. Students who completed test
preparation had an average math score of 69.70, while students who did
not had an average of 64.08. These averages do not account for the other
factors in my model.
model <- lm(math ~ gender + education + lunch + prep, data = clean)
summary(model)
##
## Call:
## lm(formula = math ~ gender + education + lunch + prep, data = clean)
##
## Residuals:
## Min 1Q Median 3Q Max
## -51.151 -9.070 0.284 9.683 37.147
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 61.7196 1.2730 48.485 < 2e-16 ***
## gendermale 5.1081 0.8538 5.983 3.06e-09 ***
## educationbachelor's degree 1.7401 1.5344 1.134 0.257023
## educationhigh school -5.3726 1.3225 -4.063 5.24e-05 ***
## educationmaster's degree 3.1550 1.9746 1.598 0.110402
## educationsome college -0.5630 1.2727 -0.442 0.658327
## educationsome high school -4.8722 1.3538 -3.599 0.000335 ***
## lunchstandard 11.2017 0.8907 12.576 < 2e-16 ***
## prepnone -5.6962 0.8926 -6.381 2.69e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 13.47 on 991 degrees of freedom
## Multiple R-squared: 0.2177, Adjusted R-squared: 0.2114
## F-statistic: 34.47 on 8 and 991 DF, p-value: < 2.2e-16
results <- cbind(coef(summary(model)), confint(model))
knitr::kable(results, digits = 4, caption = "Regression coefficients and 95% confidence intervals")
| Estimate | Std. Error | t value | Pr(>|t|) | 2.5 % | 97.5 % | |
|---|---|---|---|---|---|---|
| (Intercept) | 61.7196 | 1.2730 | 48.4852 | 0.0000 | 59.2216 | 64.2176 |
| gendermale | 5.1081 | 0.8538 | 5.9826 | 0.0000 | 3.4326 | 6.7836 |
| educationbachelor’s degree | 1.7401 | 1.5344 | 1.1341 | 0.2570 | -1.2708 | 4.7511 |
| educationhigh school | -5.3726 | 1.3225 | -4.0625 | 0.0001 | -7.9677 | -2.7774 |
| educationmaster’s degree | 3.1550 | 1.9746 | 1.5978 | 0.1104 | -0.7198 | 7.0299 |
| educationsome college | -0.5630 | 1.2727 | -0.4424 | 0.6583 | -3.0604 | 1.9345 |
| educationsome high school | -4.8722 | 1.3538 | -3.5989 | 0.0003 | -7.5288 | -2.2155 |
| lunchstandard | 11.2017 | 0.8907 | 12.5764 | 0.0000 | 9.4539 | 12.9496 |
| prepnone | -5.6962 | 0.8926 | -6.3812 | 0.0000 | -7.4479 | -3.9445 |
# Partial F-tests for each predictor as a whole.
drop1(model, test = "F")
## Single term deletions
##
## Model:
## math ~ gender + education + lunch + prep
## Df Sum of Sq RSS AIC F value Pr(>F)
## <none> 179685 5209.2
## gender 1 6489.6 186175 5242.7 35.7914 3.064e-09 ***
## education 5 8141.6 187827 5243.5 8.9805 2.337e-08 ***
## lunch 1 28678.3 208364 5355.3 158.1666 < 2.2e-16 ***
## prep 1 7383.3 187069 5247.5 40.7203 2.692e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The groups I use for comparison are female students, parents with an associate’s degree, free or reduced lunch, and students who completed test preparation. The starting predicted math score for a student in all four groups is 61.72.
When the other factors are kept the same, male students scored about 5.11 points higher than female students (95% confidence interval: 3.43 to 6.78). Students with standard lunch scored about 11.20 points higher than students with free or reduced lunch (9.45 to 12.95). Students who did not complete test preparation scored about 5.70 points lower than those who did (-7.45 to -3.94). All three differences were statistically significant because their p-values were below 0.05.
Compared with students whose parents had an associate’s degree, students whose parents finished high school or attended some high school scored about 5.37 and 4.87 points lower. Both differences were statistically significant. The differences for a bachelor’s degree (+1.74), master’s degree (+3.15), and some college (-0.56) were not statistically significant. These results show relationships, but they do not prove that these factors cause higher or lower scores.
par(mfrow = c(2, 2))
plot(model, which = c(1, 2, 3, 5))
par(mfrow = c(1, 1))
# Breusch-Pagan test using an auxiliary regression.
aux <- lm(I(residuals(model)^2) ~ gender + education + lunch + prep, data = clean)
bp <- nrow(clean) * summary(aux)$r.squared
c(statistic = bp, p_value = pchisq(bp, df = 8, lower.tail = FALSE))
## statistic p_value
## 9.430259 0.307309
shapiro.test(residuals(model))
##
## Shapiro-Wilk normality test
##
## data: residuals(model)
## W = 0.99608, p-value = 0.01239
# Residuals in original observation order.
plot(residuals(model), type = "p", xlab = "Observation order", ylab = "Residual")
abline(h = 0, col = "red")
# VIF for each dummy-coded predictor, without extra packages.
X <- model.matrix(model)[, -1, drop = FALSE]
vif <- sapply(seq_len(ncol(X)), function(j) {
1 / (1 - summary(lm(X[, j] ~ X[, -j, drop = FALSE]))$r.squared)
})
setNames(vif, colnames(X))
## gendermale educationbachelor's degree
## 1.003867 1.351353
## educationhigh school educationmaster's degree
## 1.519997 1.193855
## educationsome college educationsome high school
## 1.562585 1.485505
## lunchstandard prepnone
## 1.001849 1.010029
max(cooks.distance(model))
## [1] 0.01510746
Linearity: All four predictors are groups, so I do not need to check for a straight-line relationship with a numerical predictor. My model assumes that the differences between groups stay the same when the other factors change. The residuals-versus-fitted plot helps me check for patterns that the model may have missed.
Independence: Each row represents one student. I used a plot of the residuals in row order to check for patterns. However, I do not know if students came from the same schools or classrooms, so I cannot fully confirm this assumption.
Homoscedasticity (equal spread): This means that the prediction errors should have a similar spread. I used the residuals-versus-fitted and Scale-Location plots to check this. The Breusch-Pagan test gave a p-value of about 0.307, which is above 0.05. This means there is not strong evidence that the spread is unequal.
Normality: This means the prediction errors should follow a roughly bell-shaped distribution. I used the Q-Q plot and the Shapiro-Wilk test. The errors are slightly skewed to the left. The p-value is about 0.012, so the errors are not perfectly normal. Since the dataset has 1,000 students, this small difference is less likely to be a major problem, but I still need to be careful.
Multicollinearity: This checks whether the predictors are too closely related to each other. The VIF values range from about 1.00 to 1.56. They are all below 5, so this does not appear to be a major problem. I also checked the residuals-versus-leverage plot and Cook’s distance for students who might have a large effect on the model. The largest Cook’s distance is about 0.015, which is well below 1.
My results show that gender, parents’ education, lunch type, and test preparation are related to math scores. Standard lunch had the largest positive difference among the groups I compared. Students who completed test preparation also had higher predicted math scores. The overall model was statistically significant (F = 34.47, p < 0.001). The R-squared was 0.218, which means my model explains about 21.8% of the differences in math scores. The adjusted R-squared was 0.211.
My model does not explain all the differences in math scores. Other factors, such as study time, attendance, family income, and school resources, may also play a role. This study does not prove that any of these factors cause better scores. In the future, I would like to include more variables and see whether test preparation works differently for students with different lunch types. I could also test how well other models predict scores for new students.
Students Performance in Exams. Dataset provided in the course
Blackboard datasets folder (StudentsPerformance.csv).
R Core Team. R: A Language and Environment for Statistical Computing. https://www.R-project.org/
Wickham, H. ggplot2: Elegant Graphics for Data Analysis. https://ggplot2.tidyverse.org/