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
# Welcome to the COPSAC ggplot Kaplan-Meier template. It builds nice and very customizable KM curves as shown below. Please run the two functions to load into memory, it works like a SAS macro. | |
# Load dependencies | |
library(survival) | |
library(ggplot2) | |
library(cowplot) | |
library(reshape2) | |
# Run these functions to load into memory | |
# Define custom function to create a survival data.frame. Credit http://www.ceb-institute.org/bbs/wp-content/uploads/2011/09/handout_ggplot2.pdf | |
createSurvivalFrame <- function(f.survfit){ |
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
getSNPImputed <- function(SNPs) | |
{ | |
#Counting allele 1 | |
if (!require("data.table")) stop("Install packages data.table") | |
if (!require("RMySQL")) stop("Install packages RMySQL") | |
if (!require("rio")) stop("Install packages rio") | |
if (!require("gtools")) stop("Install packages gtools") | |
convertProbsToDoses <- function(props, alleleToReturnAorB="A") |
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
source("lib/check_packages.R") | |
check_packages() | |
load("data/ubp_phyloseq_metagenomeseq.RData") | |
library(vegan) | |
library(ggplot2) | |
library(cluster) | |
library(phyloseq) | |
library(cowplot) | |
library(reshape2) | |
library(scales) |
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
library("googlesheets") | |
# Forbind til google. Efter denne kommando åbner browseren, og du skal logge ind på google. | |
gs_ls() | |
# Herefter registerer du det pågældende ark med enten ark-navnet, eller som her, ark-nøglen (står i URLen) | |
sheet_key <- "15WDkfscZ-3xuSE33WRmj5PIpC-q5Eto8wUlWqlv-x2w" | |
sheet_object <- gs_key(sheet_key) | |
sheet_csv <- gs_read_csv(sheet_object, "Fanenavn") |
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
get_loadings <- function(physeq, ord, method = c("pearson", "spearman"), scaled=TRUE, cor = FALSE, axes=1:2, taxranks=6:7, top = NULL){ | |
if(!all(rownames(ord$vectors) == sample_names(physeq))) | |
stop("Taxa names do not match") | |
counts <- t(as(otu_table(physeq),"matrix")) | |
scores <- ord$vectors | |
cov <- cov(counts, scores, method=method[1]) | |
if(cor){ | |
cov <- cor(counts, scores, method=method[1]) | |
} | |
if(scaled){ |
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
ggroc <- function(roc, showAUC = TRUE, interval = 0.2, breaks = seq(0, 1, interval)){ | |
require(pROC) | |
if(class(roc) != "roc") | |
simpleError("Please provide roc object from pROC package") | |
plotx <- rev(roc$specificities) | |
ploty <- rev(roc$sensitivities) | |
ggplot(NULL, aes(x = plotx, y = ploty)) + | |
geom_segment(aes(x = 0, y = 1, xend = 1,yend = 0), alpha = 0.5) + | |
geom_step() + |
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
lowest <- function(tax){ | |
returnPhyseq <- FALSE | |
if(class(tax) == "phyloseq"){ | |
physeq <- tax | |
tax <- tax_table(physeq) | |
returnPhyseq <- TRUE | |
} | |
tax <- tax[, colnames(tax) != "lowest"] | |
tax <- tax %>% as("matrix") | |
low <- tax[,ncol(tax)] %>% as.vector |
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] |
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
getBestFit <- function(x, y, ..., ncomp = 5, nfolds = 5, minvar = 1, maxvar = ncol(x), direction = ">", seed = 42, run = NULL){ | |
set.seed(seed) | |
auc <- expand.grid(AUC = 0, ncomp = 1:ncomp, nvar = minvar:maxvar, stringsAsFactors = FALSE) | |
folds <- sample.int(nfolds, size = nrow(x), replace = TRUE) | |
for(i in minvar:maxvar){ | |
pred <- matrix(rep(NA,nrow(x)*ncomp), ncol = ncomp) | |
for(fold in 1:nfolds){ | |
train <- folds != fold | |
test <- folds == fold | |
fit <- splsda(x[train,], y[train], keepX = rep(i,ncomp), ncomp = ncomp) |
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
ggphylobarplot <- function(physobj, | |
barlevel = "lowest", | |
colorlevel = "phylum", | |
splitvar = "Time", | |
splitlevelorder = c("1w", "1m", "3m"), | |
splitlevellabels = c("1 week", "1 month", "3 months"), | |
sortlevel = "1w", | |
mracut = .009, | |
agglomerate = FALSE, | |
agglomeratelevel = "genus", |
OlderNewer