This documentation is for the output of the coursera case study.
#Given that 94 degrees of freedom was used to calculate the st.dev of the residuals: n - k - 1 = 94.
#k = 5, as we have 5 predictors
n <- 94 + 5 + 1
n
## [1] 100
#Assumption: Confidence interval calculation is two-sided.
mars.coef <- -0.02668
mars.std <- 0.01250
mars95 <- mars.coef + c(-1, 1)* qt(0.975, n-1) * mars.std
mars95
## [1] -0.051482712 -0.001877288
Based on the 95% confidence interval calculation, we can reject the null hypothesis that B1 (mars) equates to 0. 0 is not in the confidence interval.
beck.coef <- 0.03227
beck.std <- 0.04024
beck.tval <- beck.coef/beck.std
beck.tval
## [1] 0.8019384
one.tail.p <-pt(beck.tval, df=n-1, lower.tail = FALSE) #degrees of freedom is n-1
final.p <- 2* one.tail.p
final.p
## [1] 0.4245094
The t value of beck is 0.8019384. Feeding the t value into the two-tailed pt function returns 0.4245094, which is greater than the p-value of 0.05. Thus, we fail to reject the null hypothesis of the slope of B5 (beck) being zero.
# Get Sum of Squares Residual
# formula to get st.dev is sqrt(SSRes/n - k -1)
# df = n - k - 1, where n is the number of cases, k is number of predictors
# therefore, one can extract SSRes from the st.dev given.
df <- 94
k <- 5
n <- df + k + 1
residualstd <- 2.388
ssres <- (residualstd*residualstd)*df
ssres
## [1] 536.0391
# Get Sum of Squares Total
# df = n - 1
# Totalstd comes from the observed data, rather than predicted.
totalstd <- 2.85818
sstotal <- (totalstd^2)*(n-1)
sstotal
## [1] 808.7501
# Total Sum of Squares = Regression Sum of Squares + Residual Sum of Squares
ssreg <- sstotal - ssres
ssreg
## [1] 272.711
# Calculate R^2
rsquared <- 1 - (ssres/sstotal)
rsquared
## [1] 0.3372005
# Calculate Adjusted R^2
adj.rsquared <- 1 - ((n-1)*(1-rsquared))/(n-k-1)
adj.rsquared
## [1] 0.3019452
# Get Mean Squares Residual
msr <- ssres/(n-k-1)
msr
## [1] 5.702544
# Get Means Square
ms <- ssreg/k
ms
## [1] 54.54219
# Get F-score
f <- ms/msr
f
## [1] 9.564537
#Then test. Our dfs are k, n-k-1.
fcrit <- qf(p = 0.05, df1 = k, df2 = n-k-1, lower.tail = FALSE)
fcrit
## [1] 2.31127
The model has low R2 and adjusted R2, being 0.33702 and 0.3019 respectively. This suggests that the model has a poor fit. However, that does not mean that the model has no significance. When putting the model through an F-Test, the F-statistic generated, 9.564537, is larger than the f critical value generated, 2.31127. Thus, the model is considered statistically significant overall.
This is to analyze student performance dataset and create a model to predict the G1, G2 and G3 Exams based from the student.csv data.
library(dplyr)
data = read.csv("students.csv")
# Create a subset without G2 and G3 columns
data_G1 <- data %>% select(-G2,-G3)
data_G2 <- data %>% select(-G3)
data_G3 <- data
We create three models based from the subset data exams: for predicting G1, G2, and G3 using all available predictor variables (features) in your dataset. ### Fitting the linear regression model
# Subset the Data by grade
model_G1 <- lm(G1 ~ ., data = data_G1)
model_G2 <- lm(G2 ~ ., data = data_G2)
model_G3 <- lm(G3 ~ ., data = data_G3)
coef(model_G1)
## (Intercept) schoolUP sexM age
## 11.38502920 -0.00996498 0.89429008 -0.07008159
## addressU famsizeLE3 PstatusT Medu
## 0.15071035 0.42917548 0.15429712 0.11794260
## Fedu Mjobhealth Mjobother Mjobservices
## 0.14377426 0.92613740 -0.78228744 0.46653237
## Mjobteacher Fjobhealth Fjobother Fjobservices
## -0.92279023 -0.55337663 -1.13484910 -0.99400828
## Fjobteacher reasonhome reasonother reasonreputation
## 1.18701726 0.16560221 -0.18120667 0.44400360
## guardianmother guardianother traveltime studytime
## 0.05021869 0.86638041 -0.02511939 0.60472503
## failures schoolsupyes famsupyes paidyes
## -1.31418300 -2.15539421 -0.97868131 -0.10238909
## activitiesyes nurseryyes higheryes internetyes
## -0.05272838 0.02958747 1.14060983 0.25541245
## romanticyes famrel freetime goout
## -0.21122278 0.02573315 0.25481668 -0.41359434
## Dalc Walc health absences
## -0.06314586 -0.02533937 -0.16753102 0.01227701
coef(model_G2)
## (Intercept) schoolUP sexM age
## 2.936500114 -0.244176792 0.100421075 -0.129664036
## addressU famsizeLE3 PstatusT Medu
## 0.292215215 0.198791152 -0.379753492 0.205127012
## Fedu Mjobhealth Mjobother Mjobservices
## -0.135890076 0.122588265 0.453888306 0.098136014
## Mjobteacher Fjobhealth Fjobother Fjobservices
## -0.200323261 0.658560261 0.754744997 1.001529524
## Fjobteacher reasonhome reasonother reasonreputation
## 0.127506609 0.108666225 0.700372135 -0.008875641
## guardianmother guardianother traveltime studytime
## -0.189753862 -0.227024173 -0.323196765 -0.017143005
## failures schoolsupyes famsupyes paidyes
## -0.110638156 0.609456046 0.048819882 0.394367723
## activitiesyes nurseryyes higheryes internetyes
## 0.078339035 0.013159148 -0.125856310 0.375343162
## romanticyes famrel freetime goout
## -0.614443728 -0.160722827 -0.028403560 -0.153523475
## Dalc Walc health absences
## -0.017941528 0.119829448 -0.056378250 -0.003346462
## G1
## 0.961239032
coef(model_G3)
## (Intercept) schoolUP sexM age
## -0.634746365 -0.480741671 0.174395802 -0.173301961
## addressU famsizeLE3 PstatusT Medu
## 0.104454940 0.036512193 -0.127673064 0.129685100
## Fedu Mjobhealth Mjobother Mjobservices
## -0.133939779 -0.146425750 0.074088203 0.046956250
## Mjobteacher Fjobhealth Fjobother Fjobservices
## -0.026276281 0.330948299 -0.083581687 -0.322141538
## Fjobteacher reasonhome reasonother reasonreputation
## -0.112363796 -0.209183465 0.307553703 0.129106359
## guardianmother guardianother traveltime studytime
## 0.195740744 0.006565203 0.096994400 -0.104753553
## failures schoolsupyes famsupyes paidyes
## -0.160539154 0.456448111 0.176870112 0.075764279
## activitiesyes nurseryyes higheryes internetyes
## -0.346046820 -0.222715666 0.225920873 -0.144462482
## romanticyes famrel freetime goout
## -0.272008228 0.356875762 0.047001549 0.012006617
## Dalc Walc health absences
## -0.185019471 0.176772037 0.062995003 0.045879080
## G1 G2
## 0.188846822 0.957329932
Using Summary, the data is analyzed to see the accuracy of each model.
summary(model_G1)
##
## Call:
## lm(formula = G1 ~ ., data = data_G1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.5043 -1.9410 -0.0326 1.7997 7.1376
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 11.385029 3.283214 3.468 0.00059 ***
## schoolUP -0.009965 0.549925 -0.018 0.98555
## sexM 0.894290 0.347385 2.574 0.01045 *
## age -0.070082 0.150905 -0.464 0.64264
## addressU 0.150710 0.405805 0.371 0.71057
## famsizeLE3 0.429175 0.339195 1.265 0.20660
## PstatusT 0.154297 0.502913 0.307 0.75917
## Medu 0.117943 0.224515 0.525 0.59969
## Fedu 0.143774 0.192870 0.745 0.45650
## Mjobhealth 0.926137 0.776837 1.192 0.23398
## Mjobother -0.782287 0.495455 -1.579 0.11524
## Mjobservices 0.466532 0.554282 0.842 0.40053
## Mjobteacher -0.922790 0.721274 -1.279 0.20160
## Fjobhealth -0.553377 0.998994 -0.554 0.57997
## Fjobother -1.134849 0.710736 -1.597 0.11122
## Fjobservices -0.994008 0.734310 -1.354 0.17671
## Fjobteacher 1.187017 0.900744 1.318 0.18841
## reasonhome 0.165602 0.384744 0.430 0.66715
## reasonother -0.181207 0.567991 -0.319 0.74989
## reasonreputation 0.444004 0.400557 1.108 0.26841
## guardianmother 0.050219 0.379042 0.132 0.89467
## guardianother 0.866380 0.694357 1.248 0.21295
## traveltime -0.025119 0.235489 -0.107 0.91511
## studytime 0.604725 0.199842 3.026 0.00266 **
## failures -1.314183 0.231280 -5.682 2.77e-08 ***
## schoolsupyes -2.155394 0.463335 -4.652 4.65e-06 ***
## famsupyes -0.978681 0.332560 -2.943 0.00347 **
## paidyes -0.102389 0.331906 -0.308 0.75789
## activitiesyes -0.052728 0.309114 -0.171 0.86465
## nurseryyes 0.029587 0.381623 0.078 0.93825
## higheryes 1.140610 0.748777 1.523 0.12857
## internetyes 0.255412 0.430423 0.593 0.55329
## romanticyes -0.211223 0.326001 -0.648 0.51746
## famrel 0.025733 0.170852 0.151 0.88036
## freetime 0.254817 0.164896 1.545 0.12316
## goout -0.413594 0.155971 -2.652 0.00837 **
## Dalc -0.063146 0.229869 -0.275 0.78370
## Walc -0.025339 0.172300 -0.147 0.88316
## health -0.167531 0.111859 -1.498 0.13510
## absences 0.012277 0.020124 0.610 0.54220
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.854 on 355 degrees of freedom
## Multiple R-squared: 0.3339, Adjusted R-squared: 0.2607
## F-statistic: 4.562 on 39 and 355 DF, p-value: 3.633e-15
summary(model_G2)
##
## Call:
## lm(formula = G2 ~ ., data = data_G2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.8930 -0.7844 0.0055 0.9891 4.9905
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.936500 2.210613 1.328 0.1849
## schoolUP -0.244177 0.364153 -0.671 0.5030
## sexM 0.100421 0.232171 0.433 0.6656
## age -0.129664 0.099957 -1.297 0.1954
## addressU 0.292215 0.268771 1.087 0.2777
## famsizeLE3 0.198791 0.225116 0.883 0.3778
## PstatusT -0.379753 0.333066 -1.140 0.2550
## Medu 0.205127 0.148729 1.379 0.1687
## Fedu -0.135890 0.127816 -1.063 0.2884
## Mjobhealth 0.122588 0.515439 0.238 0.8121
## Mjobother 0.453888 0.329234 1.379 0.1689
## Mjobservices 0.098136 0.367404 0.267 0.7895
## Mjobteacher -0.200323 0.478718 -0.418 0.6759
## Fjobhealth 0.658560 0.661806 0.995 0.3204
## Fjobother 0.754745 0.472327 1.598 0.1110
## Fjobservices 1.001530 0.487503 2.054 0.0407 *
## Fjobteacher 0.127507 0.597917 0.213 0.8313
## reasonhome 0.108666 0.254838 0.426 0.6701
## reasonother 0.700372 0.376170 1.862 0.0635 .
## reasonreputation -0.008876 0.265702 -0.033 0.9734
## guardianmother -0.189754 0.251003 -0.756 0.4502
## guardianother -0.227024 0.460800 -0.493 0.6225
## traveltime -0.323197 0.155940 -2.073 0.0389 *
## studytime -0.017143 0.134029 -0.128 0.8983
## failures -0.110638 0.159963 -0.692 0.4896
## schoolsupyes 0.609456 0.316027 1.928 0.0546 .
## famsupyes 0.048820 0.222887 0.219 0.8267
## paidyes 0.394368 0.219813 1.794 0.0736 .
## activitiesyes 0.078339 0.204700 0.383 0.7022
## nurseryyes 0.013159 0.252707 0.052 0.9585
## higheryes -0.125856 0.497448 -0.253 0.8004
## internetyes 0.375343 0.285162 1.316 0.1889
## romanticyes -0.614444 0.216001 -2.845 0.0047 **
## famrel -0.160723 0.113139 -1.421 0.1563
## freetime -0.028404 0.109559 -0.259 0.7956
## goout -0.153523 0.104300 -1.472 0.1419
## Dalc -0.017942 0.152232 -0.118 0.9062
## Walc 0.119829 0.114098 1.050 0.2943
## health -0.056378 0.074305 -0.759 0.4485
## absences -0.003346 0.013333 -0.251 0.8020
## G1 0.961239 0.035145 27.351 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.89 on 354 degrees of freedom
## Multiple R-squared: 0.7732, Adjusted R-squared: 0.7476
## F-statistic: 30.17 on 40 and 354 DF, p-value: < 2.2e-16
summary(model_G3)
##
## Call:
## lm(formula = G3 ~ ., data = data_G3)
##
## Residuals:
## Min 1Q Median 3Q Max
## -7.9339 -0.5532 0.2680 0.9689 4.6461
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.634746 2.229058 -0.285 0.775995
## schoolUP -0.480742 0.366512 -1.312 0.190485
## sexM 0.174396 0.233588 0.747 0.455805
## age -0.173302 0.100780 -1.720 0.086380 .
## addressU 0.104455 0.270791 0.386 0.699922
## famsizeLE3 0.036512 0.226680 0.161 0.872128
## PstatusT -0.127673 0.335626 -0.380 0.703875
## Medu 0.129685 0.149999 0.865 0.387859
## Fedu -0.133940 0.128768 -1.040 0.298974
## Mjobhealth -0.146426 0.518491 -0.282 0.777796
## Mjobother 0.074088 0.332044 0.223 0.823565
## Mjobservices 0.046956 0.369587 0.127 0.898973
## Mjobteacher -0.026276 0.481632 -0.055 0.956522
## Fjobhealth 0.330948 0.666601 0.496 0.619871
## Fjobother -0.083582 0.476796 -0.175 0.860945
## Fjobservices -0.322142 0.493265 -0.653 0.514130
## Fjobteacher -0.112364 0.601448 -0.187 0.851907
## reasonhome -0.209183 0.256392 -0.816 0.415123
## reasonother 0.307554 0.380214 0.809 0.419120
## reasonreputation 0.129106 0.267254 0.483 0.629335
## guardianmother 0.195741 0.252672 0.775 0.439046
## guardianother 0.006565 0.463650 0.014 0.988710
## traveltime 0.096994 0.157800 0.615 0.539170
## studytime -0.104754 0.134814 -0.777 0.437667
## failures -0.160539 0.161006 -0.997 0.319399
## schoolsupyes 0.456448 0.319538 1.428 0.154043
## famsupyes 0.176870 0.224204 0.789 0.430710
## paidyes 0.075764 0.222100 0.341 0.733211
## activitiesyes -0.346047 0.205938 -1.680 0.093774 .
## nurseryyes -0.222716 0.254184 -0.876 0.381518
## higheryes 0.225921 0.500398 0.451 0.651919
## internetyes -0.144462 0.287528 -0.502 0.615679
## romanticyes -0.272008 0.219732 -1.238 0.216572
## famrel 0.356876 0.114124 3.127 0.001912 **
## freetime 0.047002 0.110209 0.426 0.670021
## goout 0.012007 0.105230 0.114 0.909224
## Dalc -0.185019 0.153124 -1.208 0.227741
## Walc 0.176772 0.114943 1.538 0.124966
## health 0.062995 0.074800 0.842 0.400259
## absences 0.045879 0.013412 3.421 0.000698 ***
## G1 0.188847 0.062373 3.028 0.002645 **
## G2 0.957330 0.053460 17.907 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.901 on 353 degrees of freedom
## Multiple R-squared: 0.8458, Adjusted R-squared: 0.8279
## F-statistic: 47.21 on 41 and 353 DF, p-value: < 2.2e-16
Model 1:
# Identify outliers using Leverage Point
par(mfrow =c(2,2),mar=c(2,2,2,2))
plot(model_G1)
plot(hatvalues(model_G1))
abline(h=40/395, col="red")
rstudent(model_G1)
## 1 2 3 4 5 6
## -2.436643166 -1.949997221 0.412688392 1.000639819 -1.450247394 0.512657153
## 7 8 9 10 11 12
## -0.068637825 -1.397944285 1.081462413 0.622967596 -0.508018729 -0.741361020
## 13 14 15 16 17 18
## 0.557206278 -0.444793344 0.379049743 0.835618083 0.134814550 -0.144892145
## 19 20 21 22 23 24
## -0.384146960 -1.760838838 -0.337508726 -0.405050897 0.656514940 0.735258125
## 25 26 27 28 29 30
## -0.118672573 -0.719332945 0.672628797 0.914412875 0.219781211 -0.829869381
## 31 32 33 34 35 36
## -1.642793039 1.230828937 1.722383572 -1.835542223 0.626057390 -0.712359518
## 37 38 39 40 41 42
## 0.903807019 0.363994136 0.584932414 1.868761175 -0.766752006 0.113775548
## 43 44 45 46 47 48
## 1.637364923 -1.058964037 0.533205928 -0.012043968 0.402111050 1.121198666
## 49 50 51 52 53 54
## 1.701521301 -1.172123954 0.256311893 -0.268952042 -0.170796363 -0.041784531
## 55 56 57 58 59 60
## -0.125557834 -0.702668809 0.509156965 0.825056919 -0.485531223 1.518491485
## 61 62 63 64 65 66
## -1.281857216 1.200951711 -0.781946556 0.490244217 -0.360049969 1.767101954
## 67 68 69 70 71 72
## 0.259658355 -1.000113382 -0.576454018 1.321915003 0.253336577 -1.184835624
## 73 74 75 76 77 78
## 1.466724642 0.254927017 1.024033712 -0.617284212 -0.905899161 -0.334855903
## 79 80 81 82 83 84
## 1.468663011 -2.344822398 0.729775071 0.154664645 -1.715786700 0.547959743
## 85 86 87 88 89 90
## -0.320470844 -1.018304379 -0.948094625 0.531575381 -0.621736528 -1.290246935
## 91 92 93 94 95 96
## -1.214390611 1.372287485 -0.394575844 0.194385193 -1.201766961 -0.475758183
## 97 98 99 100 101 102
## 0.494015062 -0.011528920 -0.049722863 -0.727175171 -0.716842272 0.344672538
## 103 104 105 106 107 108
## -0.605630071 -0.685739061 1.332923804 -0.290280706 -0.706886531 1.204860351
## 109 110 111 112 113 114
## -0.530208451 0.558166127 1.262832644 -0.660385707 0.771433700 1.915486542
## 115 116 117 118 119 120
## -1.880005569 0.762606134 -1.182403854 0.380841601 0.257888618 0.678186720
## 121 122 123 124 125 126
## 1.547093729 1.011427739 0.914258311 0.739938304 -0.810790573 1.043750377
## 127 128 129 130 131 132
## -1.162947536 0.435110404 -0.612487487 1.879294467 0.298885890 -0.486771931
## 133 134 135 136 137 138
## 0.381603378 -0.014109980 -1.646780726 -0.688869012 -0.512564299 -1.249466181
## 139 140 141 142 143 144
## 1.155896079 1.090571609 -1.242471470 -0.295083290 -0.640296950 0.621125494
## 145 146 147 148 149 150
## -0.100773090 -0.721616991 -0.927011979 -0.212575789 -1.707801707 0.089353454
## 151 152 153 154 155 156
## 0.496865626 0.832279994 -0.321145180 -0.867012675 0.434912977 0.233682905
## 157 158 159 160 161 162
## 1.811861089 1.926112693 1.978059308 0.500431736 -0.338611821 -0.234433265
## 163 164 165 166 167 168
## -0.789025803 -0.366762566 0.028552724 0.706178411 0.193763213 0.553789373
## 169 170 171 172 173 174
## -0.891678464 0.187690420 -0.442751016 0.776863260 0.627288560 0.411361542
## 175 176 177 178 179 180
## -0.052819125 -0.591797693 0.448763047 -2.054733162 0.099399058 -0.293297606
## 181 182 183 184 185 186
## -0.810864157 -0.291833235 1.544113740 -1.005837866 0.396962835 0.230981196
## 187 188 189 190 191 192
## 0.048163886 1.430949244 -1.426258604 -1.304051994 -0.607369594 -1.241867079
## 193 194 195 196 197 198
## -2.004328737 -1.474589463 0.573138764 0.487222472 0.808209837 -0.335901076
## 199 200 201 202 203 204
## 2.305793869 -1.948330961 1.724325951 -0.005167968 -0.123501237 -1.138819918
## 205 206 207 208 209 210
## -0.553402567 -0.127616749 0.127113326 -0.129054217 -0.240161144 -1.499019206
## 211 212 213 214 215 216
## -1.589933017 0.063280694 1.730684420 -1.492800844 -1.293105072 0.794495466
## 217 218 219 220 221 222
## -0.935911867 -1.886679064 -1.026928584 -1.122400547 -1.259988174 -1.027490594
## 223 224 225 226 227 228
## 1.112252974 0.689738461 0.658725484 -0.168744307 1.985818721 -0.339646541
## 229 230 231 232 233 234
## 0.228525344 0.501840209 -0.111120094 -0.231901491 -0.151723950 0.440782544
## 235 236 237 238 239 240
## -0.916662153 -0.980923473 0.734498955 0.766313234 0.609836808 -1.030998877
## 241 242 243 244 245 246
## -0.483052556 -0.294355283 -1.904110554 -0.032319551 -1.279963640 2.600277462
## 247 248 249 250 251 252
## 0.163196352 -0.086135449 -2.415961792 0.911709209 -1.050307049 -1.547351190
## 253 254 255 256 257 258
## -0.764942330 -1.029288217 -1.074373151 -1.138672945 0.862401087 -1.656355866
## 259 260 261 262 263 264
## 1.499725423 -1.713319041 2.053189609 -1.156856873 0.255049933 -0.395393251
## 265 266 267 268 269 270
## -0.750210149 2.373831415 -0.994130954 0.496187787 -0.661785043 -0.623212933
## 271 272 273 274 275 276
## 0.696893557 1.428493517 0.388921875 0.752967583 -0.724316325 1.289750838
## 277 278 279 280 281 282
## -0.145069377 -0.741741061 -0.008314447 -0.654263401 -1.674333329 0.294357427
## 283 284 285 286 287 288
## 0.016260890 -0.051138907 0.386990390 0.175377863 2.181382369 -0.422891261
## 289 290 291 292 293 294
## 0.626347154 0.199354444 0.486305948 0.681565807 -0.148911870 2.018361950
## 295 296 297 298 299 300
## 0.081693476 0.705519053 -0.963318111 -0.327142617 0.860894595 0.780445064
## 301 302 303 304 305 306
## 0.170772148 -1.217879412 1.451969756 1.332945809 1.713612488 0.541461411
## 307 308 309 310 311 312
## 1.594065237 -0.754938027 1.678373022 1.219438254 -0.641794091 1.159897743
## 313 314 315 316 317 318
## 0.462591848 0.448514974 1.709788348 0.612452194 -1.092560368 -0.260061804
## 319 320 321 322 323 324
## 0.204999961 0.589402875 0.651795549 0.488367437 -0.461296145 0.185589091
## 325 326 327 328 329 330
## 0.891235098 -1.319386949 1.284507936 0.326714864 -0.187338484 0.510973704
## 331 332 333 334 335 336
## -0.900074235 0.338816786 -1.788563037 -1.474249421 -0.694077683 1.354286856
## 337 338 339 340 341 342
## 0.511849286 -1.170835913 0.863688454 -0.288902006 -0.142275682 0.004568187
## 343 344 345 346 347 348
## 0.962369252 -0.669754467 -0.106745516 0.449508689 1.249749114 -0.062020759
## 349 350 351 352 353 354
## 0.137911967 1.230246344 -0.085472723 0.160241811 -0.639851550 -0.206273845
## 355 356 357 358 359 360
## 0.451732584 -0.060455996 0.939925084 0.103125701 -0.584462250 1.987313575
## 361 362 363 364 365 366
## 0.953042163 1.418356869 -0.129222400 1.448455703 0.577469606 -0.143419256
## 367 368 369 370 371 372
## 0.225258774 -0.605587910 0.588570633 0.944109853 -1.344212057 1.811701342
## 373 374 375 376 377 378
## 0.182899848 -1.493575936 2.324819060 -1.209295949 1.777395590 -1.360053308
## 379 380 381 382 383 384
## 1.595955668 -0.456739303 0.490616124 -1.165876378 -0.471793736 -1.087859034
## 385 386 387 388 389 390
## -1.408852636 -0.454568947 -1.696832656 -1.653304940 -1.097909049 -1.023259367
## 391 392 393 394 395
## -0.304769600 0.665937671 0.983669972 -0.789876679 -1.314143595
outliers_G1 <- which(abs(rstudent(model_G1)) > 2)
Model 2:
# Identify outliers using Leverage Point
par(mfrow =c(2,2),mar=c(2,2,2,2))
plot(model_G2)
plot(hatvalues(model_G2))
abline(h=40/395, col="red")
rstudent(model_G2)
## 1 2 3 4 5
## 1.1153568139 0.2534716004 -0.3380187708 -0.7918572305 1.8208188412
## 6 7 8 9 10
## -0.2309899950 -0.0529447421 -0.5281764268 0.4305991259 0.2396055078
## 11 12 13 14 15
## -1.2518799838 1.4154208495 -0.6222471457 0.0849851432 1.1423928992
## 16 17 18 19 20
## 0.1338608763 0.1607776293 1.2633745643 -0.3523609272 0.3420960104
## 21 22 23 24 25
## 0.5148534659 1.1260653982 -0.1430714865 0.1018377809 -0.6858863835
## 26 27 28 29 30
## 1.0802901590 -0.4629585249 -0.5972019635 -0.4032227284 1.8024578025
## 31 32 33 34 35
## 0.5708113799 -0.5095170181 0.5775252325 0.7489660772 0.8576202824
## 36 37 38 39 40
## -0.9367002531 0.5388639696 1.2644733212 -0.1909529230 -0.7148303558
## 41 42 43 44 45
## 1.8643721968 0.3958381485 -0.1013483321 -0.6937857865 0.1248150673
## 46 47 48 49 50
## -0.7856347248 -0.1661856730 -0.0839189916 0.0550344948 -0.3762437945
## 51 52 53 54 55
## 0.5919925731 0.3654542134 -0.2736242701 0.3462167758 0.6110469588
## 56 57 58 59 60
## 0.1029698287 -0.1365852060 0.9206757869 0.2667706197 0.6735258935
## 61 62 63 64 65
## 0.8180991107 -0.5115442123 0.4383481228 -1.1105566109 -0.5103296885
## 66 67 68 69 70
## -0.1668125411 -0.6841360189 -0.5849187335 0.0382926890 -0.1461868575
## 71 72 73 74 75
## 0.7811798946 -0.3948772996 -1.0299919745 0.0002385391 -0.2976479405
## 76 77 78 79 80
## -0.3018866736 -0.0605721843 0.1348285210 -0.4110900984 -0.0243326158
## 81 82 83 84 85
## 0.2374086299 -1.1548039981 -0.7415705349 -0.1872556525 0.1248416991
## 86 87 88 89 90
## 1.3816418784 -0.2487074874 0.1916521073 -0.5263100806 -1.2145167627
## 91 92 93 94 95
## -0.1291656121 0.4316880463 -1.4630121095 -0.6429102510 1.2842939332
## 96 97 98 99 100
## 1.3292226832 2.1643431006 0.9999807675 1.6432699424 1.0461302213
## 101 102 103 104 105
## -1.1173718424 0.9646182170 1.7552081477 -0.8605034442 1.0084747600
## 106 107 108 109 110
## 0.1585377533 -0.1305800741 1.2820379827 1.7857265330 0.7232167502
## 111 112 113 114 115
## 1.0604346782 1.3727875531 1.4379911457 0.5168079591 0.4205181566
## 116 117 118 119 120
## 0.7235572111 1.5217364197 0.4536266177 -0.4372979347 -0.7294672421
## 121 122 123 124 125
## -0.4607407214 -1.2423287247 0.2153730291 -1.6691524228 -0.2508655809
## 126 127 128 129 130
## -0.1456271861 1.1265935837 1.4201215374 -1.3277591603 0.5821039519
## 131 132 133 134 135
## -5.8667862326 -3.6953945977 1.8092508861 -0.9737060325 -3.2422963825
## 136 137 138 139 140
## -5.4924053884 -4.4567644089 -2.0541844333 -0.2838802070 0.8239560364
## 141 142 143 144 145
## 0.4780187713 -0.2283060735 0.8646761040 0.5531210795 -2.5827350725
## 146 147 148 149 150
## 1.0443535064 0.1783594126 0.6100751037 0.1235196484 0.9695089959
## 151 152 153 154 155
## -0.3905962187 1.1256340173 0.2402925343 -1.7647486483 0.4193006667
## 156 157 158 159 160
## -1.7822417109 -2.0253411156 0.7719757817 -0.1713345435 1.0572450995
## 161 162 163 164 165
## 0.0053400737 2.2879604684 -3.6119316220 0.0265652192 2.9757160156
## 166 167 168 169 170
## -0.3546792173 -0.1854528222 0.4292685589 0.4556726746 0.3082016961
## 171 172 173 174 175
## -0.4553189003 1.2526306697 -1.0618868012 0.3432249334 0.5460631742
## 176 177 178 179 180
## -0.5167343680 0.0975050452 -0.8391334612 -1.2824286214 0.2092043686
## 181 182 183 184 185
## -0.8984471172 0.6571931690 0.9987503426 0.2225941073 -0.1975514435
## 186 187 188 189 190
## 0.0069211165 0.2252688905 -0.0461592538 -0.4543575512 0.8605207512
## 191 192 193 194 195
## 0.7146716158 0.2528362597 0.2787082172 0.4036745204 0.7469139113
## 196 197 198 199 200
## 1.0035218961 -0.4389966198 -0.0136129069 0.8565467360 0.1709922625
## 201 202 203 204 205
## -0.0657106473 0.6758647068 -0.0004396913 -0.4131254616 0.5069340585
## 206 207 208 209 210
## -0.2485794926 -0.5033432798 0.1101281387 0.1571571815 0.6192471851
## 211 212 213 214 215
## -0.1295034262 0.1792859805 -0.1369899468 0.4879942182 0.7808260995
## 216 217 218 219 220
## 0.3856409072 -0.0025690532 -0.3805070509 0.0463046498 1.0627680320
## 221 222 223 224 225
## 0.4898071250 0.4514579776 -0.3002943265 0.3040960119 0.1935225138
## 226 227 228 229 230
## 0.0816700379 -0.4093659318 -0.8164656770 -1.0105637301 -0.9142515942
## 231 232 233 234 235
## 0.1370499607 -0.0958431482 -0.4149886324 -0.9581307594 -1.4152439367
## 236 237 238 239 240
## -0.5410635471 -0.8848418018 -0.2189066256 -0.6008748100 -0.2701238474
## 241 242 243 244 245
## 0.4012406050 0.4816495921 -3.1516285721 -0.5766626002 -3.4895614011
## 246 247 248 249 250
## 0.4569509277 0.1892101783 1.4790571731 1.2886535289 0.5577853365
## 251 252 253 254 255
## 1.4526802259 1.8213480678 1.1908988807 0.5998014166 2.1033132860
## 256 257 258 259 260
## 1.4713911435 -1.2631541129 0.1240866879 -0.3655074040 -0.7942116161
## 261 262 263 264 265
## 0.5223970409 -0.0355686669 -0.1371590678 -0.4341624771 0.8111087948
## 266 267 268 269 270
## -0.0238193612 -0.2013627649 -0.4482219079 -0.3081501397 -2.7058965130
## 271 272 273 274 275
## -0.0113828271 -0.3214977442 0.2299848782 0.5143027360 0.7946977946
## 276 277 278 279 280
## 0.2163784394 -0.3339189683 -0.0870044853 -0.3818766089 0.4097972014
## 281 282 283 284 285
## -0.1957591218 -1.3043689217 0.6666429576 0.6862373419 -0.1916325279
## 286 287 288 289 290
## -1.3140745537 0.3075894355 -0.0269027832 -0.6704492692 -0.7757907308
## 291 292 293 294 295
## -0.4677994324 -0.2927442540 1.2585308563 0.3047791639 -0.2435719072
## 296 297 298 299 300
## -1.1826879749 -0.5462916343 -0.8632849306 -0.7655670894 -0.2111583223
## 301 302 303 304 305
## -0.7348346280 -0.1880360398 -1.7095976752 -0.0051418895 0.1323145415
## 306 307 308 309 310
## -0.2618603805 1.2372415282 1.4465015887 -0.9032468621 -1.0663184200
## 311 312 313 314 315
## 0.8514598708 -0.6411498144 -0.8670389664 -1.2104084881 -0.0671812183
## 316 317 318 319 320
## -0.1732878823 0.1002044523 0.7643024898 -0.0508029416 0.2845690852
## 321 322 323 324 325
## -0.0157052914 -0.5370019825 -0.2690144513 0.8745885918 0.2471584432
## 326 327 328 329 330
## 1.5823376658 0.4601674287 0.3859768513 -0.4745904110 1.0856695155
## 331 332 333 334 335
## -0.5422420194 2.1460871746 -3.6752459528 0.2531174257 0.5232099675
## 336 337 338 339 340
## 0.0632078457 0.3154181399 0.2667127101 -0.3699854368 0.0179413630
## 341 342 343 344 345
## 0.8760877895 0.0030331164 0.0018017370 -0.1042273208 -0.1084286981
## 346 347 348 349 350
## -0.1202138812 0.2621978172 0.6388303554 1.2700736262 1.2733613448
## 351 352 353 354 355
## 0.0841563120 0.0499058604 -0.2147834163 0.6199852505 -0.7160778768
## 356 357 358 359 360
## 0.1585955482 0.2767816216 0.2261143592 -0.1089542340 -0.7440423297
## 361 362 363 364 365
## 1.3488898772 -0.2618274381 -0.0337206218 -0.0847951579 -0.3732903199
## 366 367 368 369 370
## 0.5386621846 -0.0898106983 -0.3120895344 -0.1089956019 -0.7616503794
## 371 372 373 374 375
## 0.4751780018 -1.0863720422 -0.2716866051 -0.5201861323 -0.0287354101
## 376 377 378 379 380
## 0.5706619922 0.2438145233 0.3919417410 0.0069652341 -0.1281278778
## 381 382 383 384 385
## -0.5121978298 -0.5140648615 -0.1399675374 -0.6360457989 -0.6599469060
## 386 387 388 389 390
## -0.3240540958 0.7289112022 -0.5876993125 0.7893446418 -0.5795958538
## 391 392 393 394 395
## -0.0310909515 0.8551881120 -0.2948023613 0.7853829555 0.6708795878
outliers_G2 <- which(abs(rstudent(model_G2)) > 2)
Model 3:
# Identify outliers using Leverage Point
par(mfrow =c(2,2),mar=c(2,2,2,2))
plot(model_G3)
plot(hatvalues(model_G3))
abline(h=40/395, col="red")
rstudent(model_G3)
## 1 2 3 4 5
## 0.4907872219 1.0846548868 1.2140275869 1.0296848161 0.5650774225
## 6 7 8 9 10
## -0.8073797203 -0.3304841166 0.9230598887 0.2616118284 -0.1429085055
## 11 12 13 14 15
## 0.5772718154 0.0818105613 -0.0811731862 0.1865307977 0.1593243249
## 16 17 18 19 20
## -0.0240167106 0.3784711646 -0.1235185502 0.1564914139 0.7571188370
## 21 22 23 24 25
## 0.5183242026 -0.5326088138 0.1889261169 -1.0063345510 -0.5377938825
## 26 27 28 29 30
## 0.3775285100 -0.7469529421 -0.6936148634 -0.1787189786 -0.3019214439
## 31 32 33 34 35
## 0.6425937359 0.2417315487 -0.2494454353 1.4155523442 0.1786229984
## 36 37 38 39 40
## -0.0996780785 0.9863181862 -0.3212663887 -0.7489161261 -0.5022261483
## 41 42 43 44 45
## 0.9902005562 -0.5134727608 -0.4144085353 1.7573288389 -0.5570052873
## 46 47 48 49 50
## -1.2860267949 -0.3588540761 0.5257878660 -1.0425285251 0.2577749212
## 51 52 53 54 55
## -0.0270977811 -0.1915356358 -1.5843648470 0.7982882922 -0.2515973754
## 56 57 58 59 60
## 0.7673589262 0.0767527455 -0.2545994776 -0.2780281839 -0.2866671162
## 61 62 63 64 65
## 0.5766071426 1.5505010524 0.1657411766 0.0039519851 0.3100054038
## 66 67 68 69 70
## -0.3172168357 0.1715142418 -0.5711358357 -0.5181243606 -0.8294772560
## 71 72 73 74 75
## -0.1304034526 0.3838500126 -0.4130145723 0.7797606542 -1.8640318127
## 76 77 78 79 80
## 0.6719632663 -0.3659309480 0.1133411391 1.8297586467 0.7968630450
## 81 82 83 84 85
## 0.3332396708 0.0364341701 -0.1100918996 -0.3713890257 0.4329816951
## 86 87 88 89 90
## 0.1251860552 -0.3281219661 -0.3896962228 -0.0018636881 -0.1980072764
## 91 92 93 94 95
## 1.1341661487 0.0781694718 0.2958553052 0.3304555379 0.4565595291
## 96 97 98 99 100
## 1.0740563472 -0.0233876108 0.8656313980 -0.0408991362 -0.5237281543
## 101 102 103 104 105
## -1.2832039793 0.0668210670 0.3999403855 -0.4931396629 -0.3099419929
## 106 107 108 109 110
## -0.4400516802 -0.0379312383 -0.0489760246 1.0937235043 0.2627569537
## 111 112 113 114 115
## -0.7288739552 0.9213491531 0.7004749751 -0.6698645439 0.3222930734
## 116 117 118 119 120
## 0.2890904551 0.7175150939 -0.3025027536 0.3370586439 -0.1850302856
## 121 122 123 124 125
## 0.0703966311 0.2002566983 0.1393315957 0.5306836894 1.0068663256
## 126 127 128 129 130
## -0.6745208789 1.2649169308 1.9540466772 -1.2538635013 -0.5104727733
## 131 132 133 134 135
## 0.3670154601 0.1499507684 -0.2970449100 -0.2403590451 -0.1859431104
## 136 137 138 139 140
## 0.1870336413 -0.3447264652 0.9846942262 -0.2025171603 -0.6307972374
## 141 142 143 144 145
## -4.2889587458 0.9462683016 0.6436106071 -0.3654341397 0.4472477615
## 146 147 148 149 150
## 0.4519629694 -2.4895617878 0.0720530959 -2.4378055856 0.4446309305
## 151 152 153 154 155
## -1.4695534654 0.5268110640 0.6952581440 1.2482614678 0.6747701009
## 156 157 158 159 160
## 0.1775744235 0.4529839769 1.1258285935 -0.6826539241 0.4771503929
## 161 162 163 164 165
## -2.0617686932 -0.9727129588 0.2374182435 0.4027125580 0.3419721197
## 166 167 168 169 170
## 0.3441925728 0.2078972269 1.1312581692 -3.4922237645 -0.0971781607
## 171 172 173 174 175
## -2.0043494088 0.6203997044 -0.6989205872 -2.5039098092 -0.9249446311
## 176 177 178 179 180
## 0.2877239883 -1.2653129908 0.9230492616 0.6010909464 0.8848513407
## 181 182 183 184 185
## 0.3390631276 -0.0662102342 0.0099879995 -1.5217951798 -0.3178790115
## 186 187 188 189 190
## -0.5153012220 -0.0556153643 -0.0265279617 1.5336835859 1.1194895657
## 191 192 193 194 195
## 0.8899175938 1.3666986346 0.3465180254 0.8616586785 0.0815298978
## 196 197 198 199 200
## 0.8593562714 0.0019672625 0.9546574203 -0.7682767001 0.7505472999
## 201 202 203 204 205
## -0.2353166917 -0.0674588282 0.5307172006 -0.1876587501 0.6549271344
## 206 207 208 209 210
## 0.1101739374 0.8274949788 1.4559981451 0.3554565455 0.7103235993
## 211 212 213 214 215
## 0.7061129742 0.2996600330 0.4468919654 0.9843736539 -0.2415448661
## 216 217 218 219 220
## -0.2579141685 -0.6539266645 1.5748167356 1.1335088881 0.5765305716
## 221 222 223 224 225
## 0.7562593595 -1.7604410771 0.5277514433 0.4698961793 0.7905451842
## 226 227 228 229 230
## -0.2579707862 -0.6487348405 0.3054486802 0.3253694512 1.4686680791
## 231 232 233 234 235
## 0.9919122836 0.4131497834 -0.3078297482 0.0285584769 -0.6513861094
## 236 237 238 239 240
## 0.3681032824 0.3090618480 -0.3418558697 0.7902253508 -3.2454552822
## 241 242 243 244 245
## 0.1800333369 0.8346732434 0.2368098958 -0.0255711092 0.5754728846
## 246 247 248 249 250
## -0.7524743160 0.6298941811 0.9589109480 0.9746052125 -0.3885956811
## 251 252 253 254 255
## 0.6232701275 -0.4032952899 0.0322427342 -0.1834912071 0.1866401736
## 256 257 258 259 260
## 0.0651910547 0.6398696958 -0.0643231272 -0.1996588200 -3.7191809895
## 261 262 263 264 265
## -0.1983436787 0.3412223140 0.0543158909 0.7253305289 -4.3235375199
## 266 267 268 269 270
## -0.4329724409 0.5996410186 0.8441178582 0.4958663742 0.7289192143
## 271 272 273 274 275
## 0.9129630224 0.4058879859 0.1400720999 0.4123382371 0.3775974832
## 276 277 278 279 280
## -0.3014842954 -1.2523785433 0.2958110028 0.6402187367 -0.0931338977
## 281 282 283 284 285
## -0.5088619061 0.4011358071 0.2863145136 0.9673403831 0.8422197556
## 286 287 288 289 290
## 0.4338445644 0.0617378233 -0.0378204890 -0.0076191941 0.7152672117
## 291 292 293 294 295
## -0.0853706002 0.0663540432 0.4420046330 -0.3607048582 0.2183309502
## 296 297 298 299 300
## -0.8535305888 -3.9674127712 0.2868316383 0.7393421041 1.2165644373
## 301 302 303 304 305
## 0.7844484676 -0.1572192889 0.7726366597 0.0639880485 -0.3626317627
## 306 307 308 309 310
## 0.2978126752 -0.1380729012 -0.5817615212 0.4932970312 -0.3389331079
## 311 312 313 314 315
## -3.6607982344 0.5943674837 0.4336098662 0.2630583357 0.2517000326
## 316 317 318 319 320
## -0.0889092254 -3.9875753012 0.0378023201 -0.0922656231 0.2926801660
## 321 322 323 324 325
## -0.6317632637 0.0769433943 0.8428862060 0.8701653201 0.4910181541
## 326 327 328 329 330
## 0.2824033660 0.3877075960 -0.2018601819 0.1423957939 -0.0840399494
## 331 332 333 334 335
## 0.3731013625 0.5873206180 0.9004048682 -3.2533068019 -4.2631000437
## 336 337 338 339 340
## -0.5592022796 -0.3063706577 -3.6372996591 1.1531680284 0.3385708752
## 341 342 343 344 345
## 0.5224311960 -4.4674516519 -0.0191528099 -3.6497165450 0.3643966348
## 346 347 348 349 350
## 0.5858901325 0.3803957358 -0.3874044036 0.2905455214 -0.0286016951
## 351 352 353 354 355
## 1.2282028384 -0.3717135356 1.2913443527 0.4074961169 -0.3336348898
## 356 357 358 359 360
## 0.2694645380 0.0187748256 -0.2840807820 0.4919837189 -0.3225656067
## 361 362 363 364 365
## -0.0876969272 -0.2183665147 -0.5126313255 0.3824862980 0.8469696574
## 366 367 368 369 370
## 0.1079877601 0.0798107145 -3.0824293165 0.1814437942 -0.7014455859
## 371 372 373 374 375
## 2.6396836035 0.0844053306 0.3140694118 0.4612072462 -0.0484542165
## 376 377 378 379 380
## 1.5113770927 0.4940021199 1.0820115221 0.1806768256 -0.4744100994
## 381 382 383 384 385
## -0.0059088709 0.5153161332 0.0001318181 -2.5177963282 0.0668198654
## 386 387 388 389 390
## 0.1973981871 0.9904799196 -2.1984580381 0.0168640664 -1.4291745534
## 391 392 393 394 395
## -0.0247006355 -0.1628380983 -0.2292329231 -1.4175670239 0.5530912212
outliers_G3 <- which(abs(rstudent(model_G3)) > 2)
#G1
G1removedoutliers <- data_G1[-c(1,80, 178, 193, 199, 246, 249, 261, 266, 287, 294, 375), ]
G1removedoutliers.fit <- lm(G1 ~ ., data=G1removedoutliers)
summary(G1removedoutliers.fit)
##
## Call:
## lm(formula = G1 ~ ., data = G1removedoutliers)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.504 -1.881 -0.015 1.749 5.393
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 11.023708 3.074315 3.586 0.000385 ***
## schoolUP 0.076799 0.516558 0.149 0.881898
## sexM 0.979764 0.325480 3.010 0.002804 **
## age -0.072381 0.142293 -0.509 0.611306
## addressU 0.312128 0.385330 0.810 0.418487
## famsizeLE3 0.342357 0.322092 1.063 0.288568
## PstatusT -0.017866 0.477766 -0.037 0.970192
## Medu 0.140202 0.210888 0.665 0.506615
## Fedu 0.161796 0.181121 0.893 0.372322
## Mjobhealth 0.421033 0.732222 0.575 0.565663
## Mjobother -1.190478 0.471761 -2.523 0.012071 *
## Mjobservices -0.216246 0.532075 -0.406 0.684688
## Mjobteacher -1.414362 0.685421 -2.063 0.039818 *
## Fjobhealth -0.200462 0.937490 -0.214 0.830808
## Fjobother -0.860084 0.675648 -1.273 0.203889
## Fjobservices -0.405829 0.696933 -0.582 0.560742
## Fjobteacher 1.556865 0.857111 1.816 0.070180 .
## reasonhome -0.002914 0.359037 -0.008 0.993528
## reasonother -0.250935 0.540975 -0.464 0.643044
## reasonreputation 0.350161 0.375661 0.932 0.351930
## guardianmother 0.049023 0.356894 0.137 0.890826
## guardianother 1.216849 0.657679 1.850 0.065142 .
## traveltime -0.149729 0.221444 -0.676 0.499403
## studytime 0.596402 0.186815 3.192 0.001541 **
## failures -1.345928 0.215340 -6.250 1.22e-09 ***
## schoolsupyes -1.872729 0.434702 -4.308 2.15e-05 ***
## famsupyes -1.089675 0.313580 -3.475 0.000577 ***
## paidyes -0.126305 0.310776 -0.406 0.684689
## activitiesyes 0.040578 0.291209 0.139 0.889260
## nurseryyes -0.142339 0.356053 -0.400 0.689576
## higheryes 1.276850 0.697725 1.830 0.068115 .
## internetyes 0.287973 0.404829 0.711 0.477354
## romanticyes -0.143955 0.307097 -0.469 0.639538
## famrel -0.054517 0.160027 -0.341 0.733557
## freetime 0.397706 0.155449 2.558 0.010943 *
## goout -0.439882 0.145466 -3.024 0.002683 **
## Dalc -0.140267 0.216547 -0.648 0.517584
## Walc 0.035538 0.161879 0.220 0.826363
## health -0.047793 0.105139 -0.455 0.649707
## absences 0.006728 0.018830 0.357 0.721082
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.641 on 343 degrees of freedom
## Multiple R-squared: 0.3748, Adjusted R-squared: 0.3037
## F-statistic: 5.273 on 39 and 343 DF, p-value: < 2.2e-16
par(mfrow =c(2,2),mar=c(2,2,2,2))
plot(G1removedoutliers.fit)
#G2
G2removedoutliers <- data_G2[-c(97,131,132,135,136,137,138,145,157,162,163,165,243,245,255,270,332,333), ]
G2removedoutliers.fit <- lm(G2 ~ ., data=G2removedoutliers)
summary(G2removedoutliers.fit)
##
## Call:
## lm(formula = G2 ~ ., data = G2removedoutliers)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.8753 -0.7800 -0.0314 0.6853 3.1114
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.611890 1.492754 3.759 0.000201 ***
## schoolUP 0.084947 0.244713 0.347 0.728711
## sexM -0.044970 0.156990 -0.286 0.774709
## age -0.285902 0.068067 -4.200 3.42e-05 ***
## addressU 0.306286 0.186991 1.638 0.102364
## famsizeLE3 0.169568 0.150832 1.124 0.261724
## PstatusT -0.192067 0.224144 -0.857 0.392117
## Medu 0.163594 0.100281 1.631 0.103752
## Fedu -0.005160 0.086595 -0.060 0.952517
## Mjobhealth 0.033629 0.345852 0.097 0.922598
## Mjobother 0.312651 0.227871 1.372 0.170962
## Mjobservices 0.174961 0.249220 0.702 0.483143
## Mjobteacher -0.375983 0.323652 -1.162 0.246187
## Fjobhealth -0.001214 0.444317 -0.003 0.997821
## Fjobother 0.247310 0.322184 0.768 0.443261
## Fjobservices 0.336184 0.331236 1.015 0.310865
## Fjobteacher 0.131220 0.407636 0.322 0.747725
## reasonhome -0.069284 0.172357 -0.402 0.687953
## reasonother 0.419910 0.250035 1.679 0.094003 .
## reasonreputation -0.216067 0.180314 -1.198 0.231652
## guardianmother -0.029532 0.169135 -0.175 0.861494
## guardianother 0.375104 0.313223 1.198 0.231931
## traveltime -0.073445 0.108900 -0.674 0.500503
## studytime -0.004155 0.091074 -0.046 0.963642
## failures -0.160804 0.113032 -1.423 0.155768
## schoolsupyes -0.185162 0.215671 -0.859 0.391207
## famsupyes -0.011103 0.149927 -0.074 0.941010
## paidyes 0.138838 0.147404 0.942 0.346925
## activitiesyes 0.040914 0.139952 0.292 0.770203
## nurseryyes -0.024923 0.172539 -0.144 0.885232
## higheryes 0.072575 0.346983 0.209 0.834450
## internetyes 0.235969 0.195595 1.206 0.228507
## romanticyes -0.340530 0.147603 -2.307 0.021659 *
## famrel -0.037655 0.076480 -0.492 0.622788
## freetime -0.124520 0.073249 -1.700 0.090067 .
## goout -0.025639 0.070904 -0.362 0.717879
## Dalc 0.134494 0.103935 1.294 0.196548
## Walc -0.023290 0.078423 -0.297 0.766663
## health -0.004548 0.049586 -0.092 0.926978
## absences -0.018950 0.008965 -2.114 0.035269 *
## G1 0.893618 0.023909 37.375 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.248 on 336 degrees of freedom
## Multiple R-squared: 0.8729, Adjusted R-squared: 0.8578
## F-statistic: 57.69 on 40 and 336 DF, p-value: < 2.2e-16
par(mfrow =c(2,2),mar=c(2,2,2,2))
plot(G2removedoutliers.fit)
#G3
G3removedoutliers <- data_G3[-c(141,147,149,161,169,171,174,240,260,265,297,311,317,334,335,338,342,344,368,371,384,388), ]
G3removedoutliers.fit <- lm(G3 ~ ., data=G3removedoutliers)
summary(G3removedoutliers.fit)
##
## Call:
## lm(formula = G3 ~ ., data = G3removedoutliers)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.6298 -0.4931 -0.0415 0.5536 2.2218
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.3095048 1.1282551 -1.161 0.24662
## schoolUP 0.2143879 0.1867972 1.148 0.25192
## sexM -0.0420204 0.1174444 -0.358 0.72073
## age 0.0409396 0.0510871 0.801 0.42349
## addressU 0.0602394 0.1350743 0.446 0.65591
## famsizeLE3 -0.0929229 0.1147907 -0.809 0.41881
## PstatusT -0.2515662 0.1682894 -1.495 0.13591
## Medu -0.0167732 0.0752359 -0.223 0.82372
## Fedu -0.0334289 0.0650752 -0.514 0.60781
## Mjobhealth 0.2484723 0.2647949 0.938 0.34874
## Mjobother -0.1736784 0.1711082 -1.015 0.31084
## Mjobservices -0.0069468 0.1918509 -0.036 0.97114
## Mjobteacher 0.2206809 0.2451943 0.900 0.36876
## Fjobhealth 0.1981070 0.3318343 0.597 0.55091
## Fjobother 0.0840749 0.2394881 0.351 0.72577
## Fjobservices 0.0752638 0.2487252 0.303 0.76239
## Fjobteacher 0.0923559 0.3030118 0.305 0.76072
## reasonhome 0.1896112 0.1310199 1.447 0.14879
## reasonother 0.0576504 0.1916073 0.301 0.76370
## reasonreputation 0.0127303 0.1344622 0.095 0.92463
## guardianmother -0.0969067 0.1284022 -0.755 0.45096
## guardianother -0.3598379 0.2449894 -1.469 0.14284
## traveltime 0.1013376 0.0792731 1.278 0.20203
## studytime 0.0416075 0.0689549 0.603 0.54665
## failures -0.1114026 0.0857673 -1.299 0.19488
## schoolsupyes -0.0008611 0.1600379 -0.005 0.99571
## famsupyes 0.0869895 0.1133892 0.767 0.44352
## paidyes -0.1111658 0.1113145 -0.999 0.31869
## activitiesyes -0.1431917 0.1046973 -1.368 0.17234
## nurseryyes -0.2695707 0.1298686 -2.076 0.03869 *
## higheryes 0.0595421 0.2678865 0.222 0.82424
## internetyes 0.0586842 0.1457999 0.402 0.68758
## romanticyes -0.0570805 0.1117580 -0.511 0.60987
## famrel 0.2698969 0.0581785 4.639 5.05e-06 ***
## freetime 0.0551286 0.0560304 0.984 0.32588
## goout -0.1076184 0.0550177 -1.956 0.05130 .
## Dalc -0.0005226 0.0770302 -0.007 0.99459
## Walc 0.0474131 0.0588693 0.805 0.42117
## health -0.1046917 0.0380465 -2.752 0.00626 **
## absences -0.0002087 0.0067873 -0.031 0.97548
## G1 0.0456898 0.0311407 1.467 0.14327
## G2 0.9734064 0.0264079 36.860 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9325 on 331 degrees of freedom
## Multiple R-squared: 0.9509, Adjusted R-squared: 0.9448
## F-statistic: 156.3 on 41 and 331 DF, p-value: < 2.2e-16
par(mfrow =c(2,2),mar=c(2,2,2,2))
plot(G3removedoutliers.fit)
In this section, stepwise regression is done to iteratively select a
subset of predictor variables that contribute significantly to the
model’s fit for each response variable. The
direction = "both" argument indicates that both forward and
backward selection steps are used. Since the process is so long, the
process is excluded and summary for the stepewise regression models are
presented.
summary(step1)
##
## Call:
## lm(formula = G1 ~ sex + address + Fedu + Mjob + Fjob + guardian +
## studytime + failures + schoolsup + famsup + higher + freetime +
## goout, data = G1removedoutliers)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.3643 -1.9363 -0.1022 1.7557 5.6823
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 9.15426 1.19223 7.678 1.52e-13 ***
## sexM 0.96543 0.30042 3.214 0.001429 **
## addressU 0.52108 0.33165 1.571 0.117019
## Fedu 0.23845 0.14879 1.603 0.109900
## Mjobhealth 0.83876 0.60786 1.380 0.168487
## Mjobother -1.01986 0.43163 -2.363 0.018665 *
## Mjobservices 0.05411 0.46274 0.117 0.906980
## Mjobteacher -1.01367 0.55454 -1.828 0.068382 .
## Fjobhealth -0.27277 0.89005 -0.306 0.759422
## Fjobother -0.85090 0.63693 -1.336 0.182408
## Fjobservices -0.48711 0.65956 -0.739 0.460667
## Fjobteacher 1.40523 0.81699 1.720 0.086284 .
## guardianmother 0.09204 0.34090 0.270 0.787310
## guardianother 1.10334 0.57849 1.907 0.057277 .
## studytime 0.64870 0.17268 3.757 0.000201 ***
## failures -1.37022 0.20732 -6.609 1.38e-10 ***
## schoolsupyes -1.76601 0.40856 -4.323 2.00e-05 ***
## famsupyes -1.10465 0.28849 -3.829 0.000152 ***
## higheryes 1.35216 0.65207 2.074 0.038817 *
## freetime 0.37801 0.14723 2.567 0.010646 *
## goout -0.45917 0.12737 -3.605 0.000356 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.597 on 362 degrees of freedom
## Multiple R-squared: 0.3618, Adjusted R-squared: 0.3266
## F-statistic: 10.26 on 20 and 362 DF, p-value: < 2.2e-16
summary(step2)
##
## Call:
## lm(formula = G2 ~ age + address + Medu + Mjob + failures + romantic +
## freetime + Dalc + absences + G1, data = G2removedoutliers)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.4093 -0.7952 0.0023 0.6926 3.2430
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.129287 0.981465 5.226 2.93e-07 ***
## age -0.264588 0.054095 -4.891 1.51e-06 ***
## addressU 0.404964 0.161607 2.506 0.0127 *
## Medu 0.137674 0.078938 1.744 0.0820 .
## Mjobhealth 0.169752 0.310649 0.546 0.5851
## Mjobother 0.322939 0.207593 1.556 0.1207
## Mjobservices 0.274174 0.227944 1.203 0.2298
## Mjobteacher -0.250907 0.292351 -0.858 0.3913
## failures -0.160832 0.101826 -1.579 0.1151
## romanticyes -0.263242 0.140629 -1.872 0.0620 .
## freetime -0.138255 0.065461 -2.112 0.0354 *
## Dalc 0.158854 0.074342 2.137 0.0333 *
## absences -0.017203 0.008203 -2.097 0.0367 *
## G1 0.898633 0.021219 42.350 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.229 on 363 degrees of freedom
## Multiple R-squared: 0.8667, Adjusted R-squared: 0.862
## F-statistic: 181.6 on 13 and 363 DF, p-value: < 2.2e-16
summary(step3)
##
## Call:
## lm(formula = G3 ~ failures + nursery + famrel + goout + health +
## G1 + G2, data = G3removedoutliers)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.7068 -0.4262 -0.0597 0.5675 2.6300
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.43281 0.34990 -1.237 0.2169
## failures -0.11395 0.07142 -1.595 0.1115
## nurseryyes -0.23928 0.11914 -2.008 0.0453 *
## famrel 0.25914 0.05368 4.828 2.03e-06 ***
## goout -0.06547 0.04430 -1.478 0.1403
## health -0.08878 0.03462 -2.565 0.0107 *
## G1 0.04787 0.02711 1.766 0.0783 .
## G2 0.97496 0.02390 40.792 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9164 on 365 degrees of freedom
## Multiple R-squared: 0.9477, Adjusted R-squared: 0.9467
## F-statistic: 944.8 on 7 and 365 DF, p-value: < 2.2e-16
#G1
final1 <- lm(G1 ~ sex + address + Fedu + Mjob + Fjob + guardian + studytime +
failures + schoolsup + famsup + higher + freetime + goout, data = G1removedoutliers)
#G2
final2 <- lm(G2 ~ age + address + Medu + Mjob + failures + romantic + freetime +
Dalc + absences + G1, data = G2removedoutliers)
#G3
final3 <- lm(G3 ~ failures + nursery + famrel + goout + health + G1 + G2, data = G3removedoutliers)
#Final Model of G1
summary(final1)
##
## Call:
## lm(formula = G1 ~ sex + address + Fedu + Mjob + Fjob + guardian +
## studytime + failures + schoolsup + famsup + higher + freetime +
## goout, data = G1removedoutliers)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.3643 -1.9363 -0.1022 1.7557 5.6823
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 9.15426 1.19223 7.678 1.52e-13 ***
## sexM 0.96543 0.30042 3.214 0.001429 **
## addressU 0.52108 0.33165 1.571 0.117019
## Fedu 0.23845 0.14879 1.603 0.109900
## Mjobhealth 0.83876 0.60786 1.380 0.168487
## Mjobother -1.01986 0.43163 -2.363 0.018665 *
## Mjobservices 0.05411 0.46274 0.117 0.906980
## Mjobteacher -1.01367 0.55454 -1.828 0.068382 .
## Fjobhealth -0.27277 0.89005 -0.306 0.759422
## Fjobother -0.85090 0.63693 -1.336 0.182408
## Fjobservices -0.48711 0.65956 -0.739 0.460667
## Fjobteacher 1.40523 0.81699 1.720 0.086284 .
## guardianmother 0.09204 0.34090 0.270 0.787310
## guardianother 1.10334 0.57849 1.907 0.057277 .
## studytime 0.64870 0.17268 3.757 0.000201 ***
## failures -1.37022 0.20732 -6.609 1.38e-10 ***
## schoolsupyes -1.76601 0.40856 -4.323 2.00e-05 ***
## famsupyes -1.10465 0.28849 -3.829 0.000152 ***
## higheryes 1.35216 0.65207 2.074 0.038817 *
## freetime 0.37801 0.14723 2.567 0.010646 *
## goout -0.45917 0.12737 -3.605 0.000356 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.597 on 362 degrees of freedom
## Multiple R-squared: 0.3618, Adjusted R-squared: 0.3266
## F-statistic: 10.26 on 20 and 362 DF, p-value: < 2.2e-16
#Final Model of G2
summary(final2)
##
## Call:
## lm(formula = G2 ~ age + address + Medu + Mjob + failures + romantic +
## freetime + Dalc + absences + G1, data = G2removedoutliers)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.4093 -0.7952 0.0023 0.6926 3.2430
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.129287 0.981465 5.226 2.93e-07 ***
## age -0.264588 0.054095 -4.891 1.51e-06 ***
## addressU 0.404964 0.161607 2.506 0.0127 *
## Medu 0.137674 0.078938 1.744 0.0820 .
## Mjobhealth 0.169752 0.310649 0.546 0.5851
## Mjobother 0.322939 0.207593 1.556 0.1207
## Mjobservices 0.274174 0.227944 1.203 0.2298
## Mjobteacher -0.250907 0.292351 -0.858 0.3913
## failures -0.160832 0.101826 -1.579 0.1151
## romanticyes -0.263242 0.140629 -1.872 0.0620 .
## freetime -0.138255 0.065461 -2.112 0.0354 *
## Dalc 0.158854 0.074342 2.137 0.0333 *
## absences -0.017203 0.008203 -2.097 0.0367 *
## G1 0.898633 0.021219 42.350 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.229 on 363 degrees of freedom
## Multiple R-squared: 0.8667, Adjusted R-squared: 0.862
## F-statistic: 181.6 on 13 and 363 DF, p-value: < 2.2e-16
#Final Model of G3
summary(final3)
##
## Call:
## lm(formula = G3 ~ failures + nursery + famrel + goout + health +
## G1 + G2, data = G3removedoutliers)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.7068 -0.4262 -0.0597 0.5675 2.6300
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.43281 0.34990 -1.237 0.2169
## failures -0.11395 0.07142 -1.595 0.1115
## nurseryyes -0.23928 0.11914 -2.008 0.0453 *
## famrel 0.25914 0.05368 4.828 2.03e-06 ***
## goout -0.06547 0.04430 -1.478 0.1403
## health -0.08878 0.03462 -2.565 0.0107 *
## G1 0.04787 0.02711 1.766 0.0783 .
## G2 0.97496 0.02390 40.792 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9164 on 365 degrees of freedom
## Multiple R-squared: 0.9477, Adjusted R-squared: 0.9467
## F-statistic: 944.8 on 7 and 365 DF, p-value: < 2.2e-16
# ANOVA comparison
anova(step1, step2, step3)
## Warning in anova.lmlist(object, ...): models with response 'c("G2", "G3")'
## removed because response differs from model 1
## Analysis of Variance Table
##
## Response: G1
## Df Sum Sq Mean Sq F value Pr(>F)
## sex 1 56.00 56.00 8.3001 0.0042005 **
## address 1 35.94 35.94 5.3273 0.0215565 *
## Fedu 1 158.12 158.12 23.4355 1.916e-06 ***
## Mjob 4 95.45 23.86 3.5369 0.0075665 **
## Fjob 4 76.00 19.00 2.8162 0.0252094 *
## guardian 2 1.22 0.61 0.0905 0.9134846
## studytime 1 171.54 171.54 25.4241 7.288e-07 ***
## failures 1 428.45 428.45 63.5021 2.108e-14 ***
## schoolsup 1 139.77 139.77 20.7161 7.283e-06 ***
## famsup 1 88.68 88.68 13.1438 0.0003298 ***
## higher 1 28.20 28.20 4.1801 0.0416250 *
## freetime 1 17.63 17.63 2.6133 0.1068438
## goout 1 87.69 87.69 12.9972 0.0003557 ***
## Residuals 362 2442.40 6.75
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
This code performs an ANOVA comparison between the final models obtained after stepwise regression to evaluate if there are significant differences in their fits.
significant_var1 <- summary(final1)$coefficients[summary(final1)$coefficients[, 4] < 0.05, ]
significant_var2 <- summary(final2)$coefficients[summary(final2)$coefficients[, 4] < 0.05, ]
significant_var3 <- summary(final1)$coefficients[summary(final3)$coefficients[, 4] < 0.05, ]
These lines extract the coefficients of the predictor variables that
have p-values less than 0.05 from the summary of the final models
(final1, final2, final3). This
helps to identify the significant variables in the models that have a
meaningful impact on the response variable.
The Significant features for each model are as follows:
| G1 Model | G2 Model | G3 Model |
|---|---|---|
| sexM | age | failures |
| address | addressU | nursery |
| Fedu | Medu | famrel |
| Mjobother | Mjob | goout |
| studytime | failures | health |
| failures | freetime | failures |
| schoolsupyes | Dalc | higheryes |
| famsupyes | absences | freetime |
| higheryes | G1 | G1 |
| freetime | G2 | |
| goout |
## The output of the Model of G3 Exam provides a summary of a linear regression model that provides information about the goodness of fit and model performance. Here's a breakdown of the values:
## Residual Standard Error: 0.9163565
## Multiple R-squared: 0.9476972
## Adjusted R-squared: 0.9466941
## F-statistic: 944.7991 on 7 and 365 DF, p-value < 2.2e-16