Homework II by Fabian Roth

This Homework investigates the components that influence the outcome of students test results (variable “G3”) Since G3 is numeric (points from 0 to 20) the appropriate method for investigation is PCA

mat2 <- read.table("./mat2.csv", header = TRUE, dec = ".", sep = ",")
head(mat2)
##   X school sex age address famsize Pstatus Medu Fedu     Mjob     Fjob
## 1 0     GP   F  18       U     GT3       A    4    4  at_home  teacher
## 2 1     GP   F  17       U     GT3       T    1    1  at_home    other
## 3 2     GP   F  15       U     LE3       T    1    1  at_home    other
## 4 3     GP   F  15       U     GT3       T    4    2   health services
## 5 4     GP   F  16       U     GT3       T    3    3    other    other
## 6 5     GP   M  16       U     LE3       T    4    3 services    other
##       reason guardian traveltime studytime failures schoolsup famsup paid
## 1     course   mother          2         2        0       yes     no   no
## 2     course   father          1         2        0        no    yes   no
## 3      other   mother          1         2        3       yes     no  yes
## 4       home   mother          1         3        0        no    yes  yes
## 5       home   father          1         2        0        no    yes  yes
## 6 reputation   mother          1         2        0        no    yes  yes
##   activities nursery higher internet romantic famrel freetime goout Dalc Walc
## 1         no     yes    yes       no       no      4        3     4    1    1
## 2         no      no    yes      yes       no      5        3     3    1    1
## 3         no     yes    yes      yes       no      4        3     2    2    3
## 4        yes     yes    yes      yes      yes      3        2     2    1    1
## 5         no     yes    yes       no       no      4        3     2    1    2
## 6        yes     yes    yes      yes       no      5        4     2    1    2
##   health absences G1 G2 G3
## 1      3        6  5  6  6
## 2      3        4  5  5  6
## 3      3       10  7  8 10
## 4      5        2 15 14 15
## 5      5        4  6 10 10
## 6      5       10 15 15 15

Unit of observation G3 sample size n = 395

school student’s school (binary: ‘GP’ - Gabriel Pereira or ‘MS’ - Mousinho da Silveira) sex student’s sex (binary: ‘F’ - female or ‘M’ - male) age student’s age (numeric: from 15 to 22) address student’s home address type (binary: ‘U’ - urban or ‘R’ - rural) famsize family size (binary: ‘LE3’ - less or equal to 3 or ‘GT3’ - greater than 3) Pstatus parent’s cohabitation status (binary: ‘T’ - living together or ‘A’ - apart) Medu mother’s education (numeric: 0 - none, 1 - primary education (4th grade), 2 – 5th to 9th grade, 3 – secondary education or 4 – higher education) Fedu father’s education (numeric: 0 - none, 1 - primary education (4th grade), 2 – 5th to 9th grade, 3 – secondary education or 4 – higher education) Mjob mother’s job (nominal: ‘teacher’, ‘health’ care related, civil ‘services’ (e.g. administrative or police), ‘at_home’ or ‘other’) Fjob father’s job (nominal: ‘teacher’, ‘health’ care related, civil ‘services’ (e.g. administrative or police), ‘at_home’ or ‘other’) reason reason to choose this school (nominal: close to ‘home’, school ‘reputation’, ‘course’ preference or ‘other’) guardian student’s guardian (nominal: ‘mother’, ‘father’ or ‘other’) traveltime home to school travel time (numeric: 1 - <15 min., 2 - 15 to 30 min., 3 - 30 min. to 1 hour, or 4 - >1 hour) studytime weekly study time (numeric: 1 - <2 hours, 2 - 2 to 5 hours, 3 - 5 to 10 hours, or 4 - >10 hours) failures number of past class failures (numeric: n if 1<=n<3, else 4) schoolsup extra educational support (binary: yes or no) famsup family educational support (binary: yes or no) paid extra paid classes within the course subject (Math or Portuguese) (binary: yes or no) activities extra-curricular activities (binary: yes or no) nursery attended nursery school (binary: yes or no) higher wants to take higher education (binary: yes or no) internet Internet access at home (binary: yes or no) romantic with a romantic relationship (binary: yes or no) famrel quality of family relationships (numeric: from 1 - very bad to 5 - excellent) freetime free time after school (numeric: from 1 - very low to 5 - very high) goout going out with friends (numeric: from 1 - very low to 5 - very high) Dalc workday alcohol consumption (numeric: from 1 - very low to 5 - very high) Walc weekend alcohol consumption (numeric: from 1 - very low to 5 - very high) health current health status (numeric: from 1 - very bad to 5 - very good) absences number of school absences (numeric: from 0 to 93) these grades are related with the course subject, Math or Portuguese: G1 - first period grade (numeric: from 0 to 20) G2 - second period grade (numeric: from 0 to 20) G3 - final grade (numeric: from 0 to 20, output target)

soruce of Data is https://www.kaggle.com/datasets/henryshan/student-performance-prediction/data

library(dplyr)
## 
## Attache Paket: 'dplyr'
## Die folgenden Objekte sind maskiert von 'package:stats':
## 
##     filter, lag
## Die folgenden Objekte sind maskiert von 'package:base':
## 
##     intersect, setdiff, setequal, union
#remove unsuitable variables G1, G2 since they represtent parts of the final outcome 
mat3 <- select(mat2, -G1, -G2)
head(mat3)
##   X school sex age address famsize Pstatus Medu Fedu     Mjob     Fjob
## 1 0     GP   F  18       U     GT3       A    4    4  at_home  teacher
## 2 1     GP   F  17       U     GT3       T    1    1  at_home    other
## 3 2     GP   F  15       U     LE3       T    1    1  at_home    other
## 4 3     GP   F  15       U     GT3       T    4    2   health services
## 5 4     GP   F  16       U     GT3       T    3    3    other    other
## 6 5     GP   M  16       U     LE3       T    4    3 services    other
##       reason guardian traveltime studytime failures schoolsup famsup paid
## 1     course   mother          2         2        0       yes     no   no
## 2     course   father          1         2        0        no    yes   no
## 3      other   mother          1         2        3       yes     no  yes
## 4       home   mother          1         3        0        no    yes  yes
## 5       home   father          1         2        0        no    yes  yes
## 6 reputation   mother          1         2        0        no    yes  yes
##   activities nursery higher internet romantic famrel freetime goout Dalc Walc
## 1         no     yes    yes       no       no      4        3     4    1    1
## 2         no      no    yes      yes       no      5        3     3    1    1
## 3         no     yes    yes      yes       no      4        3     2    2    3
## 4        yes     yes    yes      yes      yes      3        2     2    1    1
## 5         no     yes    yes       no       no      4        3     2    1    2
## 6        yes     yes    yes      yes       no      5        4     2    1    2
##   health absences G3
## 1      3        6  6
## 2      3        4  6
## 3      3       10 10
## 4      5        2 15
## 5      5        4 10
## 6      5       10 15
library(dplyr)
#remove unsuitable variables due to violation of numeric - exepct for sex to include one categorical variable in the final result
mat4 <- select(mat3, -school, -address, -famsize, -Pstatus, -Mjob, -Fjob, -reason, -guardian, -failures, -famsup, -schoolsup, -paid, -activities, -nursery, -higher, -internet, -romantic)
head(mat4)
##   X sex age Medu Fedu traveltime studytime famrel freetime goout Dalc Walc
## 1 0   F  18    4    4          2         2      4        3     4    1    1
## 2 1   F  17    1    1          1         2      5        3     3    1    1
## 3 2   F  15    1    1          1         2      4        3     2    2    3
## 4 3   F  15    4    2          1         3      3        2     2    1    1
## 5 4   F  16    3    3          1         2      4        3     2    1    2
## 6 5   M  16    4    3          1         2      5        4     2    1    2
##   health absences G3
## 1      3        6  6
## 2      3        4  6
## 3      3       10 10
## 4      5        2 15
## 5      5        4 10
## 6      5       10 15

Description of remaining variables

age student’s age (numeric: from 15 to 22) Medu mother’s education (numeric: 0 - none, 1 - primary education (4th grade), 2 – 5th to 9th grade, 3 – secondary education or 4 – higher education) Fedu father’s education (numeric: 0 - none, 1 - primary education (4th grade), 2 – 5th to 9th grade, 3 – secondary education or 4 – traveltime home to school travel time (numeric: 1 - <15 min., 2 - 15 to 30 min., 3 - 30 min. to 1 hour, or 4 - >1 hour) studytime weekly study time (numeric: 1 - <2 hours, 2 - 2 to 5 hours, 3 - 5 to 10 hours, or 4 - >10 hours) famrel quality of family relationships (numeric: from 1 - very bad to 5 - excellent) freetime free time after school (numeric: from 1 - very low to 5 - very high) goout going out with friends (numeric: from 1 - very low to 5 - very high) Dalc workday alcohol consumption (numeric: from 1 - very low to 5 - very high) Walc weekend alcohol consumption (numeric: from 1 - very low to 5 - very high) health current health status (numeric: from 1 - very bad to 5 - very good) absences number of school absences (numeric: from 0 to 93) G3 - final grade (numeric: from 0 to 20, output target)

library(psych)
## Warning: Paket 'psych' wurde unter R Version 4.3.2 erstellt
describe(mat4[ ,-1])
##            vars   n  mean   sd median trimmed  mad min max range  skew kurtosis
## sex*          1 395  1.47 0.50      1    1.47 0.00   1   2     1  0.11    -1.99
## age           2 395 16.70 1.28     17   16.63 1.48  15  22     7  0.46    -0.03
## Medu          3 395  2.75 1.09      3    2.82 1.48   0   4     4 -0.32    -1.10
## Fedu          4 395  2.52 1.09      2    2.53 1.48   0   4     4 -0.03    -1.21
## traveltime    5 395  1.45 0.70      1    1.31 0.00   1   4     3  1.59     2.27
## studytime     6 395  2.04 0.84      2    1.96 0.00   1   4     3  0.63    -0.04
## famrel        7 395  3.94 0.90      4    4.04 1.48   1   5     4 -0.94     1.09
## freetime      8 395  3.24 1.00      3    3.23 1.48   1   5     4 -0.16    -0.33
## goout         9 395  3.11 1.11      3    3.09 1.48   1   5     4  0.12    -0.79
## Dalc         10 395  1.48 0.89      1    1.27 0.00   1   5     4  2.17     4.65
## Walc         11 395  2.29 1.29      2    2.15 1.48   1   5     4  0.61    -0.81
## health       12 395  3.55 1.39      4    3.69 1.48   1   5     4 -0.49    -1.03
## absences     13 395  5.71 8.00      4    4.24 5.93   0  75    75  3.64    21.31
## G3           14 395 10.42 4.58     11   10.84 4.45   0  20    20 -0.73     0.37
##              se
## sex*       0.03
## age        0.06
## Medu       0.06
## Fedu       0.05
## traveltime 0.04
## studytime  0.04
## famrel     0.05
## freetime   0.05
## goout      0.06
## Dalc       0.04
## Walc       0.06
## health     0.07
## absences   0.40
## G3         0.23
#check for multicolinearity
library(car)
## Warning: Paket 'car' wurde unter R Version 4.3.2 erstellt
## Lade nötiges Paket: carData
## Warning: Paket 'carData' wurde unter R Version 4.3.2 erstellt
## 
## Attache Paket: 'car'
## Das folgende Objekt ist maskiert 'package:psych':
## 
##     logit
## Das folgende Objekt ist maskiert 'package:dplyr':
## 
##     recode
fit <- lm(G3 ~ age + Medu + Fedu + traveltime + studytime + famrel + freetime + goout + Dalc + Walc + sex,
          data = mat4)
vif(fit)
##        age       Medu       Fedu traveltime  studytime     famrel   freetime 
##   1.075813   1.716558   1.670106   1.067586   1.170898   1.065499   1.204192 
##      goout       Dalc       Walc        sex 
##   1.350066   1.817231   2.108204   1.245217
mean(vif(fit))
## [1] 1.408306

All outcomes are way below 5, such is the average. We can proceed since no too strong multicolinearity was detected

library(pastecs)
## Warning: Paket 'pastecs' wurde unter R Version 4.3.2 erstellt
## 
## Attache Paket: 'pastecs'
## Die folgenden Objekte sind maskiert von 'package:dplyr':
## 
##     first, last
mat4_PCA <- mat4[,c("age", "Medu", "Fedu", "traveltime", "studytime", "famrel", "freetime", "goout", "Dalc", "Walc")]
round(stat.desc(mat4_PCA, basic = FALSE), 2)
##                age Medu Fedu traveltime studytime famrel freetime goout Dalc
## median       17.00 3.00 2.00       1.00      2.00   4.00     3.00  3.00 1.00
## mean         16.70 2.75 2.52       1.45      2.04   3.94     3.24  3.11 1.48
## SE.mean       0.06 0.06 0.05       0.04      0.04   0.05     0.05  0.06 0.04
## CI.mean.0.95  0.13 0.11 0.11       0.07      0.08   0.09     0.10  0.11 0.09
## var           1.63 1.20 1.18       0.49      0.70   0.80     1.00  1.24 0.79
## std.dev       1.28 1.09 1.09       0.70      0.84   0.90     1.00  1.11 0.89
## coef.var      0.08 0.40 0.43       0.48      0.41   0.23     0.31  0.36 0.60
##              Walc
## median       2.00
## mean         2.29
## SE.mean      0.06
## CI.mean.0.95 0.13
## var          1.66
## std.dev      1.29
## coef.var     0.56
mat5 <- scale(mat4_PCA) #standardize Data for same impacts
R <- cor(mat5) #check for correlation between variables
round(R,3)
##               age   Medu   Fedu traveltime studytime famrel freetime  goout
## age         1.000 -0.164 -0.163      0.071    -0.004  0.054    0.016  0.127
## Medu       -0.164  1.000  0.623     -0.172     0.065 -0.004    0.031  0.064
## Fedu       -0.163  0.623  1.000     -0.158    -0.009 -0.001   -0.013  0.043
## traveltime  0.071 -0.172 -0.158      1.000    -0.101 -0.017   -0.017  0.029
## studytime  -0.004  0.065 -0.009     -0.101     1.000  0.040   -0.143 -0.064
## famrel      0.054 -0.004 -0.001     -0.017     0.040  1.000    0.151  0.065
## freetime    0.016  0.031 -0.013     -0.017    -0.143  0.151    1.000  0.285
## goout       0.127  0.064  0.043      0.029    -0.064  0.065    0.285  1.000
## Dalc        0.131  0.020  0.002      0.138    -0.196 -0.078    0.209  0.267
## Walc        0.117 -0.047 -0.013      0.134    -0.254 -0.113    0.148  0.420
##              Dalc   Walc
## age         0.131  0.117
## Medu        0.020 -0.047
## Fedu        0.002 -0.013
## traveltime  0.138  0.134
## studytime  -0.196 -0.254
## famrel     -0.078 -0.113
## freetime    0.209  0.148
## goout       0.267  0.420
## Dalc        1.000  0.648
## Walc        0.648  1.000

#many variables are below threshold. This implies that there might be problems with the interpretability of compontents, also that less variance will be explained and that the result might be unrealiable. Due to short term notice I will proceed anyway. Correctly I should reconsider the variables included or transform them.

library(psych) #check for sphericity
cortest.bartlett(R, n=nrow(mat5)) 
## $chisq
## [1] 639.1945
## 
## $p.value
## [1] 5.043089e-106
## 
## $df
## [1] 45

we can reject Barlett –> we can proceed

det(R)
## [1] 0.1940458

#Determent above 0,00001 –> we can proceed

library(psych)
KMO(R) #checking for suitability of Data
## Kaiser-Meyer-Olkin factor adequacy
## Call: KMO(r = R)
## Overall MSA =  0.59
## MSA for each item = 
##        age       Medu       Fedu traveltime  studytime     famrel   freetime 
##       0.74       0.53       0.53       0.80       0.68       0.52       0.58 
##      goout       Dalc       Walc 
##       0.62       0.61       0.58

All variables are at least miserable. KMO is the same –> we can proceed | Also below would be okay for PCA

#determine the amount of principal components to use
library(FactoMineR)
## Warning: Paket 'FactoMineR' wurde unter R Version 4.3.2 erstellt
components <- PCA(mat5,
                  scale.unit = TRUE,
                  graph = FALSE)
library(factoextra)
## Warning: Paket 'factoextra' wurde unter R Version 4.3.2 erstellt
## Lade nötiges Paket: ggplot2
## Warning: Paket 'ggplot2' wurde unter R Version 4.3.2 erstellt
## 
## Attache Paket: 'ggplot2'
## Die folgenden Objekte sind maskiert von 'package:psych':
## 
##     %+%, alpha
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
get_eigenvalue(components)
##        eigenvalue variance.percent cumulative.variance.percent
## Dim.1   2.2128470        22.128470                    22.12847
## Dim.2   1.7635002        17.635002                    39.76347
## Dim.3   1.2138004        12.138004                    51.90148
## Dim.4   1.0039394        10.039394                    61.94087
## Dim.5   0.9045429         9.045429                    70.98630
## Dim.6   0.8407246         8.407246                    79.39354
## Dim.7   0.7234537         7.234537                    86.62808
## Dim.8   0.6652528         6.652528                    93.28061
## Dim.9   0.3731700         3.731700                    97.01231
## Dim.10  0.2987690         2.987690                   100.00000

#to catch min. 40% –> at least 3 PCs

library(factoextra)
fviz_eig(components,
         choice = "eigenvalue", 
         main = "Scree plot",
         ylab = "Eigenvalue",
         xlab = "Principal component",
         addlabels = TRUE)

#Kaiser rule should be bigger than 1 –> 3 PCs #break at 4 –> 4-1 = 3

library(psych)
fa.parallel(mat5,
            sim = FALSE,
            fa = "pc")

## Parallel analysis suggests that the number of factors =  NA  and the number of components =  3

theoretical and emp cross at 4 –> 4-1 = 3

methods are showing that 3 pcs seem to be the best solution

library(FactoMineR)
components <- PCA(mat5,
                  scale.unit = TRUE,
                  graph = FALSE,
                  ncp = 3)
components
## **Results for the Principal Component Analysis (PCA)**
## The analysis was performed on 395 individuals, described by 10 variables
## *The results are available in the following objects:
## 
##    name               description                          
## 1  "$eig"             "eigenvalues"                        
## 2  "$var"             "results for the variables"          
## 3  "$var$coord"       "coord. for the variables"           
## 4  "$var$cor"         "correlations variables - dimensions"
## 5  "$var$cos2"        "cos2 for the variables"             
## 6  "$var$contrib"     "contributions of the variables"     
## 7  "$ind"             "results for the individuals"        
## 8  "$ind$coord"       "coord. for the individuals"         
## 9  "$ind$cos2"        "cos2 for the individuals"           
## 10 "$ind$contrib"     "contributions of the individuals"   
## 11 "$call"            "summary statistics"                 
## 12 "$call$centre"     "mean of the variables"              
## 13 "$call$ecart.type" "standard error of the variables"    
## 14 "$call$row.w"      "weights for the individuals"        
## 15 "$call$col.w"      "weights for the variables"
print(components$var$cor) #see loadings
##                  Dim.1        Dim.2       Dim.3
## age         0.28782840 -0.308917524  0.22661200
## Medu       -0.14917338  0.851464194 -0.04193421
## Fedu       -0.13903220  0.841128663 -0.10170770
## traveltime  0.28031920 -0.340919590 -0.22345296
## studytime  -0.41392635 -0.018121712  0.19939666
## famrel     -0.04778071  0.001420453  0.76825687
## freetime    0.41763005  0.173937192  0.53934867
## goout       0.60138625  0.234850738  0.31743666
## Dalc        0.77915976  0.142266991 -0.18577695
## Walc        0.83249751  0.115654402 -0.21037933
print(components$var$contrib) #see contribution 
##                 Dim.1        Dim.2      Dim.3
## age         3.7438281 5.411399e+00  4.2307616
## Medu        1.0056139 4.111093e+01  0.1448737
## Fedu        0.8735331 4.011893e+01  0.8522371
## traveltime  3.5510298 6.590652e+00  4.1136274
## studytime   7.7427414 1.862185e-02  3.2755823
## famrel      0.1031701 1.144138e-04 48.6256746
## freetime    7.8819212 1.715574e+00 23.9658015
## goout      16.3438965 3.127579e+00  8.3016976
## Dalc       27.4347897 1.147712e+00  2.8433897
## Walc       31.3194763 7.584882e-01  3.6463544
library(factoextra)
fviz_pca_var(components,
             repel = TRUE)

#since it is 3 PCs we can not tell much from this 2 dimensional diagramm

library(factoextra)
fviz_pca_biplot(components)

#since it is 3 PCs we can not tell nothing about the 3rd dimension

head(components$ind$coord) #we can determine single students location with on the biplot with these numbers
##         Dim.1      Dim.2      Dim.3
## 1 -0.47204730  1.1198185  0.3145785
## 2 -0.76070315 -1.9946775  1.3231318
## 3  0.06686429 -1.5340217 -0.5239336
## 4 -2.33077444  0.4034669 -1.2803286
## 5 -1.16060385  0.4337741 -0.2640424
## 6 -1.00676590  1.1526991  0.9705479

#problem of naming the variables due to low correlation, as already indicated earlier. Dim1 is alcohol consumption Dim2 is problematic to define Dim3 Quality time spend at home