1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
# file MASS/stdres.q
# copyright (C) 1994-9 W. N. Venables and B. D. Ripley
#
lmwork <- function(object)
{
resid <- object$resid
hat <- lm.influence(object, do.coef = FALSE)$hat
hat <- hat[hat > 0]
ok <- !(is.na(resid))
n.miss <- sum(!ok)
switch(ifelse(n.miss > 2, 2, n.miss),
warning("1 missing observation deleted"),
warning(n.miss, " missing observations deleted")
)
resid <- resid[ok]
n <- length(resid)
p <- object$rank
rdf <- object$df.resid
if(is.null(rdf))
rdf <- n - p
if(!is.null(object$weights)) {
wt <- object$weights[ok]
resid <- resid * wt^0.5
excl <- wt == 0
if(any(excl)){
warning(sum(excl), " rows with zero weights not counted")
resid <- resid[!excl]
if(is.null(object$df.resid))
rdf <- rdf - sum(excl)
}
}
stdres <- studres <- resid
if(n > p) {
stddev <- sqrt(sum(resid^2)/rdf)
sr <- resid/(sqrt(1 - hat) * stddev)
stdres <- sr
studres <- sr/sqrt((n-p-sr^2)/(n-p-1))
if(!is.null(object$na.action)) {
stdres <- naresid(object$na.action, stdres)
studres <- naresid(object$na.action, studres)
}
}
else stddev <- stdres[] <- studres[]<- NA
list(stdedv=stddev, stdres=stdres, studres=studres)
}
stdres <- function(object) lmwork(object)$stdres
studres <- function(object) lmwork(object)$studres
|