-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtidymodels.R
180 lines (117 loc) · 3.13 KB
/
tidymodels.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
### Esempio Tidymodels
### S.B. 25.01.2023
options(scipen = 999)
## libraries to be used
library(ISLR2)
library(tidymodels)
tidymodels_prefer(quiet=F)
## how does initial_split work?
?initial_split()
## set seed for reproducibility
set.seed(3)
## step 1 - sampling - no strata
auto_split <- initial_split(
Auto,
prop = 0.5,
# stratification by outcome variable
#strata = mpg
)
## check distribution and quantiles
Auto %>%
mutate(Q=ntile(mpg,4))%>%
group_by(Q) %>%
mutate(Q1=ifelse(Q<4,max(mpg),min(mpg)))%>%
ggplot(.,aes(mpg))+
geom_line(stat = "density")+
geom_vline(aes(xintercept=Q1),
color="blue",
linetype="dashed",
size=.2)
## new sampling using strata
set.seed(3)
auto_split <- initial_split(
Auto,
prop = 0.5,
# stratification by outcome variable
strata = mpg
)
## create training data
auto_training <- auto_split %>%
training()
## create testing data
auto_test <- auto_split %>%
testing()
## count record in training
nrow(auto_training)
## define model, package & type of response
lm_spec <- linear_reg() %>%
set_engine(engine = "lm") %>%
set_mode(mode = "regression")
## fit the model after defining the formula
m <- fit(
lm_spec, # parsnip model spec
mpg ~ horsepower, # formula
auto_training # data frame
)
## visualize output
m
## fancy output
tidy(m) %>%
knitr::kable(.,digits = 2, caption = 'Parametri Stimati')
## using fitting model to get prediction (in test dataset)
lm_pred <- m %>%
predict(new_data = auto_test)
## look at first 6 results
head(lm_pred) %>%
knitr::kable(caption = 'Predizioni',digits=.1)
## add prediction to original data
auto_test_res <- auto_test %>%
select(mpg, horsepower) %>%
bind_cols(lm_pred)
head(auto_test_res) %>%
knitr::kable(caption='Stima e Variabile Originale',digits=c(0,0,1))
## calculate root mean square error
auto_test_res %>%
rmse(truth = mpg, estimate = .pred)
## calculate r-square
auto_test_res %>%
rsq(truth = mpg, estimate = .pred)
## Plot predicted vs observed
auto_test_res %>%
ggplot(aes(x = mpg, y = .pred)) +
geom_point(alpha = .5) +
geom_abline(color = "blue", linetype = 2) +
coord_obs_pred() +
labs(x = "MPG", y = "Predicted MPG")
## what does coord_obs_pred do?
?coord_obs_pred
## Resampling (k fold cross-validation)
set.seed(123)
folds <- vfold_cv(Auto, v = 10,strata = mpg)
## fit model to samples
fit_resamples(lm_spec,mpg ~ horsepower,resamples=folds)-> res
res %>%
collect_metrics() %>%
knitr::kable(caption='Statistiche Cross-Validation')
## repeated CV
## be careful it takes a little bit longer
set.seed(123)
resall<-NULL
for (i in seq(1:5)){
folds <- vfold_cv(Auto, v = 10,
strata = mpg,
repeats =i)
fit_resamples(lm_spec,
mpg ~ horsepower,
resamples=folds)-> res
res %>%
collect_metrics()-> res1
bind_rows(resall,res1)-> resall
}
## Plot standard error of the mean error by number of replicates
resall %>%
filter(.metric=='rmse') %>%
mutate(Repliche=n/10) %>%
select(Repliche,std_err) %>%
ggplot(aes(Repliche,std_err))+
geom_line()