# Function to calculate relative importance metrics for linear models

### Description

calc.relimp calculates several relative importance metrics for the linear model.
The recommended metrics are `lmg`

(*R^2* partitioned by averaging over orders, like in Lindemann, Merenda and Gold (1980, p.119ff))
and `pmvd`

(a newly proposed metric by Feldman (2005) that is provided in the non-US version of the package only).
For completeness and comparison purposes, several other metrics are also on offer (cf. e.g. Darlington (1968)).

### Usage

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | ```
## generic function
calc.relimp(object, ...)
## default S3 method
## Default S3 method:
calc.relimp(object, x = NULL, ...,
type = "lmg", diff = FALSE, rank = TRUE, rela = FALSE, always = NULL,
groups = NULL, groupnames = NULL, weights=NULL, design=NULL)
## S3 method for formula object
## S3 method for class 'formula'
calc.relimp(formula, data, weights, na.action, ..., subset=NULL)
## S3 method for objects of class lm
## S3 method for class 'lm'
calc.relimp(object, type = "lmg", groups = NULL, groupnames=NULL, always = NULL, ...)
``` |

### Arguments

`object ` |
The class of this object determines which of the methods is used:
There are special methods for output objects from function Thus, object can be a formula (e.g. y\~x1+x2+x3+x2:x3) (cf. below for details) OR the output of a linear model call (inheriting from class The restrictions on usage of interactions listed under item formula below also apply to linear model objects. OR the covariance matrix of a response y and regressors x, (e.g. obtained by cov(cbind(y,x)), if y is a column vector of response values and x a corresponding matrix of regressors) OR a (raw) data matrix or data frame with the response variable in the first column OR a response vector or one-column matrix,
if |

`formula ` |
The first object, if a formula is to be given; one response only. Interaction terms are currently limited to second-order. Note: If several interaction terms are given, calculations may be very resource intensive, if these are all connected (e.g. with A:B, B:C, C:D, all A,B,C,D are connected, while with A:B, C:D, D:E there are separate groups A,B and C,D,E). Interaction terms occurring in always do not increase resource usage (but are only permitted if the respective main effects also occur in always). Interactions and groups currently cannot be used simultaneously. |

`x ` |
a (raw) data matrix or data frame containing the regressors,
if OR NULL, if |

`type ` |
can be a character string, character vector or list of character strings.
It is the collection of metrics that are to be calculated.
Available metrics: |

`diff ` |
logical; if TRUE, pairwise differences between the relative contributions are calculated; default FALSE |

`rank ` |
logical; if TRUE, ranks of regressors in terms of relative contributions are calculated; default TRUE |

`rela ` |
is a logical requesting relative importances summing to 100% ( |

`always ` |
is a vector of column numbers or names of variables to be always in the
model (adjusted for). Valid numbers are 2 to (number of regressors + 1) (1 is reserved for the response),
valid character strings are all column names of Relative importance is only assessed for the variables not selected in This option currently does not work for metrics |

`groups ` |
is a list of vectors of column numbers or names of variables to be combined into groups.
If only one group is needed, a vector can be given. The numbers and character strings needed are of the same form
as for Relative importance is only allocated between groups of regressors, no subdivision within groups is calculated.
Regressors that do not occur in any group are included as singletons.
A regressor must not occur in |

`groupnames ` |
is a vector of names for the variable groups to be used for annotation of output. |

`weights ` |
is a vector of case weights for the observations in the data frame (or matrix).
You can EITHER specify |

`design ` |
is a design object of class Also note that care is needed when using |

`data` |
if first object is of class formula: an optional matrix or data frame that the variables in formula and subset come from; if it is omitted, all names must be meaningful in the environment from which calc.relimp is called |

`subset` |
if first object is of class formula:
an optional expression indicating the subset of the observations of |

`na.action` |
if first object is of class formula:
an optional function that indicates what should happen when the data contain 'NA's.
The default is first, any na.action attribute of data, second the setting given in the call to calc.relimp,
third the na.action setting of options. Possible choices are "na.fail",
(print an error message and terminate if there are any incomplete observations),
"na.omit" or "na.exclude" (equivalent for package |

`...` |
usable for further arguments, particularly most arguments of default method can be given to all other methods (exception: weights and design cannot be given to lm-method) |

### Details

- lmg
is the

*R^2*contribution averaged over orderings among regressors, cf. e.g. Lindeman, Merenda and Gold 1980, p.119ff or Chevan and Sutherland (1991).- pmvd
is the proportional marginal variance decomposition as proposed by Feldman (2005) (non-US version only). It can be interpreted as a weighted average over orderings among regressors, with data-dependent weights.

- last
is each variables contribution when included last, also sometimes called usefulness.

- first
is each variables contribution when included first, which is just the squared covariance between y and the variable.

- betasq
is the squared standardized coefficient.

- pratt
is the product of the standardized coefficient and the correlation.

- genizi
is the

*R^2*decomposition according to Genizi 1993- car
is the

*R^2*decomposition according to Zuber and Strimmer 2010, also available from package care (squares of scores produced by function`carscore`

Each metric is calculated using the internal function “metric”`calc`

, e.g. `lmgcalc`

.

Five of the metrics in `calc.relimp`

(`lmg`

, `pmvd`

, `pratt`

, `genizi`

and `car`

),
decompose the model *R^2*.
`calc.relimp`

(`lmg`

, `pmvd`

, `pratt`

, `genizi`

and `car`

) sum to the *R^2* that is to be decomposed,
if `rela = FALSE`

and to 100pct if `rela = TRUE`

.

The other metrics also (artificially) sum to 100pct if `rela = TRUE`

.
If `rela = FALSE`

, they are given relative to var(y) (or the conditional variance of y after adjusting out the variables
requested in `always`

) but do not sum to *R^2*.

If `always`

requests some variables to be always in the model, these are conditioned upon
(i.e. included into the model first). Only the remaining *R^2* that is not explained by
these variables is decomposed among the other regressors. This currently does not work for metrics `genizi`

and `car`

.

Four of the metrics, `lmg`

, `pmvd`

, `first`

and `last`

,
are related to the order in which the variables are included into the model.
For these it is possible to consider the variables in groups that are always entered into the model together.

Note that relaimpo can only provide metric `lmg`

for models with interactions (2-way interactions only).
It averages only over those orders, for which the interactions enter the model after both their main effects.

Note that there are different types of weights, weights indicating the variability of the response
(observations with a more variable responses receive a lower weight than those with a less variable response,
like in the Aitken estimator), frequency weights indicating the number of observations with exactly the observed
data pattern of the current observation, or weights indicating the number of population units represented by the
current observation (inverse sampling probability, weights typically used in survey situations). All three types of
weight alike can be handed to function `calc.relimp`

using the `weights=`

option. Note, however, that they
have to be treated differently for bootstrapping (cf. `boot.relimp`

).

Data from complex surveys can be treated by providing a survey design with `design=`

-option.
For `calc.relimp`

, it is also sufficient to provide the weights derived from the design using the
`weights=`

-option.

`calc.relimp`

cannot handle data with missing values directly. It applies complete-case analysis,
i.e. drops all units with any missing values by default. While this can be appropriate, if there are only few
missing values, data with more severe missingness issues need special treatment. Package relaimpo
offers the function `mianalyze.relimp`

that handles multiply-imputed datasets (that can be created
by several other **R**-packages). Currently, possibilities in this function are limited due to the fact that
it uses complex survey designs and bootstrapping which do not (yet) go together well with factors, interactions
and calculated quantities in formulae.

### Value

`var.y ` |
the variance of the response |

`R2 ` |
the coefficient of determination, |

`R2.decomp ` |
the part of the coefficient of determination that is decomposed among the variables under investigation |

`lmg ` |
vector of relative contributions obtained from the |

`lmg.diff ` |
vector of pairwise differences between relative contributions obtained from the |

`lmg.rank ` |
rank of the regressors relative contributions obtained from the |

`metric, metric.diff, metric.rank ` |
analogous to |

`ave.coeffs` |
average coefficients for variables not not requested by always only for models of different sizes; note that coefficients refer to modeling residuals after adjusting out variables listed in always (both from response and other explanatory variables) |

`namen` |
names of variables, starting with response |

`type` |
character vector of metrics available |

`rela` |
Have metrics been normalized to sum 100% ? |

`always` |
column numbers of variables always in the model;
in case of factors, the column numbers given here are not identical to those in
the call to |

`alwaysnam` |
names of variables always in the model |

`call` |
contains the call that generated the object |

### Warning

`lmg`

and `pmvd`

are computer-intensive. Although they are calculated based on the
covariance matrix, which saves substantial computing time in comparison to carrying out actual regressions,
these methods still take quite long for problems with many regressors.

`relaimpo`

is a package for univariate linear models.
Using `relaimpo`

on objects that inherit from class `lm`

but are not univariate linear model objects
may produce nonsensical results without warning. Objects of class `mlm`

or `glm`

with link functions other than identity
or family other than gaussian lead to an error message.

### Note

There are two versions of this package. The version on CRAN is globally licensed under GPL version 2 (or later).
There is an extended version with the interesting additional metric `pmvd`

that is licensed according to GPL version 2
under the geographical restriction "outside of the US" because of potential issues with US patent 6,640,204.
This version can be obtained from Ulrike Groempings website (cf. references section).
Whenever you load the package, a display tells you, which version you are loading.

### Author(s)

Ulrike Groemping, BHT Berlin

### References

Chevan, A. and Sutherland, M. (1991) Hierarchical Partitioning. *The American Statistician* **45**, 90–96.

Darlington, R.B. (1968) Multiple regression in psychological research and practice. *Psychological Bulletin* **69**, 161–182.

Feldman, B. (2005) Relative Importance and Value. Manuscript (Version 1.1, March 19 2005), downloadable at http://www.prismanalytics.com/docs/RelativeImportance050319.pdf

Genizi, A. (1993) Decomposition of R2 in multiple regression with correlated regressors. *Statistica Sinica* **3**, 407–420.
Downloadable at http://www3.stat.sinica.edu.tw/statistica/password.asp?vol=3&num=2&art=10

Groemping, U. (2006) Relative Importance for Linear Regression in R: The Package relaimpo
*Journal of Statistical Software* **17**, Issue 1.
Downloadable at http://www.jstatsoft.org/v17/i01

Lindeman, R.H., Merenda, P.F. and Gold, R.Z. (1980) *Introduction to Bivariate and Multivariate Analysis*, Glenview IL: Scott, Foresman.

Zuber, V. and Strimmer, K. (2010) *Variable importance and model selection by decorrelation*. Preprint, downloadable at http://www.uni-leipzig.de/strimmer/lab/publications/preprints/carscore2010.pdf

Go to http://prof.beuth-hochschule.de/groemping/relaimpo/ for further information and references.

### See Also

relaimpo, `booteval.relimp`

, `mianalyze.relimp`

,
`classesmethods.relaimpo`

### Examples

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | ```
#####################################################################
### Example: relative importance of various socioeconomic indicators
### for Fertility in Switzerland
### Fertility is first column of data set swiss
#####################################################################
data(swiss)
calc.relimp(swiss,
type = c("lmg", "last", "first", "betasq", "pratt", "genizi", "car") )
# calculation of all available relative importance metrics
# non-US version offers the additional metric "pmvd",
# i.e. call would be
# calc.relimp(cov(swiss),
# type = c("lmg", "pmvd", "last", "first", "betasq, "pratt"),
# rela = TRUE )
## same analysis with formula or lm method and a few modified options
crf <- calc.relimp(Fertility~Agriculture+Examination+Education+Catholic+Infant.Mortality,swiss,
subset = Catholic>40,
type = c("lmg", "last", "first", "betasq", "pratt"), rela = TRUE )
crf
linmod <- lm(Fertility~Agriculture+Examination+Education+Catholic+Infant.Mortality,swiss)
crlm <- calc.relimp(linmod,
type = c("lmg", "last", "first", "betasq", "pratt", "genizi", "car"), rela = TRUE )
plot(crlm)
# bar plot of the relative importance metrics
#of statistical interest in this context: correlation matrix
cor(swiss)
#demonstration of conditioning on one regressor using always
calc.relimp(swiss,
type = c("lmg", "last", "first", "betasq", "pratt"), rela = FALSE,
always = "Education" )
# using calc.relimp with grouping of two regressors
# and weights (not reasonable here, purely for demo purposes)
calc.relimp(swiss,
type = c("lmg", "last", "first"), rela = FALSE,
groups = c("Education","Examination"), weights = abs(-23:23) )
# using calc.relimp with grouping of two regressors
# and a design object (not reasonable here, purely for demo purposes)
des <- svydesign(~1, data=swiss, weights=~abs(-23:23))
calc.relimp(swiss,
type = c("lmg", "last", "first"), rela = FALSE,
groups = c("Education","Examination"), groupnames ="EduExam", design = des)
# calc.relimp with factors (betasq and pratt not possible)
# (calc.relimp would not be necessary here,
# because the experiment is balanced)
calc.relimp(1/time~poison+treat,data=poisons, rela = FALSE,
type = c("lmg", "last", "first"))
# including also the interaction (lmg possible only)
calc.relimp(1/time~poison*treat,data=poisons, rela = FALSE)
``` |