Skip to content

textWordPrediction() scores each unique word by the mean value of x over the responses in which the word appears (weighted by how often it appears), predicts that mean from the word's decontextualized embedding with ridge regression (leave-one-out prediction = the plotted position), and computes p-values by permuting x across participants. Each word is compared with its own null distribution.

Usage

textWordPrediction(
  words,
  word_types_embeddings = word_types_embeddings_df,
  x,
  y = NULL,
  centre = "none",
  min_freq_words_test = 3,
  n_models = NULL,
  n_permutations = 10000,
  seed = 1003,
  case_insensitive = TRUE,
  text_remove = "[()]",
  ...
)

Arguments

words

Character vector (or single-column data frame) of text responses - one per participant.

word_types_embeddings

Word-type embeddings from textEmbed (with aggregation_from_tokens_to_word_types set, e.g., to "mean").

x

Numeric vector - the variable to relate the words to.

y

Optional second numeric variable; tested with its own, separate permutations.

centre

"none" (default) keeps the raw word means. "token_mean" subtracts the token-weighted mean of x from every word score, which separates word-specific signal from a verbosity effect (when x is related to how much people write, all frequent words are otherwise pushed in the direction of that association).

min_freq_words_test

(numeric) Only words occurring at least this many times are tested (default 3).

n_models

Deprecated; ignored with a warning. P-values are now based on full participant permutations.

n_permutations

Number of permutations of x (and y) across participants. Default 10000.

seed

Integer seed for reproducibility. Default 1003.

case_insensitive

Deprecated; matching is always lower-cased (ignored with a warning when set to FALSE).

text_remove

Regex pattern; words matching it are removed before testing (default "[()]").

...

Ignored (kept for backward compatibility).

Value

A named list:

word_data

A tibble with one row per tested word: words, n (token frequency), word_mean_value_x (mean of standardised x for the word), x_plotted (leave-one-out ridge prediction - the position), p_values_x (permutation p-value against the word's own null), p_z_x (normal approximation from the word's own null; no 1/(n_permutations + 1) floor); plus the y-equivalents when y is provided.

lambda_x

The ridge penalty chosen by leave-one-out error (lambda_y when y is provided).

loo_r

Correlation between the word means and their leave-one-out predictions (loo_r_y when y is provided).

The comment attribute on the output stores a human-readable description of the call parameters for reproducibility.

Details

Words are counted as tokens (NLTK tokenization, lower-cased), and word types from the embeddings are normalised for lookup (RoBERTa/SentencePiece markers and case are removed), so that words and embeddings match. Participants with a missing x (or y) are dropped with a message. x is standardised internally, so the plotted positions are centred and a plot split at 0 separates low from high scores.

This function is EXPERIMENTAL and its interface or defaults may still change.

Relation to textTrainRegression: earlier development versions fitted the word-level ridge model with textTrainRegression() (nested cross-validation, penalty re-tuned per fold). The current implementation fits the same kind of model – a ridge regression predicting each word's mean x from its embedding – but with the penalty chosen by exact leave-one-out error and the leave-one-out predictions computed in closed form. This matters for the p-values: with the penalty fixed, the whole pipeline is a fixed linear map of x, so each of the n_permutations participant permutations costs one matrix product instead of a full nested-CV refit. textTrainRegression() predictions are not a linear function of x (random fold splits, per-fold tuning), so using it here would make a full permutation test computationally prohibitive. Note also that the regression in this function runs across words (one observation per word type), whereas textTrainRegression() is designed for participant-level assessment models (one observation per person); the plotted positions from the two approaches agree closely in rank (Spearman correlation about .95 in the validation runs, see dev/permutation_handoff/).

Examples

if (FALSE) { # \dontrun{
library(text)

# --- Step 1: embed the text column (word-type embeddings are needed)
embeddings <- textEmbed(
  Language_based_assessment_data_8["harmonywords"],
  aggregation_from_tokens_to_word_types = "mean"
)

# --- Step 2: run textWordPrediction
result <- textWordPrediction(
  words                 = Language_based_assessment_data_8$harmonywords,
  word_types_embeddings = embeddings$word_types,
  x                     = Language_based_assessment_data_8$hilstotal,
  n_permutations        = 10000
)

# --- Step 3: inspect word-level scores
result$word_data
} # }

GitHub