Text Regression — Predicting Numbers from Text

Machine Learning
Type
Supervised regression on text features

What does it do?

Text regression predicts a continuous numeric target from features extracted from documents, such as TF-IDF scores, embeddings, or n-gram counts, which serve as the independent variables.

It draws on the text-as-data framework surveyed by Gentzkow, Kelly and Taddy (2019), which treats text as an input to economic and statistical analysis. The method is applied in social-science, economics and finance settings where a numeric quantity, such as a price or a rating, must be estimated from written content.

The intuition behind the method

A regression model needs numeric predictors, but the raw material here is text. Each document is turned into a vector of numbers that reflects its wording, and a regression of the numeric target is then fitted on those numbers. Rather than hand-coding what a document means, the model lets the words, weighted by their features, drive the prediction of a continuous quantity. The standard form assumes that the target is a linear function of the chosen features; a nonlinear relation needs a different model or a different feature design.

History

Text regression can be read as one application of the text-as-data programme reviewed by Gentzkow, Kelly and Taddy (2019), which describes text as an input to economic research and surveys the statistical methods used on it. Taddy (2013) is a case study that scores Twitter posts for positive, negative or neutral sentiment directed toward particular U.S. politicians, using multinomial inverse regression with attention to the design of the scoring sample. Its target is categorical, so it illustrates text-based inference rather than regression on a continuous outcome.

When to use it

Choose text regression when your predictor is text and your outcome is a continuous number you want to estimate from it, and when your document sample is large enough for the number of features you use. Expect to vectorise the text first, and plan for dimensionality reduction because text feature spaces are wide. Do not use it when the target is a category, such as positive, negative or neutral labels; text classification is the matching approach for that. Do not use it with a sample that is small relative to the feature count, because coefficient estimates become unstable. The predictors are word-derived counts or weights, so the text itself need not follow a normal distribution, although the usual assumptions about regression errors still apply.

Limitations and considerations

  • Coefficient estimates need a sample large enough for the number of features. With wide feature spaces, coefficients can be unstable even when the document count seems adequate.
  • Text must be vectorised first, and the wide feature matrix usually calls for dimensionality reduction, which adds choices that affect the results.
  • Predictive quality depends on whether the chosen features carry signal about the numeric target; wording unrelated to the outcome gives poor predictions whatever the model fit.
  • The standard form assumes a linear relation between the features and the target, so nonlinear patterns are missed unless the model or the feature design is changed.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Predicts a continuous outcome directly from document wording, without a manually coded measure of document content.
  • Accepts several feature representations, including TF-IDF, embeddings and n-grams, so the representation can be matched to the corpus and the signal being sought.
  • The sparse, high-dimensional features that text produces can be entered directly, without requiring the predictors to be normally distributed.

Applications

  • Estimating numeric ratings or scores from review and feedback text.
  • Predicting financial or market quantities from news, filings and disclosures.
  • Linking written content to measured economic or policy outcomes.

How it works

How it works

1

Each document is converted into a numeric vector so that its wording becomes regression input. Common choices are TF-IDF weights, word or document embeddings, and n-gram counts, and the choice determines which aspects of wording the regression can use.

2

Text vectors are usually very high-dimensional and sparse, so the feature space is often compressed or pruned before fitting. Common options include removing very rare or very frequent terms, selecting features by a relevance criterion, and projecting the matrix onto a lower-dimensional space such as principal components or a truncated singular value decomposition. Removing redundant or rarely occurring features reduces the chance that the model memorises idiosyncratic wordings in the training documents instead of learning links that generalise.

3

A regression model is trained to map the text features onto the continuous target. The standard form is linear in the features, and the coefficients are estimated by minimising a penalised least-squares objective: the squared prediction error plus a regularisation term that shrinks coefficients toward zero, as in ridge or lasso regression. Regularised linear models are a common choice for the wide, sparse matrices that text produces.

4

The fitted model is scored on documents withheld from training, using error measures suited to continuous predictions: root mean squared error (RMSE) and mean absolute error (MAE) on the held-out documents, with R-squared as a summary of predictive fit. Cross-validation or a fixed test split can be used, and the same split should be used to compare alternative models. R-squared on held-out data measures predictive fit and should not be read as a causal or explanatory effect. Inspecting the largest prediction errors shows whether the model fails on particular kinds of text before its output is relied on.

Common pitfalls

  • Fitting a very wide feature matrix without dimensionality reduction invites overfitting to rare wordings.
  • Too few documents produce unstable coefficients that may not hold on new text.
  • A vectoriser or model fitted on one corpus or domain can degrade when applied to another, so refit on the target text.
  • Training-set fit says little about predictive quality; evaluate on held-out documents and inspect the largest errors.
  • Fitting the vectoriser, any dimensionality reduction or feature selection on the full corpus before splitting leaks information from the test documents into training. These steps should be fitted on training documents only and then applied to held-out data.
  • Learned word weights describe association with the target in the training corpus, not causal effects of wording, and they can reflect bias in how the text or the outcome was recorded.

Frequently asked questions

What kind of target does text regression predict?

A continuous numeric variable, such as a price or a rating expressed on a numeric scale. If the target is a set of categories, such as positive, negative or neutral labels, text classification is the matching method instead.

Which text features should I use as predictors?

TF-IDF scores, word or document embeddings, and n-gram counts are all standard choices. The best representation depends on the corpus and on the signal you expect the wording to carry about the target.

How much data do I need?

No fixed minimum applies across corpora. Adequate size depends on the number of features relative to the number of documents, and is best checked with error on held-out documents and with the stability of coefficients across resamples. Because text features are high-dimensional, dimensionality reduction is often advisable.

Why is dimensionality reduction recommended?

Text vectorisation produces very wide, sparse feature matrices. Reducing the dimensionality removes redundant features, stabilises the fit, and helps the model generalise instead of memorising rare wordings.

Related methods

Sources

  1. Gentzkow, M., Kelly, B. & Taddy, M. (2019). Text as Data. Journal of Economic Literature, 57(3), 535-574.

    Open DOI reference
  2. Taddy, M. (2013). Measuring Political Sentiment on Twitter: Factor Optimal Design for Multinomial Inverse Regression. Technometrics, 55(4), 415-425.

    Open DOI reference