-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent Performance-Regression.Rmd
More file actions
340 lines (282 loc) · 9.96 KB
/
Student Performance-Regression.Rmd
File metadata and controls
340 lines (282 loc) · 9.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
---
output:
html_document:
toc: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
### Step 1: Identify a good research question
Proposed Hypothesis: G3(dependent variable) is affected by some or all other independent variables in the data set
Why G3:
1. G3 is the final grade, it is less sensitive on data collection timing issue than G1 and G2, since some of the data could be collected after G1, G2, complicating the casual effect. Choosing G3 as dependent variable can mitigate the problem, since the variables with potential timing problems would be independent from other possible dependent variables (such as G1 or G2) in that case.
2. G3 is close to normal distribution (except some outliers) which is ideal for regression analysis.
### Step 2: Identify the data that help answer that question
Math and Language data sets
1. run regression on Math data
2. run classification on Language data
### Step 3: Obtain and read in data
### Step 4: Clean the data. Use the proper delimit, dealing with the missing data, treat the incorrect data class/type.
```{r}
# Set working directory
setwd("C:/Users/admin/Downloads/R projects/Final Project")
require(readr)
require(ISLR)
require(ggplot2)
require(GGally)
require(caret)
require(MASS)
require(klaR)
require(glmnet)
require(class)
require(earth)
```
Read in data
```{r}
MathData <- read_delim("sm.csv", delim = ";")
# test if has missing data
table(is.na(MathData))
MathData <- as.data.frame(MathData)
# no missing value
```
### Step 5: Get to know the data with summarize and visualization tools
```{r}
attach(MathData)
summary(MathData)
charName <- names(MathData) %in% c("school", "sex", "address", "famsize", "Pstatus", "Mjob", "Fjob", "reason", "guardian", "schoolsup", "famsup", "paid", "activities", "nursery", "higher", "internet", "romantic")
charData <- MathData[charName]
numData <- MathData[!charName]
# plots for Character Variables
mainnames <- names(charData)
par(mfrow=c(3, 3))
for (i in 1:9){
barplot(table(charData[,i]), main=mainnames[i], col = "#33cccc", border = NA)
}
par(mfrow=c(3, 3))
for (i in 10:17){
barplot(table(charData[,i]), main=mainnames[i], col = "#33cccc", border = NA)
}
# plots for Numeric Variables
mainnames1 <- names(numData)
par(mfrow=c(3, 3))
for (i in 1:9){
barplot(table(numData[,i]), main=mainnames1[i], col = "#33cccc", border = NA)
}
par(mfrow=c(3, 3))
for (i in 10:16){
barplot(table(numData[,i]), main=mainnames1[i], col = "#33cccc", border = NA)
}
# correlation
ggcorr(numData, label = TRUE, legend.position = "none")
```
### Step 6: Treat Existing variables / Create new variables
Treat G1, G2, G3
```{r}
# compare G1, G2, G3
G123 <- data.frame(G1=numData$G1, G2=numData$G2, G3=numData$G3)
par(mfrow=c(2, 2))
for (i in 1:3) {
hist(G123[,i], main=names(G123)[i], probability=TRUE, breaks=20, col="#33cccc", border="white", xlab=NA)
d <- density(G123[,i])
lines(d, col="red")
}
# G3 is likely to be harder than G2 and G1 since the skewness shift towards left(lower grades) from G1, G2 to G3.
# there are also many 0 scores indicating they might be outliers.
# inspect the data to see if those scored 0 also scored low on G1, G2
inspect0score <- subset(numData, subset = G3==0, select = c(G1, G2, G3, absences, studytime))
par(mfrow=c(2, 3))
for (i in 1:5) {
barplot(table(inspect0score[,i]), main=names(inspect0score)[i], col="#33cccc", border=NA, xlab=NA)
}
mean(inspect0score$studytime)
# those students have 0 absence
# they score relatively low on G1, too many of them scored 0 on G2
# their average study time is around level 2
# check if those scored 0 on both G2 and G3 study less time
lapply(subset(numData, subset = G3==0 & G2==0, select = c(G1, G2, G3, absences, studytime)),
function(x) mean(x))
# these student did not score too low on G1 and their average study time is fair
# therefore, it is possible that they missed the exams, it may be helpful if remove these outliers
# remove potential outliers
MathData <- subset(numData, subset = !(G3==0 & G2==0))
```
Treat Alcohol Consumption variables
```{r}
# since Dalc and Walc are highly correlated and Dalc is quite skewed and they both describe the same thing,
# combine them into one varible
Math <- subset(MathData, select = -c(Dalc, Walc))
Math$Alc <- MathData$Dalc + MathData$Walc
```
### Step 7: Develop a modeling plan: which algorithms would be more likely to produce ideal results
preliminary correlation testing on different predictor combinations
```{r}
# 1
summary(lm(formula = G3 ~ ., data = Math))
# age, famrel, absences, G2 are significant
# 2
summary(lm(formula = G3 ~ .-G2, data = Math))
# age, famrel, G1 are significant, age and G1 become highly influential factors
# 3
summary(lm(formula = G3 ~ .-G1, data = Math))
# famrel, absences, G2 are significant
# 4
summary(lm(formula = G3 ~ .-G1-G2, data = Math))
# Medu, failures become significant, R2 is very low compare to other formulae
# 5
summary(lm(formula = G3 ~ famrel + absences + G2, data = Math))
# all significant
# 6
summary(lm(formula = G3 ~ famrel + absences + G2 + age + failures, data = Math))
# age and failures become less important
# in following process, formula 1,2,3,5 will be adopted
```
### Step 8: Split the data into a training set and a test set.
```{r}
set.seed(1234)
intrain <- as.integer(createDataPartition(y = Math$G1, p = 0.8, list = FALSE))
training <- Math[intrain, ]
testing <- Math[-intrain, ]
```
### Step 9: performing resampling to identify which predictor/parameter combinations could produce best results.
```{r}
# shared elements
outcomeVar <- 'G3'
trainingDV <- training[, outcomeVar]
preParam <- c("center", "scale", "nzv")
seed <- 567
train_control <- trainControl(method='cv', number= 10, returnResamp='none')
```
```{r}
# functions for all regression models
lmTrain <- function(xvars){
set.seed(seed)
train(x = xvars,
y = trainingDV,
method = "lm",
preProc = preParam,
trControl = train_control)
}
plsTrain <- function(xvars){
set.seed(seed)
train(x = xvars,
y = trainingDV,
method = "pls",
preProc = preParam,
trControl = train_control)
}
marsTrain <- function(xvars){
set.seed(seed)
train(x = xvars,
y = trainingDV,
method = "earth",
preProc = preParam,
trControl = train_control)
}
knnTrain <- function(xvars){
set.seed(seed)
train(x = xvars,
y = trainingDV,
method = "knn",
preProc = preParam,
trControl = train_control)
}
ridgeTrain <- function(xvars){
set.seed(seed)
train(x = xvars,
y = trainingDV,
method = "ridge",
preProc = preParam,
trControl = train_control)
}
```
```{r}
# build baseline model
# 10 fold cross validation
testData <- vector("list", 10)
heldoutData <- vector("list", 10)
residlist <- vector("list", 10)
held_out <- cut(seq(1, nrow(training)), breaks = 10, labels = FALSE)
for(i in 1:10) {
testIndexes <- which(held_out == i)
testData[[i]] <- mean(training[-testIndexes, ]$G3)
heldoutData[[i]] <- mean(training[testIndexes, ]$G3)
residlist[[i]] <- abs(testData[[i]] - heldoutData[[i]])
}
min(do.call(rbind, residlist))
# the smallest is [5,] 0.06463301
baselineTrain <- testData[[5]]
# calculate RSS
calRSS <- function(predictDV){
sum((as.double(testing[, outcomeVar]) - predictDV)^2)
}
baseRSS <- calRSS(baselineTrain)
testAccu <- function(lm, pls, mars, knn, ridge, predictors){
modelList <- list(lm, pls, mars, knn, ridge)
names(modelList) <- c("lm", "pls", "mars", "knn", "ridge")
# predict on test data
predictList <- lapply(modelList, function(x) as.double(predict(x, testing[, predictors])))
rssList <- lapply(predictList, function(x) calRSS(x))
rssList[["baseline"]] <- baseRSS
names(rssList) <- c("LM", "PLS", "MARS", "KNN", "RIDGE", "Baseline")
do.call(rbind, rssList)
}
```
### Step 10: Run the predictive models.
```{r}
# formula = G3 ~ .
predictors1 <- names(subset(Math, select = -G3))
xvars1 <- training[, predictors1]
lmFinal1 <- lmTrain(xvars1)
plsFinal1 <- plsTrain(xvars1)
marsFinal1 <- marsTrain(xvars1)
knnFinal1 <- knnTrain(xvars1)
ridgeFinal1 <- ridgeTrain(xvars1)
```
### Step 11: Evaluate the results of the predictive models on the test data.
```{r}
Formula1 <- testAccu(lmFinal1, plsFinal1, marsFinal1, knnFinal1, ridgeFinal1, predictors1)
```
### Step 12: Refine predictive models and repeat with different algorithms, or with more data
```{r}
# formula = G3 ~ .-G2
predictors2 <- names(subset(Math, select = -c(G2, G3)))
xvars2 <- training[, predictors2]
lmFinal2 <- lmTrain(xvars2)
plsFinal2 <- plsTrain(xvars2)
marsFinal2 <- marsTrain(xvars2)
knnFinal2 <- knnTrain(xvars2)
ridgeFinal2 <- ridgeTrain(xvars2)
Formula2 <- testAccu(lmFinal2, plsFinal2, marsFinal2, knnFinal2, ridgeFinal2, predictors2)
```
```{r}
# formula = G3 ~ .-G1
predictors3 <- names(subset(Math, select = -c(G1, G3)))
xvars3 <- training[, predictors3]
lmFinal3 <- lmTrain(xvars3)
plsFinal3 <- plsTrain(xvars3)
marsFinal3 <- marsTrain(xvars3)
knnFinal3 <- knnTrain(xvars3)
ridgeFinal3 <- ridgeTrain(xvars3)
Formula3 <- testAccu(lmFinal3, plsFinal3, marsFinal3, knnFinal3, ridgeFinal3, predictors3)
```
```{r}
# formula = G3 ~ famrel + absences + G2
predictors4 <- names(subset(Math, select = c(famrel, absences, G2)))
xvars4 <- training[, predictors4]
lmFinal4 <- lmTrain(xvars4)
plsFinal4 <- plsTrain(xvars4)
marsFinal4 <- marsTrain(xvars4)
knnFinal4 <- knnTrain(xvars4)
ridgeFinal4 <- ridgeTrain(xvars4)
Formula4 <- testAccu(lmFinal4, plsFinal4, marsFinal4, knnFinal4, ridgeFinal4, predictors4)
```
### Compare all formulae and all algorithms
```{r}
data.frame(Formula1, Formula2, Formula3, Formula4)
# In all formulae:
# LM performances the best, RIDGE, MARS are close, KNN is least accurate.
# all models significantly outpeform the baseline
# Among formulae:
# Formula4 yeilds best overall result, followed by Formula3 since it exludes G1
# The experiment reveals some sign that G2 is closely related, famrel and absences are also higly influential factors.
```