Skip to main content
v2026.11,986 entries · CC-BY 4.0

MASS R Package: Install, Core Functions and Examples

What the MASS package in R contains, how to load it, and the manual’s examples for glm.nb, polr, rlm, lda and stepAIC.

Written and maintained by CASRAI Editorial Board

Published

On this page

MASS is an R package of functions and datasets that supports the book Modern Applied Statistics with S by Venables and Ripley (4th edition, 2002). It is one of R’s recommended packages, so on a standard R installation you load it with library(MASS) rather than downloading it. Researchers use it mostly for negative binomial regression (glm.nb), ordinal logistic regression (polr), robust regression (rlm), linear discriminant analysis (lda), stepwise model selection by AIC (stepAIC) and a set of classic teaching datasets such as Boston and birthwt. This page is a reference for those functions, with commands taken from the package’s own reference manual. If you searched for the “mass package in r”, this is that package.

Ask CASRAI · free to tryQuestion about this guide? Ask CASRAI answers with linked sources. 2 free questions a day, no account.

Ask your first question freeSubscribe

What it is used for in research

The CRAN package page describes MASS as “functions and datasets that support” the Venables and Ripley book. It is authored by Brian Ripley and William Venables, with contributions from Douglas Bates, Kurt Hornik, Albrecht Gebhardt and David Firth (credited for the polr support), and Brian Ripley is the maintainer. The current CRAN release is 7.3-66, published 2026-07-15, under GPL-2 or GPL-3. It declares a dependency on R 4.4.0 or later and on the base packages grDevices, graphics, stats and utils, imports methods, and suggests lattice, nlme, nnet and survival.

In a research workflow the package fills gaps that base R’s lm() and glm() leave open:

  • Count outcomes with extra variation. glm.nb modifies glm() so that it also estimates the extra parameter theta of a negative binomial model. The CASRAI guide to negative binomial regression and overdispersion covers when that model is appropriate; this page covers the function call.
  • Ordered categories. polr fits a logistic or probit regression to an ordered factor response. With its default method it is proportional odds logistic regression.
  • Outliers and heavy-tailed errors. rlm fits a linear model by robust M-estimation using iterated re-weighted least squares.
  • Classification. lda performs linear discriminant analysis; see the CASRAI guide to linear discriminant analysis for the method itself.
  • Model selection. stepAIC runs stepwise selection with an AIC penalty on lm, glm and similar fits.
  • Simulation and distribution fitting. mvrnorm samples from a multivariate normal distribution, and fitdistr fits univariate distributions by maximum likelihood; boxcox computes the profile log-likelihood for the Box-Cox transformation parameter lambda.

The data table below the guide lists the key arguments and returned components of the functions most used in research, each taken from the reference manual.

Install or access

MASS is a recommended package. The R FAQ states that the recommended packages, which include MASS alongside survival, nlme, lattice, Matrix, boot, mgcv and others, are “to be included in all binary distributions of R”. For most users the only step is to load it in each session:

library(MASS)

If your R build does not include it, or you want to confirm the installed version, the general procedure is the same as for any CRAN package: install.packages("MASS"), then packageVersion("MASS"). The CASRAI guide to installing R packages explains mirrors, library paths and troubleshooting for that step. Because the CRAN release depends on R 4.4.0 or later, an older R installation needs an upgrade before the current release will install.

Step-by-step: from a model to a selected model

The following sequence reuses the example in the manual’s stepAIC entry, which builds a tidy data frame from the birthwt dataset (189 rows and 10 columns, collected at Baystate Medical Center, Springfield, Massachusetts, in 1986) and then selects a logistic model by AIC. Treat the data as a teaching dataset: it shows the workflow, not a clinical conclusion.

  1. Load the package. library(MASS) makes the functions and the bundled datasets available.
  2. Recode the variables. The manual’s code converts race to a labelled factor, turns previous premature labours into a yes/no factor ptd, collapses first-trimester visits ftv to “0”, “1” and “2+”, and turns smoke, ht and ui into logical values:
    bwt <- with(birthwt, {
      race <- factor(race, labels = c("white", "black", "other"))
      ptd <- factor(ptl > 0)
      ftv <- factor(ftv)
      levels(ftv)[-(1:2)] <- "2+"
      data.frame(low = factor(low), age, lwt, race, smoke = (smoke > 0),
                 ptd, ht = (ht > 0), ui = (ui > 0), ftv)
    })
  3. Set the contrasts and fit the full model. The manual fits a binomial GLM with every variable:
    options(contrasts = c("contr.treatment", "contr.poly"))
    birthwt.glm <- glm(low ~ ., family = binomial, data = bwt)
  4. Run the stepwise search. birthwt.step <- stepAIC(birthwt.glm, trace = FALSE) searches in both directions by default, or backward when no scope is given.
  5. Read the path. birthwt.step$anova lists the steps taken. The manual’s next example widens the search with stepAIC(birthwt.glm, ~ .^2 + I(scale(age)^2) + I(scale(lwt)^2), trace = FALSE), which adds two-way interactions and quadratic terms for age and mother’s weight.

Other tasks, with the manual’s own examples

  • Negative binomial counts (the quine dataset): quine.nb1 <- glm.nb(Days ~ Sex/(Age + Eth*Lrn), data = quine), then update() to add terms and anova(quine.nb1, quine.nb2, quine.nb3) to compare nested fits.
  • Ordinal outcome (the Copenhagen housing survey, 72 rows): house.plr <- polr(Sat ~ Infl + Type + Cont, weights = Freq, data = housing), then summary(house.plr, digits = 3).
  • Robust regression (the stackloss data): summary(rlm(stack.loss ~ ., stackloss)), or with a different weighting function, rlm(stack.loss ~ ., stackloss, psi = psi.bisquare).
  • Discriminant analysis (the iris measurements rebuilt as Iris): z <- lda(Sp ~ ., Iris, prior = c(1,1,1)/3, subset = train), then predict(z, Iris[-train, ])$class to classify the held-out half.

Output and how to read it

Each function returns an object whose components are documented in the manual’s Value sections, which is the quickest route to extracting numbers for a results table rather than copying them from printed output.

  • glm.nb returns a negbin object that inherits from glm and lm, with the added components theta, SE.theta and twologlik. Report theta with its standard error alongside the coefficients, because it is the quantity that distinguishes this model from a Poisson fit.
  • polr returns coefficients for the predictors and zeta for the intercepts (the cut points between ordered categories), plus deviance, fitted.values, convergence and niter. A Hessian component is present only when Hess = TRUE.
  • rlm returns the usual lm components plus s (the scale estimate), w (the final weights, which show which observations were down-weighted), psi, conv, converged and wresid.
  • lda returns an object with prior, means, scaling, svd and N. With CV = TRUE it returns only class and posterior, the classifications and posterior probabilities from cross-validation.
  • stepAIC returns the final model with an extra anova component recording the steps taken, and a keep component if you supplied keep=.

Stepwise selection deserves a caution in a methods section. The manual notes that only k = 2 gives the genuine AIC, while k = log(n) is “sometimes referred to as BIC or SBC”, so state which penalty you used. How you describe a data-driven selection in a paper is a question for your study design: see the CASRAI guide to the glm function in R for the underlying fit.

Troubleshooting

  • The package will not load or install. The CRAN release requires R 4.4.0 or later. Check R.version.string first, then follow the install guide linked above.
  • Summary shows no standard errors for polr. The Hessian is stored only when Hess = TRUE. The manual’s examples refit with update(house.plr, Hess = TRUE) before calling profile() and confint().
  • Different contrasts give different coefficients. The manual’s examples call options(contrasts = c("contr.treatment", "contr.poly")) first. Set the contrasts explicitly and record them in your analysis script so a reader can reproduce the coding.
  • rlm gives different answers with different psi functions. The manual notes that Huber’s psi gives a unique solution, while Hampel and bisquare can have multiple local minima, so a good starting point is desirable (the example uses init = "lts" with psi.hampel). Choosing method = "MM" gives a high breakdown point, but case weights are not supported with it.
  • stepAIC searches fewer or more models than expected. The search space is set by scope: a single formula is the upper model with an empty lower model, or a list gives both. With no scope the default direction is backward.
  • Reproducibility. Record packageVersion("MASS") in your methods or supplementary files, and cite the package; the CASRAI guide to citing software and R packages shows the pattern, and the CRAN page links the package’s citation entry. For the bundled datasets, see datasets in R packages.

Free alternatives

MASS is free software under GPL-2 or GPL-3, as are its base-R neighbours. The reference manual itself points to the closest base functions for three of its tools:

MASS function Closest base-R function named in the manual What the MASS version adds
glm.nb glm() The manual describes it as a modified glm() that also estimates theta for a negative binomial GLM.
rlm lm (listed under See Also, with lqs) Robust M-estimation by iterated re-weighted least squares, with Huber, Hampel and bisquare weighting.
stepAIC step (listed under See Also, with addterm and dropterm) A scope argument given as an upper model or an upper and lower pair, and an anova record of the steps.

For the tasks where base R has no direct counterpart, such as polr or lda, you would look to other CRAN packages; compare their documentation against the arguments above before switching, and keep the model specification identical when you do.

Frequently asked questions

What is the MASS package in R?

It is the CRAN package whose functions and datasets support the book Modern Applied Statistics with S by Venables and Ripley. The current release on CRAN is 7.3-66, maintained by Brian Ripley.

Is MASS installed with R?

The R FAQ lists MASS among the recommended packages that are to be included in all binary distributions of R, so library(MASS) normally works without a separate install.

Which R version does MASS need?

The CRAN page for version 7.3-66 lists R 4.4.0 or later as a dependency.

What licence is MASS released under?

GPL-2 or GPL-3, as stated on its CRAN page.

Which MASS functions do researchers use most?

The ones with no direct base-R equivalent in everyday work: glm.nb for overdispersed counts, polr for ordinal outcomes, rlm for robust regression, lda for discriminant analysis and stepAIC for AIC-based selection.

Sources

  • CRAN, MASS: Support Functions and Datasets for Venables and Ripley’s MASS, package page (version 7.3-66), https://cran.r-project.org/web/packages/MASS/index.html, read 2026-11-15.
  • CRAN, MASS reference manual, https://cran.r-project.org/web/packages/MASS/refman/MASS.html, read 2026-11-15.
  • R Core Team, R FAQ, question 5.1 (add-on packages, recommended packages), https://cran.r-project.org/doc/FAQ/R-FAQ.html, read 2026-11-15.

Data: MASS functions used in research: key arguments and returned components

Compiled from the MASS reference manual on CRAN (version 7.3-66): for each function commonly used in research, its documented default or choice of method, the components it returns and the example dataset the manual uses.

Period covered: 2026.

MASS functions used in research: key arguments and returned components
FunctionWhat it fits or doesDocumented default or choicesDocumented returned componentsManual example data
glm.nbNegative binomial GLM that also estimates thetalink = log; method = glm.fittheta, SE.theta, twologlik (negbin object inheriting glm and lm)quine
polrLogistic or probit regression for an ordered factor responsemethod = logistic (proportional odds); also probit, loglog, cloglog, cauchit; Hess = FALSEcoefficients, zeta, deviance, fitted.values, convergence, niter, Hessian (if Hess is true)housing
rlmRobust linear regression by iterated re-weighted least squaresmethod = M (or MM); psi = psi.huber; wt.method = inv.varlm components plus s, w, psi, conv, converged, wresidstackloss
ldaLinear discriminant analysisprior = proportions; tol = 1e-4; CV = FALSEprior, means, scaling, svd, N (class and posterior if CV = TRUE)iris (as Iris)
stepAICStepwise model selection by AICdirection = both (backward if scope missing); k = 2; trace = 1; steps = 1000anova, keep (if keep= supplied)birthwt, quine

Sources

Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about MASS R Package: Install, Core Functions and Examples

2 free questions a day, no account.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →