[S] functions for (pseudo-)R^2

Terry Elrod (Terry.Elrod@UAlberta.ca)
Fri, 27 Feb 1998 03:25:50 -0700


Here are some functions for calculating (pseudo-)R^2, which may be of use to some.

Rsquared <- function(o){
# o is an lm, glm or gam object
UseMethod("Rsquared")
}

Rsquared.lm _ function(o) {
R2 <- summary(o)$r.squared
names(R2) <- 'Rsquared'
R2
}

Rsquared.glm <- function(o) {
n <- length(o$residuals) # number of observations
R2 <- ( 1 - exp( (o$deviance - o$null.deviance)/n ) ) / ( 1 - exp( -o$null.deviance/n ) )
names(R2) <- 'pseudo.Rsquared'
R2
}

Rsquared.lm simply returns the ordinary R^2 for an lm object. However, using the R^2 formula for glm or gam objects (with discrete families) gives a value with a theoretical lower bound of zero but upper bound that is less than one. Pseudo-R^2 divides the ordinary R^2 statistic by its theoretical upper bound to yield a statistic with bounds [0,1].

The formulae may be used on lm, glm and gam objects. (gam objects will call Rsquared.glm.) The formula for pseudo-R^2 is taken from G. S. Maddalla, Limited-dependent and Qualitative Variables in Econometrics, Cambridge: Cambridge Univ. Press, 1983. page 40, equation 2.50.

These functions have been tested with lm and with glm families poisson and binomial.

Terry Elrod
--------
Prof. Terry Elrod; 3-23 Fac. of Business; U. of Alberta; Edmonton AB; Canada T6G 2R6
email: Terry.Elrod@Ualberta.ca; tel: (403) 492-5884; fax: (403) 492-3325
Web page: http://www.ualberta.ca/~telrod/
--------

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news