#' @title Fit a Correlated Topic Model
#' @description A wrapper for the \link[topicmodels]{CTM} function based on
#' Blei's original code that returns a nicely-formatted topic model.
#' @param dtm A document term matrix of class \code{dgCMatrix}
#' @param k Number of topics
#' @param return_all Logical. Do you want the raw results of the underlying
#' function returned along with the formatted results? Defaults to \code{TRUE}.
#' @param ... Other arguments to pass to \link[topicmodels]{CTM}.
#' @return Returns a list with a minumum of two objects, \code{phi} and
#' \code{theta}. The rows of \code{phi} index topics and the columns index tokens.
#' The rows of \code{theta} index documents and the columns index topics.
#' @examples
#' # Load a pre-formatted dtm
#' data(nih_sample_dtm)
#'
#' # Fit a CTM model on a sample of documents
#' model <- FitCtmModel(dtm = nih_sample_dtm[ sample(1:nrow(nih_sample_dtm) , 10) , ],
#' k = 3, return_all = FALSE)
#' @export
FitCtmModel <- function(dtm, k, return_all = TRUE, ...){
model <- topicmodels::CTM(x = dtm, k = k, control = list(...))
theta <- model@gamma
rownames(theta) <- model@documents
colnames(theta) <- paste("t", 1:ncol(theta), sep = "_")
phi <- exp(model@beta)
colnames(phi) <- model@terms
rownames(phi) <- colnames(theta)
result <- list(theta = theta, phi = phi)
if(return_all){
result <- c(result, etc = model)
}
return(result)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.