Author: Nik Kovač
mydata <- read.table("./student-mat.csv", header=TRUE, sep=",", dec=";") ##Creating data frame by importing a csv file
head(mydata[c("address","romantic","famrel","health")],) #Showing only the columns related to our research questions
## address romantic famrel health
## 1 U no 4 3
## 2 U no 5 3
## 3 U no 4 3
## 4 U yes 3 5
## 5 U no 4 5
## 6 U no 5 5
In a high school, it was decided to send out a survey to students on various topics. 395 students participated. I am only going to show and analyze the ones that fit our research questions.
Explanation of dataset:
The dataset is called Student Alcohol Consumption and I took it from a website called Kaggle: https://www.kaggle.com/datasets/uciml/student-alcohol-consumption/data.
mydata$ID <- seq_along(mydata$school) #Assigning an "ID" number to each student
mydata$addressF <- factor(mydata$address,
levels = c("U","R"),
labels = c("Urban", "Rural"))
mydata$romanticF <- factor(mydata$romantic,
levels = c("no","yes"),
labels = c("no", "yes"))
mydata$famrelF <- factor(mydata$famrel,
levels = c(1, 2, 3, 4, 5),
labels = c("Very bad", "bad", "mediocre", "good", "very good"))
mydata$healthF <- factor(mydata$health,
levels = c(1, 2, 3, 4, 5),
labels = c("very bad", "bad", "mediocre", "good", "very good"))
head(mydata[c("ID","addressF","romanticF","famrelF","healthF")],)
## ID addressF romanticF famrelF healthF
## 1 1 Urban no good mediocre
## 2 2 Urban no very good mediocre
## 3 3 Urban no good mediocre
## 4 4 Urban yes mediocre very good
## 5 5 Urban no good very good
## 6 6 Urban no very good very good
summary(mydata[,c("addressF","romanticF","famrelF","healthF")]) #descriptive statistics of all relevant columns
## addressF romanticF famrelF healthF
## Urban:307 no :263 Very bad : 8 very bad : 47
## Rural: 88 yes:132 bad : 18 bad : 45
## mediocre : 68 mediocre : 91
## good :195 good : 66
## very good:106 very good:146
From this table we can gather a lot of information and I will explain some:
Because I am trying to figure out the correlation between two ordinal variables, I will use the spearman rank correlation instead of pearson, because variables are not numeric.
But before doing that I need to check if all assumptions are met for spearman:
library(ggplot2)
ggplot(data = subset(mydata,),
aes(x = health, y = famrel)) +
geom_bar(stat = "summary", fun = "mean") +
ylab("Quality of family relationship") +
xlab("Current health status")
library(car)
## Loading required package: carData
scatterplotMatrix(mydata[ , c("health","famrel")], smooth=FALSE)
Even though the relationship between these variables looks a bit funny, I will still assume it is linear and that there are no outliers, therefore we can still use the data for this homework and the spearman rank correlation test is valid.
cor(mydata$health, mydata$famrel,
method = "spearman")
## [1] 0.08534102
Explanation of results:
There appears to be a positive, very weak (0 <= r =< 0.1) linear correlation between variables health and famrel.
cor.test(mydata$health, mydata$famrel,
method = "spearman",
exact = FALSE)
##
## Spearman's rank correlation rho
##
## data: mydata$health and mydata$famrel
## S = 9394993, p-value = 0.0903
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## 0.08534102
Explanation of results:
We cannot reject null hypothesis (p > 0.05) and conclude that, statistically, we cannot assume any correlation between the current health status of students and the quality of family relationships.
Research question: What is the relationship between quality of family relationships and current health status for high school students?*
Because I am trying to figure out the association between two categorical variables, I will try to use the Chi-square test. But before that I need to check if all assumptions are met:
We know that all observations are independent because each unit is only measured once and we will see later that the second assumption is also not violated, therefore we can use the chi-square test:
chi_square <- chisq.test(mydata$addressF, mydata$romanticF,
correct= TRUE) #Yates' continuity correction because of 2x2 contingency table
chi_square
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: mydata$addressF and mydata$romanticF
## X-squared = 1.2282e-29, df = 1, p-value = 1
Explanation of results:
We cannot reject the null hypothesis (p > 0.05) and conclude that we cannot statistically say there is association between variables address and romantic.
addmargins(chi_square$observed)
## mydata$romanticF
## mydata$addressF no yes Sum
## Urban 204 103 307
## Rural 59 29 88
## Sum 263 132 395
addmargins(round(chi_square$expected,2))
## mydata$romanticF
## mydata$addressF no yes Sum
## Urban 204.41 102.59 307
## Rural 58.59 29.41 88
## Sum 263.00 132.00 395
We can see that all expected values are above 5 and almost the same as our observed values, which gives us an indication that there is no association
addmargins(round(prop.table(chi_square$observed), 3))
## mydata$romanticF
## mydata$addressF no yes Sum
## Urban 0.516 0.261 0.777
## Rural 0.149 0.073 0.222
## Sum 0.665 0.334 0.999
addmargins(round(prop.table(chi_square$observed, 1), 3), 2)
## mydata$romanticF
## mydata$addressF no yes Sum
## Urban 0.664 0.336 1.000
## Rural 0.670 0.330 1.000
addmargins(round(prop.table(chi_square$observed, 2), 3), 1)
## mydata$romanticF
## mydata$addressF no yes
## Urban 0.776 0.780
## Rural 0.224 0.220
## Sum 1.000 1.000
Explanation of results:
round(chi_square$residuals, 2)
## mydata$romanticF
## mydata$addressF no yes
## Urban -0.03 0.04
## Rural 0.05 -0.08
Explanation of results of standardized residuals:
#install.packages("effectsize")
library(effectsize)
effectsize::cramers_v(mydata$addressF, mydata$romanticF)
## Cramer's V (adj.) | 95% CI
## --------------------------------
## 0.00 | [0.00, 1.00]
##
## - One-sided CIs: upper bound fixed at [1.00].
interpret_cramers_v(0.00)
## [1] "tiny"
## (Rules: funder2019)
Research question 2: Are high school students from rural areas more likely to be in a romantic relationship compared to students from urban areas?