epoh前辈,anova.rq函数是和rq函数一起使用的
ie:
data(barro)
fit0 <- rq(y.net ~ lgdp2 + fse2 + gedy2 , data = barro)
fit1 <- rq(y.net ~ lgdp2 + fse2 + gedy2 + Iy2 + gcony2, data = barro)
fit2 <- rq(y.net ~ lgdp2 + fse2 + gedy2 + Iy2 + gcony2, data = barro,tau=.75)
fit3 <- rq(y.net ~ lgdp2 + fse2 + gedy2 + Iy2 + gcony2, data = barro,tau=.25)
anova(fit1,fit0)
anova(fit1,fit2,fit3)
anova(fit1,fit2,fit3,joint=FALSE)
我要的是anova(fit1,fit2,fit3,joint=FALSE)这个实现结果。我看了anova.rq内部函数,呵呵,好复杂啊
内部程序:
> anova.rq
function (object, ...)
{
if (length(list(object, ...)) > 1) {
return(anova.rqlist(object, ...))
}
stop("Anova is only defined (yet) for lists of rq objects")
}
> anova.rqlist
function (object, ..., test = "Wald", joint = TRUE, score = "wilcoxon",
R = 200, trim = NULL)
{
objects <- list(object, ...)
responses <- as.character(lapply(objects, function(x) formula(x)[[2]]))
sameresp <- responses == responses[1]
if (!all(sameresp))
stop("Models don't all have the same response variable")
n <- length(objects[[1]]$y)
models <- as.character(lapply(objects, function(x) formula(x)))
nobjects <- length(objects)
dimp <- lapply(objects, function(x) length(coef(x)))
objects <- objects[order(-unlist(dimp))]
mf <- model.frame(objects[[1]])
models <- as.character(lapply(objects, function(x) formula(x)))
taus <- unlist(lapply(objects, function(x) x$tau))
if (is.matrix(coef(objects[[1]])))
names <- lapply(objects, function(x) dimnames(coef(x))[[1]])
else names <- lapply(objects, function(x) names(coef(x)))
if (test == "Wald")
objects <- lapply(objects, function(x) summary(x, se = "nid",
covariance = TRUE))
sametaus <- taus == taus[[1]]
if (all(sametaus)) {
Tn <- rep(0, nobjects - 1)
ndf <- Tn
ddf <- Tn
pvalue <- Tn
topnote <- paste("Model ", format(1:nobjects), ": ",
models, sep = "", collapse = "\n")
if (test == "anowar") {
X1 <- model.matrix(objects[[1]], mf, contrasts = objects[[1]]$contrasts)
y <- model.response(mf)
weights <- model.weights(mf)
tau <- taus[[1]]
for (i in 2:nobjects) {
if (!all(names[[i]] %in% names[[1]]))
stop("Models aren't nested")
mf <- model.frame(objects[[i]])
X0 <- model.matrix(objects[[i]], mf, contrasts = objects[[i]]$contrasts)
Htest <- rq.test.anowar(X0, X1, y, tau = tau,
R = R)
ndf[i - 1] <- Htest$ndf
Tn[i - 1] <- Htest$Tn
ddf[i - 1] <- Htest$ddf
pvalue[i - 1] <- Htest$pvalue
}
table <- data.frame(ndf, ddf, Tn, pvalue)
}
else if (test == "rank") {
x1 <- model.matrix(objects[[1]], mf, contrasts = objects[[1]]$contrasts)
y <- model.response(mf)
weights <- model.weights(mf)
for (i in 2:nobjects) {
if (!all(names[[i]] %in% names[[1]]))
stop("Models aren't nested")
nullH <- is.na(match(names[[1]], names[[i]]))
X1 <- as.matrix(x1[, nullH])
mf <- model.frame(objects[[i]])
X0 <- model.matrix(objects[[i]], mf, contrasts = objects[[i]]$contrasts)
if (score == "tau")
tau <- taus[[1]]
Htest <- rq.test.rank(X0, X1, y, score = score,
weights = weights, tau = tau, trim = trim)
ndf[i - 1] <- Htest$ndf
Tn[i - 1] <- Htest$Tn
ddf[i - 1] <- Htest$ddf
pvalue[i - 1] <- Htest$pvalue
}
table <- data.frame(ndf, ddf, Tn, pvalue)
}
else if (test == "Wald") {
V <- lapply(objects, function(x) x$cov)
coef <- lapply(objects, function(x) coef(x)[, 1])
for (i in 2:nobjects) {
if (!all(names[[i]] %in% names[[1]]))
stop("Models aren't nested")
nullH <- is.na(match(names[[1]], names[[i]]))
ndf[i - 1] <- sum(nullH)
Tn[i - 1] <- t((coef[[1]])[nullH]) %*% solve((V[[1]])[nullH,
nullH], (coef[[1]])[nullH])/ndf[i - 1]
ddf[i - 1] <- n - length(names[[1]])
pvalue[i - 1] <- 1 - pf(Tn[i - 1], ndf[i - 1],
ddf[i - 1])
}
table <- data.frame(ndf, ddf, Tn, pvalue)
}
else stop("test only defined for anowar, Wald and rank")
}
else {
m <- length(taus)
for (i in 2:m) {
if (!setequal(names[[i]], names[[1]]))
stop("Models with common tau don't have same X")
}
if (names[[1]][1] != "(Intercept)")
stop("Intercept required in common tau testing")
Omega <- outer(taus, taus, pmin) - outer(taus, taus)
J <- objects[[1]]$J
p <- dim(J)[1]
H <- array(unlist(lapply(objects, function(x) x$Hinv)),
c(p, p, m))
H <- matrix(aperm(H, c(1, 3, 2)), p * m, p) %*% t(chol(J))
W <- (H %*% t(H)) * (kronecker(Omega, outer(rep(1, p),
rep(1, p))))
coef <- unlist(lapply(objects, function(x) coef(x)[,
1]))
if (joint) {
D <- kronecker(diff(diag(m)), cbind(0, diag(p - 1)))
ndf <- (p - 1) * (m - 1)
Tn <- t(D %*% coef) %*% solve(D %*% W %*% t(D), D %*%
coef)/ndf
ddf <- n * m - (p - 1) * (m - 1)
pvalue <- 1 - pf(Tn, ndf, ddf)
nobjects <- 1
tnote1 <- paste("Model: ", models[[1]], "\n", sep = "")
tnote2 <- paste("Joint Test of Equality of Slopes: tau in { ",
paste(taus, collapse = " "), " }\n")
topnote <- paste(tnote1, tnote2, sep = "")
table <- data.frame(ndf, ddf, Tn, pvalue)
}
else {
Tn <- pvalue <- rep(0, p - 1)
ndf <- m - 1
ddf <- n * m - (m - 1)
for (i in 2:p) {
E <- matrix(0, 1, p)
E[1, i] <- 1
D <- kronecker(diff(diag(m)), E)
Tn[i - 1] <- t(D %*% coef) %*% solve(D %*% W %*%
t(D), D %*% coef)/ndf
pvalue[i - 1] <- 1 - pf(Tn[i - 1], ndf, ddf)
}
tnote1 <- paste("Model: ", models[[1]], "\n", sep = "")
tnote2 <- paste("Tests of Equality of Distinct Slopes: tau in { ",
paste(taus, collapse = " "), " }\n")
topnote <- paste(tnote1, tnote2, sep = "")
table <- data.frame(ndf, ddf, Tn, pvalue)
dimnames(table)[[1]] <- names[[1]][2:p]
}
}
x <- list(table = table, topnote = topnote)
class(x) <- "anova.rq"
return(x)
}
呵呵,看来rq.sfn.fit这个函数还是存在很大缺陷,主要还是用rq()函数


雷达卡
京公网安备 11010802022788号







