sentiment-functions: Sentiment functions

sentiment-functionsR Documentation

Sentiment functions

Description

Functions for computing sentiment, for textstat_polarity(). Each function takes an input dfm with fixed feature names (see Details), and returns a sparse Matrix with a single column representing the results of the sentiment calculation.

sent_logit is log(\frac{pos}{neg}).

sent_abspropdiff is \frac{pos - neg}{N}, where N is the total number of all features in a document.

sent_relpropdiff is \frac{pos - neg}{pos + neg}.

Usage

sent_logit(x, smooth = 0.5)

sent_abspropdiff(x)

sent_relpropdiff(x)

Arguments

x

a dfm that has the following required feature names: pos, neg, neut, and other

smooth

additional smoothing function added to pos and neg before logarithmic transformation

Details

User supplied functions must take x and optional additional arguments, such as smooth for a smoothing constant for the logit scaling function. feature names for the sentiment categories pos, neg, neut, and other. (The other category is only required when a scaling function needs the count of non-sentiment associated features.)

Additional arguments may be passed via ..., such as smooth for the logit scale.

Value

a sparse Matrix object of documents by sentiment score, where the sentiment score is the only column. (Its name is unimportant as this will not be used by textstat_polarity().)

References

Lowe, W., Benoit, K. R., Mikhaylov, S., & Laver, M. (2011). Scaling Policy Preferences from Coded Political Texts. Legislative Studies Quarterly, 36(1), 123–155. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1111/j.1939-9162.2010.00006.x")}

Examples

library("quanteda")
dfmat <- c("pos pos pos neg pos pos", "neg neg pos pos pos") |>
  tokens() |>
  dfm()
sent_logit(dfmat)
sent_abspropdiff(dfmat)

# user-supplied function
my_sent_fn <- function(x) (x[, "pos"] - x[, "neg"]) / rowSums(x) * 100
my_sent_fn(dfmat)

# user supplied function with fixed weights and using neutral category
dfmat2 <- c("pos pos neut neg neut pos", "neg neg neut neut pos") |>
  tokens() |>
  dfm()
my_sent_fn2 <- function(x) (x[, "pos"]*3 + x[, "neut"]*2 + x[, "neg"]*1)/3
my_sent_fn2(dfmat2)

quanteda/quanteda.sentiment documentation built on Feb. 26, 2024, 12:42 a.m.