]> git.donarmstrong.com Git - ape.git/blob - R/compar.gee.R
new mixedFontLabel() + bug fix in rTraitCont.c
[ape.git] / R / compar.gee.R
1 ## compar.gee.R (2008-02-21)
2
3 ##   Comparative Analysis with GEEs
4
5 ## Copyright 2002-2008 Emmanuel Paradis
6
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
9
10 compar.gee <- function(formula, data = NULL, family = "gaussian", phy,
11                        scale.fix = FALSE, scale.value = 1)
12 {
13     require(gee, quietly = TRUE)
14     if (is.null(data)) data <- parent.frame() else {
15         if(!any(is.na(match(rownames(data), phy$tip.label))))
16           data <- data[phy$tip.label, ]
17         else warning("the rownames of the data.frame and the tip labels of the tree
18 do not match: the former were ignored in the analysis.")
19     }
20     effect.assign <- attr(model.matrix(formula, data = data), "assign")
21     for (i in all.vars(formula)) {
22         if (any(is.na(eval(parse(text = i), envir = data))))
23           stop("the present method cannot (yet) be used directly with missing data: you may consider removing the species with missing data from your tree with the function 'drop.tip'.")
24     }
25     if (is.null(phy$edge.length)) stop("the tree has no branch lengths.")
26     R <- vcv.phylo(phy, cor = TRUE)
27     id <- rep(1, dim(R)[1])
28     geemod <- do.call("gee", list(formula, id, data = data, family = family, R = R,
29                                   corstr = "fixed", scale.fix = scale.fix,
30                                   scale.value = scale.value))
31     W <- geemod$naive.variance
32     fname <-
33         if (is.function(family)) deparse(substitute(family)) else family
34     if (fname == "binomial")
35         W <- summary(glm(formula, family = quasibinomial, data = data))$cov.scaled
36     N <- geemod$nobs
37     dfP <- sum(phy$edge.length)*N / sum(diag(vcv.phylo(phy)))
38     obj <- list(call = geemod$call,
39                 effect.assign = effect.assign,
40                 nobs = N,
41                 coefficients = geemod$coefficients,
42                 residuals = geemod$residuals,
43                 family = geemod$family$family,
44                 link = geemod$family$link,
45                 scale = geemod$scale,
46                 W = W,
47                 dfP = dfP)
48     class(obj) <- "compar.gee"
49     obj
50 }
51
52 print.compar.gee <- function(x, ...)
53 {
54     nas <- is.na(x$coef)
55     coef <- x$coef[!nas]
56     cnames <- names(coef)
57     coef <- matrix(rep(coef, 4), ncol = 4)
58     dimnames(coef) <- list(cnames,
59                            c("Estimate", "S.E.", "t", "Pr(T > |t|)"))
60     df <- x$dfP - dim(coef)[1]
61     coef[, 2] <- sqrt(diag(x$W))
62     coef[, 3] <- coef[, 1]/coef[, 2]
63     if (df < 0) {
64         warning("not enough degrees of freedom to compute P-values.")
65         coef[, 4] <- NA
66     } else coef[, 4] <- 2 * (1 -  pt(abs(coef[, 3]), df))
67     residu <- quantile(as.vector(x$residuals))
68     names(residu) <- c("Min", "1Q", "Median", "3Q", "Max")
69     cat("\nCall:\n")
70     cat("  formula: ")
71     print(x$call$formula)
72     cat("\nNumber of observations: ", x$nobs, "\n")
73     cat("\nModel:\n")
74     cat(" Link:                     ", x$link, "\n")
75     cat(" Variance to Mean Relation:", x$family, "\n")
76     cat("\nSummary of Residuals:\n")
77     print(residu)
78     if (any(nas))
79         cat("\n\nCoefficients: (", sum(nas), " not defined because of singularities)\n",
80             sep = "")
81     else cat("\n\nCoefficients:\n")
82     print(coef)
83     cat("\nEstimated Scale Parameter: ", x$scale)
84     cat("\n\"Phylogenetic\" df (dfP): ", x$dfP, "\n")
85 }
86
87 drop1.compar.gee <- function(object, scope, quiet = FALSE, ...)
88 {
89     fm <- formula(object$call)
90     trm <- terms(fm)
91     z <- attr(trm, "term.labels")
92     ind <- object$effect.assign
93     n <- length(z)
94     ans <- matrix(NA, n, 3)
95     for (i in 1:n) {
96         wh <- which(ind == i)
97         ans[i, 1] <- length(wh)
98         ans[i, 2] <- t(object$coefficients[wh]) %*%
99           solve(object$W[wh, wh]) %*% object$coefficients[wh]
100     }
101     df <- object$dfP - length(object$coefficients)
102     if (df < 0) warning("not enough degrees of freedom to compute P-values.")
103     else ans[, 3] <- pf(ans[, 2], ans[, 1], df, lower.tail = FALSE)
104     colnames(ans) <- c("df", "F", "Pr(>F)")
105     rownames(ans) <- z
106     if (any(attr(trm, "order") > 1) && !quiet)
107       warning("there is at least one interaction term in your model:
108 you should be careful when interpreting the significance of the main effects.")
109     class(ans) <- "anova"
110     attr(ans, "heading") <- c("Single term deletions\n\nModel:\n",
111                               as.character(as.expression(fm)))
112     ans
113 }