Sara Kolevska
1.1 Data
Resources: Resources: Fabio Pagnotta, & Hossain Mohammad Amran. (2023). High School Alcoholism and Academic Performance [Data set]. Kaggle. https://doi.org/10.34740/KAGGLE/DS/4087335
mydata <- read.table ("C:/Users/SARA/Desktop/HW/Home Work_2/student-lpor.csv", header = TRUE, sep = ",", dec = ".")
head(mydata)
## school sex age address famsize Pstatus Medu Fedu Mjob Fjob reason
## 1 GP F 18 U GT3 A 4 4 at_home teacher course
## 2 GP F 17 U GT3 T 1 1 at_home other course
## 3 GP F 15 U LE3 T 1 1 at_home other other
## 4 GP F 15 U GT3 T 4 2 health services home
## 5 GP F 16 U GT3 T 3 3 other other home
## 6 GP M 16 U LE3 T 4 3 services other reputation
## guardian traveltime studytime schoolsup famsup paid activities nursery higher
## 1 mother 2 2 yes no no no yes yes
## 2 father 1 2 no yes no no no yes
## 3 mother 1 2 yes no no no yes yes
## 4 mother 1 3 no yes no yes yes yes
## 5 father 1 2 no yes no no yes yes
## 6 mother 1 2 no yes no yes yes yes
## internet romantic famrel freetime goout Dalc Walc health absences G1 G2
## 1 no no 4 3 4 1 1 3 4 0 11
## 2 yes no 5 3 3 1 1 3 2 9 11
## 3 yes no 4 3 2 2 3 3 6 12 13
## 4 yes yes 3 2 2 1 1 5 0 14 14
## 5 no no 4 3 2 1 2 5 0 11 13
## 6 yes no 5 4 2 1 2 5 6 12 12
Description of data:
#Factoring some relevant categorical variables that may be useful for the analysis and research questions
mydata$schoolF <- factor (mydata$school,
levels = c(0, 1),
labels = c("GP", "MS"))
mydata$sexF <- factor (mydata$sex,
levels = c(0, 1),
labels = c("F", "M"))
mydata$addressF <- factor (mydata$address,
levels = c(0, 1),
labels = c("U", "R"))
mydata$famsizeF <- factor (mydata$famsize,
levels = c(0, 1),
labels = c("LE3", "GT3"))
mydata$PstatusF <- factor (mydata$Pstatus,
levels = c(0, 1),
labels = c("T", "A"))
mydata$MeduF <- factor (mydata$Medu,
levels = c(0, 1, 2, 3, 4),
labels = c("None", "Elementary school","Middle School", "High School", "Higer Education"))
mydata$FeduF <- factor (mydata$Fedu,
levels = c(0, 1, 2, 3, 4),
labels = c("None", "Elementary school","Middle School", "High School", "Higer Education"))
mydata$schoolsupF <- factor (mydata$schoolsup,
levels = c(0, 1),
labels = c("Yes", "No"))
mydata$famsupF <- factor (mydata$famsup,
levels = c(0, 1),
labels = c("Yes", "No"))
mydata$paidF <- factor (mydata$paid,
levels = c(0, 1),
labels = c("Yes", "No"))
mydata$activitiesF <- factor (mydata$activities,
levels = c(0, 1),
labels = c("Yes", "No"))
mydata$DalcF <- factor (mydata$Dalc,
levels = c(1, 2, 3, 4, 5),
labels = c("very low", "low", "medium", "high", "very high"))
mydata$WalcF <- factor (mydata$Walc,
levels = c(1, 2, 3, 4, 5),
labels = c("very low", "low", "medium", "high", "very high"))
mydata$healthF <- factor (mydata$health,
levels = c(1, 2, 3, 4, 5),
labels = c("very bad", "bad", "medium", "good", "very good"))
#Creating a new object called "numerical_data" to be able to perform descriptive statistics on them.
numerical.data <- mydata[c("age", "absences", "G1", "G2")]
#Performing descriptive statistics on the numerical data
summary(numerical.data)
## age absences G1 G2
## Min. :15.00 Min. : 0.000 Min. : 0.0 Min. : 0.00
## 1st Qu.:16.00 1st Qu.: 0.000 1st Qu.:10.0 1st Qu.:10.00
## Median :17.00 Median : 2.000 Median :11.0 Median :11.00
## Mean :16.74 Mean : 3.659 Mean :11.4 Mean :11.57
## 3rd Qu.:18.00 3rd Qu.: 6.000 3rd Qu.:13.0 3rd Qu.:13.00
## Max. :22.00 Max. :32.000 Max. :19.0 Max. :19.00
From this, we can conclude that the average age of students in this study is 16.74, and their average grade points from the first semester is 11.4 and for the second one is 11,57. The maximum grade for both semesters is 19 points, which means that no student achieved the maximum grade. We want to check if consummation of alcohol has is associated with the grades, however, since we are dealing with categorical variables, we’ll calculate only the frequencies.
# Select the relevant categorical variables
categorical_data <- mydata[c("school", "sex", "address", "famsize", "Pstatus", "Mjob", "Fjob", "reason", "guardian", "schoolsup", "famsup", "paid", "activities", "nursery", "higher", "internet", "romantic", "Dalc", "Walc")]
# Perform descriptive statistics on the categorical variables to be able to measure frequencies. The lapply function in R is used to apply a given function to each element of a list or vector.
lapply(categorical_data, table)
## $school
##
## GP MS
## 423 226
##
## $sex
##
## F M
## 383 266
##
## $address
##
## R U
## 197 452
##
## $famsize
##
## GT3 LE3
## 457 192
##
## $Pstatus
##
## A T
## 80 569
##
## $Mjob
##
## at_home health other services teacher
## 135 48 258 136 72
##
## $Fjob
##
## at_home health other services teacher
## 42 23 367 181 36
##
## $reason
##
## course home other reputation
## 285 149 72 143
##
## $guardian
##
## father mother other
## 153 455 41
##
## $schoolsup
##
## no yes
## 581 68
##
## $famsup
##
## no yes
## 251 398
##
## $paid
##
## no yes
## 610 39
##
## $activities
##
## no yes
## 334 315
##
## $nursery
##
## no yes
## 128 521
##
## $higher
##
## no yes
## 69 580
##
## $internet
##
## no yes
## 151 498
##
## $romantic
##
## no yes
## 410 239
##
## $Dalc
##
## 1 2 3 4 5
## 451 121 43 17 17
##
## $Walc
##
## 1 2 3 4 5
## 247 150 120 87 45
From this data we can see that the study includes 383 female students and 266 male students. They mainly live in urban areas, in families with both their parents. There is additional data about their parents’ occupation and education level. There is some information about the schools’ conditions and the students’ personal information. We can see that 17 from all 649 students consume alcohol on weekdays, and 45 on the weekend. On the other hand, 451 of have a very low consumption of alcohol during work days, and 247 answered that they don’t even drink on the weekend.From all 649 students, 569 live with both their parents, and 80 have separated families.
1.2 Analysis
Given the data, there are many interesting research questions to be analyzed, but this particular work is interested in the association between the students’ alcohol consummation habits and their parents’ cohabitation status.
Based on this, the two hypotheses were formulated.
Null hypothesis: There is no association between students’ alcohol consumption during work days and their parents’ cohabitation status.
Alternative hypothesis: There is an association between students’ alcohol consumption during work days and their parents’ cohabitation status.
#Conducting non-parametric test (Fisher's exact Probability Test of Independence)
# Create a contingency table of DalcF and PstatusF.A distinct table was created for a better visibility
cont_table <- table(mydata$Dalc, mydata$Pstatus)
# Perform Fisher's exact test
fisher_test <- fisher.test(cont_table)
# Print the results of the test
print(fisher_test)
##
## Fisher's Exact Test for Count Data
##
## data: cont_table
## p-value = 0.3691
## alternative hypothesis: two.sided
# Perform Pearson's chi-square test
chi_square_result <- chisq.test(cont_table)
## Warning in chisq.test(cont_table): Chi-squared approximation may be incorrect
# Extract expected frequencies from the chi-square test result
expected_freq <- chi_square_result$expected
#Extract observed frequencies from the chi-sqare test result
observed_freq <- chi_square_result$observed
# Check if all expected frequencies are greater than 5
all_greater_than_5 <- all(expected_freq > 5)
# Print the result
print(all_greater_than_5)
## [1] FALSE
To answer the first research question, Pearson chi-square test is a more appropriate choice. The reason for that is that all assumptions are answered:
That’s why I’ll proceed by explaining the results of the Pearson’s chi-square test and answering the research question.
results <- chisq.test(cont_table,
correct = TRUE)
## Warning in chisq.test(cont_table, correct = TRUE): Chi-squared approximation
## may be incorrect
print (results)
##
## Pearson's Chi-squared test
##
## data: cont_table
## X-squared = 4.2815, df = 4, p-value = 0.3693
Based from the results, the chi-squared value is 4.2815, which indicates that there is some difference between the observed frequencies and the expected frequencies.
However, the p-value is 0.3693, which is greater than the significance level of 0.05.
This means that we fail to reject the null hypothesis, and we conclude that there is no significant association between students’ alcohol consumption during weekdays and their parents’ cohabitation status.
1.3 Conclusion
Based on the results of the Pearson’s chi-squared test, we can answer the research question by stating that there is no evidence to suggest that students’ alcohol consumption during weekdays varies depending on their parents’ cohabitation status.
2.1 Data
To answer the second research question, I’ll continue analyzing the same data set, but now investigate the effect between different variables.
Resources: Fabio Pagnotta, & Hossain Mohammad Amran. (2023). High School Alcoholism and Academic Performance [Data set]. Kaggle. https://doi.org/10.34740/KAGGLE/DS/4087335
#A reminder of the data that we'll be working with
head (mydata)
## school sex age address famsize Pstatus Medu Fedu Mjob Fjob reason
## 1 GP F 18 U GT3 A 4 4 at_home teacher course
## 2 GP F 17 U GT3 T 1 1 at_home other course
## 3 GP F 15 U LE3 T 1 1 at_home other other
## 4 GP F 15 U GT3 T 4 2 health services home
## 5 GP F 16 U GT3 T 3 3 other other home
## 6 GP M 16 U LE3 T 4 3 services other reputation
## guardian traveltime studytime schoolsup famsup paid activities nursery higher
## 1 mother 2 2 yes no no no yes yes
## 2 father 1 2 no yes no no no yes
## 3 mother 1 2 yes no no no yes yes
## 4 mother 1 3 no yes no yes yes yes
## 5 father 1 2 no yes no no yes yes
## 6 mother 1 2 no yes no yes yes yes
## internet romantic famrel freetime goout Dalc Walc health absences G1 G2
## 1 no no 4 3 4 1 1 3 4 0 11
## 2 yes no 5 3 3 1 1 3 2 9 11
## 3 yes no 4 3 2 2 3 3 6 12 13
## 4 yes yes 3 2 2 1 1 5 0 14 14
## 5 no no 4 3 2 1 2 5 0 11 13
## 6 yes no 5 4 2 1 2 5 6 12 12
## schoolF sexF addressF famsizeF PstatusF MeduF FeduF
## 1 <NA> <NA> <NA> <NA> <NA> Higer Education Higer Education
## 2 <NA> <NA> <NA> <NA> <NA> Elementary school Elementary school
## 3 <NA> <NA> <NA> <NA> <NA> Elementary school Elementary school
## 4 <NA> <NA> <NA> <NA> <NA> Higer Education Middle School
## 5 <NA> <NA> <NA> <NA> <NA> High School High School
## 6 <NA> <NA> <NA> <NA> <NA> Higer Education High School
## schoolsupF famsupF paidF activitiesF DalcF WalcF healthF
## 1 <NA> <NA> <NA> <NA> very low very low medium
## 2 <NA> <NA> <NA> <NA> very low very low medium
## 3 <NA> <NA> <NA> <NA> low medium medium
## 4 <NA> <NA> <NA> <NA> very low very low very good
## 5 <NA> <NA> <NA> <NA> very low low very good
## 6 <NA> <NA> <NA> <NA> very low low very good
For the second research question, I’ll analyze the change in grades between first and second semester, and how this change was affected by different variables. The variables chosen are:
The expected effects of each explanatory variable on the students’ grades, and the reason I’ve included them in the analysis is:
Justification of the chosen variables:
#Creating a new variable that represents the difference between the grades in the first and second semesters (G2 - G1). This is the dependent variable y.
mydata$gradedif <- mydata$G2 - mydata$G1
#The categorical variable 'paid' is a Dummy variable
contrasts(mydata$paidF)
## No
## Yes 0
## No 1
#The categorical variable 'activities' is a Dummy variable
contrasts(mydata$activitiesF)
## No
## Yes 0
## No 1
2.2 Analysis
To answer the research question, I’ll be using regression analysis. The two hypotheses are:
Null hypothesis: There is no significant effect of private classes, extracurricular activities, and absences on the change of grades.
Alternative hypothesis: There is a significant effect of effect of private classes, extracurricular activities, and absences on the change of grades.
#Conducting linear regression
fit <- lm (gradedif ~ paid + activities + absences,
data = mydata)
Before continuing the analysis I will check the some assumptions:
Dependent variable is numerical, the explanatory variables are numerical or dichotomous. This assumption checks out, ‘gradedif’ is numerical, and so is the explanatory variable ‘absences’. The remaining two explanatory variables are dichotomous.
Each explanatory variable must vary (nonzero variance), but it is desirable that the explanatory variables assume as wide a range of possible values as possible.
#For this assumption I wanted to check the variances. I did this with the use of the function 'sapply', which estimates the selected parameter and then use 'var', to calculate the variance of the vector.
# Check variance for each explanatory variable
variances <- sapply(mydata[, c('absences', 'paid', 'activities')], var)
## Warning in FUN(X[[i]], ...): NAs introduced by coercion
## Warning in FUN(X[[i]], ...): NAs introduced by coercion
# Print variances
print(variances)
## absences paid activities
## 21.53664 NA NA
Here we can notice a violation due to the fact that categorical variable is included. The range of variables is too small. This means that there is a bit less reliability and accuracy in the results.
#Creating a new object - 'CooksD' in order to better visualize if there are any outliers.
mydata$CooksD <- round (cooks.distance(fit))
#Creating a histogram of standardized residuals
hist (mydata$CooksD,
xlab = "Cooks distance",
ylab = "Frequency",
main = "Histogram of Cooks distance")
There are no outliers in the data. The assumption checks out.
#install.packages("car")
library (car)
## Warning: package 'car' was built under R version 4.3.2
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.3.2
#Checking assumption: Not too strong multicolinearity
vif(fit)
## paid activities absences
## 1.005581 1.004510 1.001458
The VIF values are close to 1 which suggests there is minimal multidisciplinary. However, they are below 2 which suggests that these variables don’t strongly influence one another, allowing more reliable estimation of their individual effects on the dependent variable (gradedif).
Now, checking some more general assumptions:
#install.packages("car")
library(car)
#Assumption 1: Linearity in Parameters
# Scatterplot of each explanatory variable against 'gradedif'
scatterplot(mydata$gradedif ~ mydata$absences + mydata$paid + mydata$activities, data = mydata)
The parameters are linear.
# Plot standardized residuals against standardized predicted values
plot(fit, which = c(1, 3)) # Check for heteroskedasticity
#Checking homoscedasticity and linearity
#Standardized residuals
mydataStdResid <- round (rstandard(fit))
#Fitted values
mydata$StdFitted <- scale (fit$fitted.values)
library (car)
scatterplot (y = mydata$Std, x = mydata$StdFitted,
ylab = "Standardized residuals",
xlab = "Standardized fitted values",
boxplots = FALSE,
regLine = FALSE,
smooth = FALSE)
As we concluded, the values are linear. To further established heteroscedasticity, I conducted Breusch Pagan test.
#Checking homoscedasticity
#install.packages ("olsrr")
library(olsrr)
## Warning: package 'olsrr' was built under R version 4.3.2
##
## Attaching package: 'olsrr'
## The following object is masked from 'package:datasets':
##
## rivers
ols_test_breusch_pagan(fit)
##
## Breusch Pagan Test for Heteroskedasticity
## -----------------------------------------
## Ho: the variance is constant
## Ha: the variance is not constant
##
## Data
## ------------------------------------
## Response : gradedif
## Variables: fitted values of gradedif
##
## Test Summary
## ------------------------------
## DF = 1
## Chi2 = 7.148085
## Prob > Chi2 = 0.007504406
The p-value us 0.008 which is greater than 0.005. This means that we cannot reject the null hypothesis, and conclude that there is homoscedasticity.
# Histogram of standardized residuals
hist(rstandard(fit)) # Check normality graphically
The data looks a bit asymmetric to the right. I’ll continue with
Shapiro-Wilk normality test to check this.
# Shapiro-Wilk normality test
shapiro.test(rstandard(fit)) # Check normality statistically
##
## Shapiro-Wilk normality test
##
## data: rstandard(fit)
## W = 0.88549, p-value < 2.2e-16
Null Hypothesis: Errors are normally distributed. Alternative Hypothesis: Errors are normally distributed
We must reject the null hypothesis, this means that errors are not normally distributed.
That’s why, I’ll continue with robust standard errors, to mitigate the influence of outliers or non-normally distributed residuals.
summary(fit) #Results of regression model
##
## Call:
## lm(formula = gradedif ~ paid + activities + absences, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.0883 -1.0975 -0.1067 0.8543 10.8174
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.14568 0.09434 1.544 0.123
## paidyes 0.32350 0.24513 1.320 0.187
## activitiesyes -0.05740 0.11650 -0.493 0.622
## absences 0.00923 0.01254 0.736 0.462
##
## Residual standard error: 1.48 on 645 degrees of freedom
## Multiple R-squared: 0.003696, Adjusted R-squared: -0.0009377
## F-statistic: 0.7977 on 3 and 645 DF, p-value: 0.4954
The p-value is greater than 0.05 which suggests that we cannot reject the null hypothesis. This means there is there is no significant effect of private classes, extracurricular activities, and absences on the change of grades. Since the variables are statistically insignificant, I shouldn’t explain them.
However, for the purpose of the analysis, I will assume that we rejected the null hypothesis and that the variables are statistically significant.
Based on this I can conclude:
Given the absences and the extracurricular activities performed, the students that don’t take private classes on subjects related to the course have on average higher grades for 0.324 points between the second and first semester compared to those who take private classes, assuming all variables are the same.
Given the absences and the private classes, the students that don’t take any extracurricular activities have on average lower grades for 0.058 points between the second and first semester compared to those who take extracurricular activities, assuming all variables are the same.
If the absences increase by one, the difference in grade between the second and first semester on average increase by 0.009 points, assuming everything else is the same.
0,3% of variability in the difference in the grade between the second and the first semester can be explained by linear effect of absences, private classes and extracurricular activities.
F-statistics, we cannot reject the null hypothesis which means that the model is not a good fit.
#Model comparison
#Regression function for a simpler model
fit1 <- lm (gradedif ~ paid + absences,
data = mydata)
anova(fit, fit1) #Comparion of the two models
## Analysis of Variance Table
##
## Model 1: gradedif ~ paid + activities + absences
## Model 2: gradedif ~ paid + absences
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 645 1412.8
## 2 646 1413.3 -1 -0.53179 0.2428 0.6224
We cannot reject the null hypothesis. The second model is better.
2.3 Conclusion
Based on the regression analysis, I’ve concluded that there is no significant effect of of private classes, extracurricular activities, and absences on the change of grades. This means that I accepted the null hypothesis, and that the variables are not statistically significant for further explanation.