Debian Codegraph @

R/Ftest-rlm.R

raw
1 ##- From: Werner Stahel <[email protected]>
2 ##- To: [email protected], [email protected]
3 ##- Subject: robuster F-test
4 ##- Date: Fri, 14 Jul 2000 17:01:55 +0200 (CEST)
5
6 f.robftest <- function(object, var = -1)
7 {
8 ## Purpose: robust F-test: Wald test for several coefficients of
9 ## an rlm object
10 ## -------------------------------------------------------------------------
11 ## Arguments:
12 ## object result of rlm(...)
13 ## var variables. Either their names or their indices
14 ## Default: all *but* the intercept
15 ## -------------------------------------------------------------------------
16 ## Author: Werner Stahel, Date: 14 Jul 2000; MM, 2000-07-14
17
18 if (!inherits(object, "rlm"))
19 stop("f.robftest() only works for 'rlm' objects")
20
21 ## determine and check coefficients to be tested
22 cf <- object$coef
23 iind <- if(is.character(var)) match(var,names(cf)) else seq(length(cf))[var]
24 wrong <- is.na(iind) | iind > length(cf) | iind < 1
25 if (any(wrong))
26 stop(paste("variable ",var[wrong]," not found"))
27 cf <- cf[iind]
28 if (0 == (t.nv <- length(cf)))
29 stop("no variables to be tested")
30 ## covariance matrix of estimated coefficients: calls summary.rlm():
31 stopifnot(requireNamespace("MASS"))
32 t.r <- summary(object, method="XtWX")
33 ## Nota BENE: vcov() calls vcov.lm() which uses $sigma instead of $stddev !
34 t.cov <- t.r$cov.unscaled[iind,iind] * t.r$stddev ^ 2
35
36 ## Instead of c(cf %*% solve(t.cov) %*% cf)/t.nv
37 ## quite a bit more efficient (for larger p): x' A^-1 x :
38 t.f <- sum(cf * solve(t.cov, cf))/t.nv
39 df <- c(t.nv, t.r$df[2])
40
41 ## MM: Return an object of class "htest" ---> nice print.*() method !
42 struct(list(statistic = c(F = t.f), df = df,
43 data.name = paste("from", deparse(object$call)),
44 method = "robust F-test (as if non-random weights)",
45 alternative = "two.sided",
46 null.value = {c0 <- cf; c0[] <- 0; c0},
47 p.value = pf(t.f, df[1], df[2], lower.tail = FALSE)),
48 class = "htest")
49 }
50