-
Notifications
You must be signed in to change notification settings - Fork 0
/
elastic_regression.R
202 lines (143 loc) · 5.16 KB
/
elastic_regression.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
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
elasitic_model <- linear_reg(penalty = tune(),
mixture = tune()) %>%
# Set the model engine
set_engine('glmnet') %>%
# Set the model mode
set_mode('regression')
house_recipe <- recipe(adj_price ~ ., data = house_training) %>%
step_rm(id, Date) %>%
# Removed correlated predictors
step_corr(all_numeric(), threshold = 0.85) %>%
# Log transform numeric predictors
step_log(all_outcomes(), base = exp(1)) %>%
# Zero Variance Filter
step_zv(all_numeric(), -all_outcomes()) %>%
# Normalize numeric predictors
step_normalize(all_numeric(), -all_outcomes()) %>%
# Create dummy variables
step_dummy(all_nominal())
# Train recipe
house_recipe_prep <- house_recipe %>%
prep(training = house_training)
# Transform training data
house_training_enr <- house_recipe_prep %>%
bake(new_data = NULL)
folds_enr <- vfold_cv(house_training_enr, v = 6)
set.seed(123)
doParallel::registerDoParallel()
enr_grid <- expand_grid(penalty = seq(0,100, by = 20),
mixture = seq(0,1, by = 0.25))
tune_results_enr <- tune_grid(elasitic_model,
adj_price ~ .,
resamples = folds_enr,
grid = enr_grid,
metrics = metric_set(mae, rmse, rsq))
show_best(tune_results_enr, metric = "rmse")
show_best(tune_results_enr, metric = "rsq")
show_best(tune_results_enr, metric = "mae")
autoplot(tune_results_enr)
final_enr <- elasitic_model %>%
finalize_model(select_best(tune_results_enr))
best_enr_model <- final_enr %>% fit(adj_price ~ ., house_training_enr)
# Transform test data
house_test_enr <- house_recipe_prep %>%
bake(new_data = house_test)
enr_result <- predict(best_enr_model, new_data = house_test_enr)
# Combine test data with predictions
house_test_results_enr <- house_test_enr %>%
select(adj_price) %>%
bind_cols(enr_result)
# Caculate the RMSE metric
house_test_results_enr %>%
rmse(adj_price, .pred)
## rmse standard 0.370
# Calculate the R squared metric
house_test_results_enr %>%
rsq(adj_price, .pred)
## rsq standard 0.495
# Caculate the MAE metric
house_test_results_enr %>%
mae(adj_price, .pred)
## mae standard 0.281
##############################
house_training_long <- long_data %>%
filter(Date < "2021-09-01")
house_test_long <- long_data %>%
filter(Date >= "2021-09-01")
elasitic_model <- linear_reg(penalty = tune(),
mixture = tune()) %>%
# Set the model engine
set_engine('glmnet') %>%
# Set the model mode
set_mode('regression')
house_recipe <- recipe(adj_price ~ ., data = house_training) %>%
step_rm(id, Date) %>%
# Removed correlated predictors
step_corr(all_numeric(), threshold = 0.85) %>%
# Log transform numeric predictors
step_log(all_outcomes(), base = exp(1)) %>%
# Zero Variance Filter
step_zv(all_numeric(), -all_outcomes()) %>%
# Normalize numeric predictors
step_normalize(all_numeric(), -all_outcomes()) %>%
# Create dummy variables
step_dummy(all_nominal())
# Train recipe
house_recipe_prep <- house_recipe %>%
prep(training = house_training)
# Transform training data
house_training_enr <- house_recipe_prep %>%
bake(new_data = NULL)
folds_enr <- vfold_cv(house_training, v = 6)
set.seed(123)
doParallel::registerDoParallel()
enr_grid <- expand_grid(penalty = seq(0,1, by = 0.2),
mixture = seq(0,1, by = 0.25))
tune_results_enr <- tune_grid(elasitic_model,
house_recipe,
resamples = folds_enr,
grid = enr_grid,
metrics = metric_set(mae, rmse, rsq))
show_best(tune_results_enr, metric = "rmse")
show_best(tune_results_enr, metric = "rsq")
show_best(tune_results_enr, metric = "mae")
autoplot(tune_results_enr, metric = "mae")
final_enr <- elasitic_model %>%
finalize_model(select_best(tune_results_enr))
best_enr_model <- final_enr %>% fit(adj_price ~ ., house_training_enr)
# Transform test data
house_test_enr <- house_recipe_prep %>%
bake(new_data = house_test)
enr_result <- predict(best_enr_model, new_data = house_test_enr)
# Combine test data with predictions
house_test_results_enr <- house_test_enr %>%
select(adj_price) %>%
bind_cols(enr_result)
house_test_results_enr <- house_test_results_enr %>%
mutate(actual_price = exp(adj_price),
predicted_price = exp(.pred))
# Caculate the RMSE metric
house_test_results_enr %>%
rmse(adj_price, .pred)
## rmse standard 0.370
# Calculate the R squared metric
house_test_results_enr %>%
rsq(adj_price, .pred)
## rsq standard 0.495
# Caculate the MAE metric
house_test_results_enr %>%
mae(adj_price, .pred)
## mae standard 0.281
### Actual Price Errors:
# Caculate the RMSE metric
house_test_results_enr %>%
rmse(actual_price, predicted_price)
# rmse standard 442,868
# Calculate the R squared metric
house_test_results_enr %>%
rsq(actual_price, predicted_price)
## rsq standard 0.460
# Caculate the MAE metric
house_test_results_enr %>%
mae(actual_price, predicted_price)
## mae standard 292,694