[R] A suggestion for predict function(s)

From: Ross Darnell <r.darnell_at_uq.edu.au>
Date: Wed 13 Apr 2005 - 14:57:55 EST


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@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

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Wed Apr 13 15:05:26 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:07 EST