-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_transmat.R
72 lines (57 loc) · 2.62 KB
/
multi_transmat.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
#' multi_transmat: transition matrix of all the items
#'
#' @title Creates a transition matrix for each item.
#' @description Needs an 'interleaved' dataframe (see interleave function). Pre-test item should be followed by corresponding post-item item etc.
#' Don't knows must be coded as NA. Function handles items without don't know responses.
#' The function is used internally. It calls transmat.
#' @param pre_test Required. data.frame carrying responses to pre-test questions.
#' @param pst_test Required. data.frame carrying responses to post-test questions.
#' @param subgroup a Boolean vector identifying the subset. Default is NULL.
#' @param force9 Optional. There are cases where DK data doesn't have DK. But we need the entire matrix. By default it is FALSE.
#' @param agg Optional. Boolean. Whether or not to add a row of aggregate transitions at the end of the matrix. Default is FALSE.
#' @return matrix with rows = total number of items + 1 (last row contains aggregate distribution across items)
#' number of columns = 4 when no don't know, and 9 when there is a don't know option
#' @export
#' @examples
#' pre_test <- data.frame(pre_item1 = c(1,0,0,1,0), pre_item2 = c(1,NA,0,1,0))
#' pst_test <- data.frame(pst_item1 = pre_test[,1] + c(0,1,1,0,0),
#' pst_item2 = pre_test[,2] + c(0,1,0,0,1))
#' multi_transmat(pre_test, pst_test)
multi_transmat <- function (pre_test = NULL, pst_test = NULL,
subgroup = NULL, force9 = FALSE, agg = FALSE) {
# Checks
# pre_test data frame is missing
if (!is.data.frame(pre_test)) stop("Specify pre_test data.frame.")
# post_test data frame is missing
if (!is.data.frame(pst_test)) stop("Specify pst_test data.frame.")
# If different no. of items
if (length(pre_test) != length(pst_test)) {
stop("Lengths of pre_test and pst_test must be the same.")
}
# Subset
if (!is.null(subgroup)) {
pre_test <- subset(pre_test, subgroup)
pst_test <- subset(pst_test, subgroup)
}
# No. of items
n_items <- length(pre_test)
# Initialize results
res <- list()
# Get transition matrix for each item pair
for (i in 1:n_items) {
# cat("\n Item", i, "\n")
res[[i]] <- transmat(pre_test[, i], pst_test[, i], force9 = force9)
}
# Prepping results
row_names <- paste0("item", 1:n_items)
col_names <- names(res[[1]])
res <- matrix(unlist(res),
nrow = n_items,
byrow = TRUE,
dimnames = list(row_names, col_names))
if (agg == TRUE) {
res <- rbind(res, colSums(res, na.rm = T))
rownames(res)[nrow(res)] <- "agg"
}
invisible(res)
}