Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created fortify.glm #1002

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 56 additions & 0 deletions R/fortify-glm.r
@@ -0,0 +1,56 @@
#' Supplement the data fitted to a generalized linear model with
#' model fit statistics.
#'
#' If you have missing values in your model data, you may need to refit
#' the model with \code{na.action = na.exclude}.
#'
#'
#' @return The original data with extra columns:
#' \item{.hat}{Diagonal of the hat matrix}
#' \item{.sigma}{Estimate of residual standard deviation when
#' corresponding observation is dropped from model}
#' \item{.cooksd}{Cooks distance, \code{\link{cooks.distance}}}
#' \item{.fitted}{Fitted values of model}
#' \item{.resid}{Residuals}
#' \item{.stdresid}{Standardised residuals}
#' @param model linear model
#' @param data data set, defaults to data used to fit model
#' @param type the type of prediction required for fitted values.
#' See \code{link{predict.glm}}
#' @param ... not used by this method
#' @export
#' @examples
#'
#' ## Dobson (1990) Page 93: Randomized Controlled Trial :
#' counts <- c(18,17,15,20,10,20,25,13,12)
#' outcome <- gl(3,1,9)
#' treatment <- gl(3,3)
#' d.AD <- data.frame(treatment, outcome, counts)
#' glm.D93 <- glm(counts ~ outcome + treatment, family = poisson())
#'
#' head(fortify(glm.D93))
#' ## .fitted defaults to type = "link"
#' head(fortify(glm.D93, type = "response"))
#'
#' qplot(.fitted, .resid, data = mod) +
#' geom_hline(yintercept = 0) +
#' geom_smooth(se = FALSE)
#'
#'## Defaults make no sense to compare fitted vs actual values
#' qplot(.fitted, counts, data = mod) +
#' geom_smooth(se = FALSE)
#'
#' qplot(.fitted, counts, data = fortify(mod, type = "response")) +
#' geom_smooth(se = FALSE)
#'
fortify.glm <- function (model, data = model$model, type = "link", ...)
{
infl <- influence(model, do.coef = FALSE)
data$.hat <- infl$hat
data$.sigma <- infl$sigma
data$.cooksd <- cooks.distance(model, infl)
data$.fitted <- predict(model, type = type)
data$.resid <- resid(model)
data$.stdresid <- rstandard(model, infl)
data
}