wald: General Linear Hypothesis for the 'fixed' portion of a model...

View source: R/wald.R

waldR Documentation

General Linear Hypothesis for the 'fixed' portion of a model with Wald test

Description

General Linear Hypothesis with Wald test for lm, glm, lme, nlme and lmer objects. Can be extended to other objects (e.g.) 'glm' by writing 'getFix.glm'

Usage

wald(
  fit,
  Llist = "",
  clevel = 0.95,
  pred = NULL,
  data = NULL,
  debug = FALSE,
  maxrows = 25,
  full = FALSE,
  fixed = FALSE,
  invert = FALSE,
  method = "svd",
  overdispersion = FALSE,
  df = NULL,
  pars = NULL,
  ...
)

waldx(
  fit,
  Llist = "",
  clevel = 0.95,
  pred = NULL,
  data = NULL,
  debug = FALSE,
  maxrows = 25,
  full = FALSE,
  fixed = FALSE,
  invert = FALSE,
  method = "svd",
  overdispersion = FALSE,
  df = NULL,
  pars = NULL,
  robust = FALSE,
  type = "HC3",
  ...
)

waldf(...)

wald2(
  fit,
  Llist = "",
  clevel = 0.95,
  pred = NULL,
  data = NULL,
  debug = FALSE,
  maxrows = 25,
  full = FALSE,
  fixed = FALSE,
  invert = FALSE,
  method = "svd",
  df = NULL,
  pars = NULL,
  ...
)

wald.transform(x, fun, label = "Transformed coefficients")

glh(...)

Arguments

fit

a model for which a getFix method exists.

Llist

a hypothesis matrix or a pattern to be matched or a list of these

clevel

level for confidence intervals. No confidence intervals if clevel is NULL

pred

(default NULL) a data frame to use to create a model matrix. This is an alternative to 'full' when the model matrix needs to be based on data frame other than the data frame used for fitting the model.

data

data frame used as 'data' attribute fot list elements returned only if the corresponding element of Llist has a NULL data attribute

debug

(default FALSE) produce verbose information

maxrows

maximum number of rows of hypothesis matrix for which a full variance-covariance matrix is returned

full

if TRUE, the hypothesis matrix is the model matrix for fit such that the estimated coefficients are the predicted values for the fixed portion of the model. This is designed to allow the calculation of standard errors for models for which the predict method does not provide them.

fixed

if Llist is a character to be used a regular expression, if fixed is TRUE Llist is interpreted literally, i.e. characters that have a special meaning in regular expressions are interpreted literally.

invert

if Llist is a character to be used a regular expression, invert == TRUE causes the matches to be inverted so that coefficients that do not match will be selected.

method

'svd' (current default) or 'qr' is the method used to find the full rank version of the hypothesis matrix. 'svd' has correctly identified the rank of a large hypothesis matrix where 'qr' has failed.

overdispersion

(default FALSE) if TRUE, adjust variance-covariance estimate by multiplying by the overdispersion factor calculated by overdisp_fun. If overdisperion is numerical, use its value as an overdispersion factor.

pars

passed to extract method for stanfit objects.

robust

(default FALSE) use Huber-White corrected covariance matrix from sandwich package

type

(default 'HC3') type of Huber-White correction to use. See vcovHC.

include

passed to extract method for stanfit objects.

help

obsolete

Details

Tests a general linear hypothesis for the linear fixed portion of a model or the form:

H_0: L\beta = 0

The hypothesis can be specified in a variety of ways such as a hypothesis matrix or a pattern that is used as a regular expression to be matched with the names of coefficients of the model. A number of tools are available to facilitate the generation of hypothesis matrices.

Usage:

wald(fit, L) where L is a hypothesis matrix

wald(fit, 'pat') where 'pat' a regular expression (see ?regex) used to match names of coefficients of fixed effects. e.g. wald( fit, ':.*:') tests all 2nd and higher order interactions.

wald(fit, c(2,5,6)) to test 2nd, 5th and 6th coefficients.

wald(fit, list( hyp1= c(2,5,6), H2 = 'pat')) for more than one hypothesis matrix

There are number of functions to help construct hypothesis matrices. See in particular Lfx.

To extend the 'wald' function to a new class of objects, one needs to write a 'getFix' method to extract estimated coefficients, their estimated covariance matrix, and the denominator degrees of freedom for each estimated coefficient. See the examples below for a getFix method for a glm object and for 'mipo' objects in the packages 'mice':

Value

An object of class wald, with the following components: COMPLETE

Functions

  • waldx(): experimental version for singular models. Reports rows of L matrix that are not estimable.

  • waldf(): equivalent to as.data.frame(waldx(...))

  • wald2(): experimental version with RHS?

  • wald.transform(): transforms estimates canfidence intervals using delta method

  • glh(): same as wald, kept for backward compatibility

References

REFERENCES HERE

See Also

Lform, Lfx, getX, M, Lall,Lc,Lequal, Ldiff,Lmu,Lmat,Lrm, as.data.frame.wald. To extend to new models see getFix. To generate hypothesis matrices for general splines see gsp and sc.

Examples

data(hs)
library(nlme)
###
### Using wald to create and plot a data frame with predicted values
###
  fit <- lme(mathach ~ (ses+I(ses^2)) * Sex * Sector, hs, random = ~ 1|school)
  summary(fit)
  pred <- expand.grid( ses = seq(-2,2,.1), Sex = levels(hs$Sex), Sector = levels(hs$Sector))
  pred
  w <- wald(fit, getX(fit,data=pred)) # attaches data to wald.object so it can be included in data frame
  w <- wald(fit, pred = pred)
  w <- as.data.frame(w)
  head(w)
  library(latticeExtra)
  xyplot(coef ~ ses | Sector, w, groups = Sex,
     auto.key = T, type = 'l',
     fit = w$coef,
     upper = with(w,coef+2*se),
     lower = with(w,coef-2*se),
     subscript = T) +
     glayer( gpanel.fit(...))

wald( fit, 'Sex')  # sig. overall effect of Sex
wald( fit, ':Sex') # but no evidence of interaction with ses
wald( fit, '\\^2') # nor of curvature

# but we continue for the sake of illustration

L <- Lform( fit, list( 0, 1, 2*ses, 0, Sex == 'Male', (Sex == 'Male')*2*ses), hs)
L
(ww <- wald ( fit, L ))
wald.dd <- as.data.frame(ww, se = 2)
head( wald.dd )

require(lattice)
xyplot( coef + U2 + L2 ~ ses | Sex, wald.dd,
 main= 'Increase in predicted mathach per unit increase in ses')

# Example of a getFix method for a glm oject:

getFix.glm <- function(fit,...) { 
  ss <- summary(fit) 
  ret <- list() 
  ret$fixed <- coef(fit) 
  ret$vcov <- vcov(fit) 
  ret$df <- rep(ss$df.residual,length(ret$fixed)) 
  ret
} 

# Example of a getFix method for a mipo object in the mice package:

getFix.mipo <- function( fit, ...){ 
  # pooled multiple imputation object in mice 
  # 'wald' will use the minimal df for components with non-zero weights 
  #  -- this is probably too conservative and should be improved 
  ret <- list()
  ret$fixed <- fit$qbar 
  ret$vcov <- fit$t 
  ret$df <- fit$df 
  ret 
}

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