-
Notifications
You must be signed in to change notification settings - Fork 461
/
Copy pathdiamonds_dash.Rmd
331 lines (259 loc) · 8.96 KB
/
diamonds_dash.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
323
324
325
326
327
328
329
330
331
---
title: "Dashing diamonds"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: fill
css: bootswatch-3.3.5-4/flatly/bootstrap.css
logo: STATWORX_2.jpg
runtime: shiny
---
```{r setup, include=FALSE}
rm(list = ls())
# load packages ----
library(tidyverse)
library(glmnet)
library(shiny)
library(plotly)
# set seed ----
set.seed(1)
# train test split ----
df <- diamonds %>% sample_n(3000)
rows_sample <- sample(nrow(df), 0.6 * nrow(df))
train_df <- df[rows_sample,]
test_df <- df[-rows_sample,]
# type conversions ----
train_df_cont <- train_df %>% dplyr::select_if(is.numeric)
train_df_cat <- train_df %>% dplyr::select_if(is.factor)
test_df_cont <- test_df %>% dplyr::select_if(is.numeric)
```
Exploratory plots
=======================================================================
Sidebar {.sidebar data-width=700}
-----------------------------------------------------------------------
**Exploratory plots**
This dashboard explores the `diamonds` dataset from the `ggplot2` package. It was created with **R Studio**'s package `flexdashboard`.
<br>
**Scatterplots**
Choose, which variables should be displayed in the scatterplot and which model should be used. You can also
choose to display the uncertainty around the regression lines. All widgets were created with `shiny`.
```{r inputs_1}
# inputs for scatter plot ----
selectInput("x", "X-Axis", choices = names(train_df), selected = "x")
selectInput("y", "Y-Axis", choices = names(train_df), selected = "price")
selectInput("z", "Color by:", choices = names(train_df), selected = "carat")
selectInput("model_type", "Select model", choices = c("LOESS" = "loess", "Linear" = "lm"), selected = "lm")
checkboxInput("se", "Confidence intervals ?")
```
<br>
**Density plot**
Select the variable to be shown in the density plot and the categorical variable by which to group it.
```{r inputs_2}
# input for density plot
selectInput("density_var", "Select variable for density plot", choices = names(train_df_cont),
selected = names(train_df_cont)[1])
selectInput("grouping_var", "Select variable by which to group", choices = names(train_df_cat),
selected = names(train_df_cat)[1])
```
<br>
**Summary statistics**
Finally, the mean and standard deviation are calculated for the price of the diamonds:
Pick a variable by which to group the calculation.
```{r inputs_3}
# input for datatable
selectInput("summary_grouping_var", "Variable by which to group summary statistics",
choices = names(train_df_cat), selected = names(train_df_cat)[1])
```
<br>
Row {.tabset}
-----------------------------------------------------------------------
### Scatterplot of selected variables
```{r scatterplot}
renderPlotly({
p <- train_df %>% ggplot(aes_string(x = input$x, y = input$y, col = input$z))
geom_point()
theme_minimal()
geom_smooth(method = input$model_type, position = "identity", se = input$se)
scale_fill_distiller(palette = "Spectral")
labs(x = input$x, y = input$y)
p %>% ggplotly()
})
```
### Density plot for selected variable
```{r density_plot}
renderPlotly({
p <- train_df %>% ggplot(aes_string(x = input$density_var, col = input$grouping_var))
geom_density() theme_minimal() labs(x = input$density_var)
p %>% ggplotly()
})
```
Row
-----------------------------------------------------------------------
### Maximum carats {data-width=50}
```{r}
flexdashboard::valueBox(max(train_df$carat),
caption = "maximal amount of carats",
color = "info",
icon = "fa-gem")
```
### Most expensive color {data-width=50}
```{r}
most_expensive_color <- train_df %>%
group_by(color) %>%
summarise(max_price = max(price)) %>%
arrange(max_price) %>%
slice(1)
flexdashboard::valueBox(most_expensive_color$color,
caption = "most expensive color",
color = "primary",
icon = "fa-gift")
```
### Maximal price {data-width=50}
```{r}
flexdashboard::valueBox(most_expensive_color$max_price,
caption = "highest price",
color = "success",
icon = 'fa-dollar-sign')
```
Row {data-height=500}
-----------------------------------------------------------------------
### Summary statistics {data-width=500}
```{r summary_stats}
df_plot <- reactive({
train_df %>%
group_by(!!sym(input$summary_grouping_var)) %>%
summarise(
`Mean price` = round(mean(price, na.rm = TRUE), 2),
`SD price` = round(sd(price, na.rm = TRUE), 2),
`Mean carats` = round(mean(carat, na.rm = TRUE), 2),
`SD carats` = round(sd(carat, na.rm = TRUE), 2),
`Mean depth`= round(mean(depth, na.rm = TRUE), 2),
`SD depth` = round(sd(depth, na.rm = TRUE), 2)
) %>% reshape2::melt(id = input$summary_grouping_var) %>%
filter(str_detect(variable, "price"))
})
renderPlotly({
df_plot() %>%
ggplot(aes_string(x = input$summary_grouping_var, y = "value", fill = "variable"))
geom_bar(stat = "identity", position = "dodge") theme_minimal() labs(y = " ", fill = " ")
})
```
Model comparison
=======================================================================
```{r model_comp_1, include=FALSE}
X_train <- train_df %>%
dplyr::select_if(is.numeric) %>%
as.matrix()
Y_train <- train_df %>%
dplyr::select(price) %>%
as.matrix()
X_test <- test_df %>%
dplyr::select_if(is.numeric) %>%
as.matrix()
Y_test <- test_df %>%
dplyr::select(price) %>%
as.matrix()
# train linear and loess model
linear_model <- lm(price ~ carat table x y, data = train_df_cont)
glmnet_model <- glmnet::cv.glmnet(X_train, Y_train, type.measure = "mse")
# get predictions on test set ----
preds_lm <- predict(linear_model, test_df_cont)
preds_glmnet <- predict.cv.glmnet(glmnet_model, newx = X_test) %>% as.numeric()
# get fitted values ----
fitted_vals_lm <- fitted.values(linear_model)
fitted_vals_glm <- predict.cv.glmnet(glmnet_model, newx = X_train) %>% as.numeric()
# functio to compare two models ----
error_scores <- function(x_1, x_2, y){
MAE_1 <- mean(abs(y- x_1))
MAE_2 <- mean(abs(y - x_2))
RMSE_1 <- sqrt(mean((y - x_1)^2))
RMSE_2 <- sqrt(mean((y - x_2)^2))
MAPE_1 <- 100 * mean(abs((y - x_1)/ y))
MAPE_2 <- 100 * mean(abs((y - x_2)/ y))
res <- data.frame(Model = c("Linear Model", "Ridge Regression"),
MAE = c(MAE_1, MAE_2),
RMSE = c(RMSE_1, RMSE_2),
MAPE = c(MAPE_1, MAPE_2))
return(res)
}
# insample errors ----
errors_insample <- error_scores(x_1 = fitted_vals_lm,
x_2 = fitted_vals_glm,
y = train_df$price)
# test set errors ----
errors_test <- error_scores(x_1 = preds_lm,
x_2 = preds_glmnet,
y = test_df$price)
# dataframe for plots ----
df_preds <- data.frame(prediction_glm = preds_glmnet,
prediction_lm = preds_lm,
target = test_df$price)
```
Sidebar {.sidebar data-width=700}
-----------------------------------------------------------------------
**Model comparison**
<br>
This page compares the performance of an linear model and an elastic net on a 60-40 train-test split. The target variable is the price of the diamonds.
<br>
The in sample performance is:
```{r insample_performance}
renderTable({
errors_insample
}, striped = TRUE,
bordered = TRUE,
spacing = "s",
align = "l")
```
On the test set, the performance is:
```{r outofsample_performance}
renderTable({
errors_test
}, striped = TRUE,
bordered = TRUE,
spacing = "s",
align = "l")
```
Both in and out of sample errors show, that the ridge regression is clearly preferable, when compared to a simple linear model.
<br>
Row{.tabset}
-----------------------------------------------------------------------
**Comparison of Predictions and Target**
### Linear Model
```{r test_preds_lm}
renderPlot({
df_preds %>%
ggplot(aes(x = prediction_lm, y = target))
geom_point()
geom_abline(slope = 1, intercept = 0)
theme_minimal()
labs(x = "Predicted price", y = "Actual price")
})
```
### Ridge Regression
```{r test_preds_glmnet}
renderPlot({
df_preds %>%
ggplot(aes(x = prediction_glm, y = target))
geom_point()
geom_abline(slope = 1, intercept = 0)
theme_minimal()
labs(x = "Predicted price", y = "Actual price")
})
```
Row
-----------------------------------------------------------------------
### Densities of predictions vs target
```{r preds_densities_glmnet}
renderPlot({
df_preds %>%
rename("Linear model predicted price" = prediction_lm,
"Elastic Net predicted price" = prediction_glm,
"Actual price" = target) %>%
gather() %>%
ggplot(aes(x = value, color = key))
labs(x = "Price")
geom_density()
theme_minimal()
labs(color = " ")
})
```