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)}
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)
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
135121plotIndicatorsReconstruction <- 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" ),
0 commit comments