[R] Coefficient of determination for generalized linear models
eric.e.harper at us.abb.com
eric.e.harper at us.abb.com
Wed Apr 9 17:25:55 CEST 2008
Asking the question usually makes one think about the workarounds J.
I reviewed the pscl source and found the code for pR2. Sourcing the
following internal functions allowed me to compute the pseudo R2 measures.
pR2Work <- function(llh,llhNull,n){
McFadden <- 1 - llh/llhNull
G2 <- -2*(llhNull-llh)
r2ML <- 1 - exp(-G2/n)
r2ML.max <- 1 - exp(llhNull*2/n)
r2CU <- r2ML/r2ML.max
out <- c(llh=llh,
llhNull=llhNull,
G2=G2,
McFadden=McFadden,
r2ML=r2ML,
r2CU=r2CU)
out
}
pR2.glm <- function(object,...){
llh <- logLik(object)
objectNull <- update(object, ~ 1)
llhNull <- logLik(objectNull)
n <- dim(object$model)[1]
pR2Work(llh,llhNull,n)
}
Browse[1]> pR2.glm(fit[[2]])
llh llhNull G2 McFadden
r2ML r2CU
-1658.5890626 -2874.0411606 2430.9041961 0.4229070 0.9472917
0.9481926
Browse[1]>
Question 3: Is there a way to access the pR2.glm function from the library
without using the source?
______________________________________________________________________
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Eric E Harper/USABB/ABB
Sent: Wednesday, April 09, 2008 10:47 AM
To: r-help at r-project.org
Cc: Emanuel Kolb/DECRC/ABB; Hans-Werner Borchers/DECRC/ABB
Subject: [R] Coefficient of determination for generalized linear models
Thanks in advance for your kind attention.
I am using R to fit empirical data to generalized linear models. AIC (Akaike
information criterion) is a measure of the goodness of fit returned by calls
to glm(). I would also like to calculate the coefficient of determination
R2, although there is no consensus about the exact definition for
generalized models
([1]http://en.wikipedia.org/wiki/Coefficient_of_determination).
I found a package Ã¢â‚¬Å“psclÃ¢â‚¬Â with a pR2 function that computes pseudo-R2
measures for various GLMs. The arguments to the call are a fitted model
object of class glm, polr, or mulitnom, and then Ã¢â‚¬Ëœadditional arguments to
be
passed to or from functionsÃ¢â‚¬â„¢.
The example from the documentation works well.
Browse[1]> data(admit)
Browse[1]> require(MASS)
Browse[1]> ## ordered probit model
Browse[1]> op1 <- polr(score ~ gre.quant + gre.verbal + ap + pt + female,
+ Hess=TRUE,
+ data=admit,
+ method="probit")
Browse[1]> pR2(op1)
llh llhNull G2 McFadden r2ML
r2CU
-106.5088203 -151.0299826 89.0423245 0.2947836 0.5682989
0.6032041
Browse[1]>
When I try with a glm object rather than polr, I get the following error:
Browse[1]> class(fit[[2]])
[1] "glm" "lm"
Browse[1]> pR2(fit[[2]])
Error in inherits(x, "data.frame") : object "ds" not found
The ds object does exist in the environment, but I do not know how to pass
it into pR2:
Browse[1]> class(ds)
[1] "data.frame"
Browse[1]> pR2(fit[[2]], ds)
Error in inherits(x, "data.frame") : object "ds" not found
Browse[1]> pR2
function (object, ...)
{
UseMethod("pR2")
}
Browse[1]>
Question 1: How do I find the complete argument signature for pR2 in order
to perhaps pass it the ds object?
Question 2: If pR2 does not work with glm objects (for some unknown reason),
is there another function I can use to calculate R-squared and adjusted
R-squared for a generalized linear model?
Best regards,
\Eric
References
1. http://en.wikipedia.org/wiki/Coefficient_of_determination
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list