Skip to content

Instantly share code, notes, and snippets.

View trinker's full-sized avatar

Tyler Rinker trinker

View GitHub Profile
```r
p_load_gh("jcheng5/bubbles", "trinker/qdapTools")
x <- runif(26)
colfunc <- colorRampPalette(c("white", "red"))
cols <- colfunc(length(sort(unique(x))))
bubbles(value = x, label = LETTERS,
@trinker
trinker / crosstabs with tidyr and dplyr
Created December 18, 2015 22:32
crosstabs with tidyr and dplyr
if (!require("pacman")) install.packages("pacman")
pacman::p_load(dplyr, tidyr, wakefield)
set.seed(10)
dat <- r_data_frame(n = 10000,
race,
age,
sex
)
@trinker
trinker / topicmodels2LDAvis
Created December 19, 2015 02:42
Convert topicmodels to LDAvis
#' Transform Model Output for Use with the LDAvis Package
#'
#' Convert a \pkg{topicmodels} output into the JSON form required by the \pkg{LDAvis} package.
#'
#' @param model A \code{\link[]{topicmodel}} object.
#' @param \ldots Currently ignored.
#' @seealso \code{\link[LDAvis]{createJSON}}
#' @export
#' @examples
#' \dontrun{
@trinker
trinker / optimal_k
Last active December 21, 2015 04:23
Find the optimal number of topics (k) in a topicmodel
#' Find Optimal Number of Topics
#'
#' Iteratively produces models and then compares log likelihoods in a graphical output.
#'
#' @param x A \code{\link[tm]{DocumentTermMatrix}}.
#' @param max.k Maximum number of topics to fit (start small [i.e., default of 30] and add as necessary).
#' @param \ldots Ignored.
#' @return Returns the \code{\link[base]{data.frame}} of k (number of topics) and the associated log likelihood.
#' @references \url{http://stats.stackexchange.com/a/25128/7482}
#' @keywords k topicmodel
@trinker
trinker / optimal_k 2
Last active July 28, 2021 09:25
Find the optimal number of topics in a topic model using the harmonic mean of the log likelihood
#' Find Optimal Number of Topics
#'
#' Iteratively produces models and then compares of the harmonic mean of the log likelihoods in a graphical output.
#'
#' @param x A \code{\link[tm]{DocumentTermMatrix}}.
#' @param max.k Maximum number of topics to fit (start small [i.e., default of 30] and add as necessary).
#' @param burnin Object of class \code{"integer"}; number of omitted Gibbs iterations at beginning, by default equals 0.
#' @param iter Object of class \code{"integer"}; number of Gibbs iterations, by default equals 2000.
#' @param keep Object of class \code{"integer"}; if a positive integer, the log-likelihood is saved every keep iterations.
#' @param method The method to be used for fitting; currently \code{method = "VEM"} or \code{method= "Gibbs"} are supported.
@trinker
trinker / optimal_k.R
Last active May 17, 2017 10:44
Find the optimal number of topics (k) in a LDA topicmodel
#' Find Optimal Number of Topics
#'
#' Iteratively produces models and then compares the harmonic mean of the log
#' likelihoods in a graphical output.
#'
#' @param x A \code{\link[tm]{DocumentTermMatrix}}.
#' @param max.k Maximum number of topics to fit (start small [i.e., default of
#' 30] and add as necessary).
#' @param harmonic.mean Logical. If \code{TRUE} the harmonic means of the
#' log likelihoods are used to determine k (see
@trinker
trinker / radar_plot
Last active February 28, 2016 14:12
Radar Plot
#' Business Radar Plot
#'
#' Given a \code{\link[base]{data.frame}} with \code{problems}, \code{category}, \code{importance} columns, create a radar plot of problems arranged in a military style radar sequence.
#'
#' @param data A \code{\link[base]{data.frame}} with the following columns:
#' \item{problems}{A character vector listing the problems}
#' \item{category}{A character vector grouping the problems}
#' \item{importance}{The time perceived importance of each item; an integer value between 1-100 with 100 being the most important and 1 being least}
#' \item{perceived}{An optional column of organization perceived importance used for point size; takes a value between 1-4;if \code{NULL} \code{importance} column will be used to generate this value}
#' @param legend.title A title for the legend.
library(idbr)
library(ggplot2)
library(gganimate)
library(tweenr)
library(countrycode)
library(dplyr)
idb_api_key("Your API key goes here")
africa_fips <- countrycode(c('Nigeria', 'Uganda', 'Tanzania', 'Ghana'),
@trinker
trinker / Comparing list of vector to count table
Last active March 24, 2016 13:23
Comparing list of vector to count table
G <- list(c("MD", "DT", "NN", "VB", "VBG", "TO", "POS"), c("DT", "NN",
"JJ", "RB"), c("RB", "TO", "PRP"), c("VBZ", "PRP", "VBG", "RB",
"TO", "NN"), c("NN", "NN"))
G <- rep(G, 1000)
library(qdapTools)
@trinker
trinker / favicon.R
Created September 22, 2016 14:49 — forked from jeroen/favicon.R
Generate favicon with magick
library(magick)
library(magrittr)
# Convert to 'ico' format
image_read("https://www.r-project.org/logo/Rlogo.png") %>%
image_scale("32x32!") %>% # remove the "!" after 32x32 to scale proportionally
image_write("favicon.ico", format = "ico")
# Favicon can also be png/gif