sentopics_sentiment: Internal sentiment

View source: R/timeSeries.R

sentopics_sentimentR Documentation

Internal sentiment

Description

Compute, extract or replace the internal sentiment of a sentopicsmodel. The internal sentiment is used to create time series using the functions sentiment_series() or sentiment_topics(). If the input model contains a sentiment layer, sentiment can be computed directly from the output of the model. Otherwise, sentiment obtained externally should be added for each document.

Usage

sentopics_sentiment(
  x,
  method = c("proportional", "proportionalPol"),
  override = FALSE,
  quiet = FALSE,
  include_docvars = FALSE
)

sentopics_sentiment(x) <- value

Arguments

x

a sentopicsmodel created from the LDA(), JST(), rJST() or sentopicsmodel() function

method

the method used to compute sentiment, see "Methods" below. Ignored if an internal sentiment is already stored, unless override is TRUE.

override

by default, the function computes sentiment only if no internal sentiment is already stored within the sentopicsmodel object. This avoid, for example, erasing externally provided sentiment. Set to TRUE to force computation of new sentiment values. Only useful for models with a sentiment layer.

quiet

if FALSE, print a message when internal sentiment is found.

include_docvars

if TRUE the function will return all docvars stored in the internal tokens object of the model

value

a numeric vector of sentiment to input into the model.

Details

The computed sentiment varies depending on the model. For LDA, sentiment computation is not possible.

For JST, the sentiment is computed on a per-document basis according to the document-level sentiment mixtures.

For a rJST model, sentiment is computed from the sentiment probabilities conditional on each topic, resulting in K sentiment values per document in the .s_ columns. The .sentiment column is an average of these values, weighted once by their respective topic proportions.

Value

A data.frame with the stored sentiment per document.

Methods

The function accepts two methods of computing sentiment:

  • proportional computes the difference between the estimated positive and negative proportions for each document (and possibly each topic).

    positive - negative

  • proportionalPol computes the difference between positive and negative proportions, divided by the sum of positive and negative proportions. As a result, the computed sentiment lies within the (-1;1) interval.

    \frac{positive - negative}{positive + negative}

Both methods will lead to the same result for a JST model containing only negative and positive sentiments.

Note

The internal sentiment is stored internally in the docvars of the topic model. This means that sentiment may also be accessed through the docvars() function, although this is discouraged.

When sentiment is computed, the result is cached by modifying the model's tokens docvars. If a .sentiment docvar already exists and override = FALSE, the value is returned without recomputing or modifying the model.

Author(s)

Olivier Delmarcelle

See Also

Other sentopics helpers: sentopics_date(), sentopics_labels()

Examples

# sentiment from an external source can be added to the model
lda <- LDA(ECB_press_conferences_tokens)
scores <- compute_PicaultRenault_scores(ECB_press_conferences)
sentopics_sentiment(lda) <- scores[names(lda$tokens), "EC"]

# using the helper function extracts the stored sentiment
sentopics_sentiment(lda)

# internally, the sentiment is stored in the docvars of the tokens
docvars(lda$tokens, ".sentiment")

# sentiment can be removed or modified by the assignment operator
sentopics_sentiment(lda) <- NULL
sentopics_sentiment(lda) <- scores[names(lda$tokens), "EC"]

# for JST models, sentiment can be computed from the output of the model
jst <- JST(ECB_press_conferences_tokens, lexicon = LoughranMcDonald)
jst <- fit(jst, 100)
sentopics_sentiment(jst) # compute sentiment

## for rJST models one sentiment value is computed by topic
rjst <- rJST(ECB_press_conferences_tokens, lexicon = LoughranMcDonald)
rjst <- fit(rjst, 100)
sentopics_sentiment(rjst)

sentopics documentation built on Sept. 10, 2026, 5:10 p.m.