The proc_glm function performs a general linear model
analysis for one or more models. Unlike proc_reg, the
proc_glm function accepts categorical predictors through the
class parameter, and produces Type I, Type II, and Type III sums
of squares. The model(s) are passed on the model parameter, and the
input dataset is passed on the data parameter. The by
parameter allows you to subset the data into groups and run the model on
each group. The weight parameter lets you assign a weight to each
observation. The output and options parameters provide
additional customization of the results.
proc_glm(
data,
model,
class = NULL,
order = NULL,
by = NULL,
stats = NULL,
output = NULL,
weight = NULL,
lsmeans = NULL,
contrast = NULL,
estimate = NULL,
random = NULL,
options = NULL,
titles = NULL,
where = NULL
)The input data frame for which to perform the analysis. This parameter is required.
A model for the analysis. The model can be specified using
either R syntax (y ~ a + b + a:b) or SAS syntax ("y = a b a*b").
To pass multiple models, use a list (R syntax) or a vector of strings
(SAS syntax). By default, models are named "MODEL1", "MODEL2", etc.
An optional vector of variable names to treat as categorical
(factor) predictors. These variables will be converted to factors prior to
fitting the model. Pass quoted, or unquoted using the
v function.
Controls the sort order of the class variable levels. Valid
values are "internal", "formatted", "data", and "freq", matching the
order parameter on proc_freq and proc_ttest.
The default "internal" uses the natural order of the values: character values
sort alphabetically, numeric values sort by value, and an existing factor
keeps its level order. "formatted" applies the variable's format and uses
the order defined by the format. "data" uses order of appearance, and "freq"
sorts by descending frequency. Pass one keyword per class variable, or
a single keyword to apply to all of them.
Note that for proc_glm the level order is not merely cosmetic. The
last level of a class variable is the reference level, so order
changes the Type III and "solution" results. It also determines how
contrast and estimate coefficients are mapped onto levels: the
coefficients are read in level order, so the same coefficient vector means
different things under different order values.
The "formatted" keyword goes further still. If the format maps several
values onto one label, those values become a single level, and the model
loses the degrees of freedom that separated them. This matches the SAS
practice of using a format to group a class variable, but it means
order = "formatted" can change the model and not just its
presentation. Only a user-defined format created with
value carries a level order, so that is the only kind of
format "formatted" accepts.
An optional by group. If specified, the input data will be subset on the by variable(s) prior to performing the analysis.
Optional statistics keywords, corresponding to the options on the "model" statement in SAS. Valid values are "ss1", "ss2", "ss3", "solution", "est", "clparm", "p", "clm", "cli", "noint", "xpx", "inverse", and "e1", "e2", and "e3".
solution: Adds a parameter estimates table to the interactive report. "est" is an accepted alias.
clparm: Adds confidence limits for the parameter estimates,
using the alpha value from the options parameter.
p: Adds predicted values and residuals to the interactive report.
clm: Adds the standard error and confidence limits of the mean predicted value to the predicted values table. Implies "p".
cli: Adds confidence limits for an individual predicted value to the predicted values table. These are wider than the "clm" limits because they include the error variance. Implies "p".
noint: Fits the model without an intercept. Every downstream statistic reflects this, and the total sum of squares is no longer corrected for the mean.
xpx: Adds the augmented X'X crossproducts matrix, which carries the dependent variable as an extra row and column.
inverse: Adds the generalized inverse of the augmented X'X matrix. Its bottom right cell is the error sum of squares.
e1, e2, e3: Add the Type I, Type II and Type III estimable function tables.
ss1, ss2, ss3: Select which sums of squares to produce. The default is Type I and Type III, as in SAS.
Whether or not to return datasets from the function. Valid values are "out", "none", and "report", plus the data shaping keywords "long", "stacked", and "wide". Default is "out".
The name of a variable to use as a weight for each observation.
The name of one or more class variables for which to compute
least-squares means. Each requested effect produces a least-squares means
table on the interactive report. The effect(s) must also appear on the
class parameter.
A named list of contrast specifications. Each element name
is the contrast label, and each value is itself a named list mapping a model
effect to its coefficients over that effect's levels, in level order. For
example, contrast = list("F vs M" = list(Sex = c(1, -1))) mirrors the
SAS statement contrast 'F vs M' Sex 1 -1. Each contrast produces an
F-test row on the interactive report.
Give an effect a matrix instead of a vector to reproduce the comma syntax of
a SAS CONTRAST statement: each row of the matrix is one row of the
contrast, and the result is a single test whose degrees of freedom equal the
number of linearly independent rows. So
list("Any diff" = list(AgeGroup = rbind(c(-1, 0, 1), c(0, -1, 1))))
is the SAS statement
contrast 'Any diff' AgeGroup -1 0 1, AgeGroup 0 -1 1;.
Interactions may be spelled either way: the SAS operator "a*b" is
accepted and normalized to the R operator "a:b". Any effect not named
gets a coefficient of zero, as does the intercept. A contrast that is not
estimable is dropped with a warning rather than reported.
A named list of estimate specifications, using the same
structure as the contrast parameter. Each estimate produces a row on
the interactive report with the estimate, standard error, t value, p value,
and confidence limits. Unlike contrast, estimate accepts only
single-row specifications, since it estimates one linear combination.
The name of one or more class variables to treat as random
effects. Produces a table of Type III expected mean squares on the
interactive report. The effect(s) must also appear on the class
parameter.
A vector of optional keywords. Valid values are "alpha =",
"noprint", "ss1", "ss2", "ss3", "outstat", "singular =", and "zeta =". The
"outstat" option requests an output dataset of the model sums of squares.
This is the default output dataset, so the option does not normally need to
be passed. The "singular =" option sets the tolerance for detecting a linear
dependency among the design matrix columns, and "zeta =" sets the tolerance
of the estimability check applied to each contrast and
estimate. Both default to 1e-8.
A vector of one or more titles to use for the report output.
An expression to filter the rows before statistics are
calculated. Use the expression function.
Normally the requested statistics are shown interactively in the viewer, and output results are returned as a data frame. If "report" datasets are requested they are returned as a list.
The proc_glm function is a general-purpose linear modeling function
built on top of the GLM function from the sasLM
package. It produces a dataset output by default, and, when working in
RStudio, also produces an interactive report. Statistical output is
designed to match SAS.