-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulation.Rmd
322 lines (241 loc) · 6.66 KB
/
Simulation.Rmd
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
---
title: "Simulation"
author: 'NULL'
date: "8/24/2021"
output: pdf_document
---
```{r}
# This is an example problem with p=20 covariates and sample size n=25.
# The first 6 variables each have true coefficient of 1 and are normally
# distributed with a strong AR(1)-type correlation among them.
# The remaining 14 variables are independent normals with no effect.
```
```{r}
# Load the Libraries
library(glmnet)
library(corrplot)
library(caret)
library(ggplot2)
library(pheatmap)
```
```{r}
# Load the Dataset
data = read.csv("C:\\Users\\sarmad\\Desktop\\data.csv", header = TRUE)
```
```{r}
str(data)
```
```{r}
summary(data)
```
```{r}
M = cor(data)
corrplot(M, method = 'number')
corrplot(M, method = 'color', order = 'alphabet')
```
```{r}
d <- as.matrix(data)
# Default Heatmap
heatmap(d)
heatmap(d, Colv = NA, Rowv = NA, scale="column")
pheatmap(d, cutree_rows = 4)
```
```{r}
# Set seed
set.seed(123)
# Partition and create index matrix of selected values
index <- createDataPartition(data$target, p=0.7, list = FALSE, times = 1)
# Converting data to dataframe object
df <- as.data.frame(data)
# Create Train and Test dataframes
train_data <- df[index,]
test_data <- df[-index,]
```
```{r}
# k-fold cross validation (10 Folds)
Control_specifications <- trainControl(method = "cv", number = 5,
savePredictions = "all")
```
```{r}
# Specify and train LASSO Regression Model
# Create a vector of potential lambda values
lambda_vector <- 10^seq(5, -3, length=500)
# Set seed
set.seed(111)
# Specify LASSO Regression Model to be estimated using training data
model1 <- train(target ~ .,
data = train_data,
preProcess=c("center","scale"),
method="glmnet",
tuneGrid=expand.grid(alpha=1, lambda=lambda_vector),
trControl=Control_specifications,
na.action=na.omit)
```
```{r}
model1$bestTune$lambda
```
```{r}
round(coef(model1$finalModel, model1$bestTune$lambda), 3)
```
```{r}
# Plotting Log(lambda) & RMSE
plot(log(model1$results$lambda),
model1$results$RMSE,
col = "red",
xlab = "log(lambda)",
ylab = "RMSE")
```
```{r}
# Data Visualization of Variable Importance
ggplot(varImp(model1))
```
```{r}
# Model Prediction
predictions1 <- predict(model1, newdata = test_data)
predictions1
```
```{r}
# Model Performance & Accuracy
Model_performance <- data.frame(RMSE=RMSE(predictions1, test_data$target),
Rsquared=R2(predictions1, test_data$target))
Model_performance
```
```{r}
# Set seed
set.seed(333)
# Specify Ridge Regression Model to be estimated using training data
model3 <- train(target ~ .,
data = train_data,
preProcess=c("center","scale"),
method="glmnet",
tuneGrid=expand.grid(alpha=0, lambda=lambda_vector),
trControl=Control_specifications,
na.action=na.omit)
```
```{r}
# Best optimal tuning parameter lambda
model3$bestTune$lambda
```
```{r}
# Rounding off for better visualization
round(coef(model3$finalModel, model3$bestTune$lambda), 3)
```
```{r}
# Plotting Log(lambda) & RMSE
plot(log(model3$results$lambda),
model3$results$RMSE,
col = "blue",
xlab = "log(lambda)",
ylab = "RMSE")
```
```{r}
# Data Visualization of Variable Importance
ggplot(varImp(model3))
```
```{r}
# Model Prediction
predictions3 <- predict(model3, newdata = test_data)
predictions3
```
```{r}
# Model Performance & Accuracy
Model_performance3 <- data.frame(RMSE=RMSE(predictions3, test_data$target),
Rsquared=R2(predictions3, test_data$target))
Model_performance3
```
```{r}
# Set seed
set.seed(444)
# Specify Elastic-Net Regression Model to be estimated using training data
model4 <- train(target ~ .,
data = train_data,
preProcess=c("center","scale"),
method="glmnet",
tuneGrid=expand.grid(alpha=0.5, lambda=lambda_vector),
trControl=Control_specifications,
na.action=na.omit)
```
```{r}
# Best optimal tuning parameter lambda
model4$bestTune$lambda
```
```{r}
# Rounding off for better visualization
round(coef(model4$finalModel, model4$bestTune$lambda), 3)
```
```{r}
# Plotting Log(lambda) & RMSE
plot(log(model4$results$lambda),
model4$results$RMSE,
col = "green",
xlab = "log(lambda)",
ylab = "RMSE")
```
```{r}
# Data Visualization of Variable Importance
ggplot(varImp(model4))
```
```{r}
# Model Prediction
predictions4 <- predict(model4, newdata = test_data)
predictions4
```
```{r}
# Model Performance & Accuracy
Model_performance4 <- data.frame(RMSE=RMSE(predictions4, test_data$target),
Rsquared=R2(predictions4, test_data$target))
Model_performance4
```
```{r}
library(grpreg)
group1 <- rep(1:20, each=1)
group1
```
```{r}
fit <- grpreg(train_data[1:20], train_data$target, group1, penalty = "grLasso")
plot(fit)
```
```{r}
coef(fit, lambda = 0.04230847)
```
```{r}
cvfit <- cv.grpreg(train_data[1:20], train_data$target, group1, penalty = "grLasso")
plot(cvfit)
```
```{r}
coef(cvfit)
```
```{r}
round(coef(cvfit), 3)
```
```{r}
summary(cvfit)
```
```{r}
cvfit$lambda.min
```
```{r}
predictions5 <- predict(fit, as.matrix(test_data[,1:20]), type = "response", lambda = cvfit$lambda.min)
predictions5
```
```{r}
Model_performance5 <- data.frame(RMSE=RMSE(predictions5, test_data$target),
Rsquared=R2(predictions5, test_data$target))
Model_performance5
```
```{r}
# Comparison of RMSE and R-square error for LASSO, OLS Multi Linear Regression model, RIDGE Regression, Elastic-Net Regression and Group Lasso.
comp2 <- matrix(c(Model_performance$RMSE, Model_performance$Rsquared,
Model_performance3$RMSE, Model_performance3$Rsquared,
Model_performance4$RMSE, Model_performance4$Rsquared,
Model_performance5$RMSE, Model_performance5$Rsquared),
ncol = 2, byrow = TRUE)
colnames(comp2) <- c("RMSE", "R-square")
rownames(comp2) <- c("LASSO Regression", "RIDGE Regression", "Elastic-Net Regression", "Group lasso")
comp2
```
```{r}
# Comparison of RMSE and R-square error for LASSO, OLS Multi Linear Regression model, RIDGE Regression, Elastic-Net Regression and Group Lasso.
comp2 <- as.table(comp2)
round(comp2, 4)
```