Skip to content

Commit 373a066

Browse files
update C01Lb_ml1: use of {exploreit} functions
1 parent 0691ad9 commit 373a066

File tree

2 files changed

+1
-198
lines changed

2 files changed

+1
-198
lines changed

inst/tutorials/C01Lb_ml1/C01Lb_ml1.Rmd

Lines changed: 1 addition & 198 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ runtime: shiny_prerendered
1616
BioDataScience3::learnr_setup()
1717
SciViews::R()
1818
library(mlearning)
19+
library(exploreit)
1920
library(rsample)
2021
2122
# exercice preparation --------
@@ -30,13 +31,6 @@ bio_split <- initial_split(bio, prop = 2/3)
3031
bio_train <- training(bio_split)
3132
bio_test <- testing(bio_split)
3233
33-
#n <- nrow(bio)
34-
#n_train <- round(n * 2/3)
35-
#set.seed(164)
36-
#train <- sample(1:n, n_train)
37-
#bio_train <- bio[ train, ]
38-
#bio_test <- bio[ -train, ]
39-
4034
## Creation d'un modèle lda
4135
bio_lda <- mlLda(data = bio_train, gender ~ .)
4236
@@ -53,197 +47,6 @@ BioDataScience3::learnr_banner()
5347
BioDataScience3::learnr_server(input, output, session)
5448
```
5549

56-
```{r}
57-
as.prcomp <- function(x, ...)
58-
UseMethod("as.prcomp")
59-
60-
as.prcomp.default <- function(x, ...)
61-
stop("No method to convert this object into a 'prcomp'")
62-
63-
as.prcomp.prcomp <- function(x, ...)
64-
x
65-
66-
as.prcomp.princomp <- function(x, ...)
67-
structure(list(sdev = as.numeric(x$sdev), rotation = unclass(x$loadings),
68-
center = x$center, scale = x$scale, x = as.matrix(x$scores)),
69-
class = "prcomp")
70-
71-
# Comparison of pcomp() -> as.prcomp() with prcomp() directly
72-
# Almost the same, only no rownames for x (is it important?)
73-
#iris_prcomp_pcomp <- as.prcomp(pcomp(iris[, -5], scale = TRUE))
74-
#iris_prcomp <- prcomp(iris[, -5], scale = TRUE)
75-
76-
# Now, broom methods can be defined simply by converting into prcomp objects
77-
augment.princomp <- function(x, data = NULL, newdata, ...)
78-
if (missing(newdata)) {
79-
augment(as.prcomp(x), data = data, ...)
80-
} else {
81-
augment(as.prcomp(x), data = data, newdata = newdata, ...)
82-
}
83-
84-
tidy.princomp <- function(x, matrix = "u", ...)
85-
tidy(as.prcomp(x), matrix = matrix, ...)
86-
87-
# There is no glance.prcomp() method
88-
89-
# There is a problem with pcomp() that returns a data.frame in scores,
90-
# while it is a matrix in the original princomp object. pca() corrects this
91-
pca <- function(x, ...) {
92-
res <- SciViews::pcomp(x, ...)
93-
# Change scores into a matrix
94-
res$scores <- as.matrix(res$scores)
95-
res
96-
}
97-
98-
scale_axes <- function(data, aspect.ratio = 1) {
99-
range_x <- range(data[, 1])
100-
span_x <- abs(max(range_x) - min(range_x))
101-
range_y <- range(data[, 2])
102-
span_y <- abs(max(range_y) - min(range_y))
103-
if ((span_y / aspect.ratio) > span_x) {
104-
# Adjust range_x
105-
span_x_2 <- span_y / aspect.ratio / 2
106-
range_x_mid <- sum(range_x) / 2
107-
range_x <- c(range_x_mid - span_x_2, range_x_mid + span_x_2)
108-
} else {
109-
# Adjust range_y
110-
span_y_2 <- span_x * aspect.ratio / 2
111-
range_y_mid <- sum(range_y) / 2
112-
range_y <- c(range_y_mid - span_y_2, range_y_mid + span_y_2)
113-
}
114-
list(x = range_x, y = range_y)
115-
}
116-
117-
autoplot.pcomp <- function(object,
118-
type = c("screeplot", "altscreeplot", "loadings", "correlations", "scores", "biplot"),
119-
choices = 1L:2L, name = deparse(substitute(object)), ar.length = 0.1,
120-
circle.col = "gray", col = "black", fill = "gray", scale = 1, aspect.ratio = 1,
121-
repel = FALSE, labels, title, xlab, ylab, ...) {
122-
type = match.arg(type)
123-
124-
if (missing(title))
125-
title <- paste(name, type, sep = " - ")
126-
127-
contribs <- paste0(names(object$sdev), " (",
128-
round((object$sdev^2/object$totdev^2) * 100, digits = 1), "%)")[choices]
129-
130-
scores <- as.data.frame(object$scores[, choices])
131-
names(scores) <- c("x", "y")
132-
if (!missing(labels)) {
133-
if (length(labels) != nrow(scores))
134-
stop("You must provide a character vector of length ", nrow(scores),
135-
" for 'labels'")
136-
scores$labels <- labels
137-
} else {# Default labels are row numbers
138-
scores$labels <- 1:nrow(scores)
139-
}
140-
141-
lims <- scale_axes(scores, aspect.ratio = aspect.ratio)
142-
143-
if (!missing(col)) {
144-
if (length(col) != nrow(scores))
145-
stop("You must provide a vector of length ", nrow(scores), " for 'col'")
146-
scores$color <- col
147-
scores_formula <- y ~ x %col=% color %label=% labels
148-
} else {
149-
if (missing(labels)) {
150-
scores_formula <- y ~ x %label=% labels
151-
} else {
152-
scores_formula <- y ~ x %col=% labels %label=% labels
153-
}
154-
}
155-
156-
res <- switch(type,
157-
screeplot = object %>.% # Classical screeplot
158-
tidy(., "pcs") %>.%
159-
chart(data = ., std.dev^2 ~ PC) +
160-
geom_col(col = col, fill = fill) +
161-
labs(y = "Variances", title = title),
162-
163-
altscreeplot = object %>.% # screeplot represented by dots and lines
164-
tidy(., "pcs") %>.%
165-
chart(data = ., std.dev^2 ~ PC) +
166-
geom_line(col = col) +
167-
geom_point(col = "white", fill = col, size = 2, shape = 21, stroke = 3) +
168-
labs(y = "Variances", title = title),
169-
170-
loadings = object %>.% # Plots of the variables
171-
tidy(., "variables") %>.%
172-
spread(., key = PC, value = value) %>.%
173-
#rename_if(., is.numeric, function(x) paste0("PC", x)) %>.%
174-
select(., c(1, choices + 1)) %>.%
175-
set_names(., c("labels", "x", "y")) %>.%
176-
chart(data = ., y ~ x %xend=% 0 %yend=% 0 %label=% labels) +
177-
annotate("path", col = circle.col,
178-
x = cos(seq(0, 2*pi, length.out = 100)),
179-
y = sin(seq(0, 2*pi, length.out = 100))) +
180-
geom_hline(yintercept = 0, col = circle.col) +
181-
geom_vline(xintercept = 0, col = circle.col) +
182-
geom_segment(arrow = arrow(length = unit(ar.length, "inches"),
183-
ends = "first")) +
184-
ggrepel::geom_text_repel(hjust = "outward", vjust = "outward") +
185-
coord_fixed(ratio = 1) +
186-
labs(x = contribs[1], y = contribs[2], title = title),
187-
188-
correlations = object %>.% # Correlations plot
189-
Correlation(.) %>.%
190-
as_tibble(., rownames = "labels") %>.%
191-
select(., c(1, choices + 1)) %>.%
192-
set_names(., c("labels", "x", "y")) %>.%
193-
chart(data = ., y ~ x %xend=% 0 %yend=% 0 %label=% labels) +
194-
annotate("path", col = circle.col,
195-
x = cos(seq(0, 2*pi, length.out = 100)),
196-
y = sin(seq(0, 2*pi, length.out = 100))) +
197-
geom_hline(yintercept = 0, col = circle.col) +
198-
geom_vline(xintercept = 0, col = circle.col) +
199-
geom_segment(arrow = arrow(length = unit(ar.length, "inches"),
200-
ends = "first")) +
201-
ggrepel::geom_text_repel(hjust = "outward", vjust = "outward") +
202-
coord_fixed(ratio = 1) +
203-
labs(x = contribs[1], y = contribs[2], title = title),
204-
205-
scores = scores %>.% # Plot of the individuals
206-
chart(data = ., scores_formula) +
207-
geom_hline(yintercept = 0, col = circle.col) +
208-
geom_vline(xintercept = 0, col = circle.col) +
209-
coord_fixed(ratio = 1, xlim = lims$x, ylim = lims$y, expand = TRUE) +
210-
labs(x = contribs[1], y = contribs[2], title = title) +
211-
theme(legend.position = "none"),
212-
213-
biplot = object %>.% # Biplot using ggfortify function
214-
as.prcomp(.) %>.%
215-
ggfortify:::autoplot.prcomp(., x = choices[1], y = choices[2],
216-
scale = scale, size = -1, label = TRUE, loadings = TRUE,
217-
loadings.label = TRUE) +
218-
geom_hline(yintercept = 0, col = circle.col) +
219-
geom_vline(xintercept = 0, col = circle.col) +
220-
theme_sciviews() +
221-
labs(x = contribs[1], y = contribs[2], title = title),
222-
223-
stop("Unrecognized type, must be 'screeplot', 'altscreeplot', loadings', 'correlations', 'scores' or 'biplot'")
224-
)
225-
226-
if (type == "scores") {
227-
if (isTRUE(repel)) {
228-
res <- res + geom_point() + ggrepel::geom_text_repel()
229-
} else {# Use text
230-
res <- res + geom_text()
231-
}
232-
}
233-
234-
if (!missing(xlab))
235-
res <- res + xlab(xlab)
236-
if (!missing(ylab))
237-
res <- res + ylab(ylab)
238-
res
239-
}
240-
241-
chart.pcomp <- function(data, choices = 1L:2L, name = deparse(substitute(data)),
242-
..., type = NULL, env = parent.frame())
243-
autoplot.pcomp(data, choices = choices, name = name, ..., type = type, env = env)
244-
class(chart.pcomp) <- c("function", "subsettable_type")
245-
```
246-
24750
------------------------------------------------------------------------
24851

24952
## Objectifs
184 KB
Loading

0 commit comments

Comments
 (0)