| sentopics_sentiment | R Documentation |
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.
sentopics_sentiment(
x,
method = c("proportional", "proportionalPol"),
override = FALSE,
quiet = FALSE,
include_docvars = FALSE
)
sentopics_sentiment(x) <- value
x |
a |
method |
the method used to compute sentiment, see "Methods" below.
Ignored if an internal sentiment is already stored, unless |
override |
by default, the function computes sentiment only if no
internal sentiment is already stored within the |
quiet |
if |
include_docvars |
if |
value |
a numeric vector of sentiment to input into the model. |
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.
A data.frame with the stored sentiment per document.
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.
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.
Olivier Delmarcelle
Other sentopics helpers:
sentopics_date(),
sentopics_labels()
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.