Skip to content

Commit 0ab1e3d

Browse files
committed
feat: compute state column when reconstructing indicators
1 parent 6d9f191 commit 0ab1e3d

File tree

3 files changed

+26
-30
lines changed

3 files changed

+26
-30
lines changed

R/reconstructData.R

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#'
33
#' @description
44
#' The reconstruction formula is:
5-
#' \deqn{1^{x}(t) = p^x(t) ( 1 + \sum_{i\geq 1} z_i^{kx}*a_i^x(t)})
5+
#' \deqn{1^{x}(t) = p^x(t) ( 1 + \sum_{i\geq 1} z_i*a_i^x(t)})
66
#'
77
#' with \eqn{z_i}, the i-th principal component,
88
#' encoding \eqn{a_i^x = \sum_j \alpha_{(x, j)} * \phi_j(t)}
@@ -13,7 +13,9 @@
1313
#' @param timeValues vector containing time values at which compute the indicators. If NULL, the time values from the data
1414
#' @param propMinEigenvalues Only if nComp = NULL. Minimal proportion used to estimate the number of non-null eigenvalues
1515
#'
16-
#' @return a data.frame with columns: time, id, state1, ..., stateK
16+
#' @return a data.frame with columns: time, id, state1, ..., stateK, state.
17+
#' state1 contains the estimated indicator values for the first state.
18+
#' state contains the state with the maximum values of all indicators
1719
#'
1820
#' @examples
1921
#' set.seed(42)
@@ -37,11 +39,15 @@
3739
#' plotData(d_JK2[d_JK2$id == iInd, ])
3840
#'
3941
#' plotIndicatorsReconstruction(indicators, id = iInd)
42+
#'
43+
#' # the column state contains the state associated with the greatest indicator.
44+
#' # So, the output can be used with plotData function
45+
#' plotData(remove_duplicated_states(indicators[indicators$id == iInd, ]))
4046
#' }
4147
#' @author Quentin Grimonprez
4248
#' @seealso \code{\link{plotIndicatorsReconstruction}}
4349
#' @export
44-
reconstructIndicators <- function(x, nComp = NULL, timeValues = NULL, propMinEigenvalues = 1e-6) {
50+
reconstructIndicators <- function(x, nComp = NULL, timeValues = NULL, propMinEigenvalues = 1e-4) {
4551
checkFmca(x)
4652
if (
4753
any(is.na(propMinEigenvalues)) || !is.numeric(propMinEigenvalues) || (length(propMinEigenvalues) != 1) || (
@@ -51,6 +57,7 @@ reconstructIndicators <- function(x, nComp = NULL, timeValues = NULL, propMinEig
5157
}
5258
if (is.null(nComp)) {
5359
nComp <- sum(x$eigenvalues / sum(x$eigenvalues) > propMinEigenvalues)
60+
print(paste0("Reconstruct data using ", nComp, " components (out of ", length(x$eigenvalues), ")"))
5461
}
5562
checkInteger(
5663
nComp,
@@ -92,6 +99,7 @@ reconstructIndicators <- function(x, nComp = NULL, timeValues = NULL, propMinEig
9299
df[[paste0("state", x$label$label[i])]] <- yy[, i]
93100
}
94101
allDf[[iInd]] <- df
102+
allDf[[iInd]]$state <- x$label$label[apply(allDf[[iInd]][paste0("state", x$label$label)], 1, which.max)]
95103
}
96104

97105
return(do.call(rbind, allDf))
@@ -105,36 +113,15 @@ reconstructIndicators <- function(x, nComp = NULL, timeValues = NULL, propMinEig
105113
#'
106114
#' @return ggplot
107115
#'
108-
#' @examples
109-
#' set.seed(42)
110-
#' # Simulate the Jukes-Cantor model of nucleotide replacement
111-
#' K <- 3
112-
#' Tmax <- 1
113-
#' d_JK <- generate_Markov(n = 100, K = K, Tmax = Tmax)
114-
#' d_JK2 <- cut_data(d_JK, Tmax)
115-
#'
116-
#' # create basis object
117-
#' m <- 20
118-
#' b <- create.bspline.basis(c(0, Tmax), nbasis = m, norder = 4)
119-
#' \donttest{
120-
#' # compute encoding
121-
#' encoding <- compute_optimal_encoding(d_JK2, b, computeCI = FALSE, nCores = 1)
122-
#'
123-
#' indicators <- reconstructIndicators(encoding)
124-
#'
125-
#' # we plot the first path and its reconstructed indicators
126-
#' iInd <- 3
127-
#' plotData(d_JK2[d_JK2$id == iInd, ])
128-
#'
129-
#' plotIndicatorsReconstruction(indicators, id = iInd)
130-
#' }
116+
#' @inherit reconstructIndicators examples
131117
#'
132118
#' @author Quentin Grimonprez
133119
#' @seealso \code{\link{reconstructIndicators}}
134120
#' @export
135121
plotIndicatorsReconstruction <- function(reconstruction, id) {
136122
x <- reconstruction[reconstruction$id == id, ]
137123
x$id <- NULL
124+
x$state <- NULL
138125

139126
p <- ggplot(
140127
data = pivot_longer(x, cols = -1, values_to = "probability", names_to = "state"),

man/plotIndicatorsReconstruction.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/reconstructIndicators.Rd

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)