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.
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.nbmodifiesglm()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.
polrfits 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.
rlmfits a linear model by robust M-estimation using iterated re-weighted least squares. - Classification.
ldaperforms linear discriminant analysis; see the CASRAI guide to linear discriminant analysis for the method itself. - Model selection.
stepAICruns stepwise selection with an AIC penalty on lm, glm and similar fits. - Simulation and distribution fitting.
mvrnormsamples from a multivariate normal distribution, andfitdistrfits univariate distributions by maximum likelihood;boxcoxcomputes 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.
- Load the package.
library(MASS)makes the functions and the bundled datasets available. - Recode the variables. The manual’s code converts
raceto a labelled factor, turns previous premature labours into a yes/no factorptd, collapses first-trimester visitsftvto “0”, “1” and “2+”, and turnssmoke,htanduiinto 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) }) - 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) - Run the stepwise search.
birthwt.step <- stepAIC(birthwt.glm, trace = FALSE)searches in both directions by default, or backward when noscopeis given. - Read the path.
birthwt.step$anovalists the steps taken. The manual’s next example widens the search withstepAIC(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
quinedataset):quine.nb1 <- glm.nb(Days ~ Sex/(Age + Eth*Lrn), data = quine), thenupdate()to add terms andanova(quine.nb1, quine.nb2, quine.nb3)to compare nested fits. - Ordinal outcome (the Copenhagen
housingsurvey, 72 rows):house.plr <- polr(Sat ~ Infl + Type + Cont, weights = Freq, data = housing), thensummary(house.plr, digits = 3). - Robust regression (the
stacklossdata):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), thenpredict(z, Iris[-train, ])$classto 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.nbreturns anegbinobject that inherits fromglmandlm, with the added componentstheta,SE.thetaandtwologlik. Report theta with its standard error alongside the coefficients, because it is the quantity that distinguishes this model from a Poisson fit.polrreturnscoefficientsfor the predictors andzetafor the intercepts (the cut points between ordered categories), plusdeviance,fitted.values,convergenceandniter. AHessiancomponent is present only whenHess = TRUE.rlmreturns the usuallmcomponents pluss(the scale estimate),w(the final weights, which show which observations were down-weighted),psi,conv,convergedandwresid.ldareturns an object withprior,means,scaling,svdandN. WithCV = TRUEit returns onlyclassandposterior, the classifications and posterior probabilities from cross-validation.stepAICreturns the final model with an extraanovacomponent recording the steps taken, and akeepcomponent if you suppliedkeep=.
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.stringfirst, then follow the install guide linked above. - Summary shows no standard errors for
polr. The Hessian is stored only whenHess = TRUE. The manual’s examples refit withupdate(house.plr, Hess = TRUE)before callingprofile()andconfint(). - 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. rlmgives different answers with differentpsifunctions. 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 usesinit = "lts"withpsi.hampel). Choosingmethod = "MM"gives a high breakdown point, but case weights are not supported with it.stepAICsearches fewer or more models than expected. The search space is set byscope: 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.
| Function | What it fits or does | Documented default or choices | Documented returned components | Manual example data |
|---|---|---|---|---|
| glm.nb | Negative binomial GLM that also estimates theta | link = log; method = glm.fit | theta, SE.theta, twologlik (negbin object inheriting glm and lm) | quine |
| polr | Logistic or probit regression for an ordered factor response | method = logistic (proportional odds); also probit, loglog, cloglog, cauchit; Hess = FALSE | coefficients, zeta, deviance, fitted.values, convergence, niter, Hessian (if Hess is true) | housing |
| rlm | Robust linear regression by iterated re-weighted least squares | method = M (or MM); psi = psi.huber; wt.method = inv.var | lm components plus s, w, psi, conv, converged, wresid | stackloss |
| lda | Linear discriminant analysis | prior = proportions; tol = 1e-4; CV = FALSE | prior, means, scaling, svd, N (class and posterior if CV = TRUE) | iris (as Iris) |
| stepAIC | Stepwise model selection by AIC | direction = both (backward if scope missing); k = 2; trace = 1; steps = 1000 | anova, keep (if keep= supplied) | birthwt, quine |
Sources
- MASS reference manual (CRAN) (accessed 2026-11-15)
- MASS package page (CRAN) (accessed 2026-11-15)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







