[d79ff0]: / vignettes / vignette.R

Download this file

194 lines (151 with data), 7.1 kB

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