sentopics_sentiment {sentopics}R Documentation

Internal sentiment

Description

Compute, extract or replace the internal sentiment of a sentopicmodel. 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 sentopicmodel created from the LDA(), JST(), rJST() or sentopicmodel() 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 sentopicmodel 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, a sentiment is computed for each topic, resulting in K sentiment values per document. In that case, the .sentiment column is an average of the K sentiment values, weighted by they respective topical proportions.

Value

A data.frame with the stored sentiment per document.

Methods

The function accepts two methods of computing sentiment:

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.

Author(s)

Olivier Delmarcelle

See Also

Other sentopics helpers: sentopics_date(), sentopics_labels()

Examples

# example dataset already contains ".sentiment" docvar
docvars(ECB_press_conferences_tokens)
# sentiment is automatically stored in the sentopicmodel object
lda <- LDA(ECB_press_conferences_tokens)
sentopics_sentiment(lda)

# sentiment can be removed or modified by the assignment operator
sentopics_sentiment(lda) <- NULL
sentopics_sentiment(lda) <- docvars(ECB_press_conferences_tokens, ".sentiment")

# 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, override = TRUE) # replace existing 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, override = TRUE)

[Package sentopics version 0.7.3 Index]