-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 18.3 KB
/
.Rhistory
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
knitr::opts_chunk$set(warning = F, message = F)
# Chunk 2: libraries
# load libraries
library(plotly)
library(tidyverse)
library(readxl)
library(knitr)
library(scales)
library(ggplot2)
library(fastDummies)
library(DT)
library(bit64)
library(psych)
library(kableExtra)
library(mice)
library(skimr)
library(glmnet)
library(caTools)
library(xfun)
library(mice)
library(leaps)
library(openxlsx)
library(randomForest)
library(gbm)
library(rpart)
# prevent scientific notation
options(scipen = 999)
# Chunk 3
# show data in environment window
diamonds = diamonds
# show the first 10 rows of the data, exclude row names
datatable(head(diamonds, n=10), rownames = F)
# Chunk 4
# structure
str(diamonds)
# Chunk 5
# find count of missing values per column
sapply(diamonds, function(x) sum(is.na(x)))
# Chunk 6
unique(diamonds$cut)
diamonds$cut = as.numeric(diamonds$cut)
# Chunk 7
unique(fct_rev(diamonds$color))
diamonds$color = as.numeric(fct_rev(diamonds$color))
# Chunk 8
unique(diamonds$clarity)
diamonds$clarity = as.numeric(diamonds$clarity)
diamonds = diamonds %>%
dplyr::filter(x > 0)
diamonds$y = NULL
diamonds$z = NULL
pdf("rplot.pdf")
pairs.panels(diamonds, pch=5, stars=T, lm=T, ellipses=F)
dev.off()
pdf("rplot.pdf")
pairs.panels(diamonds, pch=5, stars=T, lm=T, ellipses=F, jiggle=T)
dev.off()
pdf("rplot.pdf")
pairs.panels(diamonds, pch=20, stars=T, lm=T, ellipses=F, jiggle=T)
dev.off()
pdf("rplot.pdf")
pairs.panels(diamonds, stars=T, lm=T, ellipses=F, jiggle=T)
dev.off()
pdf("rplot.pdf")
pairs.panels(diamonds, pch='.', stars=T, lm=T, ellipses=F, jiggle=T)
dev.off()
x=diamonds %>%
mutate(LogPrice=log(price),
LogCarat=log(carat),
LogCut=log(cut),
SqrtDepth=sqrt(depth),
LogTable=log(table),
SqrtX=sqrt(x))
View(x)
names(x)
diamonds=diamonds %>%
mutate(LogPrice=log(price),
LogCarat=log(carat),
LogCut=log(cut),
SqrtDepth=sqrt(depth),
LogTable=log(table),
SqrtX=sqrt(x))
pdf("rplot2.pdf")
pairs.panels(diamonds %>% dplyr::select(LogPrice,LogCarat,LogCut,SqrtDepth,LogTable,SqrtX,color,clarity), pch='.', stars=T, lm=T, ellipses=F, jiggle=T)
dev.off()
pdf("rplot3.pdf")
pairs.panels(diamonds %>% dplyr::select(LogPrice,LogCarat,SqrtCut,InvDepth,InvTable,SqrtX,color,clarity), pch='.', stars=T, lm=T, ellipses=F, jiggle=T)
dev.off()
diamonds=diamonds %>%
mutate(LogPrice=log(price),
LogCarat=log(carat),
SqrtCut=sqrt(cut),
InvDepth=1/depth,
InvTable=1/table,
SqrtX=sqrt(x))
pdf("rplot3.pdf")
pairs.panels(diamonds %>% dplyr::select(LogPrice,LogCarat,SqrtCut,InvDepth,InvTable,SqrtX,color,clarity), pch='.', stars=T, lm=T, ellipses=F, jiggle=T)
dev.off()
diamonds=diamonds %>%
mutate(LogPrice=log(price),
LogCarat=log(carat),
InvCut=sqrt(cut),
InvDepth=1/depth,
InvTable=1/table,
SqrtX=sqrt(x))
diamonds=diamonds %>%
mutate(LogPrice=log(price),
LogCarat=log(carat),
InvCut=1/cut,
InvDepth=1/depth,
InvTable=1/table,
SqrtX=sqrt(x))
pdf("rplot4.pdf")
pairs.panels(diamonds %>% dplyr::select(LogPrice,LogCarat,SqrtCut,InvDepth,InvTable,SqrtX,color,clarity), pch='.', stars=T, lm=T, ellipses=F, jiggle=T)
dev.off()
diamonds=diamonds %>%
mutate(LogPrice=log(price),
LogCarat=log(carat),
InvCut=1/cut,
InvDepth=1/depth,
InvTable=1/table,
SqrtX=sqrt(x))
pdf("rplot4.pdf")
pairs.panels(diamonds %>% dplyr::select(LogPrice,LogCarat,InvCut,InvDepth,InvTable,SqrtX,color,clarity), pch='.', stars=T, lm=T, ellipses=F, jiggle=T)
dev.off()
diamonds=diamonds %>%
mutate(LogPrice=log(price),
LogCarat=log(carat),
InvCut=1/cut^(2/3),
InvDepth=1/depth,
InvTable=1/table,
SqrtX=sqrt(x))
pdf("rplot4.pdf")
pairs.panels(diamonds %>% dplyr::select(LogPrice,LogCarat,InvCut,InvDepth,InvTable,SqrtX,color,clarity), pch='.', stars=T, lm=T, ellipses=F, jiggle=T)
dev.off()
diamonds=diamonds %>%
mutate(LogPrice=log(price),
LogCarat=log(carat),
InvCut=1/cut^(3/2),
InvTable=1/table,
SqrtX=sqrt(x))
pdf("rplot4.pdf")
pairs.panels(diamonds %>% dplyr::select(LogPrice,LogCarat,InvCut,Depth,InvTable,SqrtX,color,clarity), pch='.', stars=T, lm=T, ellipses=F, jiggle=T)
dev.off()
pdf("rplot4.pdf")
pairs.panels(diamonds %>% dplyr::select(LogPrice,LogCarat,InvCut,depth,InvTable,SqrtX,color,clarity), pch='.', stars=T, lm=T, ellipses=F, jiggle=T)
dev.off()
pdf("rplot4.pdf")
pairs.panels(diamonds %>% dplyr::select(LogPrice,LogCarat,cut,SqrtDepth,InvTable,SqrtX,color,clarity), pch='.', stars=T, lm=T, ellipses=F, jiggle=T)
dev.off()
library(MASS)
data(cats) # load cats data
force(cats)
View(cats)
Y = cats[,1] # extract labels from the data
msk = sample.split(Y, SplitRatio=3/4)
library(caTools)
msk = sample.split(Y, SplitRatio=3/4)
msk
View(cats)
# Chunk 1: setup
#knitr::opts_chunk$set(echo = F, warning = F, message = F)
knitr::opts_chunk$set(warning = F, message = F)
# Chunk 2: libraries
# load libraries
library(plotly)
library(tidyverse)
library(readxl)
library(knitr)
library(scales)
library(ggplot2)
library(fastDummies)
library(DT)
library(bit64)
library(psych)
library(kableExtra)
library(mice)
library(skimr)
library(glmnet)
library(caTools)
library(xfun)
library(mice)
library(leaps)
library(openxlsx)
library(randomForest)
library(gbm)
library(rpart)
# set working directory
setwd('C:\\Users\\danie\\Music\\Enova')
# prevent scientific notation and do not load strings as factors
options(scipen = 999, stringsAsFactors = F)
# load tittle logo
htmltools::img(src=knitr::image_uri('C:\\Users\\danie\\Music\\Enova\\Data\\enova_logo.png'),
alt='logo',
style='position:absolute; top:15px; right:0; padding:10px:')
# Chunk 3: data_dictionary
dictionary <- read_excel("data_dictionary.xlsx")
dictionary %>%
datatable(rownames = F,
caption = 'Table 1. Data dictionary.',
filter = 'top',
options = list(paging=T,
selection="multiple",
escape=F,
filter = list(position = 'top'),
sDom = '<"top">lrt<"bottom">ip',
columnDefs=list(list(className='nowrap', targets='_all'))))
# Chunk 4
training = read.csv('training.csv')
offers = read.csv('offers_original.csv')
dim(training)
sapply(training, function(x) sum(is.na(x)))
dim(offers)
sapply(offers, function(x) sum(is.na(x)))
# Chunk 5
table(training$Cert, useNA = 'ifany')
training$Cert_Dummy = as.numeric(recode(training$Cert,
'AGSL'='0',
'GemEx'='1',
.default = 'NA'))
table(training$Cert_Dummy, useNA = 'ifany')
# Chunk 6
table(training$Clarity, useNA = 'ifany')
training$Clarity_Coded = as.numeric(recode(training$Clarity,
'FL'='1',
'IF'='2',
'VVS1'='3',
'VVS2'='4',
'VS1'='5',
'VS2'='6',
'SI1'='7',
'SI2'='8',
'I1'='9',
'I2'='10',
'I3'='11',
.default = 'NA'))
table(training$Clarity_Coded, useNA = 'ifany')
# Chunk 7
table(training$Color, useNA = 'ifany')
# "Ffancy darkbrown", "Flby", "S-t", "T" exists in offers, but not in training ***************
training$Color_Coded = as.numeric(recode(training$Color,
'D'='1',
'E'='2',
'F'='3',
'Fdpink'='3',
'Fdy'='3',
'Ffcdbrown'='3',
'Ffcg'='3',
'Ffcly'='3',
'Ffcy'='3',
'Ffg'='3',
'Fiy'='3',
'Fiyellow'='3',
'Fly'='3',
'Flyellow'='3',
'Fvyellow'='3',
'Fyellow'='3',
'Ffancy darkbrown'='3',
'Flby' = '3',
'G'='4',
'Gy'='4',
'H'='5',
'I'='6',
'J'='7',
'K'='8',
'L'='9',
'Lb'='9',
'M'='10',
'N'='11',
'O-p'='12',
'P'='13',
'Q-r'='14',
'S'='16',
'S-t'='16',
'T'='17',
'U'='18',
'W'='20',
.default = 'NA'))
table(training$Color_Coded, useNA = 'ifany')
# Chunk 8
table(training$Cut, useNA = 'ifany')
training$Cut_Coded = as.numeric(recode(training$Cut,
'Excellent'='1',
'Ideal'='1',
'Very good'='2',
'Good'='3',
'Fair'='4',
.default = 'NA'))
table(training$Cut_Coded, useNA = 'ifany')
# Chunk 9
table(training$Polish, useNA = 'ifany')
training$Polish_Coded = as.numeric(recode(training$Polish,
'Excellent' = '1',
'Very good' = '2',
'Good' = '3',
.default = 'NA'))
table(training$Polish_Coded, useNA = 'ifany')
# Chunk 10
table(training$Symmetry, useNA = 'ifany')
training$Symmetry_Coded = as.numeric(recode(training$Symmetry,
'Excellent' = '1',
'Execllent' = '1',
'Very good' = '2',
'Good' = '3',
'Fair' = '4',
'Faint' = '5',
.default = 'NA'))
table(training$Symmetry_Coded, useNA = 'ifany')
# Chunk 11
training = training[training$Vendor > 1, ]
training = dummy_cols(training,
select_columns = c('Regions', 'Vendor'),
remove_first_dummy = T)
# Chunk 12
training$round_shape = if_else(training$Shape == 'Round' | training$Shape == 'ROUND', 1, 0)
# Chunk 13
training = cbind(training,
as.data.frame(training[,c('Measurements')]) %>%
separate(1, c('Length', 'Width', 'Height'), sep = 'x'))
training$Length = as.numeric(training$Length)
training$Width = as.numeric(training$Width)
training$Height = as.numeric(training$Height)
training$Volume = training$Length * training$Width * training$Height
# Chunk 14
training$Depth = if_else(is.na(training$Depth), 2*100*training$Height/(training$Length+training$Width), training$Depth)
# Chunk 15: missing_certification
training = training[training$Known_Conflict_Diamond == F, ]
training = training[!is.na(training$Cert), ]
# Chunk 16
pairs.panels(training[,c('Carats','Depth','Table','Length','Width','Height','LogPrice','LogRetail')], pch=4, stars=T, lm=T, ellipses=F)
pairs.panels(training[,c('Clarity_Coded','Color_Coded','Cut_Coded','Polish_Coded','Symmetry_Coded','LogPrice','LogRetail')], pch=4, stars=T, lm=T, ellipses=F)
ggplot(training, aes(x=Regions, y=LogPrice)) +
geom_violin()+
geom_boxplot(width=0.15)
ggplot(training, aes(x=Regions, y=LogRetail)) +
geom_violin()+
geom_boxplot(width=0.15)
ggplot(training, aes(x=Cert, y=LogPrice)) +
geom_violin()+
geom_boxplot(width=0.15)
ggplot(training, aes(x=Cert, y=LogRetail)) +
geom_violin()+
geom_boxplot(width=0.15)
ggplot(training, aes(x=Shape, y=LogPrice)) +
geom_violin()+
geom_boxplot(width=0.15)
ggplot(training, aes(x=Shape, y=LogRetail)) +
geom_violin()+
geom_boxplot(width=0.15)
ggplot(training, aes(x=as.character(round_shape), y=LogPrice)) +
geom_violin()+
geom_boxplot(width=0.15)
ggplot(training, aes(x=as.character(round_shape), y=LogRetail)) +
geom_violin()+
geom_boxplot(width=0.15)
# Chunk 17
training = training[training$Depth > 0 & training$Carats < 4.05, ]
training2=training[,c('id', 'Carats', 'Depth', 'Length', 'Width', 'Height', 'Cert_Dummy', 'Regions_Australia', 'Regions_Botswana', 'Regions_Canada', 'Regions_DR Congo', 'Regions_Other/Unknown', 'Regions_Russia', 'Regions_South Africa', 'Regions_Zimbabwe', 'Vendor_3', 'Vendor_4', 'round_shape', 'Clarity_Coded', 'Color_Coded', 'Cut_Coded', 'Polish_Coded', 'Symmetry_Coded', 'LogPrice', 'LogRetail')]
names(training2)=c('id', 'Carats', 'Depth', 'Length', 'Width', 'Height', 'Cert_Dummy', 'Regions_Australia', 'Regions_Botswana', 'Regions_Canada', 'Regions_DR_Congo', 'Regions_Other_Unknown', 'Regions_Russia', 'Regions_South_Africa', 'Regions_Zimbabwe', 'Vendor_3', 'Vendor_4', 'round_shape', 'Clarity_Coded', 'Color_Coded', 'Cut_Coded', 'Polish_Coded', 'Symmetry_Coded', 'LogPrice', 'LogRetail')
skim(training2[,c(-1)])
# Chunk 18
#imp$loggedEvents #collinear Width
imp = mice(training2)
# Chunk 19
training2=complete(imp)
sapply(training2, function(x) sum(is.na(x)))
# Chunk 20
training2$Log_Carats = log(training2$Carats)
training2$Log_Depth = log(training2$Depth)
training2$Reciprocal_Length = 1/(training2$Length)
training2$Reciprocal_Width = 1/(training2$Width)
training2$Reciprocal_Height = 1/(training2$Height)
training2=training2[,c('id', 'Log_Carats', 'Log_Depth', 'Reciprocal_Length', 'Reciprocal_Width', 'Reciprocal_Height', 'Cert_Dummy', 'Regions_Australia', 'Regions_Botswana', 'Regions_Canada', 'Regions_DR_Congo', 'Regions_Other_Unknown', 'Regions_Russia', 'Regions_South_Africa', 'Regions_Zimbabwe', 'Vendor_3', 'Vendor_4', 'round_shape', 'Clarity_Coded', 'Color_Coded', 'Cut_Coded', 'Polish_Coded', 'Symmetry_Coded', 'LogPrice', 'LogRetail')]
pairs.panels(training2[,c('Log_Carats','Log_Depth','Reciprocal_Length','Reciprocal_Width','Reciprocal_Height','LogPrice',
'LogRetail')], pch=4, stars=T, lm=T, ellipses=F)
pairs.panels(training2[,c('Clarity_Coded','Color_Coded','Cut_Coded','Polish_Coded','Symmetry_Coded','LogPrice','LogRetail')], pch=4, stars=T, lm=T, ellipses=F)
# check all correlations
#round(cor(training2[,c(-1)]),2)
# Chunk 21
ggplot(training, aes(x=as.character(Vendor), y=LogPrice)) +
geom_violin()+
geom_boxplot(width=0.15)
training$LogPrice_to_Carat = training$LogPrice/training$Carats
ggplot(training, aes(x=as.character(Vendor), y=LogPrice_to_Carat)) +
geom_violin()+
geom_boxplot(width=0.15)
answers=merge(training[,c('id','Vendor')],training2,by='id')
plot_ly(data=training,
x=~Carats,
mode='markers',
type='scatter',
marker=list(size=4),
y=~LogPrice,
#symbol=~Cut,
#frame=~Regions,
color=~as.character(Vendor))
answers %>%
group_by(Vendor) %>%
summarise(Average_clarity = mean(Clarity_Coded, na.rm=T),
Average_cut = mean(Cut_Coded, na.rm=T),
Average_Color = mean(Color_Coded, na.rm=T),
Average_Polish = mean(Polish_Coded, na.rm=T),
Average_Color = mean(Symmetry_Coded, na.rm=T),
Average_Log_Depth = mean(Log_Depth, na.rm=T),
Average_Log_Carats = mean(Log_Carats, na.rm=T))
# Chunk 22
plot_ly(data=training,
x=~Carats,
mode='markers',
type='scatter',
marker=list(size=4),
y=~Retail
#symbol=~Cut,
#frame=~Regions
) %>%
add_lines(y = ~fitted(loess(Retail ~ Carats)))
# Chunk 23
knitr::opts_chunk$set(cache = T)
DfCut = sample.split(training2, SplitRatio = .7)
train = training2[DfCut,]
test = training2[!DfCut,]
xTrain = train[,-c(1,4,5,25,24)]
yTrainPrice = train[,c(24)]
yTrainRetail = train[,c(25)]
xTest = test[,-c(1,4,5,25,24)]
yTestPrice = test[,c(24)]
yTestRetail = test[,c(25)]
# lasso regression for price
cvModel = cv.glmnet(x = as.matrix(xTrain), y=yTrainPrice, alpha = 1)
LassoModel = glmnet(x = as.matrix(xTrain), y=yTrainPrice, alpha=1, lambda=cvModel$lambda.min)
yTestPricePredict = as.vector(predict(LassoModel, s=cvModel$lambda.min, newx=as.matrix(xTest)))
rmse = sqrt(mean((yTestPrice - yTestPricePredict)^2))
se = sd((yTestPrice - yTestPricePredict)^2)/sqrt(length(yTestPricePredict))
models = data.frame(Model='Lasso Regression',Type='Price',RMSE=rmse, SE=se)
#lasso regression for retail
cvModel = cv.glmnet(x = as.matrix(xTrain), y=yTrainRetail, alpha = 1)
LassoModel = glmnet(x = as.matrix(xTrain), y=yTrainRetail, alpha=1, lambda=cvModel$lambda.min)
yTestPricePredict = as.vector(predict(LassoModel, s=cvModel$lambda.min, newx=as.matrix(xTest)))
rmse = sqrt(mean((yTestRetail - yTestPricePredict)^2))
se = sd((yTestPrice - yTestPricePredict)^2)/sqrt(length(yTestPricePredict))
models[nrow(models) + 1,] = c('Lasso Regression', 'Retail', rmse, se)
# ridge regression for price
cvModel = cv.glmnet(x = as.matrix(xTrain), y=yTrainPrice, alpha = 0)
LassoModel = glmnet(x = as.matrix(xTrain), y=yTrainPrice, alpha=0, lambda=cvModel$lambda.min)
yTestPricePredict = as.vector(predict(LassoModel, s=cvModel$lambda.min, newx=as.matrix(xTest)))
rmse = sqrt(mean((yTestPrice - yTestPricePredict)^2))
se = sd((yTestPrice - yTestPricePredict)^2)/sqrt(length(yTestPricePredict))
models[nrow(models) + 1,] = c('Lasso Regression', 'Price', rmse, se)
#ridge regression for retail
cvModel = cv.glmnet(x = as.matrix(xTrain), y=yTrainRetail, alpha = 0)
LassoModel = glmnet(x = as.matrix(xTrain), y=yTrainRetail, alpha=0, lambda=cvModel$lambda.min)
yTestPricePredict = as.vector(predict(LassoModel, s=cvModel$lambda.min, newx=as.matrix(xTest)))
rmse = sqrt(mean((yTestRetail - yTestPricePredict)^2))
se = sd((yTestRetail - yTestPricePredict)^2)/sqrt(length(yTestPricePredict))
models[nrow(models) + 1,] = c('Lasso Regression', 'Retail', rmse, se)
# net elastic regression for price
cvModel = cv.glmnet(x = as.matrix(xTrain), y=yTrainPrice, alpha = .5)
LassoModel = glmnet(x = as.matrix(xTrain), y=yTrainPrice, alpha=.5, lambda=cvModel$lambda.min)
yTestPricePredict = as.vector(predict(LassoModel, s=cvModel$lambda.min, newx=as.matrix(xTest)))
rmse = sqrt(mean((yTestPrice - yTestPricePredict)^2))
se = sd((yTestPrice - yTestPricePredict)^2)/sqrt(length(yTestPricePredict))
models[nrow(models) + 1,] = c('Net Elastic', 'Price', rmse, se)
#lasso regression for retail
cvModel = cv.glmnet(x = as.matrix(xTrain), y=yTrainRetail, alpha = .5)
LassoModel = glmnet(x = as.matrix(xTrain), y=yTrainRetail, alpha=.5, lambda=cvModel$lambda.min)
yTestPricePredict = as.vector(predict(LassoModel, s=cvModel$lambda.min, newx=as.matrix(xTest)))
rmse = sqrt(mean((yTestRetail - yTestPricePredict)^2))
se = sd((yTestRetail - yTestPricePredict)^2)/sqrt(length(yTestPricePredict))
models[nrow(models) + 1,] = c('Net Elastic Regression', 'Retail', rmse, se)
# decision trees for price
fit = rpart(yTrainPrice~., data = xTrain, control = rpart.control(cp = 0.00001))
yTestPricePredict = predict(fit, data.frame(xTest))
rmse = sqrt(mean((yTestPrice - yTestPricePredict)^2))
se = sd((yTestPrice - yTestPricePredict)^2)/sqrt(length(yTestPricePredict))
models[nrow(models) + 1,] = c('Decision Trees', 'Price', rmse, se)
# decision trees for retail
fit = rpart(yTrainRetail~., data = xTrain, control = rpart.control(cp = 0.00001))
yTestPricePredict = predict(fit, data.frame(xTest))
rmse = sqrt(mean((yTestRetail - yTestPricePredict)^2))
se = sd((yTestRetail - yTestPricePredict)^2)/sqrt(length(yTestPricePredict))
models[nrow(models) + 1,] = c('Decision Trees', 'Retail', rmse, se)
# random forest for price
fit = randomForest(yTrainPrice ~ ., data=xTrain, ntree=1000)
yTestPricePredict = predict(fit, data.frame(xTest))
rmse = sqrt(mean((yTestPrice - yTestPricePredict)^2))
se = sd((yTestPrice - yTestPricePredict)^2)/sqrt(length(yTestPricePredict))
models[nrow(models) + 1,] = c('Random Forest', 'Price', rmse, se)
# random forest for retail
fit = randomForest(yTrainRetail ~ ., data=xTrain, ntree=1000)
yTestPricePredict = predict(fit, data.frame(xTest))
rmse = sqrt(mean((yTestRetail - yTestPricePredict)^2))
se = sd((yTestRetail - yTestPricePredict)^2)/sqrt(length(yTestPricePredict))
models[nrow(models) + 1,] = c('Random Forest', 'Retail', rmse, se)
# gradient boosting for price
gbm_price = gbm(yTrainPrice ~., data=xTrain, distribution="gaussian", cv.folds=10,interaction.depth=5, shrinkage=0.01, n.trees=1000)
gbm_price_predict = predict(gbm_price, xTest)
rmse = sqrt(mean((yTestPrice - gbm_price_predict)^2))
se = sd((yTestPrice - gbm_price_predict)^2)/sqrt(length(gbm_price_predict))
models[nrow(models) + 1,] = c('Gradient Boosting', 'Price', rmse, se)
# gradient boosting for retail
fit = gbm(yTrainRetail ~., data=xTrain, distribution="gaussian", cv.folds=10, interaction.depth=5, shrinkage=0.01, n.trees=1000)
yTestPricePredict = predict(fit, data.frame(xTest))
rmse = sqrt(mean((yTestRetail - yTestPricePredict)^2))
se = sd((yTestRetail - yTestPricePredict)^2)/sqrt(length(yTestPricePredict))
models[nrow(models) + 1,] = c('Gradient Boosting', 'Retail', rmse, se)
datatable(models, options = list(pageLength = 12))
x=cbind(xTest, gbm_price_predict, yTestPrice)
View(x)
pairs(mtcars[1:3], upper.panel=panel.regression, lower.panel=panel.cor, cex.labels=1)
pairs(mtcars[1:3], upper.panel=panel.regression, cex.labels=1)
pairs(mtcars[1:3],cex.labels=1)
pairs(mtcars[1:3],cex.labels=2)