Diff of /vignettes/vignette.R [000000] .. [d79ff0]

Switch to unified view

a b/vignettes/vignette.R
1
## ---- echo =  FALSE-----------------------------------------------------------
2
knitr::opts_chunk$set(eval = TRUE, 
3
                      echo = TRUE,
4
                      fig.align = "center",
5
                      warning = FALSE,
6
                      message = FALSE)
7
8
## ---- message=FALSE, warning=FALSE--------------------------------------------
9
library(timeOmics)
10
11
## ---- message=F---------------------------------------------------------------
12
library(tidyverse)
13
14
## -----------------------------------------------------------------------------
15
data("timeOmics.simdata")
16
sim.data <- timeOmics.simdata$sim
17
18
dim(sim.data) 
19
head(sim.data[,1:6])
20
21
## -----------------------------------------------------------------------------
22
remove.low.cv <- function(X, cutoff = 0.5){
23
  # var.coef
24
  cv <- unlist(lapply(as.data.frame(X), 
25
                      function(x) abs(sd(x)/mean(x))))
26
  return(X[,cv > cutoff])
27
}
28
29
data.filtered <- remove.low.cv(sim.data, 0.5)
30
31
## ---- message=FALSE-----------------------------------------------------------
32
# numeric vector containing the sample time point information
33
time <- timeOmics.simdata$time
34
head(time)
35
36
## ----eval=FALSE---------------------------------------------------------------
37
#  # example of lmms
38
#  lmms.output <- lmms::lmmSpline(data = data.filtered, time = time,
39
#                           sampleID = rownames(data.filtered), deri = FALSE,
40
#                           basis = "p-spline", numCores = 4, timePredict = 1:9,
41
#                           keepModels = TRUE)
42
#  modelled.data <- t(slot(lmms.output, 'predSpline'))
43
44
## ---- warning=FALSE, message=FALSE--------------------------------------------
45
lmms.output <- timeOmics.simdata$lmms.output
46
modelled.data <- timeOmics.simdata$modelled
47
48
## -----------------------------------------------------------------------------
49
# gather data
50
data.gathered <- modelled.data %>% as.data.frame() %>% 
51
  rownames_to_column("time") %>%
52
  mutate(time = as.numeric(time)) %>%
53
  pivot_longer(names_to="feature", values_to = 'value', -time)
54
55
# plot profiles
56
ggplot(data.gathered, aes(x = time, y = value, color = feature)) + geom_line() +
57
  theme_bw() + ggtitle("`lmms` profiles") + ylab("Feature expression") +
58
  xlab("Time")
59
60
## -----------------------------------------------------------------------------
61
filter.res <- lmms.filter.lines(data = data.filtered, 
62
                                lmms.obj = lmms.output, time = time)
63
profile.filtered <- filter.res$filtered
64
65
## -----------------------------------------------------------------------------
66
# run pca
67
pca.res <- pca(X = profile.filtered, ncomp = 5, scale=FALSE, center=FALSE)
68
69
# tuning ncomp
70
pca.ncomp <- getNcomp(pca.res, max.ncomp = 5, X = profile.filtered, 
71
                      scale = FALSE, center=FALSE)
72
73
pca.ncomp$choice.ncomp
74
plot(pca.ncomp)
75
76
## -----------------------------------------------------------------------------
77
# final model
78
pca.res <- pca(X = profile.filtered, ncomp = 2, scale = FALSE, center=FALSE)
79
80
## -----------------------------------------------------------------------------
81
# extract cluster
82
pca.cluster <- getCluster(pca.res)
83
head(pca.cluster)
84
85
## -----------------------------------------------------------------------------
86
plotIndiv(pca.res)
87
88
## -----------------------------------------------------------------------------
89
plotVar(pca.res)
90
91
## -----------------------------------------------------------------------------
92
plotLoadings(pca.res)
93
94
## -----------------------------------------------------------------------------
95
plotLong(pca.res, scale = FALSE, center = FALSE, 
96
         title = "PCA longitudinal clustering")
97
98
## -----------------------------------------------------------------------------
99
tune.spca.res <- tuneCluster.spca(X = profile.filtered, ncomp = 2, 
100
                                  test.keepX = c(2:10))
101
# selected features in each component
102
tune.spca.res$choice.keepX
103
plot(tune.spca.res)
104
105
## -----------------------------------------------------------------------------
106
# final model
107
spca.res <- spca(X = profile.filtered, ncomp = 2, 
108
                 keepX = tune.spca.res$choice.keepX, scale = FALSE)
109
plotLong(spca.res)
110
111
## -----------------------------------------------------------------------------
112
X <- profile.filtered
113
Y <- timeOmics.simdata$Y
114
115
pls.res <- pls(X,Y, ncomp = 5, scale = FALSE)
116
pls.ncomp <- getNcomp(pls.res, max.ncomp = 5, X=X, Y=Y, scale = FALSE)
117
pls.ncomp$choice.ncomp
118
plot(pls.ncomp)
119
120
## -----------------------------------------------------------------------------
121
# final model
122
pls.res <- pls(X,Y, ncomp = 2, scale = FALSE)
123
124
# info cluster
125
head(getCluster(pls.res))
126
# plot clusters
127
plotLong(pls.res, title = "PLS longitudinal clustering", legend = TRUE)
128
129
## -----------------------------------------------------------------------------
130
tune.spls <- tuneCluster.spls(X, Y, ncomp = 2, test.keepX = c(4:10), test.keepY <- c(2,4,6))
131
132
# selected features in each component on block X
133
tune.spls$choice.keepX
134
# selected features in each component on block Y
135
tune.spls$choice.keepY
136
137
# final model
138
spls.res <- spls(X,Y, ncomp = 2, scale = FALSE, 
139
                 keepX = tune.spls$choice.keepX, keepY = tune.spls$choice.keepY)
140
141
# spls cluster
142
spls.cluster <- getCluster(spls.res)
143
144
# longitudinal cluster plot
145
plotLong(spls.res, title = "sPLS clustering")
146
147
## -----------------------------------------------------------------------------
148
X <- list("X" = profile.filtered, "Z" = timeOmics.simdata$Z)
149
Y <- as.matrix(timeOmics.simdata$Y)
150
151
block.pls.res <- block.pls(X=X, Y=Y, ncomp = 5, 
152
                           scale = FALSE, mode = "canonical")
153
block.ncomp <- getNcomp(block.pls.res,X=X, Y=Y, 
154
                        scale = FALSE, mode = "canonical")
155
block.ncomp$choice.ncomp
156
plot(block.ncomp)
157
158
## -----------------------------------------------------------------------------
159
# final model
160
block.pls.res <- block.pls(X=X, Y=Y, ncomp = 1, scale = FALSE, mode = "canonical")
161
# block.pls cluster
162
block.pls.cluster <- getCluster(block.pls.res)
163
164
# longitudinal cluster plot
165
plotLong(block.pls.res)
166
167
## -----------------------------------------------------------------------------
168
test.list.keepX <- list("X" = 4:10, "Z" = c(2,4,6,8))
169
test.keepY <- c(2,4,6)
170
171
tune.block.res <- tuneCluster.block.spls(X= X, Y= Y, 
172
                                         test.list.keepX=test.list.keepX, 
173
                                         test.keepY= test.keepY, 
174
                                         scale=FALSE, 
175
                                         mode = "canonical", ncomp = 1)
176
# ncomp = 1 given by the getNcomp() function
177
178
# selected features in each component on block X
179
tune.block.res$choice.keepX
180
# selected features in each component on block Y
181
tune.block.res$choice.keepY
182
183
# final model
184
block.pls.res <- block.spls(X=X, Y=Y, 
185
                            ncomp = 1, 
186
                            scale = FALSE, 
187
                            mode = "canonical", 
188
                            keepX = tune.block.res$choice.keepX, 
189
                            keepY = tune.block.res$choice.keepY)
190
191
head(getCluster(block.pls.res))
192
plotLong(block.pls.res)
193