# Logistic Regression: Classification Tables a la SPSS in R

In SPSS output there is a pretty little classification table available when you perform a logistic regression, is the same possible with R? If so, how?

-
 You should post an example of the SPSS output you want to replicate in R, or explicitly detail what the SPSS output is reporting – Andy W Nov 23 '10 at 17:48 Good idea, output added for clarity. Thanks for the suggestion Andy. – Brandon Bertelsen Nov 23 '10 at 17:55

I'm not aware of a specific command, but this might be a start:

# generate some data
> N <- 100
> X <- rnorm(N, 175, 7)
> Y <- 0.4*X + 10 + rnorm(N, 0, 3)

# dichotomize Y
> Yfac <- cut(Y, breaks=c(-Inf, median(Y), Inf), labels=c("lo", "hi"))

# logistic regression
> glmFit <- glm(Yfac ~ X, family=binomial(link="logit"))

# predicted probabilities
> Yhat <- fitted(glmFit)

# choose a threshold for dichotomizing according to predicted probability
> thresh  <- 0.5
> YhatFac <- cut(Yhat, breaks=c(-Inf, thresh, Inf), labels=c("lo", "hi"))

# contingency table and marginal sums
> cTab <- table(Yfac, YhatFac)
YhatFac
Yfac   lo  hi Sum
lo   36  14  50
hi   12  38  50
Sum  48  52 100

# percentage correct for training data
> sum(diag(cTab)) / sum(cTab)
[1] 0.74

-
 Thanks Caracal, let me try this out on my data – Brandon Bertelsen Nov 23 '10 at 18:05 How would I generate Yfac if my variable is already a 1/2 dichotomous variable saved as a factor in R? – Brandon Bertelsen Nov 23 '10 at 22:27 Nevermind, worked like a charm, just set Yfac <- df\$dichotomous.var – Brandon Bertelsen Nov 23 '10 at 22:32

Thomas D. Fletcher has a function called ClassLog() (for "classification analysis for a logistic regression model") in his QuantPsyc package. However, I like @caracal's response because it is self-made and easily customizable.

-