Debian Codegraph @

R/withVCov.R

raw
1 withSE <- function(object, vcov, ...){
2 if(is.matrix(vcov)) UseMethod("withVCov")
3 else if(is.function(vcov)) UseMethod("withVCov")
4 else if(is.character(vcov)){
5 if(exists(vcov))
6 vcov <- get(vcov)
7 else if(exists(vcov2 <- paste0("vcov",vcov)))
8 vcov <- get(vcov2)
9 else
10 stop("neither '",vcov,"' nor '", vcov2, "' is a known function")
11 withVCov(object, vcov=vcov, ...)
12 }
13 else stop("cannot handle 'vcov' argument of type",typeof(vcov))
14 }
15
16 withVCov <- function(object, vcov, ...) UseMethod("withVCov")
17
18 withVCov.lm <- function(object, vcov, ...){
19
20 if(is.function(vcov))
21 V <- vcov(object, ...)
22 else if(is.matrix(vcov))
23 V <- vcov
24 else
25 stop("argument 'vcov' should be a matrix")
26
27 cls <- class(object)
28 cls <- c(paste("withVCov",cls[1],sep="."),
29 "withVCov",
30 class(object))
31 structure(object,
32 .VCov=V,
33 class=cls)
34 }
35
36 vcov.withVCov <- function(object, ...) attr(object,".VCov")
37
38 summary.withVCov <- function(object, ...){
39
40 V <- attr(object,".VCov")
41
42 res <- NextMethod()
43 coefTab <- res$coefficients
44
45 est <- coefTab[,1]
46 se <- sqrt(diag(V))
47 zval <- est/se
48 pval <- 2 * pnorm(abs(zval),lower.tail=FALSE)
49 coefTab[,2] <- se
50 coefTab[,3] <- zval
51 coefTab[,4] <- pval
52 res$coefficients <- coefTab
53
54 res
55 }
56
57 summary.withVCov.lm <- function(object, ...){
58
59 V <- attr(object,".VCov")
60
61 res <- NextMethod()
62 coefTab <- res$coefficients
63 rdf <- res$df[2]
64
65 est <- coefTab[,1]
66 se <- sqrt(diag(V))
67 tval <- est/se
68 pval <- 2 * pt(abs(tval),df=rdf,lower.tail=FALSE)
69 coefTab[,2] <- se
70 coefTab[,3] <- tval
71 coefTab[,4] <- pval
72 res$coefficients <- coefTab
73
74 res
75 }