library(corrplot)
library(tidyverse)
data <- read_csv('../data/student_data.csv')
Through the analysis of this data, we aimed to determine which variables affect student performance in school. The explanatory variables we analyzed were the number of hours studied on average per week, average score obtained by the student on the previous three tests, whether or not the student participates in extracurricular activities, the average number of sleep hours per night, and how many sample questions papers the student had completed. The response variable in this case is performance index which is a value given on a scale of 10-100 that represents the overall performance of each student (100 being highest performance). The summary statistics for the numerical variables analyzed demonstrated symmetry (nearly equal mean and median) for each of them. The data seems to be unusually uniform with an equal distribution in response for some (as seen through the pie charts of data for extracurricular activities and that of sleep hours). It should be noted that this is a synthetic dataset, which we did not realize when we intially chose it for this project.
summary(data$hours_studied)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.000 3.000 5.000 4.993 7.000 9.000
boxplot(data$hours_studied, main="Boxplot of Hours Studied", ylab = "Hours")
pie(table(data$hours_studied), main="Pie Chart of Hours Studied")
summary(data$previous_scores)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 40.00 54.00 69.00 69.45 85.00 99.00
hist(data$previous_scores)
summary(data$sleep_hours)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 4.000 5.000 7.000 6.531 8.000 9.000
boxplot(data$sleep_hours, main="Boxplot of Hours Slept")
pie(table(data$sleep_hours), main="Pie Chart of Hours Slept")
summary(data$sample_question_papers_practiced)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.000 2.000 5.000 4.583 7.000 9.000
boxplot(data$sample_question_papers_practiced, main="Sample Question Papers Practiced")
pie(table(data$sample_question_papers_practiced), main="Sample Question Papers Practiced")
### performance_index ( for some reason this refuses to turn into a
title)
summary(data$performance_index)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 10.00 40.00 55.00 55.22 71.00 100.00
hist(data$performance_index)
pie(table(data$extracurricular_activities))
plot(data$previous_scores, data$performance_index,
pch = 16, col = "blue",
xlab = "Previous Test Scores", ylab = "Performance Index",
main = "Scatter Plot of previous scores vs performance index")
ggplot(data, aes(x = data$performance_index, fill = factor(data$hours_studied))) +
geom_boxplot() +
labs(title = "Boxplots Performance Index by Hours Studied", x = "Performance Index", y = "Frequency")
ggplot(data, aes(x = data$performance_index, fill = factor(data$sleep_hours))) +
geom_boxplot() +
labs(title = "Boxplots Performance Index by Hours Slept", x = "Performance Index", y = "Frequency")
ggplot(data, aes(x = data$performance_index, fill = factor(data$sample_question_papers_practiced))) +
geom_boxplot() +
labs(title = "Boxplots Performance Index by Sample Question Papers Practiced", x = "Performance Index", y = "Frequency")
As stated previously, many of our variables appear to be distributed
unusually uniformly. There are few outliers. None of the variables are
continuous except for performance_index, which appears to be normally
distributed. From the two variable relationship graphs, there appear to
be linear relationships between performance_index and some of the other
variables, especially previous_scores. This bodes well for the multiple
linear regression that will be conducted later.
We will conduct a two-sample t-test on whether the true mean
difference in performance_index is different for students who
participate in an extracurricular activity versus those that do not.
extracurricular_activites is a dummy variable within the dataset, where
1 signifies that the student participates in an extracurricular activity
and 0 signifies that the student did not.
H0: the true mean difference in performance_index between students who
participate in an extracurricular activity and those that do not is
equal to 0.
Ha: the true mean difference in performance_index between students who
participate in an extracurricular activity and those that do not is not
equal to 0.
For the purpose of this test we will assume that the data was collected using a random sample. We will also assume independence; it is not possible for a student to both participate in an extracurricular activity and not. Normaility is checked by the following histograms:
ggplot(data = data, mapping = aes(x = performance_index)) +
geom_histogram(bins = 20, color = "dark blue", fill = "purple") +
ggtitle("HIST OF PERFORMANCE INDEX") +
facet_wrap( ~ extracurricular_activities)
The data appears to be normally distributed, and regardless, CLT is met.
Now on to constant variance:
ggplot(data, aes(x = performance_index, group = extracurricular_activities)) +
geom_boxplot() +
labs(title = "Boxplots of Performance Index", x = "Level", y = "Frequency")
var.test(data[data$extracurricular_activities == 1,]$performance_index,
data[data$extracurricular_activities == 0,]$performance_index)
##
## F test to compare two variances
##
## data: data[data$extracurricular_activities == 1, ]$performance_index and data[data$extracurricular_activities == 0, ]$performance_index
## F = 1.0118, num df = 4947, denom df = 5051, p-value = 0.6792
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 0.957198 1.069463
## sample estimates:
## ratio of variances
## 1.011767
As shown in the boxplots, variance appears to be constant. This is
supported by the results of the variance test, which resulted in a
p-value of 0.6792. As a result, we do not need to use a Welch test.
t.test(performance_index ~ extracurricular_activities, data= data, var.equal = TRUE)
##
## Two Sample t-test
##
## data: performance_index by extracurricular_activities
## t = -2.453, df = 9998, p-value = 0.01418
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
## -1.6954392 -0.1893163
## sample estimates:
## mean in group 0 mean in group 1
## 54.75851 55.70089
The t value is -2.453 on 9998 degrees of freedom, with a p-value of
0.01418. Whether we reject the null hypothesis depends on the confidence
level. At a= 0.05, we have sufficient evidence to reject the null
hypothesis, but at a=0.01, we do not.
Assuming a confidence level of 0.05, we reject the null hypothesis. At
a=0.05, we have sufficient evidence to conclude that the true mean
difference in performance_index between students who participate in an
extracurricular activity and those that do not is not equal to 0.
We are 95% confident that the true difference in means between
performance_index between group 1 (extracurricular activities) and group
2 (no extracurricular activities) falls in the interval [-1.6954392,
-0.1893163].
ANOVA Test: Testing if there is at least one difference in
previous_scores between groups of students who sleep for a different
number of hours (4,5,6,7,8,or 9).
H0: the true mean previous_scores is the same across all groups of
sleep_hours.
Ha: at least one group’s previous_scores is different.
For the purpose of this test we will assume that the data was collected using a random sample. We will also assume independence; it is not possible for a student to sleep both 6 hours a night and 7. Next, homogeneity of variance:
boxplot(data$previous_scores ~ data$sleep_hours, data = data,
main = "Boxplot of Previous Scores by Sleep Hours",
xlab = "Sleep Hours",
ylab = "Previous Scores",
col='green')
tapply(data$performance_index, data$sleep_hours, sd)
## 4 5 6 7 8 9
## 19.11286 19.16177 19.26631 19.25093 19.29345 19.05922
The boxplots suggest constant variance. This is reinforced, the the
standard deviations, which are very similar and well within the rule of
thumb that the largest is less than double the smallest. Finally,
normality:
ggplot(data, aes(x = data$previous_scores)) +
geom_histogram(bins=7,color='black') +
labs(title = "Histogram of Previous Scores",
x = "Scores",
y = "Frequency")
previous_scores appears to be sort of normal, and regardless, CLT is
met.
# First, we must make the numeric variable categorical
aov_data <- data
aov_data$sleep_hours <- as.factor(aov_data$sleep_hours)
# Fit a linear model
model <- lm(previous_scores ~ sleep_hours, data = aov_data)
# Performing ANOVA
anova_result <- anova(model)
print(anova_result)
## Analysis of Variance Table
##
## Response: previous_scores
## Df Sum Sq Mean Sq F value Pr(>F)
## sleep_hours 5 324 64.734 0.2151 0.9562
## Residuals 9994 3007225 300.903
The F value is 0.2151, and the p-value is 0.9562. The ANOVA test
shows no significance, indicating the failure to reject the null
hypothesis. It suggests that there isn’t enough evidence to support the
idea that the number of hours of sleep significantly affects test score
performance. This implies that variations in sleep duration may not be a
crucial factor influencing how individuals perform on tests. It’s
important to note the study’s limitations, such as the specific
population and methodology used (i.e., synthetic data), and consider
that other factors like study habits or individual differences might
have a more substantial impact on academic performance. Future research
could explore these factors further for a more comprehensive
understanding.
Because we failed to reject the null hypothesis, we did not perform a
post-hoc test.
pairs(data, pch = 16, col = "blue", upper.panel = NULL)
Because there are 10,000 observations in the dataset, it’s difficult to
tell from this plot whether some of the variables have a linear
relationship with performance_index. However, previous_scores clearly
has a linear relationship with performance_index, as shown below.
ggplot(aes(previous_scores, performance_index), data=data) +
geom_point(color = "blue") +
theme_bw()
cor(data$previous_scores, data$performance_index)
## [1] 0.9151891
previous_scores is a pretty obvious predictor of performance_index. It has a correlation coefficient of 0.915 with performance_index, and would probably do a good job explaining performance_index on its own. As a result, we decided to first run a SLR on performance_index on using previous_scores, with the plan of explaining the residuals with the other variables.
model1 <- lm(performance_index ~ previous_scores, data=data)
summary(model1)
##
## Call:
## lm(formula = performance_index ~ previous_scores, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -17.7729 -6.5239 -0.0082 6.3689 19.4346
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -15.181799 0.319605 -47.5 <2e-16 ***
## previous_scores 1.013837 0.004465 227.1 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 7.744 on 9998 degrees of freedom
## Multiple R-squared: 0.8376, Adjusted R-squared: 0.8376
## F-statistic: 5.156e+04 on 1 and 9998 DF, p-value: < 2.2e-16
anova(model1)
## Analysis of Variance Table
##
## Response: performance_index
## Df Sum Sq Mean Sq F value Pr(>F)
## previous_scores 1 3091353 3091353 51555 < 2.2e-16 ***
## Residuals 9998 599501 60
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The regression line is performance_index (estimate) = -15.181799 +
1.013837(previous_scores). For every one point increase in previous
scores, we expect an average increase of 1.013837 to the performance
index. If previous_scores = 0, we expect that performance_index =
-15.181799, which does not make sense in a real-world application.
As can be seen, the SLR does a very good job fitting the data, with an F
value of 51555 and a tiny p-value. Based on the R^2, 83.76% of the
variation in performance_index can be explained using a regression with
previous_scores. Before going any further, we wanted to make sure that
the assumptions were met.
par(mfrow = c(2, 2))
plot(model1)
All the plots look good except the Q-Q residuals plot. The residuals vs
fitted plot does not have a discernible pattern, the scale-location plot
indicates homoscedasticity with no discernible pattern, and the
residuals vs leverage plot does not show any outliers. Though the
residuals on the Q-Q plot do not follow the 45 degree reference line at
the extremes, the dataset is large enough that CLT is met. All in all,
the assumptions for the SLR appear met.
# Applying the residuals from the SLR to a new dataset
adj_data <- data
adj_data$performance_resids <- model1$residuals
adj_data <- adj_data[-c(6)]
# Double checking for outliers
fivenum(adj_data$performance_resids)
## 2570 5610 6070 7077 7469
## -17.772932982 -6.523872354 -0.008156909 6.372352385 19.434617542
The residuals are now applied to a new dataset, and the variable is
names performance_resids. There do not appear to be any outliers with an
IQR of about 13 by the 1.5 * IQR rule.
Before running the MLR, we need to establish linear relationships
between performance_index and the explanatory variables. We already
showed a linear relationship between previous_scores and
performance_index, but what about the other ones? Here’s
hours_studied:
ggplot(aes(hours_studied, performance_resids), data=adj_data) +
geom_boxplot(aes(group= hours_studied), color = "lightblue") +
scale_x_discrete(limit = c(1, 2, 3, 4, 5, 6, 7, 8, 9)) +
geom_smooth(method = "lm") +
theme_bw()
cor(adj_data$hours_studied, adj_data$performance_resids)
## [1] 0.955449
A SLR line was overlayed on the graph to help show the relationship. As can be seen, there is a very strong linear relationship between hours_studied and performance_resids. The strong positive linear relationship is also shown by the correlation coefficient of 0.95.
ggplot(aes(sleep_hours, performance_resids), data=adj_data) +
geom_boxplot(aes(group= sleep_hours), color = "lightblue") +
scale_x_discrete(limit = c(4, 5, 6, 7, 8, 9)) +
geom_smooth(method = "lm") +
theme_bw()
cor(adj_data$sleep_hours, adj_data$performance_resids)
## [1] 0.1058639
A SLR line was overlayed on the graph to help show the relationship. As can be seen, there is a slight linear relationship between sleep_hours and performance_resids. The slight positive linear relationship is also shown by the correlation coefficient of 0.1.
ggplot(aes(sample_question_papers_practiced, performance_resids), data=adj_data) +
geom_boxplot(aes(group= sample_question_papers_practiced), color = "lightblue") +
scale_x_discrete(limit = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)) +
geom_smooth(method = "lm") +
theme_bw()
cor(adj_data$sample_question_papers_practiced, adj_data$performance_resids)
## [1] 0.08944687
A SLR line was overlayed on the graph to help show the relationship.
As can be seen, there is a slight linear relationship between
sample_question_papers_practiced and performance_resids. The slight
positive linear relationship is also shown by the correlation
coefficient of 0.08.
extracurricular_activities is a dummy variable, with 0 and 1 as possible
responses. As a result, we won’t be able to establish a linear
relationship between extracurricular_activities and performance_resids,
but we will initially include it in the model anyway.
model2 <- lm(performance_resids ~
hours_studied +
sleep_hours +
sample_question_papers_practiced +
extracurricular_activities,
data=adj_data)
summary(model2)
##
## Call:
## lm(formula = performance_resids ~ hours_studied + sleep_hours +
## sample_question_papers_practiced + extracurricular_activities,
## data = adj_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.5131 -1.3648 -0.0174 1.3485 8.6730
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -18.576144 0.097897 -189.75 <2e-16 ***
## hours_studied 2.852595 0.007878 362.07 <2e-16 ***
## sleep_hours 0.480848 0.012031 39.97 <2e-16 ***
## sample_question_papers_practiced 0.194024 0.007115 27.27 <2e-16 ***
## extracurricular_activities 0.614246 0.040808 15.05 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.04 on 9995 degrees of freedom
## Multiple R-squared: 0.9306, Adjusted R-squared: 0.9306
## F-statistic: 3.353e+04 on 4 and 9995 DF, p-value: < 2.2e-16
anova(model2)
## Analysis of Variance Table
##
## Response: performance_resids
## Df Sum Sq Mean Sq F value Pr(>F)
## hours_studied 1 547274 547274 131563.12 < 2.2e-16 ***
## sleep_hours 1 6569 6569 1579.06 < 2.2e-16 ***
## sample_question_papers_practiced 1 3139 3139 754.56 < 2.2e-16 ***
## extracurricular_activities 1 942 942 226.56 < 2.2e-16 ***
## Residuals 9995 41577 4
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The regression line is performance_resids (estimate) = -18.576144 +
2.852595(hours_studied) + 0.480848(sleep_hours) +
0.194024(sample_question_papers_practiced) +
0.614246(extracurricular_activities). With 0 hours_studied, 0
sleep_hours, 0 sample_question_papers_practiced, and no extracurricular
activities, we expect a performance_resid of -18.576144.
Based on these results, the MLR is clearly an excellent fit for the
data. The smallest F value is 226.56, and as a result all of the
variables have a tiny p-value. Based on the R^2, 93.06% of the variation
in the residuals can be explained by the model. Before going further, we
need to check that the assumptions of the model have been met.
par(mfrow = c(2, 2))
plot(model2)
All of the graphs look good. There are no discernible patterns in the
residuals v fitted plot or the scale-location plot. The Q-Q plot looks
great, although CLT is met anyway. Finally, there are no outliers in the
residuals vs leverage plot.
Obviously, the MLR is a great fit for the model. But just in case, we
decided to test the model against a reduced model with just
hours_studied, as hours_studied had a much higher linear correlation
with performace_resids than any other explanatory variables in the
model.
H0: the full model is not significantly different from the reduced
model.
Ha: the full model is significantly better than the reduced model.
model3 <- lm(performance_resids ~ hours_studied, data= adj_data)
anova(model2)
## Analysis of Variance Table
##
## Response: performance_resids
## Df Sum Sq Mean Sq F value Pr(>F)
## hours_studied 1 547274 547274 131563.12 < 2.2e-16 ***
## sleep_hours 1 6569 6569 1579.06 < 2.2e-16 ***
## sample_question_papers_practiced 1 3139 3139 754.56 < 2.2e-16 ***
## extracurricular_activities 1 942 942 226.56 < 2.2e-16 ***
## Residuals 9995 41577 4
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova(model3)
## Analysis of Variance Table
##
## Response: performance_resids
## Df Sum Sq Mean Sq F value Pr(>F)
## hours_studied 1 547274 547274 104767 < 2.2e-16 ***
## Residuals 9998 52227 5
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
p <- 5
q <- 2
n <- nrow(adj_data)
SSE_full <- 41577
SSE_reduced <- 52227
extraSS <- SSE_reduced - SSE_full
F <-((extraSS)/(p-q))/((SSE_full)/(n-p))
pf(F, (p-q), (n-p), lower.tail = FALSE)
## [1] 0
Because the p-value, approximately 0, is less than any standard significance level, we reject the null hypothesis. We have sufficient evidence to conclude that the full model is a better fit for our data than the reduced model. Our final model is the full model.
Our final model to explain performance_resids is as follows:
performance_resids (estimate) = -18.576144 + 2.852595(hours_studied) +
0.480848(sleep_hours) + 0.194024(sample_question_papers_practiced) +
0.614246(extracurricular_activities)
93.06% of the variation in performance_resids can be explained by the
model. It can be interpreted as follows:
- The y-intercept, -18.576144, is our predicted performance_resids with
0 hours_studied, 0 sleep_hours, 0 sample_question_papers_practiced, and
no extracurricular activities.
- For every increase of one hour studied, performance_resids increases
by an average of 2.852595.
- For every increase of one hour of sleep, performance_resids increases
by an average of 0.480848.
- For every increase of one sample question paper practiced,
performance_resids increases by an average of 0.194024.
- If a student participates in extracurricular activities,
performance_resids increases by an average of 0.614246.
In closing, the model fits the data extremely well–too well. It appears
that whoever created this dataset did not do a very good job making the
data realistic.