

























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


























































































3. Generalized linear models
Use models to investigate the relationships (associations)
among categorical and continuous variables.
Reason for using models (p. 65):
Helps describe the pattern of association and
interaction
Inferences for model parameters help determine which
explanatory variables affect the response while
controlling for other variables
Estimate model parameters to determine the strength
and importance of effects
Models can more easily handle complicated problems
A general class of models are “generalized linear models”
(GLMs). You have already studied a special case of GLMs,
linear models, previously in regression and ANOVA courses.
In addition to Agresti (2002), other references on GLMs
include:
McCullagh, P. and Nelder, J. A. (1989). Generalized
Linear Models. 2
nd
edition. London: Chapman and Hall.
McCulloch, C. and Searle, S. R. (2000). Generalized,
Linear, and Mixed Models. New York: Wiley.
3.1 Components of a generalized linear model
Review of regression models
i
0
1
x
i
2
x
i
p-
x
ik
i
where
i
~independent N(0,
2
Note that
i
0
1
x
i
2
x
i
p-
x
ik
i
) is what one would expect Y
i
to be on average for a
set of x
i
, x
i
, …, x
ik
values.
One of the important things to realize here is that Y has a
normal distribution. What if this is not true? Suppose Y
is a nominal categorical variable. Suppose Y has a
Poisson distribution. There are many other possibilities.
GLMs allow us to generalize the model structure!
Three components of a GLM
Random
For a sample of size n, denote the observations of the
response variable Y as Y
1
2
n
. Assume Y
1
2
n
are obtained independently here. We will specifically
be interested in the E(Y)=.
that the x’s above could be a transformation of an original
explanatory variable(s) – such as a quadratic or
interactions.
Link
This component “links” the random and systematic
component. In other words, this shows how the mean of
the distribution for Y is related to the explanatory variables.
Let g() be a function of the E(Y)=. This is the link
function. Specifically, the GLM is
g() = +
1
x
1
2
x
2
k
x
k
Link functions:
Identity – g() =
1
x
1
2
x
2
k
x
k
This is used for regression and ANOVA models!
Log – g() = log()
log() = +
1
x
1
2
x
2
k
x
k
= exp( + 1
x 1
2
x 2
k
x k
The log link is used for “loglinear” models in Chapter 7.
Most often, Y is assumed to have a Poisson distribution.
Notice that all values of will be positive. This is why
the link is used with modeling counts in a contingency
table!
Logit – g() =
log
= logit()
log
1
x
1
2
x
2
k
x
k
1 1 2 2 k k
1 1 2 2 k k
exp( x x ... x )
1 exp( x x ... x )
The logit link is used for “logit” and logistic regression
models in Chapters 4-5. Notice that all values of will
be between 0 and 1 (try a few sample cases to see this).
This is why the link is used with modeling probabilities!
Remember that the mean of a Bernoulli random variable
is .
Other links are possible such as the probit and
complementary log-log. These will be discussed later.
Read Section 3.1.4 about the normal GLM on p.67-8.
a 50 yard field goal. Thus, it would be of interest to
incorporate length of a field goal in a model for .
Notation: Agresti (2007) uses (x) to denote here. The
reason is because explanatory variables (x’s) will be
used to try to predict the value of . Thus, “depends”
on the level of the explanatory variables.
To simplify the upcoming discussion, only one explanatory
variable, x, will be used to model the probability of success,
(x).
Linear probability model
Suppose an ordinary regression model was used to
model the probability of success. Thus,
E(Y) = (x) = + x
This is called a linear probability model because the
probability of success changes in a linear manner.
Problems with model:
o Violates the distributional assumptions for Y. Y is
Bernoulli, not normal.
o Probabilities can be less than 0 or greater than 1!
o Non constant variance – Var(Y) = (x)(1-(x));
variance changes as a function of x
Therefore, do not use this model!
Logistic regression model
A great introductory reference on logistic regression is
Hosmer, D. W. and Lemeshow, S. (2000). Applied
Logisitic Regression, 2
nd
edition. New York: Wiley.
Many STAT 870 books also will include a chapter on
logistic regression. For example, see Chapter 14 of
Kutner, Nachtsheim, and Neter (2004).
The model is
(x)
log
1 (x)
= logit[(x)] = + x
The random component is Bernoulli. The logit
transformation is the link function. The model can be
equivalently written as:
x
x
e
(x)
1 e
What does a plot of (x) vs. x look like?
R code:
alpha<-
beta1<-0.
par(pty="s")
curve(expr = exp(alpha+beta1x)/(1+exp(alpha+beta1x)),
from = -15, to = 15, col = "red", main =
expression(pi(x) == frac(e^{alpha+beta*x},
1+e^{alpha+beta*x})), xlab = "x",
ylab = expression(pi(x)), panel.first = grid(nx =
NULL, ny = NULL, col = "gray", lty = "dotted"))
#See help(plotmath) for more on the expression function
and see demo(plotmath)
Notes:
When >0, there is a positive relationship between x
and (x). When <0, there is a negative relationship
between x and (x).
The shape of the function is similar to an “s”.
Notice the symmetric shape about (x) = 0.
0<(x)<
Questions:
What happens to the =0.5 plot when is
increased?
What happens to the =0.5 plot when is
decreased to be close to 0?
Suppose a plot of logit[(x)] vs. x was made. What
would the plot look like?
Parameter estimation
Suppose there is a random sample of size n providing
(y
1
, x
1
), (y
2
, x
2
), …, (y
n
, x
n
) where the y
i
’s are 0’s or 1’s.
The probability of observing a 1 for y
i
is denoted by
i
(x).
The logistic regression model is
i
i
(x)
log
1 (x)
= + x
i
for i=1,…,n
This is the assumed relationship between the x
i
and
i
(x). The model can be rewritten as
i
(x) =
i
i
exp( x )
1 exp( x )
Parameter estimates can be found from maximum
likelihood estimation – see Chapter 1’s discussion.
i i
1 n 1 n
n
i
i 1
n
y 1 y
i i
i 1
( (x),..., (x) | y ,...,y )
f(y )
(x) (1 (x))
Then the log likelihood function is
N different
parameters
Example: Placekicking (placekick_ch3.R, place.s.csv)
See Bilder and Loughin ( Chance , 1998) and the video!
The purpose of this example is to estimate the
probability of success for a placekick in football. The
place.s.csv data file contains a sample of 1,
placekicks attempted during the 1995 National Football
League season. Below is a brief description of the
variables in the data set:
week = Week of the season
dist = distance of the placekick in yards
change = binary variable denoting lead-change
placekicks (1) vs. non lead-change (0) placekicks
elap30 = continuous variable denoting the number of
minutes left in a half with overtime placekicks
assigned a value of 0
pat1 = binary variable for whether the placekick is a
point after touchdown (1) or a field goal (0)
type1 = binary variable for placekicks in a dome (0)
or outdoors (1)
field 1 = binary variable for placekicks on grass (1)
or artificial turf (0)
good1 = binary variable for placekicks which are
successes (1) or failures (0)
wind = binary variable for placekicks attempted in
“windy” conditions (1) at kickoff versus non-windy
conditions (0) using a 15 mph cutoff for non-windy
The data was actually first stored in an Excel file. While
one can use the xlsReadWrite or RODBC packages to
read in an Excel file (see R introduction lecture), I used a
different method to read in the file. First, I re-saved the
Excel file as a .csv format. To do this, select FILE >
SAVE AS in Excel. Then select the .csv format in the
SAVE AS TYPE box. Choose a file name and then
select SAVE.
This creates an ASCII text file which has commas
separating each variable.
where x=distance of the placekick, (x) = E(Y), Y=
for success or 0 for failure.
This particular GLM is used since the response variable
(good1) is binary. To find the estimated model in R, the
glm() function is used. Below is the code.
mod.fit <- glm(formula = good1 ~ dist, data = place.s,
family = binomial(link = logit), na.action = na.exclude,
control = list(epsilon = 0.0001, maxit = 50, trace = T))
Deviance = 836.7715 Iterations - 1
Deviance = 781.1072 Iterations - 2
Deviance = 775.8357 Iterations - 3
Deviance = 775.7451 Iterations - 4
Deviance = 775.745 Iterations - 5
names(mod.fit)
[1] "coefficients" "residuals" "fitted.values"
"effects" "R" "rank"
"qr" "family" "linear.predictors"
"deviance" "aic" "null.deviance"
"iter" "weights" "prior.weights"
"df.residual" "df.null" "y"
"converged" "boundary" "model"
"call" "formula" "terms"
"data" “offset" "control"
"method" "contrasts" "xlevels"
mod.fit$coefficients
(Intercept) dist
5.812045 -0.
mod.fit
Call: glm(formula = good1 ~ dist, family = binomial(link =
logit), data = place.s, na.action = na.exclude, control =
list(epsilon = 1e-04, maxit = 50, trace = T))
Coefficients:
(Intercept) dist
5.8121 -0.
Degrees of Freedom: 1424 Total (i.e. Null); 1423 Residual
Null Deviance: 1013
Residual Deviance: 775.7 AIC: 779.
summary(mod.fit)
Call:
glm(formula = good1 ~ dist, family = binomial(link = logit),
data = place.s, na.action = na.exclude, control =
list(epsilon = 1e-04, maxit = 50, trace = T))
Deviance Residuals:
Min 1Q Median 3Q Max
-2.7441 0.2425 0.2425 0.3801 1.
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 5.812079 0.326158 17.82 <2e-16 ***
dist -0.115027 0.008337 -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: 1013.43 on 1424 degrees of freedom
Residual deviance: 775.75 on 1423 degrees of freedom
AIC: 779.
Number of Fisher Scoring iterations: 5
There are many different things that need to be discussed
about the code and output. Only a few of them will be
discussed here. More will be discussed later in this
chapter and in Chapter 5.
Notice the syntax used with the glm() function.
The names(mod.fit) shows the different components of
the mod.fit object.
The z value in the output is a z test statistic which
gives a test for whether the corresponding parameter
is 0 or not. This test statistic can be compared to a
standard normal distribution. Is distance important to
predicting the probability of success for a placekick?
A simple plot of the “fitted values” versus distance is:
Distance (yards)
Estimated probability
20 30 40 50 60
Estimated probability of success of a placekick
#Simple plot
plot(x = place.s$dist, y = mod.fit$fitted.values,
xlab="Distance (yards)", ylab="Estimated probability",
main = "Estimated probability of success of a
placekick")
Note that this plot would not be appropriate to hand in
for a project. Much better plots will be shown soon.
There are often many observations for the same
distance. For example, there are 20 placekicks from
21 yards and 19 of them are successful. This
information can be found from using the table() or
xtabs() functions.
#Summary of the placekicks by distance
dist.good <- table(place.s$dist, place.s$good1)
dist.good
integer matrix: 43 rows, 2 columns.
0 1
18 1 2
19 0 7
20 13 776
21 1 19
22 2 12
23 1 26
24 0 7
25 1 12
EDITED
55 1 2
56 0 1
59 0 1
62 1 0
63 1 0
66 1 0
Another way to put the data into this format is to use
the gsummary() function.
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)
prop<-place.sum$good/place.length$good