Skip to content

Commit b09effe

Browse files
committed
Fix supplementary variables not displayed in individual plot for speMCA (#42)
1 parent 496b994 commit b09effe

File tree

4 files changed

+48
-28
lines changed

4 files changed

+48
-28
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- Fix duplicated level names in supplementary variables in speMCA
44
- Fix warnings in Font Awesome icon names (#39, thanks @jl5000)
5+
- Fix supplementary variables not displayed in individual plot for speMCA (thanks @419kfj)
56

67
# explor 0.3.9
78

R/prepare_results_speMCA.R

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
##' @export
99

1010
prepare_results.speMCA <- function(obj) {
11-
1211
if (!inherits(obj, "speMCA")) stop("obj must be of class speMCA")
1312

1413
## Extract variable names from results row names
@@ -23,13 +22,13 @@ prepare_results.speMCA <- function(obj) {
2322
vars <- data.frame(obj$var$coord)
2423
## Axes names and inertia
2524
axes <- seq_len(ncol(obj$var$coord))
26-
names(axes) <- paste("Axis", axes, paste0("(", head(round(obj$eig$rate, 2), length(axes)),"%)"))
25+
names(axes) <- paste("Axis", axes, paste0("(", head(round(obj$eig$rate, 2), length(axes)), "%)"))
2726
## Eigenvalues
2827
eig <- data.frame(dim = seq_len(length(obj$eig$rate)), percent = obj$eig$rate)
2928

3029
## Variables coordinates
31-
varnames <- sapply(obj$call$X[,obj$call$quali, drop = FALSE], nlevels)
32-
varnames <- rep(names(varnames),varnames)
30+
varnames <- sapply(obj$call$X[, obj$call$quali, drop = FALSE], nlevels)
31+
varnames <- rep(names(varnames), varnames)
3332
if (!is.null(obj$call$excl)) varnames <- varnames[-obj$call$excl]
3433
vars$varname <- varnames
3534
vars$modname <- rownames(vars)
@@ -44,16 +43,22 @@ prepare_results.speMCA <- function(obj) {
4443
vars <- vars %>% bind_rows(tmp_sup)
4544
}
4645

47-
vars <- vars %>% pivot_longer(names_to = "Axis", values_to = "Coord", starts_with("dim.")) %>%
48-
mutate(Axis = gsub("dim.", "", Axis, fixed = TRUE),
49-
Coord = round(Coord, 3))
46+
vars <- vars %>%
47+
pivot_longer(names_to = "Axis", values_to = "Coord", starts_with("dim.")) %>%
48+
mutate(
49+
Axis = gsub("dim.", "", Axis, fixed = TRUE),
50+
Coord = round(Coord, 3)
51+
)
5052

5153
## Variables contrib
5254
tmp <- data.frame(obj$var$contrib)
53-
tmp <- tmp %>% mutate(modname = rownames(tmp), Type = "Active", Class = "Qualitative") %>%
55+
tmp <- tmp %>%
56+
mutate(modname = rownames(tmp), Type = "Active", Class = "Qualitative") %>%
5457
pivot_longer(names_to = "Axis", values_to = "Contrib", starts_with("dim.")) %>%
55-
mutate(Axis = gsub("dim.", "", Axis, fixed = TRUE),
56-
Contrib = round(Contrib, 3))
58+
mutate(
59+
Axis = gsub("dim.", "", Axis, fixed = TRUE),
60+
Contrib = round(Contrib, 3)
61+
)
5762

5863
vars <- vars %>% left_join(tmp, by = c("modname", "Type", "Class", "Axis"))
5964

@@ -71,12 +76,14 @@ prepare_results.speMCA <- function(obj) {
7176
}
7277
tmp <- tmp %>%
7378
pivot_longer(names_to = "Axis", values_to = "Cos2", starts_with("dim.")) %>%
74-
mutate(Axis = gsub("dim.", "", Axis, fixed = TRUE),
75-
Cos2 = round(Cos2, 3))
79+
mutate(
80+
Axis = gsub("dim.", "", Axis, fixed = TRUE),
81+
Cos2 = round(Cos2, 3)
82+
)
7683
vars <- vars %>% left_join(tmp, by = c("modname", "Type", "Class", "Axis"))
7784

7885
vars$modname <- mapply(vars$modname, vars$varname, FUN = function(mod, var) {
79-
sub(paste0("^",var,"\\."), "", mod)
86+
sub(paste0("^", var, "\\."), "", mod)
8087
}, USE.NAMES = FALSE)
8188
vars <- vars %>%
8289
rename(Variable = varname, Level = modname) %>%
@@ -104,15 +111,20 @@ prepare_results.speMCA <- function(obj) {
104111
}
105112
ind <- ind %>%
106113
pivot_longer(names_to = "Axis", values_to = "Coord", starts_with("dim.")) %>%
107-
mutate(Axis = gsub("dim.", "", Axis, fixed = TRUE),
108-
Coord = round(Coord, 3))
114+
mutate(
115+
Axis = gsub("dim.", "", Axis, fixed = TRUE),
116+
Coord = round(Coord, 3)
117+
)
109118

110119
## Individuals contrib
111120
tmp <- data.frame(obj$ind$contrib)
112-
tmp <- tmp %>% mutate(Name = rownames(tmp), Type = "Active") %>%
121+
tmp <- tmp %>%
122+
mutate(Name = rownames(tmp), Type = "Active") %>%
113123
pivot_longer(names_to = "Axis", values_to = "Contrib", starts_with("dim.")) %>%
114-
mutate(Axis = gsub("dim.", "", Axis, fixed = TRUE),
115-
Contrib = round(Contrib, 3))
124+
mutate(
125+
Axis = gsub("dim.", "", Axis, fixed = TRUE),
126+
Contrib = round(Contrib, 3)
127+
)
116128

117129
ind <- ind %>% left_join(tmp, by = c("Name", "Type", "Axis"))
118130

@@ -122,24 +134,25 @@ prepare_results.speMCA <- function(obj) {
122134
tmp <- tmp %>%
123135
mutate(Name = rownames(tmp), Type = "Supplementary") %>%
124136
pivot_longer(names_to = "Axis", values_to = "Cos2", starts_with("dim.")) %>%
125-
mutate(Axis = gsub("dim.", "", Axis, fixed = TRUE),
126-
Cos2 = round(Cos2, 3))
137+
mutate(
138+
Axis = gsub("dim.", "", Axis, fixed = TRUE),
139+
Cos2 = round(Cos2, 3)
140+
)
127141
ind <- ind %>% left_join(tmp, by = c("Name", "Type", "Axis"))
128142
} else {
129143
ind$Cos2 <- NA
130144
}
131145

132146
## Qualitative data for individuals plot color mapping
133-
quali_data <- obj$call$X[,obj$call$quali]
134-
if (!is.null(obj$quali.sup)) {
147+
quali_data <- obj$call$X[, obj$call$quali]
148+
if (!is.null(obj$supv)) {
135149
quali_data <- quali_data %>%
136-
bind_cols(obj$call$X[,obj$call$quali.sup, drop = FALSE])
150+
bind_cols(obj$supv$tab)
137151
}
138152
quali_data$Name <- rownames(obj$call$X)
139153

140154

141155
return(
142156
list(vars = vars, ind = ind, eig = eig, axes = axes, vareta2 = vareta2, quali_data = quali_data)
143157
)
144-
145-
}
158+
}

R/utils.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ speMCA_varsup <- function(mca, df) {
2424
})
2525
l
2626
})
27-
Reduce(function(acc, cur) {
27+
res <- Reduce(function(acc, cur) {
2828
for (name in names(acc)) {
2929
if (name == "weight") next
3030
acc[[name]] <- dplyr::bind_rows(
@@ -34,4 +34,8 @@ speMCA_varsup <- function(mca, df) {
3434
}
3535
acc
3636
}, res)
37-
}
37+
38+
res$tab <- df
39+
40+
res
41+
}

tests/testthat/test_prepare_results_speMCA.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ test_that("Supplementary variables results are equal", {
6767

6868
test_that("Qualitative data are equal", {
6969
ids <- c("4731", "31", "2489", "4125", "280")
70+
tmp <- res$quali_data
71+
rownames(tmp) <- tmp$Name
7072
expect_equal(
71-
as.character(res$quali_data[ids, "FrenchPop"]),
73+
as.character(tmp[ids, "FrenchPop"]),
7274
as.character(Music[ids, "FrenchPop"])
7375
)
7476
})

0 commit comments

Comments
 (0)