-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmm.R
367 lines (282 loc) · 9.32 KB
/
mm.R
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#' Markov model specification
#'
#' @description Creates a model specification of a Markov model.
#'
#' @param states Vector of state space of length s.
#' @param k Order of the Markov chain.
#' @param init Vector of initial distribution of length s ^ k.
#' @param ptrans Matrix of transition probabilities of dimension \eqn{(s, s)}.
#' @return An object of class [mm].
#'
#' @seealso [simulate.mm], [fitmm]
#'
#' @export
#'
#' @examples
#' states <- c("a", "c", "g", "t")
#' s <- length(states)
#' k <- 1
#' init <- rep.int(1 / s, s)
#' p <- matrix(c(0, 0, 0.3, 0.4, 0, 0, 0.5, 0.2, 0.7, 0.5,
#' 0, 0.4, 0.3, 0.5, 0.2, 0), ncol = s)
#'
#' # Specify a Markov model of order 1
#' markov <- mm(states = states, init = init, ptrans = p, k = k)
#'
#' @testexamples
#' expect_true(all(markov$param == p))
#' expect_true(all(markov$init == init))
#'
mm <- function(states, init, ptrans, k = 1) {
#############################
# Checking parameter states
#############################
s <- length(states)
if (!(is.vector(states) & (length(unique(states)) == s))) {
stop("The state space 'states' is not a vector of unique elements")
}
#############################
# Checking parameter init
#############################
if (!(is.numeric(init) & !anyNA(init) & is.vector(init) & length(init) == s ^ k)) {
stop("'init' is not a numeric vector of length s ^ k")
}
if (!(all(init >= 0) & all(init <= 1))) {
stop("Probabilities in 'init' must be between [0, 1]")
}
if (!((sum(init) >= 1 - sqrt(.Machine$double.eps)) | (sum(init) <= 1 + sqrt(.Machine$double.eps)))) {
stop("The sum of 'init' is not equal to one")
}
#############################
# Checking parameter k
#############################
if (!((k > 0) & ((k %% 1) == 0))) {
stop("'k' must be a strictly positive integer")
}
#############################
# Checking parameter ptrans
#############################
if (!(is.numeric(ptrans) & !anyNA(ptrans) & is.matrix(ptrans))) {
stop("'ptrans' is not a matrix with numeric values")
}
if (!((dim(ptrans)[1] == s ^ k) & (dim(ptrans)[2] == s))) {
stop("The dimension of the matrix 'ptrans' must be equal to (s ^ k, s)")
}
if (!(all(ptrans >= 0) & all(ptrans <= 1))) {
stop("Probabilities in 'ptrans' must be between [0, 1]")
}
if (!all((apply(ptrans, 1, sum) >= 1 - sqrt(.Machine$double.eps)) | (apply(ptrans, 1, sum) <= 1 + sqrt(.Machine$double.eps)))) {
stop("'ptrans' is not a stochastic matrix (column sums accross rows must be equal to one for each row)")
}
# Add names to the attributes init, ptrans, for readability
colnames(ptrans) <- words(length = 1, alphabet = states)
row.names(ptrans) <- words(length = k, alphabet = states)
names(init) <- row.names(ptrans)
ans <- list(states = states, s = s, k = k, init = init, ptrans = ptrans)
class(ans) <- "mm"
return(ans)
}
#' Function to check if an object is of class `mm`
#'
#' @description `is.mm` returns `TRUE` if `x` is an object of
#' class `mm`.
#'
#' @param x An arbitrary R object.
#' @return `is.mm` returns `TRUE` or `FALSE` depending on whether `x` is an
#' object of class `mm` or not.
#'
#' @export
#'
is.mm <- function(x) {
inherits(x, "mm")
}
# Method to get the stationary distribution
#' @export
get.stationaryDistribution.mm <- function(x) {
p <- .stationaryDistribution(ptrans = .blockMatrix(ptrans = x$ptrans))
return(p)
}
# Method to get the number of parameters
# (useful for the computation of criteria such as AIC and BIC)
.get.Kpar.mm <- function(x) {
s <- x$s
kpar <- (s - 1) * s ^ x$k
return(kpar)
}
# Method to get the number of parameters
# (useful for the computation of criteria such as AIC and BIC)
#' @export
get.Kpar.mm <- function(x) {
kpar <- .get.Kpar.mm(x)
return(kpar)
}
#' Log-likelihood Function
#'
#' @description Computation of the log-likelihood for a Markov model
#'
#' @param x An object of class [mm].
#' @param processes An object of class `processesMarkov`.
#'
#' @noRd
#'
.logLik.mm <- function(x, processes) {
#############################
# Let's compute the log-likelihood
#############################
Nstarti <- processes$Nstarti
maskNstarti <- processes$Nstarti != 0 & x$init != 0
Nij <- processes$Nij
maskNij <- processes$Nij != 0 & x$ptrans != 0
logLik <- sum(Nstarti[maskNstarti] * log(x$init[maskNstarti])) + sum(Nij[maskNij] * log(x$ptrans[maskNij]))
return(logLik)
}
#' Akaike Information Criterion (AIC)
#'
#' @description Computation of the Akaike Information Criterion.
#'
#' @param x An object of class [mm].
#' @param sequences A list of vectors representing the sequences for which the
#' AIC will be computed based on `x`.
#' @return Value of the AIC.
#'
#' @noRd
#'
#' @export
#'
AIC.mm <- function(object, ...) {
sequences = list(...)[1]
logLik <- logLik(object, sequences)
kpar <- .get.Kpar(object)
AIC <- -2 * logLik + 2 * kpar
return(AIC)
}
#' Bayesian Information Criterion (BIC)
#'
#' @description Computation of the Bayesian Information Criterion.
#'
#' @param x An object of class [mm].
#' @param sequences A list of vectors representing the sequences for which the
#' BIC will be computed based on `x`.
#' @return Value of the BIC.
#'
#' @noRd
#'
#' @export
#'
BIC.mm <- function(object, ...) {
sequences = list(...)[1]
logLik <- logLik(object, sequences)
kpar <- .get.Kpar(object)
n <- sum(sapply(sequences, length))
BIC <- -2 * logLik + log(n) * kpar
return(BIC)
}
#' Loglikelihood
#'
#' @description Computation of the log-likelihood for a Markov model
#'
#' @param x An object of class [mm].
#' @param sequences A list of vectors representing the sequences for which the
#' log-likelihood will be computed based on `x`.
#' @return Value of the log-likelihood.
#'
#' @noRd
#'
#' @export
#'
logLik.mm <- function(object, ...) {
sequences = list(...)[1]
#############################
# Checking parameters sequences and states
#############################
if (!(is.list(sequences) & all(sapply(sequences, class) %in% c("character", "numeric")))) {
stop("The parameter 'sequences' should be a list of vectors")
}
if (!all(unique(unlist(sequences)) %in% object$states)) {
stop("Some states in the list of observed sequences 'sequences'
are not in the state space given by the model 'object'")
}
processes <- processesMarkov(sequences = sequences, states = object$states, k = object$k, verbose = FALSE)
logLik <- .logLik.mm(x = object, processes = processes)
return(logLik)
}
#' Simulates k-th order Markov chains
#'
#' @description Simulates k-th order Markov chains.
#'
#' @details If `nsim` is a single integer then a chain of that length is
#' produced. If `nsim` is a vector of integers, then `length(nsim)`
#' sequences are generated with respective lengths.
#'
#' @param object An object of class [mm].
#' @param nsim An integer or vector of integers (for multiple sequences)
#' specifying the length of the sequence(s).
#' @param seed Optional. `seed` for the random number generator.
#' If no `seed` is given, then seed is set by using the command
#' `set.seed(round(as.numeric(Sys.time()))`.
#' @param ... further arguments passed to or from other methods.
#' @return A list of vectors representing the sequences.
#'
#' @seealso [mm], [fitmm]
#'
#' @export
#'
#' @examples
#' states <- c("a", "c", "g", "t")
#' s <- length(states)
#' k <- 2
#' init <- rep.int(1 / s ^ k, s ^ k)
#' p <- matrix(0.25, nrow = s ^ k, ncol = s)
#'
#' # Specify a Markov model of order 1
#' markov <- mm(states = states, init = init, ptrans = p, k = k)
#'
#' seqs <- simulate(object = markov, nsim = c(1000, 10000, 2000), seed = 150)
#'
#' @testexamples
#' expect_equal(length(seqs), 3)
#' expect_equal(sapply(seqs, length), c(1000, 10000, 2000))
#' expect_equal(seqs[[1]][995:1000], c("g","c","a","c","a","t"))
#'
simulate.mm <- function(object, nsim = 1, seed = NULL, ...) {
#############################
# Checking parameter nsim
#############################
if (!all(is.numeric(nsim), is.vector(nsim), !anyNA(nsim), nsim > 0, (nsim %% 1) == 0)) {
stop("'nsim' must be a strictly positive integer or a vector of striclty positive integers")
}
#############################
# Checking parameter seed
#############################
if (!is.null(seed)) {
if (!all(is.numeric(seed), seed >= 0, (seed %% 1) == 0)) {
stop("'seed' must be a positive integer")
}
{
set.seed(seed)
}
}
s <- length(object$states)
out <- list()
nbseq <- length(nsim)
for (n in 1:nbseq) {
if (nsim[n] <= object$k) {
y <- s2c(sample(x = names(object$init), size = 1, prob = object$init))
} else {
y <- rep.int(NA, nsim[n])
# Initial state(s)
y[1:object$k] <- s2c(sample(x = names(object$init), size = 1, prob = object$init))
for (i in 1:(nsim[n] - object$k)) {
ind <- which(object$states == y[i + object$k - 1])
if (object$k > 1) {
for (j in (object$k - 2):0) {
ind <- ind + s ^ (j + 1) * (which(object$states == y[i + j]) - 1)
}
}
y[i + object$k] <- sample(object$states, 1, prob = object$ptrans[ind, ])
}
}
out[[n]] <- y
}
return(out)
}