-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 15.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
ale <- FeatureEffect$new(predictor, feature = "lstat")
ale$plot()
ale$set.feature("rm")
ale$plot()
effs <- FeatureEffects$new(predictor)
plot(effs)
str(X)
str(Boston)
library(box)
installed.packages()
install.packages("box")
ale$set.feature("a1age")
plot.FeatureEffect
iml:::plot.FeatureEffect
effs
library(iml)
# We train a random forest on the Boston dataset:
data("Boston", package = "MASS")
str(Boston)
library("rpart")
rf <- rpart(medv ~ ., data = Boston)
mod <- Predictor$new(rf, data = Boston)
mod
rf
# Compute the accumulated local effects for the first feature
eff <- FeatureEffect$new(mod, feature = "rm", grid.size = 30)
eff$plot()
# Again, but this time with a partial dependence plot and ice curves
eff <- FeatureEffect$new(mod,
feature = "rm", method = "pdp+ice",
grid.size = 30
)
plot(eff)
eff
eff$results |> str()
eff$results |>
count(.type)
eff$results |>
group_by(.type) |>
slice_head(n=3)
eff$results |>
group_by(.type, .id) |>
slice_head(n=3)
eff$results |>
count(.type, .id)
eff$results |>
count(.type, .id) |> view()
# Since the result is a ggplot object, you can extend it:
library("ggplot2")
plot(eff) +
# Adds a title
ggtitle("Partial dependence") +
# Adds original predictions
geom_point(
data = Boston, aes(y = mod$predict(Boston)[[1]], x = rm),
color = "pink", size = 0.5
)
mod$predict(Boston)[[1]] |> str()
mod$predict(Boston) |> str()
mod
str(mod)
# If you want to do your own thing, just extract the data:
eff.dat <- eff$results
head(eff.dat)
str(eff.dat)
# You can also use the object to "predict" the marginal values.
eff$predict(Boston[1:3, ])
# Instead of the entire data.frame, you can also use feature values:
eff$predict(c(5, 6, 7))
eff$predictor
eff$predict
eff$method
# Instead of the entire data.frame, you can also use feature values:
eff$predict(c(5, 6, 7))
# You can also use the object to "predict" the marginal values.
eff$predict(Boston[1:3, ])
# You can reuse the pdp object for other features:
eff$set.feature("lstat")
plot(eff)
# Only plotting the aggregated partial dependence:
eff <- FeatureEffect$new(mod, feature = "crim", method = "pdp")
eff$plot()
# Only plotting the individual conditional expectation:
eff <- FeatureEffect$new(mod, feature = "crim", method = "ice")
eff$plot()
# Accumulated local effects and partial dependence plots support up to two
# features:
eff <- FeatureEffect$new(mod, feature = c("crim", "lstat"))
install.packages("yaImpute")
# Accumulated local effects and partial dependence plots support up to two
# features:
eff <- FeatureEffect$new(mod, feature = c("crim", "lstat"))
plot(eff)
# FeatureEffect plots also works with multiclass classification
rf <- rpart(Species ~ ., data = iris)
mod <- Predictor$new(rf, data = iris, type = "prob")
mod
rf
mod$predict()
mod$predict(iris)
# For some models we have to specify additional arguments for the predict
# function
plot(FeatureEffect$new(mod, feature = "Petal.Width"))
library(iml)
# We train a random forest on the Boston dataset:
data("Boston", package = "MASS")
str(Boston)
library("rpart")
rf <- rpart(medv ~ ., data = Boston)
mod <- Predictor$new(rf, data = Boston)
# Compute the accumulated local effects for the first feature
eff <- FeatureEffect$new(mod, feature = "rm", grid.size = 30)
eff$plot()
# Again, but this time with a partial dependence plot and ice curves
eff <- FeatureEffect$new(mod,
feature = "rm", method = "pdp+ice",
grid.size = 30
)
plot(eff)
eff$results |> str()
eff$results |>
count(.type, .id) |> view()
eff$results |>
group_by(.type, .id) |>
slice_head(n=3)
# Since the result is a ggplot object, you can extend it:
library("ggplot2")
plot(eff) +
# Adds a title
ggtitle("Partial dependence") +
# Adds original predictions
geom_point(
data = Boston, aes(y = mod$predict(Boston)[[1]], x = rm),
color = "pink", size = 0.5
)
# If you want to do your own thing, just extract the data:
eff.dat <- eff$results
str(eff.dat)
head(eff.dat)
# You can also use the object to "predict" the marginal values.
eff$predict(Boston[1:3, ])
# Instead of the entire data.frame, you can also use feature values:
eff$predict(c(5, 6, 7))
# You can reuse the pdp object for other features:
eff$set.feature("lstat")
plot(eff)
# Only plotting the aggregated partial dependence:
eff <- FeatureEffect$new(mod, feature = "crim", method = "pdp")
eff$plot()
# Only plotting the individual conditional expectation:
eff <- FeatureEffect$new(mod, feature = "crim", method = "ice")
eff$plot()
# Accumulated local effects and partial dependence plots support up to two
# features:
eff <- FeatureEffect$new(mod, feature = c("crim", "lstat"))
plot(eff)
# FeatureEffect plots also works with multiclass classification
rf <- rpart(Species ~ ., data = iris)
mod <- Predictor$new(rf, data = iris, type = "prob")
# For some models we have to specify additional arguments for the predict
# function
plot(FeatureEffect$new(mod, feature = "Petal.Width"))
# FeatureEffect plots support up to two features:
eff <- FeatureEffect$new(mod, feature = c("Sepal.Length", "Petal.Length"))
eff$plot()
# show where the actual data lies
eff$plot(show.data = TRUE)
# For multiclass classification models, you can choose to only show one class:
mod <- Predictor$new(rf, data = iris, type = "prob", class = 1)
plot(FeatureEffect$new(mod, feature = "Sepal.Length"))
# For some models we have to specify additional arguments for the predict
# function
plot(FeatureEffect$new(mod, feature = "Petal.Width"))
# FeatureEffect plots also works with multiclass classification
rf <- rpart(Species ~ ., data = iris)
mod <- Predictor$new(rf, data = iris, type = "prob")
# For some models we have to specify additional arguments for the predict
# function
plot(FeatureEffect$new(mod, feature = "Petal.Width"))
# FeatureEffect plots support up to two features:
eff <- FeatureEffect$new(mod, feature = c("Sepal.Length", "Petal.Length"))
eff$plot()
# show where the actual data lies
eff$plot(show.data = TRUE)
# For multiclass classification models, you can choose to only show one class:
mod <- Predictor$new(rf, data = iris, type = "prob", class = 1)
plot(FeatureEffect$new(mod, feature = "Sepal.Length"))
data("Boston", package = "MASS")
head(Boston)
set.seed(42)
library("iml")
library("randomForest")
data("Boston", package = "MASS")
rf <- randomForest(medv ~ ., data = Boston, ntree = 50)
X <- Boston[which(names(Boston) != "medv")]
predictor <- Predictor$new(rf, data = X, y = Boston$medv)
imp <- FeatureImp$new(predictor, loss = "mae")
library("ggplot2")
plot(imp)
imp$results
imp$features
ale <- FeatureEffect$new(predictor, feature = "lstat")
ale$plot()
ale$set.feature("rm")
ale$plot()
interact <- Interaction$new(predictor)
plot(interact)
interact <- Interaction$new(predictor, feature = "crim")
plot(interact)
library("yardstick")
# Two class
data("two_class_example")
brier_class(two_class_example, truth, Class1)
glimpse(two_class_example)
# Multiclass
library(dplyr)
data(hpc_cv)
glimpse(hpc_cv)
# You can use the col1:colN tidyselect syntax
hpc_cv %>%
filter(Resample == "Fold01") %>%
brier_class(obs, VF:L)
# You can use the col1:colN tidyselect syntax
hpc_cv %>%
filter(Resample == "Fold01") %>%
brier_class(obs, VF:L)
data(hpc_cv)
glimpse(hpc_cv)
# You can use the col1:colN tidyselect syntax
hpc_cv %>%
filter(Resample == "Fold01") %>%
brier_class(obs, VF:L)
# You can use the col1:colN tidyselect syntax
hpc_cv %>%
filter(Resample == "Fold01") #%>%
# You can use the col1:colN tidyselect syntax
hpc_cv %>%
dplyr::filter(Resample == "Fold01") #%>%
# You can use the col1:colN tidyselect syntax
hpc_cv %>%
dplyr::filter(Resample == "Fold01") %>%
brier_class(obs, VF:L)
# Groups are respected
hpc_cv %>%
group_by(Resample) %>%
brier_class(obs, VF:L)
# Two class
data("two_class_example")
library("yardstick")
# Two class
data("two_class_example")
mn_log_loss(two_class_example, truth, Class1)
# Multiclass
library(dplyr)
data(hpc_cv)
# You can use the col1:colN tidyselect syntax
hpc_cv %>%
filter(Resample == "Fold01") %>%
mn_log_loss(obs, VF:L)
# You can use the col1:colN tidyselect syntax
hpc_cv %>%
dplyr::filter(Resample == "Fold01") %>%
mn_log_loss(obs, VF:L)
glimpse(hpc_cv)
# Groups are respected
hpc_cv %>%
group_by(Resample) %>%
mn_log_loss(obs, VF:L)
# Vector version
# Supply a matrix of class probabilities
fold1 <- hpc_cv %>%
filter(Resample == "Fold01")
# Vector version
# Supply a matrix of class probabilities
fold1 <- hpc_cv %>%
dplyr::filter(Resample == "Fold01")
mn_log_loss_vec(
truth = fold1$obs,
matrix(
c(fold1$VF, fold1$F, fold1$M, fold1$L),
ncol = 4
)
)
str(fold1)
matrix(
c(fold1$VF, fold1$F, fold1$M, fold1$L),
ncol = 4
)
matrix(
c(fold1$VF, fold1$F, fold1$M, fold1$L),
ncol = 4
) |> str()
str(fold1)
library(dplyr)
# Multiple regression metrics
multi_metric <- metric_set(rmse, rsq, ccc)
# The returned function has arguments:
# fn(data, truth, estimate, na_rm = TRUE, ...)
multi_metric(solubility_test, truth = solubility, estimate = prediction)
glimpse(solubility_test)
# Multiple regression metrics
multi_metric <- metric_set(rmse, rsq, ccc)
multi_metric
metric_set
# Groups are respected on the new metric function
class_metrics <- metric_set(accuracy, kap)
class_metrics
hpc_cv %>%
group_by(Resample) %>%
class_metrics(obs, estimate = pred)
# If you need to set options for certain metrics,
# do so by wrapping the metric and setting the options inside the wrapper,
# passing along truth and estimate as quoted arguments.
# Then add on the function class of the underlying wrapped function,
# and the direction of optimization.
ccc_with_bias <- function(data, truth, estimate, na_rm = TRUE, ...) {
ccc(
data = data,
truth = !!rlang::enquo(truth),
estimate = !!rlang::enquo(estimate),
# set bias = TRUE
bias = TRUE,
na_rm = na_rm,
...
)
}
# Use `new_numeric_metric()` to formalize this new metric function
ccc_with_bias <- new_numeric_metric(ccc_with_bias, "maximize")
multi_metric2 <- metric_set(rmse, rsq, ccc_with_bias)
multi_metric2
multi_metric2(solubility_test, truth = solubility, estimate = prediction)
str(multi_metric)
multi_metric()
multi_metric
multi_metric |> str()
class_and_probs_metrics <- metric_set(roc_auc, pr_auc, accuracy)
hpc_cv %>%
group_by(Resample) %>%
class_and_probs_metrics(obs, VF:L, estimate = pred)
glimpse(hpc_cv)
hpc_cv %>%
group_by(Resample) %>%
class_and_probs_metrics(obs, VF:L, estimate = pred)
hpc_cv %>%
# group_by(Resample) %>%
class_and_probs_metrics(obs, VF:L, estimate = pred)
class_and_probs_metrics <- metric_set(roc_auc, pr_auc, accuracy, logloss)
hpc_cv %>%
# group_by(Resample) %>%
class_and_probs_metrics(obs, VF:L, estimate = pred)
class_and_probs_metrics <- metric_set(roc_auc, pr_auc, accuracy, mn_log_loss)
hpc_cv %>%
# group_by(Resample) %>%
class_and_probs_metrics(obs, VF:L, estimate = pred)
class_and_probs_metrics <- metric_set(roc_auc, pr_auc, accuracy, mn_log_loss, brier_class)
hpc_cv %>%
# group_by(Resample) %>%
class_and_probs_metrics(obs, VF:L, estimate = pred)
?accuracy
class_and_probs_metrics <- metric_set(roc_auc, pr_auc, accuracy, mn_log_loss, brier_class, kap)
hpc_cv %>%
# group_by(Resample) %>%
class_and_probs_metrics(obs, VF:L, estimate = pred)
class_and_probs_metrics <- metric_set(roc_auc, pr_auc, accuracy, mn_log_loss, brier_class, kap, mcc)
hpc_cv %>%
# group_by(Resample) %>%
class_and_probs_metrics(obs, VF:L, estimate = pred)
class_and_probs_metrics <- metric_set(roc_auc, pr_auc, accuracy, mn_log_loss, brier_class, kap, mcc, roc_auc)
hpc_cv %>%
# group_by(Resample) %>%
class_and_probs_metrics(obs, VF:L, estimate = pred)
class_and_probs_metrics <- metric_set(roc_auc, pr_auc, accuracy, mn_log_loss, brier_class, kap, mcc, roc_auc, roc_aunp)
hpc_cv %>%
# group_by(Resample) %>%
class_and_probs_metrics(obs, VF:L, estimate = pred)
class_and_probs_metrics <- metric_set(roc_auc, pr_auc, accuracy, mn_log_loss, brier_class, kap, mcc, roc_auc, roc_aunu)
hpc_cv %>%
# group_by(Resample) %>%
class_and_probs_metrics(obs, VF:L, estimate = pred)
class_and_probs_metrics <- metric_set(roc_auc, pr_auc, accuracy, mn_log_loss, brier_class, kap, mcc, roc_auc)
hpc_cv %>%
# group_by(Resample) %>%
class_and_probs_metrics(obs, VF:L, estimate = pred)
?ggplot
?aes
aes(x = mpg, y = wt)
ae <- aes(x = mpg, y = wt)
ae
ae$x
aes(col = x)
aes(fg = x)
aes(color = x)
aes(colour = x)
scatter_by <- function(data, ...) {
ggplot(data) + geom_point(aes(...))
}
scatter_by(mtcars, disp, drat)
scatter_by <- function(data, x, y) {
ggplot(data) + geom_point(aes({{ x }}, {{ y }}))
}
scatter_by(mtcars, disp, drat)
cut3 <- function(x) cut_number(x, 3)
scatter_by(mtcars, cut3(disp), drat)
library(ComplexUpset)
install.packages("ComplexUpset")
library(ComplexUpset)
movies = as.data.frame(ggplot2movies::movies)
install.packages("ggplot2movies")
movies = as.data.frame(ggplot2movies::movies)
head(movies, 3)
head(movies, 3)
t(head(movies[genres], 3))
genres
genres = colnames(movies)[18:24]
genres
movies[genres] = movies[genres] == 1
t(head(movies[genres], 3))
movies[movies$mpaa == '', 'mpaa'] = NA
movies = na.omit(movies)
str(movies)
set_size = function(w, h, factor=1.5) {
s = 1 * factor
options(
repr.plot.width=w * s,
repr.plot.height=h * s,
repr.plot.res=100 / factor,
jupyter.plot_mimetypes='image/png',
jupyter.plot_scale=1
)
}
set_size()
set_size
genres
library(shiny)
library(reactable)
install.packages("reactable")
library(shiny)
library(reactable)
library(widgetTools)
install.packages("widgetTools")
library(widgetTools)
library(chattr)
remotes::install_github("mlverse/chattr")
library(rstan)
install.packages(rstan)
install.packages("rstan")
library(rstan)
install.packages("INLA",repos=c(getOption("repos"),INLA="https://inla.r-inla-download.org/R/stable"), dep=TRUE)
library(INLA)
## Loading required package: Matrix
## Loading required package: sp
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, will retire in October 2023.
## Please refer to R-spatial evolution reports for details, especially
## https://r-spatial.org/r/2023/05/15/evolution4.html.
## It may be desirable to make the sf package available;
## package maintainers should consider adding sf to Suggests:.
## The sp package is now running under evolution status 2
## (status 2 uses the sf package in place of rgdal)
## This is INLA_23.06.14 built 2023-06-13 23:16:56 UTC.
## - See www.r-inla.org/contact-us for how to get help.
n = 100; a = 1; b = 1; tau = 100
z = rnorm(n)
eta = a + b*z
scale = exp(rnorm(n))
prec = scale*tau
y = rnorm(n, mean = eta, sd = 1/sqrt(prec))
data = list(y=y, z=z)
formula = y ~ 1+z
result = inla(formula, family = "gaussian", data = data)
summary(result)
result
str(z)
str(prec)
R.version
search()
# install.packages("INLA",repos=c(getOption("repos"),INLA="https://inla.r-inla-download.org/R/stable"), dep=TRUE)
library(INLA)
n = 100; a = 1; b = 1; tau = 100
z = rnorm(n)
eta = a + b*z
scale = exp(rnorm(n))
prec = scale*tau
y = rnorm(n, mean = eta, sd = 1/sqrt(prec))
data = list(y=y, z=z)
formula = y ~ 1+z
result = inla(formula, family = "gaussian", data = data)
summary(result)
library(rjags)
install.packages("rjags")
library(rjags)
library(geomtextpath)
install.packages("geomtextpath")
install.packages("geomtextpath")
library(geomtextpath)
?geom_textdensity
ggplot(iris, aes(Sepal.Length, label = Species, color = Species)) +
geom_textdensity()
quarto_version()
setwd("~/gDoc/learn/Quarto/quarto_website_positconf2024")
library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg, colour = factor(cyl))) +
geom_point()
install.packages("here")
install.packages("janitor")
install.packages("extrafont")
install.packages("hrbrthemes")
install.packages("digest")
install.packages("digest")
library(digest)
install.packages("digest")
install.packages("digest")