Switch to unified view

a b/data-raw/script_timeOmics.simdata.R
1
library(tidyverse)
2
library(lmms)
3
4
# RAW DATA
5
c1 <- c(0, 0.5,1,1.1,1.2,1.8,2.5,5,9)
6
c3 <-  c(-2,4, 8, 6,4.5,4,3.9, 3, 1)
7
c2 <- -c1
8
c4 <- -c3
9
10
list(c1,c2,c3,c4)
11
12
13
c1.0 <-  c1
14
c1.1 <-  c1*1.5
15
c1.2 <- (c1-0.3)*0.3
16
c1.3 <- (c1 +0.5)*0.8
17
c1.4 <- (c1-1)*1.1
18
19
c2.0 <-  c2
20
c2.1 <-  c2*1.5
21
c2.2 <- (c2-0.3)*0.3
22
c2.3 <- (c2 +0.5)*0.8
23
c2.4 <- (c2-1)*1.1
24
25
c3.0 <-  c3
26
c3.1 <-  c3*1.5
27
c3.2 <- (c3-0.3)*0.3
28
c3.3 <- (c3 +0.5)*0.8
29
c3.4 <- (c3-1)*1.1
30
31
c4.0 <-  c4
32
c4.1 <-  c4*1.5
33
c4.2 <- (c4-0.3)*0.3
34
c4.3 <- (c4 +0.5)*0.8
35
c4.4 <- (c4-1)*1.4
36
37
# noise 
38
c0 <- c(0,0.1,0.05,0,0,0.1,0,0.05,0.1) +1
39
sd(c0)/mean(c0)
40
41
data <- list(c1.0,c1.1,c1.2,c1.3,c1.4,c2.0,c2.1,c2.2,c2.3,c2.4,c3.0,c3.1,c3.2,c3.3,c3.4,c4.0,c4.1,c4.2,c4.3,c4.4, c0)
42
names(data) <- c("c1.0", "c1.1", "c1.2", "c1.3", "c1.4",
43
                 "c2.0", "c2.1", "c2.2", "c2.3", "c2.4",
44
                 "c3.0", "c3.1", "c3.2", "c3.3", "c3.4",
45
                 "c4.0", "c4.1", "c4.2", "c4.3", "c4.4",
46
                 "c0")
47
raw.data <- as.data.frame(data)setwd("~")
48
load("~/Downloads/lmms.out2.rda")
49
data.gather <- raw.data %>% rownames_to_column("time") %>%
50
    mutate(time = as.numeric(time)) %>%
51
    gather(sample, value, -time)
52
53
# SIM DATA
54
sd <- 0.3
55
N_Ind <- 5
56
set.seed(123)
57
58
tmp <- data.gather
59
for(ind in 1:N_Ind){
60
    vect <- vector(length = nrow(tmp), mode = "numeric")
61
    for(x in 1:length(vect)){
62
        vect[x] <- rnorm(1, mean = tmp$value[x], sd = sd)
63
    }
64
    name.c <- names(tmp)
65
    tmp <- data.frame(tmp, vect)
66
    colnames(tmp) <- c(name.c, LETTERS[ind])
67
}
68
69
sim.data <- tmp %>% dplyr::select(-c(value)) %>%
70
    gather(ind, value, -c(sample, time))%>%
71
    mutate(ind = c(paste0(ind, "_", time))) %>% dplyr::select(-time) %>%
72
    spread(ind, value) %>% column_to_rownames("sample") %>% t
73
74
# modelled data
75
time <- rownames(sim.data) %>% str_split("_") %>% map_chr(~.x[2]) %>% as.numeric()
76
sampleID <- rownames(sim.data)
77
lmms.out <- lmmSpline(data = sim.data, time = time, sampleID = sampleID, keepModels = TRUE)
78
79
# build new s4
80
#setClass('lmms',slots=c(basis="character", knots="numeric",errorMolecules="character"))
81
setClass("lmmspline",slots= c(predSpline="data.frame", modelsUsed="numeric",models="list",derivative='logical', basis="character", knots="numeric",errorMolecules="character"))
82
83
lmms.out2 <- new("lmmspline", 
84
                 predSpline = lmms.out@predSpline,
85
                 modelsUsed = lmms.out@modelsUsed,
86
                 models = lmms.out@models,
87
                 derivative = lmms.out@derivative, 
88
                 basis = lmms.out@basis, 
89
                 knots = lmms.out@knots, 
90
                 errorMolecules = lmms.out@errorMolecules )
91
92
save(lmms.out2, file = "~/Downloads/lmms.out2.rda")
93
94
modelled.data <-  as.data.frame(t(lmms.out2@predSpline))
95
96
detach("package:lmms", unload=TRUE)
97
98
timeOmics.simdata <- list(rawdata = raw.data, sim = sim.data,
99
                          modelled = modelled.data[,-c0], 
100
                          lmms.output = lmms.out2,
101
                          time = time)
102
103
library(lmms)
104
# Y same as data but increase noise
105
sd <- 0.5
106
N_Ind <- 4
107
set.seed(123)
108
109
tmp <- data.gather %>% filter(time %in% c(1,2,3,5,7,9))
110
for(ind in 1:N_Ind){
111
    vect <- vector(length = nrow(tmp), mode = "numeric")
112
    for(x in 1:length(vect)){
113
        vect[x] <- rnorm(1, mean = tmp$value[x], sd = sd)
114
    }
115
    name.c <- names(tmp)
116
    tmp <- data.frame(tmp, vect)
117
    colnames(tmp) <- c(name.c, LETTERS[ind])
118
}
119
120
Y <- tmp %>% dplyr::select(-c(value)) %>%
121
    gather(ind, value, -c(sample, time))%>%
122
    mutate(ind = c(paste0(ind, "_", time))) %>% dplyr::select(-time) %>%
123
    spread(ind, value) %>% column_to_rownames("sample") %>% t
124
125
time.Y <- rownames(Y) %>% str_split("_") %>% map_chr(~.x[2]) %>% as.numeric()
126
sampleID.Y <- rownames(Y)
127
lmms.Y <- lmmSpline(data = Y, time = time.Y, sampleID = sampleID.Y, keepModels = TRUE, 
128
                          timePredict = 1:9)
129
modelled.Y <- lmms.Y@predSpline %>% t %>% as.data.frame()
130
colnames(modelled.Y) <- paste0("Y_", seq_along(colnames(modelled.Y)))
131
132
timeOmics.simdata[["Y"]] <- modelled.Y
133
134
# Z
135
# Y same as data but increase noise
136
sd <- 1
137
N_Ind <- 4
138
set.seed(123)
139
140
tmp <- data.gather %>% filter(time %in% c(1,3,4,5,8,9))
141
for(ind in 1:N_Ind){
142
    vect <- vector(length = nrow(tmp), mode = "numeric")
143
    for(x in 1:length(vect)){
144
        vect[x] <- rnorm(1, mean = tmp$value[x], sd = sd)
145
    }
146
    name.c <- names(tmp)
147
    tmp <- data.frame(tmp, vect)
148
    colnames(tmp) <- c(name.c, LETTERS[ind])
149
}
150
151
Z <- tmp %>% dplyr::select(-c(value)) %>%
152
    gather(ind, value, -c(sample, time))%>%
153
    mutate(ind = c(paste0(ind, "_", time))) %>% dplyr::select(-time) %>%
154
    spread(ind, value) %>% column_to_rownames("sample") %>% t
155
156
time.Z <- rownames(Z) %>% str_split("_") %>% map_chr(~.x[2]) %>% as.numeric()
157
sampleID.Z <- rownames(Z)
158
lmms.Z <- lmms::lmmSpline(data = Z, time = time.Z, sampleID = sampleID.Z, keepModels = TRUE, 
159
                          timePredict = 1:9)
160
modelled.Z <- lmms.Z@predSpline %>% t %>% as.data.frame()
161
colnames(modelled.Z) <- paste0("Z_", seq_along(colnames(modelled.Z)))
162
163
timeOmics.simdata[["Z"]] <- modelled.Z
164
165
usethis::use_data(timeOmics.simdata, overwrite = TRUE)
166
#save(timeOmics.simdata, file = "./data/timeOmics.simdata.rda", compress = "gzip", ascii = FALSE)