[R] Re:logistic regression
Vito Ricci
vito_ricci at yahoo.com
Tue Feb 8 12:20:16 CET 2005
Hi,
I don't know if a pseudo squared R for glm exists in
any R package, but I find some interesting functions
in S mailing list:
http://www.math.yorku.ca/Who/Faculty/Monette/S-news/0422.html
Here are some functions for calculating (pseudo-)R^2,
which may be of use to some.
Rsquared <- function(object)
{
# object is an lm, glm or gam object
# Rsquared() is implemented only for glm or gam
objects with
# one of the three link/variance combinations:
# (1) log and mu (which is canonical for Poisson),
# (2) logit and mu(1-mu) (which is canonical for
binomial), or
# (3) identity and constant (which is canonical for
gaussian).
# However these link/variance pairs may have been
passed to quasi()
# to allow for overdispersion.
UseMethod("Rsquared")
}
Rsquared.lm <-
function(o)
{
R2 <- summary.lm(o)$r.squared
names(R2) <- "Rsquared"
R2
}
Rsquared.glm <-
function(o)
{ def <- family(o)$family[-1] # character vector of
link and variance
typ <- matrix(c("Logit: log(mu/(1 - mu))", "Log:
log(mu)", "Identity: mu",
"Binomial: mu(1-mu)", "Identity: mu", "Constant: 1" ),
2, 3, byrow=T) #
# typ is matrix of supported link and variance
combinations
ml <- match(def[1],typ[1,], nomatch=-1)
mv <- pmatch(def[2],typ[2,], nomatch=-1)
if ( (ml != mv) || (ml <1 ) )
stop("Implemented only for canonical links for
gaussian, binomial or poisson
(with optional provision for overdispersion using
quasi)")
if (ml == 3)
return(Rsquared.lm(o)) # gaussian family
# Remainder of code is for binomial and poisson
families (perhaps with provision for overdispersion)
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
}
http://www.math.yorku.ca/Who/Faculty/Monette/S-news/0408.html
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
}
I don't know if S code is completely running in R
environment.
See:
http://www.econ.ucdavis.edu/faculty/cameron/research/je97preprint.pdf
Hoping I helped you!
you wrote:
Hi,
I'm using glm function to do logistic regression and
now I want to know if it exists a kind of R-squared
with this function in order to check the model.
Thank you.
=====
Diventare costruttori di soluzioni
Became solutions' constructors
"The business of the statistician is to catalyze
the scientific learning process."
George E. P. Box
Top 10 reasons to become a Statistician
1. Deviation is considered normal
2. We feel complete and sufficient
3. We are 'mean' lovers
4. Statisticians do it discretely and continuously
5. We are right 95% of the time
6. We can legally comment on someone's posterior distribution
7. We may not be normal, but we are transformable
8. We never have to say we are certain
9. We are honestly significantly different
10. No one wants our jobs
Visitate il portale http://www.modugno.it/
e in particolare la sezione su Palese http://www.modugno.it/archivio/palese/
More information about the R-help
mailing list