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/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/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/R/postprocess.R b/R/postprocess.R new file mode 100644 index 0000000..929e41e --- /dev/null +++ b/R/postprocess.R @@ -0,0 +1,59 @@ + +# 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 +#' +#' @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 +#' 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..962b211 --- /dev/null +++ b/man/postprocess_attribute_correlation.Rd @@ -0,0 +1,32 @@ +% 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} +} +\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. +} +\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) +} 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}. -} 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) {