From 1657be44a1451964b43774b2257135f54f0fe79d Mon Sep 17 00:00:00 2001 From: James Balamuta Date: Sat, 28 Aug 2021 16:13:50 -0500 Subject: [PATCH 1/5] Update for Rcpp 1.0.7 --- DESCRIPTION | 4 ++-- src/RcppExports.cpp | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 0a254cf..d795071 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,10 +23,10 @@ Description: Provides a common set of functions used in the exploratory URL: https://tmsalab.github.io/edmcore, https://github.com/tmsalab/edmcore BugReports: https://github.com/tmsalab/edmcore/issues License: GPL (>= 2) -Depends: R (>= 3.5.0) +Depends: R (>= 3.6.0) LinkingTo: Rcpp, RcppArmadillo Imports: - Rcpp, + Rcpp (>= 1.0.7), gtools, ggplot2 Suggests: diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 1cdd37c..22647cc 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -7,6 +7,11 @@ using namespace Rcpp; +#ifdef RCPP_USE_GLOBAL_ROSTREAM +Rcpp::Rostream& Rcpp::Rcout = Rcpp::Rcpp_cout_get(); +Rcpp::Rostream& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get(); +#endif + // attribute_bijection arma::uvec attribute_bijection(unsigned int K); RcppExport SEXP _edmcore_attribute_bijection(SEXP KSEXP) { From 600d6495721ce8a224e2f133d06da1d2ffad0982 Mon Sep 17 00:00:00 2001 From: James Balamuta Date: Sat, 28 Aug 2021 16:14:53 -0500 Subject: [PATCH 2/5] Add postprocessing recipes --- R/postprocess.R | 54 ++++++++++++++++++++++++ man/postprocess_attribute_correlation.Rd | 28 ++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 R/postprocess.R create mode 100644 man/postprocess_attribute_correlation.Rd diff --git a/R/postprocess.R b/R/postprocess.R new file mode 100644 index 0000000..644625e --- /dev/null +++ b/R/postprocess.R @@ -0,0 +1,54 @@ + +# Latent Class Membership ---- + +#' Correlation Among Attribute Membership +#' +#' Given the estimated latent class membership probabilities, we compute the +#' correlation across classes. +#' +#' @param pi_hat Estimated latent class membership probabilities +#' @param k Number of attributes +#' @param order Number of interactions in the model +#' +#' @examples +#' +#' # Sample calculation when K = 3 and number of classses is 2^3 = 8 +#' pi_hat = c(0.10, 0.10, 0.01, 0.13, 0.03, 0.26, 0.05, 0.31) +#' k = 3 +#' +#' # Compute attribute correlation matrix +#' postprocess_attribute_correlation(pi_hat, k) +postprocess_attribute_correlation = function(pi_hat, k, order = k) { + + # Obtain the attribute profiles that follow the bijection + alpha_profiles = + t(GenerateAtable( + nClass = 2 ^ k, + K, + M = 2, + order = order + )$DtoQtable) + + # Obtain the expected value (mean) and standard deviation + # using formulas for the binomial distribution + E_alpha = pi_hat %*% alpha_profiles # p * n + sd_alpha = sqrt(E_alpha * (1 - E_alpha)) # p * n * q + + # Construct a correlation matrix + cor_alpha = matrix(0, k, k) + + # Compute under the assumption of an upper triangular matrix + for (k_1 in seq_len(k - 1)) { + for (k_2 in (k_1 + 1):k) { + cov_tmp = + pi_hat %*% (alpha_profiles[, k_1] * alpha_profiles[, k_2]) - E_alpha[k_1] * E_alpha[k_2] + cor_alpha[k_1, k_2] = + cov_tmp / (sd_alpha[k_1] * sd_alpha[k_2]) + } + } + # Modify to incorporate + cor_alpha = cor_alpha + t(cor_alpha) + diag(k) + + # Return correlation matrix + cor_alpha +} diff --git a/man/postprocess_attribute_correlation.Rd b/man/postprocess_attribute_correlation.Rd new file mode 100644 index 0000000..bebcef8 --- /dev/null +++ b/man/postprocess_attribute_correlation.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/postprocess.R +\name{postprocess_attribute_correlation} +\alias{postprocess_attribute_correlation} +\title{Correlation Among Attribute Membership} +\usage{ +postprocess_attribute_correlation(pi_hat, k, order = k) +} +\arguments{ +\item{pi_hat}{Estimated latent class membership probabilities} + +\item{k}{Number of attributes} + +\item{order}{Number of interactions in the model} +} +\description{ +Given the estimated latent class membership probabilities, we compute the +correlation across classes. +} +\examples{ + +# Sample calculation when K = 3 and number of classses is 2^3 = 8 +pi_hat = c(0.10, 0.10, 0.01, 0.13, 0.03, 0.26, 0.05, 0.31) +k = 3 + +# Compute attribute correlation matrix +postprocess_attribute_correlation(pi_hat, k) +} From f11eedc41f77f8789901afd18feee7165628f916 Mon Sep 17 00:00:00 2001 From: James Balamuta Date: Sat, 28 Aug 2021 16:17:46 -0500 Subject: [PATCH 3/5] Fix EDM print statement --- R/edm-model.R | 8 +++-- man/print.edm_model.Rd | 63 ++-------------------------------- man/print.edm_property_list.Rd | 63 ++-------------------------------- 3 files changed, 10 insertions(+), 124 deletions(-) diff --git a/R/edm-model.R b/R/edm-model.R index 1fd46a4..1de792e 100644 --- a/R/edm-model.R +++ b/R/edm-model.R @@ -26,7 +26,9 @@ new_edm_default_property_list = function(n, j, k, runtime, ..., class = characte #' Display Exploratory Diagnostic Model Properties #' #' Custom print method for viewing Exploratory Diagnostic Model properties. -#' @inherit base::print +#' +#' @param x an edm modeling object +#' @param ... additional arguments passed to or from other methods. #' @export print.edm_property_list = function(x, ...) { # Display call @@ -83,7 +85,9 @@ new_edm_model = function(chain, property_list, estimate = NA, ..., class = chara #' Print Exploratory Diagnostic Model Components #' #' Custom print method for viewing Exploratory Diagnostic Model components. -#' @inherit base::print +#' +#' @param x an edm modeling object +#' @param ... additional arguments passed to or from other methods. #' @export print.edm_model = function(x, ...) { print(x$property_list) diff --git a/man/print.edm_model.Rd b/man/print.edm_model.Rd index 3784b2b..de1fd49 100644 --- a/man/print.edm_model.Rd +++ b/man/print.edm_model.Rd @@ -7,69 +7,10 @@ \method{print}{edm_model}(x, ...) } \arguments{ -\item{x}{an object used to select a method.} +\item{x}{an edm modeling object} -\item{...}{further arguments passed to or from other methods.} +\item{...}{additional arguments passed to or from other methods.} } \description{ Custom print method for viewing Exploratory Diagnostic Model components. } -\details{ -The default method, \code{\link[base]{print.default}} has its own help page. - Use \code{\link{methods}("print")} to get all the methods for the - \code{print} generic. - - \code{print.factor} allows some customization and is used for printing - \code{\link[base]{ordered}} factors as well. - - \code{print.table} for printing \code{\link[base]{table}}s allows other - customization. As of R 3.0.0, it only prints a description in case of a table - with 0-extents (this can happen if a classifier has no valid data). - - See \code{\link[base]{noquote}} as an example of a class whose main - purpose is a specific \code{print} method. -} -\examples{ -require(stats) - -ts(1:20) #-- print is the "Default function" --> print.ts(.) is called -for(i in 1:3) print(1:i) - -## Printing of factors -attenu$station ## 117 levels -> 'max.levels' depending on width - -## ordered factors: levels "l1 < l2 < .." -esoph$agegp[1:12] -esoph$alcgp[1:12] - -## Printing of sparse (contingency) tables -set.seed(521) -t1 <- round(abs(rt(200, df = 1.8))) -t2 <- round(abs(rt(200, df = 1.4))) -table(t1, t2) # simple -print(table(t1, t2), zero.print = ".") # nicer to read - -## same for non-integer "table": -T <- table(t2,t1) -T <- T * (1+round(rlnorm(length(T)))/4) -print(T, zero.print = ".") # quite nicer, -print.table(T[,2:8] * 1e9, digits=3, zero.print = ".") -## still slightly inferior to Matrix::Matrix(T) for larger T - -## Corner cases with empty extents: -table(1, NA) # < table of extent 1 x 0 > -} -\references{ -Chambers, J. M. and Hastie, T. J. (1992) - \emph{Statistical Models in S.} - Wadsworth & Brooks/Cole. -} -\seealso{ -The default method \code{\link[base]{print.default}}, and help for the - methods above; further \code{\link[base]{options}}, \code{\link[base]{noquote}}. - - For more customizable (but cumbersome) printing, see - \code{\link[base]{cat}}, \code{\link[base]{format}} or also \code{\link[base]{write}}. - For a simple prototypical print method, see - \code{\link[tools]{.print.via.format}} in package \pkg{tools}. -} diff --git a/man/print.edm_property_list.Rd b/man/print.edm_property_list.Rd index dd319dc..4a8ce8c 100644 --- a/man/print.edm_property_list.Rd +++ b/man/print.edm_property_list.Rd @@ -7,69 +7,10 @@ \method{print}{edm_property_list}(x, ...) } \arguments{ -\item{x}{an object used to select a method.} +\item{x}{an edm modeling object} -\item{...}{further arguments passed to or from other methods.} +\item{...}{additional arguments passed to or from other methods.} } \description{ Custom print method for viewing Exploratory Diagnostic Model properties. } -\details{ -The default method, \code{\link[base]{print.default}} has its own help page. - Use \code{\link{methods}("print")} to get all the methods for the - \code{print} generic. - - \code{print.factor} allows some customization and is used for printing - \code{\link[base]{ordered}} factors as well. - - \code{print.table} for printing \code{\link[base]{table}}s allows other - customization. As of R 3.0.0, it only prints a description in case of a table - with 0-extents (this can happen if a classifier has no valid data). - - See \code{\link[base]{noquote}} as an example of a class whose main - purpose is a specific \code{print} method. -} -\examples{ -require(stats) - -ts(1:20) #-- print is the "Default function" --> print.ts(.) is called -for(i in 1:3) print(1:i) - -## Printing of factors -attenu$station ## 117 levels -> 'max.levels' depending on width - -## ordered factors: levels "l1 < l2 < .." -esoph$agegp[1:12] -esoph$alcgp[1:12] - -## Printing of sparse (contingency) tables -set.seed(521) -t1 <- round(abs(rt(200, df = 1.8))) -t2 <- round(abs(rt(200, df = 1.4))) -table(t1, t2) # simple -print(table(t1, t2), zero.print = ".") # nicer to read - -## same for non-integer "table": -T <- table(t2,t1) -T <- T * (1+round(rlnorm(length(T)))/4) -print(T, zero.print = ".") # quite nicer, -print.table(T[,2:8] * 1e9, digits=3, zero.print = ".") -## still slightly inferior to Matrix::Matrix(T) for larger T - -## Corner cases with empty extents: -table(1, NA) # < table of extent 1 x 0 > -} -\references{ -Chambers, J. M. and Hastie, T. J. (1992) - \emph{Statistical Models in S.} - Wadsworth & Brooks/Cole. -} -\seealso{ -The default method \code{\link[base]{print.default}}, and help for the - methods above; further \code{\link[base]{options}}, \code{\link[base]{noquote}}. - - For more customizable (but cumbersome) printing, see - \code{\link[base]{cat}}, \code{\link[base]{format}} or also \code{\link[base]{write}}. - For a simple prototypical print method, see - \code{\link[tools]{.print.via.format}} in package \pkg{tools}. -} From 5cbfd44fb903681a0f568b6bf93076e7335804a9 Mon Sep 17 00:00:00 2001 From: James Balamuta Date: Sun, 29 Aug 2021 00:59:27 -0500 Subject: [PATCH 4/5] To lower... --- R/postprocess.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/postprocess.R b/R/postprocess.R index 644625e..2e86fd1 100644 --- a/R/postprocess.R +++ b/R/postprocess.R @@ -24,7 +24,7 @@ postprocess_attribute_correlation = function(pi_hat, k, order = k) { alpha_profiles = t(GenerateAtable( nClass = 2 ^ k, - K, + k, M = 2, order = order )$DtoQtable) From f0ef503079e31e4f2435fedcf51a12b700e2a10e Mon Sep 17 00:00:00 2001 From: James Balamuta Date: Sun, 29 Aug 2021 01:18:16 -0500 Subject: [PATCH 5/5] Oh, how quickly we forget everything... --- NAMESPACE | 1 + R/postprocess.R | 5 +++++ man/postprocess_attribute_correlation.Rd | 4 ++++ 3 files changed, 10 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index 7ef8588..3ffa424 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -41,6 +41,7 @@ export(new_edm_summary) export(permutate_attribute_level_table) export(permutate_binary_matrix) export(permutate_theta_order) +export(postprocess_attribute_correlation) export(q_matrix) export(read_item_matrix) export(read_q_matrix) diff --git a/R/postprocess.R b/R/postprocess.R index 2e86fd1..929e41e 100644 --- a/R/postprocess.R +++ b/R/postprocess.R @@ -10,6 +10,11 @@ #' @param k Number of attributes #' @param order Number of interactions in the model #' +#' @return +#' A `matrix` containing the correlations. Note, as it is a correlation matrix, +#' the diagonal will be equivalent to 1. +#' +#' @export #' @examples #' #' # Sample calculation when K = 3 and number of classses is 2^3 = 8 diff --git a/man/postprocess_attribute_correlation.Rd b/man/postprocess_attribute_correlation.Rd index bebcef8..962b211 100644 --- a/man/postprocess_attribute_correlation.Rd +++ b/man/postprocess_attribute_correlation.Rd @@ -13,6 +13,10 @@ postprocess_attribute_correlation(pi_hat, k, order = k) \item{order}{Number of interactions in the model} } +\value{ +A \code{matrix} containing the correlations. Note, as it is a correlation matrix, +the diagonal will be equivalent to 1. +} \description{ Given the estimated latent class membership probabilities, we compute the correlation across classes.