[R] A suggestion for predict function(s)

Ross Darnell r.darnell at uq.edu.au
Wed Apr 13 06:57:55 CEST 2005


Maybe a useful addition to the predict functions would be to return the 
values of the predictor variables. It just (unless there are problems) 
requires an extra line. I have inserted an example below.

"predict.glm" <-
   function (object, newdata = NULL, type = c("link", "response",
                                       "terms"), se.fit = FALSE, 
dispersion = NULL, terms = NULL,
             na.action = na.pass, ...)
{
   type <- match.arg(type)
   na.act <- object$na.action
   object$na.action <- NULL
   if (!se.fit) {
     if (missing(newdata)) {
       pred <- switch(type, link = object$linear.predictors,
                      response = object$fitted, terms = predict.lm(object,
                                                  se.fit = se.fit, scale 
= 1, type = "terms",
                                                  terms = terms))
       if (!is.null(na.act))
         pred <- napredict(na.act, pred)
     }
     else {
       pred <- predict.lm(object, newdata, se.fit, scale = 1,
                          type = ifelse(type == "link", "response", type),
                          terms = terms, na.action = na.action)
       switch(type, response = {
         pred <- family(object)$linkinv(pred)
       }, link = , terms = )
     }
   }
   else {
     if (inherits(object, "survreg"))
       dispersion <- 1
     if (is.null(dispersion) || dispersion == 0)
       dispersion <- summary(object, dispersion = dispersion)$dispersion
     residual.scale <- as.vector(sqrt(dispersion))
     pred <- predict.lm(object, newdata, se.fit, scale = residual.scale,
                        type = ifelse(type == "link", "response", type),
                        terms = terms, na.action = na.action)
     fit <- pred$fit
     se.fit <- pred$se.fit
     switch(type, response = {
       se.fit <- se.fit * abs(family(object)$mu.eta(fit))
       fit <- family(object)$linkinv(fit)
     }, link = , terms = )
     if (missing(newdata) && !is.null(na.act)) {
       fit <- napredict(na.act, fit)
       se.fit <- napredict(na.act, se.fit)
     }
     predictors <- if (missing(newdata)) model.matrix(object) else newdata
     pred <- list(predictors=predictors,
                  fit = fit, se.fit = se.fit,
                  residual.scale = residual.scale)
   }
   pred


#______________________ end of R code



Ross Darnell
-- 
School of Health and Rehabilitation Sciences
University of Queensland, Brisbane QLD 4072 AUSTRALIA
Email: <r.darnell at uq.edu.au>
Phone: +61 7 3365 6087     Fax: +61 7 3365 4754  Room:822, Therapies Bldg.
http://www.shrs.uq.edu.au/shrs/school_staff/ross_darnell.html




More information about the R-help mailing list