Lfx: Hypothesis matrix generated by expressions for each column or...

LfxR Documentation

Hypothesis matrix generated by expressions for each column or term

Description

Lfx facilitates generating linear hypothesis matrices for complex models by using M objects to easily generate portions of the hypothesis matrix. Lfx(fit) with no other arguments returns a list that is easy to edit to differentiate with respect to numeric variables.

Usage

Lfx(
  fit,
  expr.list,
  data = getData(fit),
  prefix = "1 * ",
  wrap = FALSE,
  debug = FALSE
)

Arguments

fit

a fitted model with a 'getFix' method.

expr.list

a list of expressions with one component for each column (or groups of columns) of the hypothesis matrix corresponding to each term of the model. A term with multiple degrees of freedom can either be generated as separate single terms or with an expression that evaluates to a suitable matrix.

data

the data frame in which expressions are evaluated.

wrap

if TRUE return expression wrapped in with(data, ...), default FALSE

debug

if TRUE provide verbose output, default FALSE

formula

as an argument of M, a one-sided formula defining a main effect or an interaction term involving factors.

expr

as an argument of M, an expression that can be evaluated in each row of data to form element(s) of the corresponding row of L. formula defining a main effect or an interaction term involving factors.

Details

Creates an L matrix using expressions evaluated in data for each column or multi-column term of the L matrix. Differentiating with respect to numeric variables is easy. Generating pairwise differences between factor levels requires the intermediate step of creating a data frame (usually with expand.grid, with crossed factors to be differenced with differently named versions of themselves.

For example, letting fac be a factor, fac0, the same factor in a different order and x a numerical variable:

  • factor prediction: M(fac)

  • factor pairwise differences: M(fac) - M(fac0)

  • interaction terms: M(x) * M(fac0)

  • zero block of the correct size: 0 * M(fac)

See Also

Lfx M M.default M.factor M.formula M.M <.factor >.factor <=.factor =>.factor

Examples


##
## The increase in income associated with an additional year of education
##

data(Prestige)   # from library(car)
fit <- lm( income ~ (education+I(education^2)) * type, Prestige)
summary(fit)
Lfx(fit)  # generates expression to cut and paste and differentiate

pred <- expand.grid( education = 3:16, type = levels(Prestige$type))

Lf.ed <- Lfx( fit,
              list( 0,
                    1 + 0* education,
                    2 * M(I(education)),
                    0 * M(type),
                    1 * 1 * M(type),
                    2 * M(I(education)) * M(type)
              ),pred)
zw <- as.data.frame(wald(fit,Lf.ed))
head(zw)
xyplot( coef ~ education , zw, groups = type, type = 'l', auto.key = T)

fit2 <- lm( income ~ (education+I(education^2)) * type*women, Prestige)
pred <- expand.grid( education = 3:16, type = levels(Prestige$type), women=c(0,50,100))
Lfx(fit2)
Lhyp <- Lfx(fit2,
    list( 0,   # differentiated wrt education
          1 * 1,
          1 * M(I(education)),
          0 * M(type),
          0 * women,
          1 * 1 * M(type),
          2 * M(I(education)) * M(type),
          1 * 1 * women,
          2 * M(I(education)) * women,
          0 * M(type) * women,
          1 * 1 * M(type) * women,
          2 * M(I(education)) * M(type) * women
    ),pred)
Lhyp
Lhyp <- as.data.frame( wald(fit2, zw))
summary(fit2)
xyplot( coef ~ education|women , zw, groups = type, type = 'l', auto.key = T,
        ylim = c(-30000,30000))

##
##  Differencing of factor
##

pred <- expand.grid( education = 3:18, women = c(0,50,100),
                     type = levels(Prestige$type),
                     type0 = levels(Prestige$type))

Lfx(fit2)

Lcomp <- Lfx(fit2,
  list( 0,
      0 * education,
      0 * M(I(education^2)),
      1 * M(type) - M(type0),
      0 * women,
      1 * education * (M(type)-M(type0)),
      1 * M(I(education^2)) * (M(type) - M(type0)),
      0 * education * women,
      0 * M(I(education^2)) * women,
      1 * (M(type)-M(type0)) * women,
      1 * education * (M(type) - M(type0)) * women,
      1 * M(I(education^2)) * (M(type) - M(type0)) * women
  ), pred)

zw <- as.data.frame( wald( fit2, Lcomp))
zw

gmonette/spida2 documentation built on Aug. 11, 2024, 7:52 p.m.