Created
June 16, 2016 21:22
-
-
Save jwaage/ac393ebdc93dbf3d02c8bbc7b29bc2a8 to your computer and use it in GitHub Desktop.
mixOmics splsda - examine variable contributions across CV folds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getBugsCV <- function(x, y, ncomp = 5, nfolds = 5, nvar = 5, direction = ">", seed = 42, nreps = 1){ | |
set.seed(seed) | |
sigbugs <- list(nfolds*nreps) | |
for(rep in 1:nreps){ | |
folds <- sample.int(nfolds, size = nrow(x), replace = TRUE) | |
for(fold in 1:nfolds){ | |
train <- folds != fold | |
test <- folds == fold | |
fit <- splsda(x[train,], y[train], keepX = rep(nvar,ncomp), ncomp = ncomp) | |
sigbugs[[(rep-1)*nfolds + fold]] <- names(fit$loadings$X[,1])[fit$loadings$X[,1] != 0] | |
} | |
} | |
sigbugs | |
} | |
countlist <- function(list){ | |
tab <- list %>% unlist %>% table | |
data.frame(counts = tab, stringsAsFactors = FALSE) %>% setNames(c("OTU", "freq")) %>% mutate(OTU = as.character(OTU), freq = freq/length(list)) %>% arrange(desc(freq)) | |
} | |
buglist <- getBugsCV(x, y, ncomp = 1, nfolds = 5, nvar = 4, nreps = 20, seed = 100) | |
overview <- countlist(buglist) %>% mutate(type = as.vector(tax_table(xt1mlda)[OTU,"lowest"])) %>% mutate(label = paste(OTU, type) %>% gsub("OTU", "", .)) %>% mutate( label = factor(label, levels = rev(label))) | |
overview | |
ggplot(overview, aes(x = label, y = freq, group = 1)) + geom_point() + geom_line() + coord_flip() + scale_y_continuous(labels = scales::percent) + theme_bw() + xlab("Contributors") + ylab("Frequency present") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment