-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathML_tutorial_code.R
More file actions
238 lines (198 loc) · 9.03 KB
/
Copy pathML_tutorial_code.R
File metadata and controls
238 lines (198 loc) · 9.03 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
#This code is for the ML for neuroimagers tutorial
#Produced by Mohan Gupta (https://mohanwugupta.com)
#Github: mohanwugupta
####################################################################################################
####################################################################################################
#SETTING UP R ENVIRONMENT
#Install packages - uncomment if this is your first time
# install.packages("e1071")
# install.packages("caret")
# install.packages("dplyr")
# install.packages("randomForest")
# install.packages("caretEnsemble")
# install.packages("skimr")
# install.packages("RANN")
# install.packages("LiblineaR")
# install.packages("corrplot")
# install.packages("glmnet")
#Load libraries
library(e1071) # SVM models and more
library(caret) #Implementation of ML methods
library(dplyr) #data formatting
library(randomForest) #RF ML models
library(caretEnsemble) #Allows for stacking ML models
library(skimr) #feature visualization
library(RANN) #ML models
library(LiblineaR) #ML models
library(corrplot) #feature visualization
library(glmnet) #Other ML models and crossover features of caret
####################################################################################################
####################################################################################################
#DEMOGRAPHICS AND FILTERING
#Load in data - CHANGE THIS PATH TO WHERE YOU DOWNLOADED OR MOVED THE CSV FILE FROM MY GITHUB
HCP = read.csv(file="/Users/CCNLAB/Box Sync/Education_Youtube/Machine Learning/Code/HCP_morph_tutorial_dat.csv", header = TRUE)
#Get Demographics
#Make Female dataframe
F_HCP = HCP%>%
filter(!Sex == "Male")
F_Age_avg = mean(F_HCP$Age)
#Make Male dataframe
M_HCP = HCP%>%
filter(!Sex == "Female")
M_Age_avg = mean(M_HCP$Age)
#t.test age to see if there are sig diffs
t.test(M_HCP$Age, F_HCP$Age, var.equal = T)
#Get rid of unwanted features
HCP$SUBJ <- NULL
HCP$Age <- NULL
####################################################################################################
#Small Note: There cannot be any NAs or empty cells for this method to work
#Fix missing data points - there aren't any missing data points for morphemtry
anyNA(HCP)
HCP = na.omit(HCP)
anyNA(HCP)
####################################################################################################
####################################################################################################
#Preprocess - scale and demean = standard deviation = 1 and mean = 0
y=HCP$Sex
preProcess_range_model <- preProcess(HCP[,2:length(HCP)], method=c('center'))
HCP = predict(preProcess_range_model, HCP[,2:length(HCP)])
preProcess_range_model <- preProcess(HCP[,2:length(HCP)], method=c('scale'))
HCP = predict(preProcess_range_model, HCP[,2:length(HCP)])
HCP$Sex=y
#Move Sex to the front
HCP = HCP[,c(ncol(HCP),1:(ncol(HCP)-1))]
####################################################################################################
####################################################################################################
#Feature Plotting and Selection
#Plot Features - helps tell us which variables may be important
featurePlot(x = HCP[, 2:18],
y = HCP$Sex,
plot = "density",
strip=strip.custom(par.strip.text=list(cex=.7)),
scales = list(x = list(relation="free"),
y = list(relation="free")))
#LASSO Feature Selection
#alpha controls how much penalty is applied and lambda controls the overall magnitude of the penalty
set.seed(30)
#CV which lambda to use for lasso
cvfit <- glmnet::cv.glmnet(as.matrix(HCP[,2:length(HCP)]), factor(HCP$Sex), type.measure='mse',
nfolds=10,alpha=.1, family = "binomial")
#Lasso feature selection
sel_feats = as.matrix(coef(cvfit, s = "lambda.1se"))
#Get list of non-zero features
features_lasso = which(sel_feats!=0, arr.ind = T)
features_lasso = rownames(features_lasso)
features_lasso = features_lasso[2:length(features_lasso)]
HCP = HCP%>%
select(Sex, features_lasso)
#Plot MSE
plot(cvfit, ylab = "Mean-Squared Error", xlab = "log(Lambda)")
#Correlation Matrix of kept features
#Correlation Matrix - Need short names in order to fit them on the plot!
# M = cor(HCP[,2:length(HCP)])
# res1 = cor.mtest(HCP[, 2:length(HCP)], conf.level = .95)
# col = colorRampPalette(c("blue", "red"))
# Feature.matrix <- corrplot(M, p.mat = res1$p, sig.level = c(.001, .01, .05), pch.cex = .5,
# insig = "label_sig", method = "circle", tl.pos = "y", type = "upper",
# col = col(10), addrect = 3)
#Plot Selected Features
featurePlot(x = HCP[, 2:18],
y = HCP$Sex,
plot = "density",
strip=strip.custom(par.strip.text=list(cex=.7)),
scales = list(x = list(relation="free"),
y = list(relation="free")))
#Look at mins and maxes of features
apply(HCP[, 2:18], 2, FUN=function(x){c('min'=min(x), 'max'=max(x))})
####################################################################################################
####################################################################################################
#Data splitting
#IT IS ALWAYS BEST TO HAVE AN INDEPENDENT DATASET!!!!!
#Create Training and testing data - 80% training, 20% testing
#Group based on sex
set.seed(100)
trainIndex <- createDataPartition(as.factor(HCP$Sex), p = .8, list= FALSE)
traindata = HCP[trainIndex,]
testdata = HCP[-trainIndex,]
#Descriptive Stats and visualization
skimmed <- skim_to_wide(traindata)
skimmed[, c(1:5, 9:11, 13)]
####################################################################################################
####################################################################################################
#Cross Validation of ML parameters
set.seed(102)
fitControl <- trainControl(
method = 'repeatedcv', # k-fold cross validation
number = 10, # number of folds
savePredictions = 'final', # saves predictions for optimal tuning parameter
classProbs = T, # should class probabilities be returned
summaryFunction=twoClassSummary # results summary function
)
#Ensure factors
traindata$Sex = factor(traindata$Sex)
####################################################################################################
####################################################################################################
#ML Models
#Train model and cv tuning parameters
model_svm2 = train(Sex ~ ., data=traindata,
method='svmPoly',
tuneLength = 5, metric='ROC',
preProc = c("center","scale"),
verbose="FALSE",
trControl = fitControl)
model_svm2
plot(model_svm2, main="Model Accuracies with SVM")
varimp_svm <- varImp(model_svm2)
plot(varimp_svm, main="Variable Importance with SVM")
####################################################################################################
####################################################################################################
#Prediction and Confusion Matrices
predicted <- predict(model_svm2, testdata)
testdata$Sex = factor(testdata$Sex) #Make factors in data
levels(testdata$Sex) <- c("Female", "Male") #Ensure names
u=union(predicted, testdata$Sex)
t = table(factor(predicted, u), factor(testdata$Sex, u))
confusionMatrix(t)
####################################################################################################
####################################################################################################
#Stacking
#TAKES A LONG TIME TO RUN - DO WITH CAUTION
# Stacking Algorithms - Voting method - most votes classify
#setup CV for models
set.seed(56)
trainControl <- trainControl(method="repeatedcv",
number=10,
repeats=3,
savePredictions=TRUE,
classProbs=TRUE)
#algos to use in stack
algorithmList <- c('rf', 'xgbDART','knn','mlp')
#run algos
models <- caretList(Sex ~ ., data=traindata,
trControl=trainControl,
tuneLength = 3,
preProc = c("center", "scale"),
methodList=algorithmList)
results <- resamples(models)
summary(results)
set.seed(101)
#setup CV for stack
stackControl <- trainControl(method="repeatedcv",
number=10,
repeats=3,
savePredictions=TRUE,
preProc = c("center", "scale"),
classProbs=TRUE)
stack.glm <- caretStack(models, method="glm", metric="ROC", trControl=stackControl)
print(stack.glm)
#Compare algorithms
scales <- list(x=list(relation="free"), y=list(relation="free"))
bwplot(results, scales=scales)
splom(results)
# Step 2: Predict on testdata and Compute the confusion matrix
predicted3 <- predict(stack.glm, testdata)
testdata$Sex = factor(testdata$Sex) #Make factors in data
u=union(predicted3, testdata$Sex)
t = table(factor(predicted3, u), factor(testdata$Sex, u))
confusionMatrix(t)