annotate ssq.R @ 0:93312041f1d5 draft default tip

planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
author marie-tremblay-metatoul
date Fri, 21 Sep 2018 05:51:14 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
1 library(lmdme)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
2 library(MetStaT)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
3
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
4 ## Data : attention standardiser au prealable
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
5 data <- read.table("E:/PROJETS/Asca_W4M/Test_Matlab_data.txt", sep="\t", dec=",", header=TRUE, row.names=1)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
6 design <- read.table("E:/PROJETS/Asca_W4M/Test_Matlab_design.txt", sep="\t", dec=",", header=TRUE)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
7 design[,1] <- as.factor(design[,1])
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
8 design[,2] <- as.factor(design[,2])
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
9
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
10 ## Verifier noms
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
11
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
12
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
13 fit <- lmdme(model=~F1 + F2 + F1:F2, data=data, design=design)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
14
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
15 permuted <- permutation(model=~F1*F2, data=data, design=design, NPermutations=100, nCpus=3)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
16
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
17 decomposition(fit, decomposition = "pca", scale="none", type="coefficient")
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
18
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
19
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
20
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
21
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
22 ssq <- function(fit)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
23 {
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
24 Overall_means <- sum(sum(fitted.values(fit)$'(Intercept)'^2))/sum(sum(data^2))
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
25 Factors <- c(sum(sum(fitted.values(fit)$'F1'^2))/sum(sum(data^2)), sum(sum(fitted.values(fit)$'F2'^2))/sum(sum(data^2)))
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
26 Interactions <- sum(sum(fitted.values(fit)$'F1:F2'^2))/sum(sum(data^2))
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
27 Residuals <- 1 - Overall_means - Factors[1] - Factors[2] - Interactions
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
28
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
29 return(list(Overall_means, Factors, Interactions, Residuals))
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
30 }
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
31
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
32
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
33 par(mfrow=c(2,2))
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
34 biplot(fit, xlabs="o", mfcol=NULL)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
35 ##Just the term of interest
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
36 biplot(fit, xlabs="o", term="F1")
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
37 ##In separate graphics
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
38 biplot(fit, xlabs="o", term=c("F1", "F2"), mfcol=c(1,1))
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
39 ##All terms in the same graphic
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
40 biplot(fit, xlabs="o", mfcol=c(1,3))
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
41
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
42 test <-lapply(permuted, FUN = ssq)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
43 test1 <- matrix(unlist(test), ncol=5, byrow=TRUE)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
44 test[2:101]
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
45
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
46 apply(apply(test1, 2, FUN = function(x){x > x[1]})[-1,], 2 , sum) / (length(test)-1)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
47
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
48 score_moyen <- data.frame(fit@components$F1$rotation)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
49
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
50 score <- data.frame(cbind(design, t(fit@residuals$'F1:F2')%*%fit@components$F1$rotation))
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
51
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
52 pc <- fit@components$F1$sdev / sum(fit@components$F1$sdev)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
53
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
54 sp <- ggplot(score_moyen, aes(x=PC1, y=PC2))
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
55 sp + geom_point(size=2) + xlab(paste("PC1", round(pc[1]*100,1), "%")) + ylab(paste("PC2", round(pc[2]*100,1), "%")) +
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
56 geom_text(data=score, aes(PC1, PC2, label=Ind, col=F1), size=4, hjust=0, nudge_x=0.05, vjust=0, nudge_y=0.5)
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
57
93312041f1d5 planemo upload for repository https://github.com/workflow4metabolomics/ascaw4m commit 7ea9b0f8abc5a60c2c04fd2098788497f14766b6
marie-tremblay-metatoul
parents:
diff changeset
58