forked from DrewWham/PSU_Stat_380
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glmnet_model.R
115 lines (62 loc) · 2.21 KB
/
glmnet_model.R
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
library(data.table)
library(caret)
library(Metrics)
library(glmnet)
library(plotmo)
library(lubridate)
set.seed(7)
#read in data, notice the path will always look like this because the assumed working directory is the repo level folder
train<-fread("./project/volume/data/interim/train_mtg.csv")
test<-fread("./project/volume/data/interim/test_mtg.csv")
example_sub<-fread("./project/volume/data/raw/example_submission.csv")
##########################
# Prep Data for Modeling #
##########################
train$current_date<-as_date(train$current_date)
train<-train[order(-current_date)]
# subset out only the columns to model
drops<- c('id','future_date','current_date')
train<-train[, !drops, with = FALSE]
test<-test[, !drops, with = FALSE]
#keep<-c("current_price","future_price","rarity","Land","BS_199","BS_200","BS_201")
#train<-train[, keep, with = FALSE]
#test<-test[, keep, with = FALSE]
#save the response var because dummyVars will remove
train_y<-train$future_price
test$future_price<-0
# work with dummies
dummies <- dummyVars(future_price ~ ., data = train)
train<-predict(dummies, newdata = train)
test<-predict(dummies, newdata = test)
train<-data.table(train)
test<-data.table(test)
########################
# Use cross validation #
########################
train<-as.matrix(train)
test<-as.matrix(test)
gl_model<-cv.glmnet(train, train_y, alpha = 1,family="gaussian")
bestlam<-gl_model$lambda.min
####################################
# fit the model to all of the data #
####################################
#now fit the full model
#fit a logistic model
gl_model<-glmnet(train, train_y, alpha = 1,family="gaussian")
plot_glmnet(gl_model)
#save model
saveRDS(gl_model,"./project/volume/models/gl_model.model")
test<-as.matrix(test)
#use the full model
pred<-predict(gl_model,s=bestlam, newx = test)
bestlam
predict(gl_model,s=bestlam, newx = test,type="coefficients")
gl_model
#########################
# make a submision file #
#########################
#our file needs to follow the example submission file format.
#we need the rows to be in the correct order
example_sub$future_price<-pred
#now we can write out a submission
fwrite(example_sub,"./project/volume/data/processed/submit_17.csv")