posterior: Draw from the marginal posteriors of a tidylda topic model

Description Usage Arguments Details Value References Examples

View source: R/posterior.tidylda.R

Description

These functions are used to sample from the marginal posteriors of a tidylda topic model. This is useful for quantifying uncertainty around the parameters of beta or theta.

Usage

1
2
3
4
5
6
7
posterior(x, ...)

## S3 method for class 'tidylda'
posterior(x, ...)

## S3 method for class 'tidylda_posterior'
generate(x, matrix, which, times, ...)

Arguments

x

For posterior, an object of class tidylda. For generate, an object of class tidylda_posterior obtained by a call to posterior.

...

Other arguments, currently not used.

matrix

A character of either 'theta' or 'beta', indicating from which matrix to draw posterior samples.

which

Row index of theta, for document, or beta, for topic, from which to draw samples. which may also be a vector of indices.

times

Integer number of samples to draw.

Details

To sample from the marginal posteriors of a model, you must first make a call to posterior and then a call to generate.

posterior takes an object of class tidylda and constructs an object of class tidylda_posterior which contains two matrices. The rows of these matrices are Dirichlet parameters used to sample from the marginal posteriors of theta and beta.

generate takes an object of class tidylda_posterior and samples from the marginal posterior of the parameters specified by the matrix argument.

Value

posterior returns an object of class tidylda_posterior.

generate returns a tibble with one row per parameter per sample.

Returns a data frame where each row is a single sample from the posterior. Each column is the distribution over a single parameter. The variable var is a facet for subsetting by document (for theta) or topic (for beta).

References

Heinrich, G. (2005) Parameter estimation for text analysis. Technical report. http://www.arbylon.net/publications/text-est.pdf

Examples

 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
# load some data
data(nih_sample_dtm)

# fit a model
set.seed(12345)

m <- tidylda(
  data = nih_sample_dtm[1:20, ], k = 5,
  iterations = 200, burnin = 175
)

# construct a posterior object
p <- posterior(m)

# sample from the marginal posterior corresponding to topic 1
t1 <- generate(
  x = p,
  matrix = "beta",
  which = 1,
  times = 100  
)

# sample from the marginal posterior corresponding to document 5
d5 <- generate(
  x = p,
  matrix = "theta",
  which = 5,
  times = 100
)

tidylda documentation built on Dec. 11, 2021, 10:02 a.m.