contr.wsum: contrasts for zeroing the weighted sum of coefficients or...

Description Usage Arguments Value Note Author(s) See Also Examples

Description

Whereas contr.sum generates contrasts for factors in regression models that lead to a (unweighted) sum of estimated coefficients of zero, this function does so for a weighted sum. Typically, the weights are the frequencies of the levels of the factor in the dataset serving to fit the model.

contr.wpoly adjusts the generated variables for a polynomial to render them orthogonal for the given frequencies of an ordered factor instead of doing this only for equal weights.

contr.wsumpoly will do either of these, depending on the arguments.

Usage

1
2
3
4
5
6
contr.wsumpoly(n, scores = NULL, y = NULL, w = NULL,
               contrasts = TRUE, sparse = FALSE, poly = NA)
contr.wsum(n, scores = NULL, y = NULL, w = NULL, contrasts = TRUE,
           sparse = FALSE)
contr.wpoly(n, scores = NULL, y = NULL, w = NULL, contrasts = TRUE,
            sparse = FALSE)

Arguments

n

a factor, a vector of levels for a factor, the number of levels, or a data.frame.

scores

scores for the levels of the factor, see ?contr.poly. Ignored in contr.wsum.

y

response variable, used to drop observations with NA response

w

weights for the levels. If n if this is a factor, the frequency vector is used and w is ignored.

contrasts

a logical indicating whether contrasts should be computed, see ?contr.sum.

sparse

logical indicating if the result should be sparse (of class 'dgCMatrix'), using package 'Matrix'.

poly

(only for contr.wsumpoly) If TRUE, polynomial contrasts are generated; if FALSE, (weighted) sum contrasts are given; if NA (default), sum or polynomial contrasts will result for unordered and ordered factors, respectively.

Value

If n is a number, a vector of levels, or a factor, the value is the contrast matrix that can be assigned to attr(factor, "contrasts") and then will be used in setting up the model.matrix for a regression or anova model. If it is a data.frame, this attribute is attached to each unordered factor and the complemented data.frame is returned.

Note

Using these contrasts makes individual coefficients interpretable as the effect of the respective level compared to the weighted average of effects. It also makes the intercept – for which the interpretation depends on the choice of contrasts – as precisely estimable as possible.

The function regr chooses these contrasts as a default (through the default of userOptions). This, however, only works for terms that are factors when before the formula of the model is translated into the model.frame. (This will probably change soon.)

Author(s)

Werner A. Stahel, Seminar f. Statistik, ETHZ

See Also

link{contrasts}, link{contr.sum}

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
contr.wsum(factor(c(1,1,1,2,3,3)))
##    [,1] [,2]
##  1  1.0  0.0
##  2  0.0  1.0
##  3 -1.5 -0.5

contr.wpoly(factor(c(1,1,1,2,3,3)))
##       ^0     ^1     ^2
##  [1,]  1 -0.833  0.138
##  [2,]  1  0.167 -0.828
##  [3,]  1  1.167  0.207


data(d.blast)
contr.wsum(d.blast$location)
wg <- table(d.blast$location)
( r.blast <-
  regr(log10(tremor)~location+log10(distance)+log10(charge), data=d.blast) )
sum(wg * r.blast$allcoef$location[,1]) ## approximately 0

regr0 documentation built on May 2, 2019, 4:52 p.m.