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