Skip to content

Commit 49adbc8

Browse files
committed
feat: add functiosn to convert multivariate to univariate cfd
1 parent 90b3ca5 commit 49adbc8

File tree

5 files changed

+186
-0
lines changed

5 files changed

+186
-0
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export(compute_optimal_encoding)
1616
export(compute_optimal_encoding_multivariate)
1717
export(compute_time_spent)
1818
export(convert2mvcfd)
19+
export(convertListCfd2Cfd)
20+
export(convertMvcfd2cfd)
1921
export(convertToCfd)
2022
export(cut_data)
2123
export(estimate_Markov)

R/multivariateFormat.R

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,60 @@ convert2mvcfd <- function(x, stateColumns = NULL) {
5151
return(distinct(x))
5252
}
5353

54+
#' Convert a multivariate cfd to an univariate one
55+
#'
56+
#' @param x multivariate cfd: data.frame with id, time columns and several stateColumns (output of \code{\link{convert2mvcfd}})
57+
#' @param sep separator used to concatenate states
58+
#' @param stateColumns names of the state columns. If NULL, it looks for columns with state in their names
59+
#'
60+
#' @return univariate cfd to use in \code{\link{compute_optimal_encoding}}. A data.frame with is, time and state columns
61+
#'
62+
#' @examples
63+
#' set.seed(42)
64+
#' x1 <- generate_Markov(n = 10, K = 2)
65+
#' x1 <- cut_data(x1, Tmax = 1)
66+
#' x2 <- generate_Markov(n = 10, K = 2)
67+
#' x2 <- cut_data(x2, Tmax = 1)
68+
#'
69+
#' x <- list(x1, x2)
70+
#'
71+
#' mvcfd <- convert2mvcfd(x)
72+
#' cfd <- convertMvcfd2cfd(mvcfd)
73+
#'
74+
#' @export
75+
convertMvcfd2cfd <- function(x, sep = "_", stateColumns = NULL) {
76+
checkData(x, requiredColNames = c("id", "time"))
77+
78+
if (is.null(stateColumns)) {
79+
stateColumns <- colnames(x)[grep("state", colnames(x))]
80+
}
81+
82+
xCfd <- x[c("id", "time")]
83+
xCfd$state <- as.factor(do.call(paste, c(x[stateColumns], sep = sep)))
84+
85+
return(xCfd)
86+
}
87+
88+
#' Convert a list of univariate cfds to an unique univariate cfd
89+
#'
90+
#' @param x list of cfd
91+
#' @param sep separator used to concatenate states
92+
#'
93+
#' @return univariate cfd to use in \code{\link{compute_optimal_encoding}}. A data.frame with is, time and state columns
94+
#'
95+
#' @examples
96+
#' set.seed(42)
97+
#' x1 <- generate_Markov(n = 10, K = 2)
98+
#' x1 <- cut_data(x1, Tmax = 1)
99+
#' x2 <- generate_Markov(n = 10, K = 2)
100+
#' x2 <- cut_data(x2, Tmax = 1)
101+
#'
102+
#' x <- list(x1, x2)
103+
#'
104+
#' cfd <- convertListCfd2Cfd(mvcfd)
105+
#'
106+
#' @export
107+
convertListCfd2Cfd <- function(x, sep = "_") {
108+
xMcfd <- convert2mvcfd(x)
109+
return(convertMvcfd2cfd(xMcfd, sep = sep))
110+
}

man/convertListCfd2Cfd.Rd

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

man/convertMvcfd2cfd.Rd

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

tests/testthat/test.multivariateFormat.R

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,65 @@ test_that("convert2mvcfd works with stateColumns", {
7171
expect_equal(out, expectedOut)
7272
})
7373

74+
test_that("convertMvcfd2cfd works", {
75+
# 2 columns
76+
x <- data.frame(id = c(1, 1, 1), time = c(0, 0.5, 1), state1 = c(1, 2, 1), state2 = c(2, 1, 3))
77+
expectedOut <- data.frame(id = c(1, 1, 1), time = c(0, 0.5, 1), state = as.factor(c("1_2", "2_1", "1_3")))
78+
79+
out <- convertMvcfd2cfd(x)
80+
expect_equal(out, expectedOut)
81+
82+
# > 2 columns
83+
x <- data.frame(id = c(1, 1, 1), time = c(0, 0.5, 1), state1 = c(1, 2, 1), state2 = c(2, 1, 3), state3 = c("a", "b", "c"))
84+
expectedOut <- data.frame(id = c(1, 1, 1), time = c(0, 0.5, 1), state = as.factor(c("1_2_a", "2_1_b", "1_3_c")))
85+
86+
out <- convertMvcfd2cfd(x)
87+
expect_equal(out, expectedOut)
88+
})
89+
90+
test_that("convertMvcfd2cfd works with custom sep", {
91+
x <- data.frame(id = c(1, 1, 1), time = c(0, 0.5, 1), state1 = c(1, 2, 1), state2 = c(2, 1, 3))
92+
expectedOut <- data.frame(id = c(1, 1, 1), time = c(0, 0.5, 1), state = as.factor(c("1 & 2", "2 & 1", "1 & 3")))
93+
94+
out <- convertMvcfd2cfd(x, sep = " & ")
95+
expect_equal(out, expectedOut)
96+
})
97+
98+
test_that("convertMvcfd2cfd works with custom stateColumns", {
99+
x <- data.frame(id = c(1, 1, 1), time = c(0, 0.5, 1), state1 = c(1, 2, 1), state2 = c(2, 1, 3))
100+
expectedOut <- data.frame(id = c(1, 1, 1), time = c(0, 0.5, 1), state = as.factor(c("1 & 2", "2 & 1", "1 & 3")))
101+
102+
out <- convertMvcfd2cfd(x, sep = " & ")
103+
expect_equal(out, expectedOut)
104+
})
105+
106+
test_that("convertMvcfd2cfd throws error with bad input", {
107+
x1 <- data.frame(id1 = c(1, 1, 1), time = c(0, 0.5, 1))
108+
109+
expect_error(convertMvcfd2cfd(2), regexp = "data must be a data.frame")
110+
expect_error(convertMvcfd2cfd(x1), regexp = "Missing columns in data: id.")
111+
})
112+
113+
test_that("convertListCfd2Cfd works", {
114+
x1 <- data.frame(id = c(1, 1, 1), time = c(0, 0.5, 1), state = c(1, 2, 1))
115+
x2 <- data.frame(id = c(1, 1, 1), time = c(0, 0.25, 0.9), state = c(1, 2, 3))
116+
x <- list(x1, x2)
117+
118+
expectedOut <- data.frame(
119+
id = rep(1, 5), time = c(0, 0.25, 0.5, 0.9, 1), state = as.factor(c("1_1", "1_2", "2_2", "2_3", "1_3"))
120+
)
121+
out <- convertListCfd2Cfd(x)
122+
expect_equal(out, expectedOut)
123+
})
124+
125+
test_that("convertListCfd2Cfd works with custom sep", {
126+
x1 <- data.frame(id = c(1, 1, 1), time = c(0, 0.5, 1), state = c(1, 2, 1))
127+
x2 <- data.frame(id = c(1, 1, 1), time = c(0, 0.25, 0.9), state = c(1, 2, 3))
128+
x <- list(x1, x2)
129+
130+
expectedOut <- data.frame(
131+
id = rep(1, 5), time = c(0, 0.25, 0.5, 0.9, 1), state = as.factor(c("1 & 1", "1 & 2", "2 & 2", "2 & 3", "1 & 3"))
132+
)
133+
out <- convertListCfd2Cfd(x, sep = " & ")
134+
expect_equal(out, expectedOut)
135+
})

0 commit comments

Comments
 (0)