Docsity

Building and Applying Logistic Regression Models - Notes | STAT 870, Study notes of Statistics

Toggle sidebar
bg1
5. Building and applying logistic regression models
We will start with Section 5.2 and then go back for Section
5.1.
5.1 Model Checking
Determine how well the model fits the data.
Explanatory variable pattern (EVP) form
In the placekicking data set (consider ONLY distance
and good1 variables), each of the 1,425 rows
represented one observation. For example, the first
observation was a placekick attempted at 21 yards. The
“good1” variable represents a Bernoulli observation – 0
for a failure and 1 for a success. When examining
goodness-of-fit measures and residuals, it is often better
to examine the data in “explanatory variable pattern”
form. This format has one row in the data set for each of
the unique sets of explanatory variables. For example,
there are 20 observations with distance=21 in the
placekicking data set. Of the 20, 19 are successes.
Therefore, the response variable is now binomial instead
of Bernoulli.
Hosmer and Lemeshow (2000) call explanatory variable
pattern form “covariate pattern form”.
2009 Christopher R. Bilder
5.1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47

Partial preview of the text

Download Building and Applying Logistic Regression Models - Notes | STAT 870 and more Study notes Statistics in PDF only on Docsity!

5. Building and applying logistic regression models

We will start with Section 5.2 and then go back for Section

5.1 Model Checking

Determine how well the model fits the data.

Explanatory variable pattern (EVP) form

In the placekicking data set (consider ONLY distance

and good1 variables), each of the 1,425 rows

represented one observation. For example, the first

observation was a placekick attempted at 21 yards. The

“good1” variable represents a Bernoulli observation – 0

for a failure and 1 for a success. When examining

goodness-of-fit measures and residuals, it is often better

to examine the data in “explanatory variable pattern”

form. This format has one row in the data set for each of

the unique sets of explanatory variables. For example,

there are 20 observations with distance=21 in the

placekicking data set. Of the 20, 19 are successes.

Therefore, the response variable is now binomial instead

of Bernoulli.

Hosmer and Lemeshow (2000) call explanatory variable

pattern form “covariate pattern form”.

Example: Placekicking (placekick_ch5_pattern.R,

place.s.csv)

R code:

library(nlme) place.small<-data.frame(good=place.s$good1, dist=place.s$dist) place.sum<-gsummary(object = place.small, FUN = sum, groups = place.small$dist) place.length<-gsummary(object = place.small, FUN = length, groups = place.small$dist) place.pattern <- data.frame(y = place.sum$good, fail = place.length$good - place.sum$good, n = place.length$good, distance = place.sum$dist)

The explanatory variable pattern form of the data set is

below. Y is the observed binomial value and n is the total

number of trials.

place.pattern y fail n distance 1 2 1 3 18 2 7 0 7 19 3 776 13 789 20 4 19 1 20 21 5 12 2 14 22 6 26 1 27 23 7 7 0 7 24 8 12 1 13 25 9 8 1 9 26 10 24 5 29 27 11 20 2 22 28 12 16 1 17 29 13 12 2 14 30 14 10 1 11 31 15 23 7 30 32 16 20 1 21 33

mod.fit <- glm(formula = y/n ~ distance, data = place.pattern, weight = n, family = binomial(link = logit), na.action = na.exclude, control = list(epsilon = 0.0001, maxit = 50, trace = T)) Deviance = 47.12782 Iterations - 1 Deviance = 44.52325 Iterations - 2 Deviance = 44.49945 Iterations - 3 Deviance = 44.49945 Iterations - 4 summary(mod.fit) Call: glm(formula = y/n ~ distance, family = binomial(link = logit), data = place.pattern, weights = n, na.action = na.exclude, control = list(epsilon = 1e-04, maxit = 50, trace = T)) Deviance Residuals: Min 1Q Median 3Q Max -2.0373 -0.6449 -0.1424 0.5004 2. Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) 5.812080 0.326245 17.82 <2e-16 *** distance -0.115027 0.008338 -13.80 <2e-16 ***


Signif. codes: 0 ***' 0.001**' 0.01 *' 0.05.' 0.1 ` ' 1 (Dispersion parameter for binomial family taken to be 1) Null deviance: 282.181 on 42 degrees of freedom Residual deviance: 44.499 on 41 degrees of freedom AIC: 148. Number of Fisher Scoring iterations: 4

Pearson residuals and measures for goodness of fit

A simple measure of goodness of fit is to plot the

estimated proportions versus the explanatory variable.

The plotting point can be made to be proportional to the

sample size for each explanatory variable pattern. See

Chapter 4 for an illustration.

In the placekicking example, the explanatory variable

pattern form of the data creates a 432 contingency of

responses. The residuals for each cell can be found.

The residuals result from the observe cell count minus

the estimated cell count using the model fit.

For example, the predicted probability at

distance=21 is

exp[5.8120 0.1150(21)]

1 exp[5.8120 0.1150(21)]

For 20 placekicks, the estimated number that will be

successful is 0.967620 = 19.35. The estimated

number of failures is 20-19.35 = 0.65.

The residual for the successes is 19 – 19.35 = -0.

and the residual for the failures is 1 – 0.65 = 0.

Instead of using both columns (success and failure) of

the table, only the success column is used.

y n ˆ 19 20 0.

e 0.

n ˆ (1 ˆ ) 20 0.9676(1 0.9676)

Why should the Pearson residual be defined as shown

here instead of defined using the Bernoulli form of the

data set?

In order to find outliers, one would like to use the

standard normal approximation to the distribution of

the Pearson residual. One could conclude outliers

are outside of -1.96 or 1.96 (for 95% confidence) or

-2.576 to 2.576 (for 99% confidence). This works

fine provided a moderate to large sample size is

available for each explanatory variable pattern. With

the Bernoulli data, the sample size is 1 trial for each

residual.

With the explanatory variable pattern form (binomial)

version of the data, there is at least a possibility the

sample size (nj) may be large enough for the normal

approximation to work. Unfortunately with a

“continuous” explanatory variable, this will usually not be

the case. Therefore, one needs be careful with

interpreting Pearson residuals.

Pearson residuals outside of 2.576 may need to be

investigated for being potential outliers. However,

care needs to be used in saying which explanatory

variable patterns are actually detrimental to the

model.

In the derivation of the Pearson residual, the estimated

standard deviation of yj is used in the denominator. This

derivation treats n^ j ˆ^ j as a constant, not as a random

variable (which it is). The standardized residual treats

the n^ j ˆ^ j as a random variable which allows for it to be

more closely approximated by a standard normal

distribution. The standardized residual is:

j j j j

j

j j j j j

e y n ˆ

e

1 h n ˆ (1 ˆ )(1 h )

where hj is the j

th

diagonal value from the hat matrix.

Let X be the matrix of the explanatory variable

patterns with 1’s in the first column. Create a

diagonal matrix, V ˆ , with diagonal elements of

n j ˆ j (1  ˆ j) in the same order as the corresponding

explanatory variable patterns listed in X. The hat

matrix is H =

V X ( X ^ V ˆ X )

X 

V. Note that this is

similar to the hat matrix used when fitting a

regression model by weighted least squares.

From Chapter 2: The Pearson residual and the

standardized residual are the equivalent of

semistudentized residuals and studentized residuals

For a review of influence measures used in regression

analysis, see my Section 10.1-10.3 and 10.4-10.6 notes

for the UNL STAT 870 course. These are accessible

from www.chrisbilder.com/stat870/schedule.htm.

To measure the influence of a particular explanatory

variable pattern, a model could be refit without this

explanatory variable pattern and X

2

could be

recalculated. The amount of change from the Pearson

statistic with all J explanatory variable patterns and with

just J-1 would be of interest. Large values would

indicate that the particular explanatory variable pattern is

“influential”.

As in regression analysis, only one model actually needs

to be fit and the diagonal of the hat matrix is used to

calculate the statistic of interest. The statistic that

measures the change in X

2

is

e j

 (squared standardized

residual)! Thus, the standardized residual plays two

different roles - it detects outliers and influential

explanatory variable patterns. Because of this

relationship, I will often just examine

e j

 instead of

ej

Since a square of standard normal random variable is a

random variable, values of

e j

 greater than

 0.95,1 3.84 or

 6.63 may indicate an outlier or

influential EVP.

A measure similar to the “DFBETAS” in regression

analysis can also be calculated. The measure is

j j

j

j

e h

(1 h )

Notice a subscript j is used for the measure to indicate

one value is calculated for EACH explanatory variable

pattern. The DFBETA in regression had a value

calculated for each observation AND for each . Thus,

j

^ ˆ

is an overall measure of influence on the ˆ^ ' s in the

model. In fact, this is very similar to Cook’s distance.

Note that these j

are the same as what Agresti (2007)

calls “confidence interval displacement diagnostic” on p.

Note that there is not a nice distributional approximation

for j

^ ˆ

as there is for

e j

 . “Large” values indicate an

explanatory variable pattern may be influential. To

assess how much an effect it has on the ˆ^ ' s, the pattern

should be TEMPORARILY removed from the data set

and the model refit. The percent change in the ˆ^ ' s can

be calculated as an assessment of influence.

Page 176 of Hosmer and Lemeshow (2000) has a nice

discussion on likely values for these influence measures.

normal is appropriate and

 1,0.95 =3.84 and

when a chi-square is appropriate are helpful in order to

view the standard normal or 

2

approximation.

The third, fourth, and fifth plots listed above are

especially helpful to diagnose if an explanatory variable

pattern is “really” something to be concerned about. For

example, some squared standardized residuals will often

be larger than 3.84 when ˆ^ j is small or large. This may

be due to the binary nature of the data.

Suppose ˆ^ j=0.9 for an explanatory variable pattern

with 1 success out of 3 trials. Then Pearson

residual is:

j j j

j

j j j

y n ˆ 1 3 0.

e -3.

n ˆ (1 ˆ ) 3 0.9(1 0.9)

Is this explanatory variable pattern really an outlier

or just a product of the data being binary? If the

model fits well for other explanatory variable

patterns with similar explanatory variable values,

then it probably is just a product of the data being

binary.

Example: Placekicking (placekick_ch5_pattern.R,

place.s.csv, examine_residuals.R)

To help automate investigating the residuals, I have

written a function called examine.resid(). This function

produces the following plots:

1.Pearson residual vs. EVP number (in the

explanatory variable pattern data set)

2.Standardized residual vs. EVP number

3.Standardized residual vs. predicted probabilities

4.Standardized residual vs. predicted probabilities with

the plotting point proportional to nj.

5. j

^ ˆ

vs. EVP number

6. j

^ ˆ

vs. predicted probabilities

7.Standardized residual vs. predicted probabilities with

the plotting point proportional to j

^ ˆ

The function also calculates the Pearson and LRT (to be

discussed later) statistics.

The function serves as an excellent example of how R

can be customized for your needs! The results of the

function will be first discussed and then the code in the

function will be discussed.

mod.fit <- glm(formula = y/n ~ distance, data = place.pattern, weight = n, family = binomial(link = logit), na.action = na.exclude, control = list(epsilon = 0.0001, maxit = 50, trace = T)) GLM linear loop 1: deviance = 173.

GLM linear loop 2: deviance = 60. GLM linear loop 3: deviance = 45. GLM linear loop 4: deviance = 44. GLM linear loop 5: deviance = 44.

0 10 20 30 40 0 5 10 15 20 25 Delta.beta vs. j j (explanatory variable pattern number) Delta.beta 0.2 0.4 0.6 0.8 1. 0 5 10 15 20 25 Delta.beta vs. pred. prob. Predicted probabilities Delta.beta 3 0.2 0.4 0.6 0.8 1. 0 5 10 15 Sq. standardized residuals vs. pred. prob. with plot point proportion to delta.beta Predicted probabilities Sq. standardized residuals X^2 = 56.11 with p-value = 0. G^2= 44.5 with p-value = 0.

The p-value for the Pearson statistic indicates the model

may not fit the data well. Since there are many

explanatory variable patterns with small sample sizes,

the chi-square approximation may not be appropriate.

There are a few EVPs that have standardized residuals

outside of 1.96. The

e j

 versus the estimated

probabilities plots help indicate why this is happening.

Many of the EVPs occur when the estimated probability

of success is large. Since there is only one explanatory

variable, a plot of the standardized residuals versus the

distance of the placekick can also be constructed:

20 30 40 50 60

0 2 Standardized Pearson residuals vs. Distance Distance Adj. Pearson residuals

The code to construct this plot is similar to the code used

in examine_residuals.R. The explanatory variable

patterns that are possible outliers are at 18, 20, 22, 27,

The 18 and 22 yard placekicks being potential outliers is

partially attributable to the data being binary – especially

the 18 yard kick. The 22, 27, 32, and 51 yard

standardized residuals are not too far past their

borderlines. The main concern for this data set is the 20

yard placekicks. Why do you think the model is not

fitting the data well at this distance?

The j

^ ˆ

plots are indicating that explanatory variable

pattern #3 is very influential. This pattern contains all

789 PATs. Since this is about ½ of the total placekicks,

it is to be expected that the pattern is influential.

Unfortunately, the large j

^ ˆ

distorts the plot. No

remedial action is taken here since a model which

incorporates more explanatory variables will be

discussed later.

Another good plot to examine when there is one

explanatory variable is shown below. This plot was

constructed in Chapter 4. Remember that the very large

distance placekicks do not have many observations.

Compare this plot to the table of observations given

previously.

10 20 30 40 50 60

Distance (yards) Estimated probability

Estimated probability of success of a placekick

with observed proportions

Below is the code in examine_residuals.R. I recommend

downloading the program from the course website instead

of copying and pasting what is below into R. A number of

students have tried the copying and pasting and then had

many problems running the program.