# 6.2 - Single Categorical Predictor

6.2 - Single Categorical Predictor

## Overview of Binary Logistic Regression

Binary logistic regression models the probability that a characteristic is present (i.e., "success"), given the values of explanatory variables $$x_1,\ldots,x_k$$. We denote this by $$\pi(x_1,\ldots,x_k) = P(\mbox{success}|x_1,\ldots,x_k)$$ or simply by $$\pi$$ for convenience---but it should be understood that $$\pi$$ will in general depend on one or more explanatory variables. For example, a physician may be interested in estimating the proportion of diabetic persons in a population. Naturally, she knows that all sections of the population do not have an equal probability of "success", i.e., being diabetic. Certain conditions, such as advanced age and hypertension, would likely contribute to a higher proportion.

#### Assumptions for logistic regression:

• The response variable $$Y$$ is a binomial random variable with a single trial and success probability $$\pi$$. Thus, $$Y=1$$ corresponds to "success" and occurs with probability $$\pi$$, and $$Y=0$$ corresponds to "failure" and occurs with probability $$1-\pi$$.
• The set of predictor or explanatory variables $$x = (x_1, x_2, \ldots, x_k)$$ are fixed (not random) and can be discrete, continuous, or a combination of both. As with classical regression, two or more of these may be indicator variables to model the nominal categories of a single predictor, and others may represent interactions between two or more explanatory variables.
• Together, the data is collected for the $$i$$th individual in the vector $$(x_{1i},\ldots,x_{ki},Y_i)$$, for $$i=1,\ldots n$$. These are assumed independent by the sampling mechanism. This also allows us to combine or group the data, which we do below, by summing over trials for which $$\pi$$ is constant. In this section of the notes, we focus on a single explanatory variable $$x$$.

The model is expressed as

$$\log\left(\dfrac{\pi_i}{1-\pi_i}\right)= \beta_0+\beta_1 x_i$$

Or, by solving for $$\pi_i$$, we have the equivalent expression

$$\pi_i=\dfrac{\exp(\beta_0+\beta_1 x_i)}{1+\exp(\beta_0+\beta_1 x_i)}$$

To estimate the parameters, we substitute this expression for $$\pi_i$$ into the joint pdf for $$Y_1,\ldots,Y_n$$

$$\prod_{i=1}^n\pi_i^{y_i}(1-\pi_i)^{1-y_i}$$

to give us the likelihood function $$L(\beta_0,\beta_1)$$ of the regression parameters. By maximizing this likelihood over all possible $$\beta_0$$ and $$\beta_1$$, we have the maximum likelihood estimates (MLEs): $$\hat{\beta}_0$$ and $$\hat{\beta}_1$$. Extending this to include additional explanatory variables is straightforward.

## Working with Grouped Data

It's worth noting that when the same value of the predictor $$x$$ occurs more than once, the success probability is constant for all $$Y$$ responses associated with that $$x$$ value, and we can work with the sum of such $$Y$$s as a binomial with the number of trials equal to the number of times that $$x$$ value occurred. Our example with parent and student smoking illustrates this. Recall the data for the two-way table:

Student smokes Student does not smoke 816 3203 188 1168

We have two approaches to work with. The first is for the individual or ungrouped data: $$x_i=1$$ if the $$i$$th student's parents smoke (0 otherwise), $$Y_i=1$$ if the $$i$$th student smokes, and $$Y_i\sim\mbox{Binomial}(1,\pi_i)$$, for $$i=1,\ldots,5375$$. The second is for the grouped data: $$x_i=1$$ if the $$i$$th table row corresponds to parents smoking (0 otherwise), $$Y_i=$$ number of students who smoke for the $$i$$th row, and $$Y_i\sim\mbox{Binomial}(n_i,\pi_i)$$, for $$i=1,2$$ and $$(n_1,n_2)=(4019,1356)$$.

With either approach, $$\pi$$ is the probability that a student smokes, and its estimate will be the same as well. Even though the index has a much greater range in the ungrouped approach, there are still only two unique values for $$\pi$$: that for the group whose parents smoke and that for the group whose parents don't. The likelihood function is simplified for the grouped data, however, and becomes

$$\prod_{i=1}^2\dfrac{n_i!}{y_i!(n_i-y_i)!}\pi_i^{y_i}(1-\pi_i)^{n_i-y_i}$$

The range for $$i$$ in the grouped approach is denoted by $$N$$ to distinguish it from the individual total $$n$$ (so $$N=2$$ in this example). Whenever possible, we will generally prefer to work with the grouped data because, in addition to having a more compact likelihood function, the larger binomial counts can be approximated by a normal distribution and will lend themselves to more meaningful residual diagnostics and large-sample significance tests.

Grouping is not always possible, however. Unless two responses have the same predictor value (or combination of predictor values if two or more predictors are present), their success probabilities would not be the same, and they cannot be added together to give a binomial variable. This is a common issue when lots of predictors and/or interactions are included in the model or with continuous predictor types because their values tend to be unique.

## Interpretation of Parameter Estimates

One source of complication when interpreting parameters in the logistic regression model is that they're on the logit or log-odds scale. We need to be careful to convert them back before interpreting the terms of the original variables.

• $$\exp(\beta_0) =$$ the odds that the success characteristic is present for an individual for which $$x = 0$$, i.e., at the baseline. If multiple predictors are involved, all would need to be set to 0 for this interpretation.
• $$\exp(\beta_1) =$$ the multiplicative increase in the odds of success for every 1-unit increase in $$x$$. This is similar to simple linear regression but instead of an additive change, it is a multiplicative change in rate. If multiple predictors are involved, others would need to be held fixed for this interpretation.
• If $$\beta_1 > 0$$, then $$\exp(\beta_j) > 1$$, indicating a positive relationship between $$x$$ and the probability and odds of the success event. If $$\beta_j < 0$$, then the opposite holds.

# 6.2.1 - Fitting the Model in SAS

6.2.1 - Fitting the Model in SAS

There are different ways to do this depending on the format of the data. As before, for details, you need to refer to SAS or R help. Here are some general guidelines to keep in mind. Please note that we make a distinction about the way the data are entered into SAS (or R).

If data come in a tabular form, i.e., the response pattern is with counts (as seen in the previous example).

model y/n = x1 x2 /link=logit dist=binomial;
model y/n = x1 x2;[enter code here]
• PROC GENMOD: We need a variable that specifies the number of cases that equals marginal frequency counts or number of trials (e.g. n), and the number of events (y)
• PROC LOGISTIC: We do need a variable that specifies the number of cases that equals marginal frequency counts

If data come in a matrix form, i.e., subject $$\times$$ variables matrix with one line for each subject, like a database

model y/n = x1 x2 / link = logit dist = binomial;
model y = x1 x2;
• PROC GENMOD: We need a variable that specifies the number of cases that equals marginal frequency counts or number of trials (e.g. n), and the number of events (y)
• PROC LOGISTIC: We do NOT need a variable that specifies the number of cases that equals marginal frequency counts

For both GENMOD and LOGISTIC, as before, include interaction terms with *, and make sure to include all lower-order terms.

We will follow both the SAS output through to explain the different parts of model fitting. The outputs from R will be essentially the same.

## Example 6-1: Student Smoking

Let's begin with collapsed $$2\times2$$ table:

Student smokes Student does not smoke 816 3203 188 1168

Let's look at one part of smoke.sas:

data smoke;
input s $y n ; cards; smoke 816 4019 nosmoke 188 1356 ; proc logistic data=smoke descending; class s (ref=first) / param=ref; model y/n = s /scale=none; run;  In the data step, the dollar sign \$ as before indicates that S is a character-string variable.

In the logistic step, the statement:  descending insures that you are modeling a probability of an "event" which takes value 1, otherwise by default SAS models the probability of "nonevent."

class S (ref=first) / param=ref;

This code says that S should be coded as a categorical variable using the first category as the reference or zero group. (The first category is "nosmoke," because it comes before "smoke" in alphabetical order). You can vary this order by additional options provided by class and/or by entering data in a different order. See SAS online help for details, and the rest of smoke.sas for more options.

model y/n = s /scale = none

Because we have grouped data (i.e. multiple trials per line of the data set), the model statement uses the "event/trial" syntax, in which y/n appears on the left-hand side of the equal sign. The predictors go on the right-hand side, separated by spaces if there are more than one. An intercept is added automatically by default.

scale=none

This option serves two purposes. One, it will give us the overall goodness-of-fit test statistics, deviance $$G^2$$ and Person $$X^2$$. It also enables us to specify a value for a dispersion parameter in order to correct for over-or under-dispersion. In this case, we are NOT controlling for either. Overdispersion is an important concept with discrete data. In the context of logistic regression, overdispersion occurs when there are discrepancies between the observed responses $$y_i$$ and their predicted values $$\hat{\mu}_i = n_{i}\hat{\pi}_i$$ and these values are larger than what the binomial model would predict.

If $$y_i \sim Binomial(n_i, \pi_i)$$, the mean is $$\mu_i = n_i \pi_i$$ and the variance is $$\dfrac{\mu_i(n_i − \mu_i)}{n_i}$$. Overdispersion means that the data show evidence that the variance of the response $$y_i$$ is greater than $$\dfrac{\mu_i(n_i − \mu_i)}{n_i}$$.

If overdispersion is present in a dataset, the estimated standard errors and test statistics for individual parameters and the overall goodness-of-fit will be distorted and adjustments should be made. We will look at this briefly later when we look into continuous predictors.

Now let's review some of the output from the program smoke.sas that uses PROC LOGISTIC

##### Model Information

Model Information
Data Set WORK.SMOKE
Response Variable (Events) y
Response Variable (Trials) n
Model binary logit
Optimization Technique Fisher's scoring

This section, as before, tells us which dataset we are manipulating, the labels of the response and explanatory variables and what type of model we are fitting (e.g. binary logit), and the type of scoring algorithm for parameter estimation. Fisher scoring is a variant of the Newton-Raphson method for ML estimation. In logistic regression they are equivalent.

##### Response Profile

Response Profile
Ordered
Value
Binary Outcome Total
Frequency
1 Event 1004
2 Nonevent 4371

This information tells us how many observations were "successful", e.g., how many "event" observations take value 1, that is how many children smoke, 816 + 188 = 1004 and "nonevent", i.e., how many do NOT smoke, (4019 − 816) + (1356 − 188) = 4371. Recall that the model is estimating the probability of the "event".

##### Class Level Information Design

Class Level Information
Class Value Design Variables
s nosmoke 0
smoke 1

From an explanatory categorical (i.e, class) variable S with 2 levels (0,1), we created one dummy variable (e.g. design variable):

$$X_1 = 1$$ ("smoke") if parent smoking = at least one,
$$X_1 = 0$$ ("nosmoke") if parent smoking = neither

parent smoking = nosmoke is equal 0 and is the baseline.

##### Model Convergence Status

Model Convergence Status
Convergence criterion (GCONV=1E-8) satisfied.

Since we are using an iterative procedure to fit the model, that is to find the ML estimates, we need some indication if the algorithm converged.

##### Overall goodness-of-fit testing

Test: $$H_0 \colon$$ current model vs. $$H_A \colon$$ saturated model

Deviance and Pearson Goodness-of-Fit Statistics
Criterion Value DF Value/DF Pr > ChiSq
Deviance 0.0000 0 . .
Pearson 0.0000 0 . .

The goodness-of-fit statistics $$X^2$$ and $$G^2$$ from this model are both zero because the model is saturated. However, suppose that we fit the intercept-only model. This is accomplished by removing the predictor from the model statement, like this:

model y/n = / scale=none;

The goodness-of-fit statistics are shown below.

Deviance and Pearson Goodness-of-Fit Statistics
Criterion Value DF Value/DF Pr > ChiSq
Deviance 29.1207 1 29.1207 <.0001
Pearson 27.6766 1 27.6766 <.0001

The Pearson statistic $$X^2 = 27.6766$$ is precisely equal to the usual $$X^2$$ for testing independence in the $$2\times2$$ table. And the deviance $$G^2 = 29.1207$$ is precisely equal to the $$G^2$$ for testing independence in the $$2\times2$$ table. Thus by the assumption, the intercept-only model or the null logistic regression model states that student's smoking is unrelated to parents' smoking (e.g., assumes independence, or odds-ratio=1). But clearly, based on the values of the calculated statistics, this model (i.e., independence) does NOT fit well. This example shows that analyzing a $$2\times2$$ table for association is equivalent to logistic regression with a single dummy variable. Later on, we will compare these tests to the loglinear model of independence.

The goodness-of-fit statistics, $$X^2$$ and $$G^2$$, are defined as before in the tests of independence and loglinear models (e.g. compare observed and fitted values). For the $$\chi^2$$ approximation to work well, we need the $$n_i$$s sufficiently large so that the expected values, $$\hat{\mu}_i\ge5$$, and $$n_i − \hat{\mu}_i \ge 5$$ for most of the rows. We can afford to have about 20% of these values less than 5, but none of them should fall below 1.

With real data, we often find that the $$n_i$$s are not big enough for an accurate test, and there is no single best solution to handle this but a possible solution may rely strongly on the data and context of the problem

## Analysis of Maximum Likelihood Estimates

Once an appropriate model is fitted, the success probabilities need to be estimated using the model parameters. Note that success probabilities are now NOT simply the ratio of observed number of successes and the number of trials. A model fit introduces a structure on the success probabilities. The estimates will now be functions of model parameters. What are the parameter estimates? What is the fitted model?

Analysis of Maximum Likelihood Estimates
Parameter   DF Estimate Standard
Error
Wald
Chi-Square
Pr > ChiSq
Intercept   1 -1.8266 0.0786 540.2949 <.0001
s smoke 1 0.4592 0.0878 27.3361 <.0001

The fitted model is

$$\mbox{logit}(\hat{\pi_i})=\log\dfrac{\hat{\pi_i}}{1-\hat{\pi_i}}=\hat{\beta_0}+\hat{\beta_1} X_i=-1.837+0.459\mbox{smoke}$$

where smoke is a dummy variable (e.g. design variable) that takes value 1 if at least one parent smokes and 0 if neither smokes as discussed above.

Estimated $$\beta_0=-1.827$$ with standard error 0.078 is significant and it says that log-odds of a child smoking versus not smoking if neither parent is smoking (the baseline level) is $$-1.827$$ and it's statistically significant.

Estimated $$\beta_1=0.459$$ with a standard error of 0.088 is significant and it says that log-odds-ratio of a child smoking versus not smoking if at least one parent smokes versus neither parents smokes (the baseline level) is 0.459 and it's statistically significant. $$\exp(0.459)=1.58$$ are the estimated odds-ratios. Thus there is a strong association between parent and child's smoking behavior, and the model fits well.

And the estimated probability of a student smoking given the explanatory variable (e.g. parent smoking behavior) is

$$\hat{\pi}_i=\dfrac{\exp(-1.826+0.4592X_i)}{1+\exp(-1.826+0.4592x_i)}$$

In particular, the predicted probability of a student smoking given that neither parent smokes is

$$P(Y_i=1|x_i=0)=\dfrac{\exp(-1.826+0.4592\times 0)}{1+\exp(-1.826+0.4592\times 0)}=0.14$$

and the predicted probability of a student being a smoker if at least one parent smokes is

$$P(Y_i=1|x_i=1)=\dfrac{\exp(-1.826+0.4592(X_i=1)}{1+\exp(-1.826+0.4592(x_i=1))}=0.20$$

By invoking the following option in the MODEL statement, output out=predict pred=probPROC LOGISTIC will print the predicted probabilities in the output:

Obs s y n prob
1 smoke 816 4019 0.20304
2 nosmoke 188 1356 0.13864

So, we do not need to do this calculation by hand, but it may be useful to try it out to be sure of what's going on. In this model,$$\beta_0$$ is the log-odds of children smoking for no-smoking parents /(x_i = 0\). Thus $$\exp(−1.8266)$$ are the odds that a student smokes when the parents do not smoke.

Looking at the $$2\times2$$ table, the estimated log-odds for nonsmokers is

$$\log\left(\dfrac{188}{1168}\right)=\log(0.161)=-1.8266$$

which agrees with $$\hat{\beta}_0$$ from the logistic model.

From the analysis of this example as a $$2\times2$$ table, $$\exp(\beta_1)$$ should be the estimated odds ratio. The estimated coefficient of the dummy variable,

$$\hat{\beta}_1=0.4592$$

agrees exactly with the log-odds ratio from the $$2\times2$$ table (i.e., $$\log(1.58) = (816\times1168) / (188\times 3203) = 0.459$$). That is exp(0.459) = 1.58, which is the estimated odds ratio of a student smoking.

To relate this to the interpretation of the coefficients in a linear regression, we could say that for every one-unit increase in the explanatory variable $$x_1$$ (e.g., changing from no smoking parents to smoking parents), the odds of "success" $$\pi_i / (1 − \pi_i)$$ will be multiplied by $$\exp(\beta_1)$$, given that all the other variables are held constant.

This is not surprising, because in the logistic regression model $$\beta_1$$ is the difference in the log-odds of children smoking as we move from "nosmoke" (neither parent smokes) /(x_i = 0/) to "smoke" (at least one parent smokes) $$x_i = 1$$, and the difference in log-odds is a log-odds ratio.

## Testing Individual Parameters

Testing the hypothesis that the probability of the characteristic depends on the value of the $$j$$th variable.

Testing $$H_0\colon \beta_j = 0$$ versus $$H_A\colon \beta_j \ne 0$$.

The Wald chi-squared statistics $$z^2=(\hat{\beta}_j/\mbox{SE}(\hat{\beta}_k))^2$$ for these tests are displayed along with the estimated coefficients in the "Analysis of Maximum Likelihood Estimates" section.

The standard error for $$\hat{\beta}_1$$, 0.0878, agrees exactly with the standard error that we can calculate from the corresponding $$2\times2$$ table.

Analysis of Maximum Likelihood Estimates
Parameter   DF Estimate Standard
Error
Wald
Chi-Square
Pr > ChiSq
Intercept   1 -1.8266 0.0786 540.2949 <.0001
s smoke 1 0.4592 0.0878 27.3361 <.0001

The values indicate the significant relationship between the logit of the odds of student smoking in parents' smoking behavior. Or, we can use the information from the "Type III Analysis of Effects" section.

Type 3 Analysis of Effects
Effect DF Wald
Chi-Square
Pr > ChiSq
s 1 27.3361 <.0001

Again, this information indicates that parents' smoking behavior is a significant factor in the model. We could compare $$z^2$$ to a chi-square with one degree of freedom; the p-value would then be the area to the right of $$z^2$$ under the $$\chi^2_1$$ density curve. A value of $$z^2$$ (Wald statistic) bigger than 3.84 indicates that we can reject the null hypothesis $$\beta_j = 0$$ at the .05-level.

$$\beta_1:\left(\dfrac{0.4592}{0.0878}\right)^2=27.354$$

## Confidence Intervals of Individual Parameters

An approximate $$(1 − \alpha) 100\%$$ confidence interval for $$\beta_j$$ is given by

$$\hat{\beta}_j \pm z_{(1-\alpha/2)}SE(\hat{\beta}_j)$$

For example, a 95% confidence interval for $$\beta_1$$

$$0.4592 \pm 1.96 (0.0878)= (0.287112, 0.63128)$$

where 0.0878 is the "Standard Error"' from the "Analysis of Maximum Likelihood Estimates" section. Then, the 95% confidence interval for the odds-ratio of a child smoking if one parent is smoking in comparison to neither smoking is

$$(\exp(0.287112), \exp(0.63128)) = (1.3325, 1.880)$$

Compare this with the output we get from PROC LOGISTIC:

Odds Ratio Estimates
Effect Point Estimate 95% Wald
Confidence Limits
s smoke vs nosmoke 1.583 1.332 1.880

When fitting logistic regression, we need to evaluate the overall fit of the model, the significance of individual parameter estimates and consider their interpretation. For assessing the fit of the model, we also need to consider the analysis of residuals. Definition of Pearson, deviance, and adjusted residuals is as before, and you should be able to carry this analysis.

If we include the statement...

output out=predict pred=phat reschi=pearson resdev=deviance;

in the PROC LOGISTIC call, then SAS creates a new dataset called "results" that includes all of the variables in the original dataset, the predicted probabilities $$\hat{\pi}_i$$, the Pearson residuals, and the deviance residuals. Then we can add some code to calculate and print out the estimated expected number of successes $$\hat{\mu}_i=n_i\hat{\pi}_i$$ and failures $$n_i-\hat{\mu}_i=n_i(1-\hat{\pi}_i)$$.

# 6.2.2 - Fitting the Model in R

6.2.2 - Fitting the Model in R

There are different ways to run logistic regression depending on the format of the data. Here are some general guidelines to keep in mind with a simple example outlined in dataformats.R where we created two binary random variables with $$n$$ number of trials, e.g., $$n=100$$.

##create two binary vectors of length 100
x=sample(c(0,1),100, replace=T)
y=sample(c(0,1),100, replace=T)

If the data come in a tabular form, i.e., response pattern is with counts (as seen in the previous example), the data are said to be "grouped".

> ##create a 2x2 table with counts
> xytab=table(x,y)
> xytab
y
x 0 1
0 24 29
1 26 21

glm(): Let $$Y$$ be the response variable capturing the number of events with the number of success ($$Y = 1$$) and failures ($$Y = 0$$). We need to create a response table that has a count for both the "success" and "failure" out of $$n$$ trials in its columns. Notice that the count table below could be also the number of success $$Y = 1$$, and then a column computed as $$n-Y$$

> count=cbind(xytab[,2],xytab[,1])
> count
[,1] [,2]
0 29 24
1 21 26

We also need a categorical predictor variable.

> xfactor=factor(c("0","1"))
> xfactor
[1] 0 1
Levels: 0 1

We need to specify the response distribution and a link, which in this case is done by specifying family=binomial("logit").

> tmp3=glm(count~xfactor, family=binomial("logit"))
> tmp3
Call: glm(formula = count ~ xfactor, family = binomial("logit"))
Coefficients:
(Intercept) xfactor1
0.1892 -0.4028
Degrees of Freedom: 1 Total (i.e. Null); 0 Residual
Null Deviance: 1.005
Residual Deviance: -4.441e-15 AIC: 12.72

If data come in a matrix form, i.e., subject $$\times$$ variables matrix with one line for each subject, like a database, where data are "ungrouped".

> xydata=cbind(x,y)

> xydata ## 100 rows, we are showing first 7
x y
[1,] 0 1
[2,] 0 1
[3,] 0 0
[4,] 0 1
[5,] 1 0
[6,] 0 1
[7,] 0 0.....

glm(): We need a binary response variable $$Y$$ and a predictor variable $$x$$, which in this case was also binary. We need to specify the response distribution and a link, which in this case is done by specifying family=binomial("logit")

> tmp1=glm(y~x, family=binomial("logit"))
> tmp1
Call: glm(formula = y ~ x, family = binomial("logit"))
Coefficients:
(Intercept) x
0.1892 -0.4028
Degrees of Freedom: 99 Total (i.e. Null); 98 Residual
Null Deviance: 138.6
Residual Deviance: 137.6 AIC: 141.6

We will follow the R output through to explain the different parts of model fitting. The output from SAS (or from many other software) will be essentially the same.

## Example 6-2: Student Smoking

Let's begin with the collapsed $$2\times2$$ table:

Student smokes Student does not smoke 816 3203 188 1168

In R, we can use the glm()function and specify the  family = binomial(link = logit). See the files smoke.R and the output generated in smoke.out. That R code corresponds to the SAS code discussed in the previous section:

#### define the explanatory variable with two levels:
#### 1=one or more parents smoke, 0=no parents smoke

parentsmoke=as.factor(c(1,0))

#### NOTE: if we do parentsmoke=c(1,0) R will treat this as
#### a numeric and not categorical variable
#### need to create a response vector so that it has counts for both "success" and "failure"

response<-cbind(yes=c(816,188),no=c(3203,1168))
response

#### fit the logistic regression model

#### OUTPUT

smoke.logistic
summary(smoke.logistic)
anova(smoke.logistic)

Here is the R output for the 2 × 2 table that we will use in R for logistics regression:

> response
yes no
[1,] 816 3203
[2,] 188 1168 

Please Note: the table above is different from the one given from the SAS program. We input $$y$$ and $$n$$ as data columns in SAS; here we just input data columns as yes and no.

summary(smoke.logistic) gives the following model information:

call:
glm(formula = response ~ parentsmoke, family = binomial(link = logit))

Deviance Residuals:
[1]  0  0

Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept)  -1.82661    0.07858 -23.244  < 2e-16 ***
parentsmoke1  0.45918    0.08782   5.228 1.71e-07 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance:  2.9121e+01  on 1  degrees of freedom
Residual deviance: -3.7170e-13  on 0  degrees of freedom
AIC: 19.242

Number of Fisher Scoring iterations: 2

#### Model Information & Model Convergence Status

Call:
glm(formula = response ~ parentsmoke, family = binomial(link = logit))
(Dispersion parameter for binomial family taken to be 1)
Number of Fisher Scoring iterations: 2

These sections tell us which dataset we are manipulating, the labels of the response and explanatory variables and what type of model we are fitting (e.g., binary logit), and the type of scoring algorithm for parameter estimation. Fisher scoring is a variant of Newton-Raphson method for ML estimation. In logistic regression they are equivalent. Since we are using an iterative procedure to fit the model, that is, to find the ML estimates, we need some indication if the algorithm converged.

Overdispersion is an important concept with discrete data. In the context of logistic regression, overdispersion occurs when the observed variance in the data tends to be larger than what the binomial model would predict. If $$Y_i\sim\mbox{Binomial}(n_i,\pi_i)$$, the mean is $$μ_i = n_i\pi_i$$, and the variance is $$n_i\pi_i(1-\pi_i$$. Both of these rely on the parameter $$\pi_i$$, which can be too restrictive. If overdispersion is present in a dataset, the estimated standard errors and test statistics for individual parameters and the overall goodness-of-fit will be distorted and adjustments should be made. We will look at this briefly later when we look into continuous predictors.

#### Table of Coefficients

Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.82661 0.07858 -23.244 < 2e-16 ***
parentsmoke1 0.45918 0.08782 5.228 1.71e-07 ***
--
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

This information gives us the fitted model:

$$\log\dfrac{\hat{\pi_i}}{1-\hat{\pi_i}}=\hat{\beta_0}+\hat{\beta_1} X_i=-1.837+0.459\mbox{parentsmoke1}$$

where parentsmoke1 is a dummy variable ((e.g. design variable) that takes value 1 if at least one parents is smoking and 0 if neither is smoking.

$$x_1 = 1$$ ("parentsmoke1") if parent smoking = at least one,
$$x_1= 0$$ ("parentsmoke0") if parent smoking = neither

The group for parent smoking = 0 is the baseline. We are modeling the probability of "children smoking".

Estimated $$\beta_0=-1.827$$ with a standard error of 0.078 is significant and it says that log-odds of a child smoking versus not smoking if neither parents is smoking (the baseline level) is -1.827 and it's statistically significant.

Estimated $$\beta_1=0.459$$ with a standard error of 0.088 is significant and it says that log-odds-ratio of a child smoking versus not smoking if at least one parent is smoking versus neither parents is smoking (the baseline level) is 0.459 and it's statistically significant. $$\exp(0.459)=1.58$$ are the estimated odds-ratios.

## Testing Individual Parameters

Testing the hypothesis that the probability of the characteristic depends on the value of the $$j$$th variable.

Testing $$H_0\colon\beta_j=0$$ versus $$H_A\colon\beta_j\ne0$$

The Wald chi-squared statistics $$z^2=(\hat{\beta}_j/\text{SE}(\hat{\beta}_k))^2$$ for these tests are displayed along with the estimated coefficients in the "Coefficients" section.

The standard error for $$\hat{\beta}_1$$, 0.0878, agrees exactly with the standard error that we can calculate from the corresponding $$2\times2$$ table. The values indicate the significant relationship between the logit of the odds of student smoking in parents' smoking behavior. This information indicates that parents' smoking behavior is a significant factor in the model. We could compare $$z^2$$ to a chi-square with one degree of freedom; the p-value would then be the area to the right of $$z^2$$ under the $$\chi^2_1$$ density curve.

A value of $$z^2$$ (Wald statistic) bigger than 3.84 indicates that we can reject the null hypothesis $$\beta_j=0$$ at the .05-level.

$$\beta_1:\left(\dfrac{0.4592}{0.0878}\right)^2=27.354$$

## Confidence Intervals of Individual Parameters:

An approximate $$(1 − \alpha) 100\%$$ confidence interval for $$\beta_j$$ is given by

$$\hat{\beta}_j \pm z_{(1-\alpha/2)} \times SE(\hat{\beta}_j)$$

For example, a 95% confidence interval for $$\beta_1$$

$$0.4592\pm 1.96 (0.0878) = (0.287112, 0.63128)$$

where 0.0878 is the "Standard Error"' from the "Coefficients" section. Then, the 95% confidence interval for the odds-ratio of a child smoking if one parent is smoking in comparison to neither smoking is

$$(\exp(0.287112), \exp(0.63128)) = (1.3325, 1.880)$$

Thus, there is a strong association between parent's and children smoking behavior. But does this model fit?

#### Overall goodness-of-fit testing

Test: $$H_0\colon$$ current model vs. $$H_A\colon$$ saturated model

Residual deviance: -3.7170e-13 on 0 degrees of freedom
AIC: 19.242

The goodness-of-fit statistics, deviance, $$G^2$$ from this model is zero, because the model is saturated. If we want to know the fit of the intercept only model that is provided by

Test: $$H_0\colon$$ intercept only model vs. $$H_A\colon$$ saturated model

Null deviance: 2.9121e+01 on 1 degrees of freedom

Suppose that we fit the intercept-only model. This is accomplished by removing the predictor from the model statement, like this:

glm(response~1, family=binomial(link=logit)

The goodness-of-fit statistics are shown below.

ull deviance: 29.121 on 1 degrees of freedom

Residual deviance: 29.121 on 1 degrees of freedom
AIC: 46.362
N

The deviance $$G^2 = 29.1207$$ is precisely equal to the $$G^2$$ for testing independence in this $$2\times2$$  table. Thus by the assumption, the intercept-only model or the null logistic regression model states that student's smoking is unrelated to parents' smoking (e.g., assumes independence, or odds-ratio=1). But clearly, based on the values of the calculated statistics, this model (i.e., independence) does NOT fit well.

#### Analysis of deviance table

In R, we can test factors’ effects with the anova function to give an analysis of deviance table. We only include one factor in this model. So R dropped this factor (parentsmoke) and fit the intercept-only model to get the same statistics as above, i.e., the deviance $$G^2 = 29.121$$.

> anova(smoke.logistic)
Analysis of Deviance Table
Response: response
Terms added sequentially (first to last)
Df Deviance Resid. Df Resid. Dev
NULL                            1     29.121
parentsmoke  1   29.121         0      0.000

This example shows that analyzing a $$2\times2$$ table for association is equivalent to logistic regression with a single dummy variable. We can further compare these tests to the loglinear model of independence in Lesson 10.

The goodness-of-fit statistics, $$X^2$$ and $$G^2$$, are defined as before in the tests of independence and loglinear models (e.g. compare observed and fitted values). For the $$chi^2$$ approximation to work well, we need the $$n_i$$s to be sufficiently large so that the expected values, $$\hat{\mu}_i \ge 5$$, and $$n_i − \hat{\mu}_i\ge 5$$ for most of the rows. We can afford to have about 20% of these values less than 5, but none of them should fall below 1.

With real data, we often find that the $$n_i$$s are not big enough for an accurate test, and there is no single best solution to handle this but a possible solution may rely strongly on the data and context of the problem.

# 6.2.3 - More on Model-fitting

6.2.3 - More on Model-fitting

Suppose two models are under consideration, where one model is a special case or "reduced" form of the other obtained by setting $$k$$ of the regression coefficients (parameters) equal to zero. The larger model is considered the "full" model, and the hypotheses would be

$$H_0$$: reduced model versus $$H_A$$: full model

Equivalently, the null hypothesis can be stated as the $$k$$ predictor terms associated with the omitted coefficients have no relationship with the response, given the remaining predictor terms are already in the model. If we fit both models, we can compute the likelihood-ratio test (LRT) statistic:

$$G^2 = −2 (\log L_0 - \log L_1)$$

where $$L_0$$ and $$L_1$$ are the max likelihood values for the reduced and full models, respectively. The degrees of freedom would be $$k$$, the number of coefficients in question. The p-value is the area under the $$\chi^2_k$$ curve to the right of $$G^2)$$.

To perform the test in SAS, we can look at the "Model Fit Statistics" section and examine the value of "−2 Log L" for "Intercept and Covariates." Here, the reduced model is the "intercept-only" model (i.e., no predictors), and "intercept and covariates" is the full model. For our running example, this would be equivalent to testing "intercept-only" model vs. full (saturated) model (since we have only one predictor).

Model Fit Statistics
Criterion Intercept Only Intercept and Covariates
Log Likelihood Full Log Likelihood
AIC 5178.510 5151.390 19.242
SC 5185.100 5164.569 32.421
-2 Log L 5176.510 5147.390 15.242

Larger differences in the "-2 Log L" values lead to smaller p-values more evidence against the reduced model in favor of the full model. For our example, $$G^2 = 5176.510 − 5147.390 = 29.1207$$ with $$2 − 1 = 1$$ degree of freedom. Notice that this matches the deviance we got in the earlier text above.

Also, notice that the $$G^2$$ we calculated for this example is equal to 29.1207 with 1df and p-value <.0001 from "Testing Global Hypothesis: BETA=0" section (the next part of the output, see below).

## Testing the Joint Significance of All Predictors

Testing the null hypothesis that the set of coefficients is simultaneously zero. For example, consider the full model

$$\log\left(\dfrac{\pi}{1-\pi}\right)=\beta_0+\beta_1 x_1+\cdots+\beta_k x_k$$

and the null hypothesis $$H_0\colon \beta_1=\beta_2=\cdots=\beta_k=0$$ versus the alternative that at least one of the coefficients is not zero. This is like the overall F−test in linear regression. In other words, this is testing the null hypothesis of the intercept-only model:

$$\log\left(\dfrac{\pi}{1-\pi}\right)=\beta_0$$

versus the alternative that the current (full) model is correct. This corresponds to the test in our example because we have only a single predictor term, and the reduced model that removes the coefficient for that predictor is the intercept-only model.

In the SAS output, three different chi-square statistics for this test are displayed in the section "Testing Global Null Hypothesis: Beta=0," corresponding to the likelihood ratio, score, and Wald tests. Recall our brief encounter with them in our discussion of binomial inference in Lesson 2.

Testing Global Null Hypothesis: BETA=0
Test Chi-Square DF Pr > ChiSq
Likelihood Ratio 29.1207 1 <.0001
Score 27.6766 1 <.0001
Wald 27.3361 1 <.0001

Large chi-square statistics lead to small p-values and provide evidence against the intercept-only model in favor of the current model. The Wald test is based on asymptotic normality of ML estimates of $$\beta$$s. Rather than using the Wald, most statisticians would prefer the LR test. If these three tests agree, that is evidence that the large-sample approximations are working well and the results are trustworthy. If the results from the three tests disagree, most statisticians would tend to trust the likelihood-ratio test more than the other two.

In our example, the "intercept only" model or the null model says that student's smoking is unrelated to parents' smoking habits. Thus the test of the global null hypothesis $$\beta_1=0$$ is equivalent to the usual test for independence in the $$2\times2$$ table. We will see that the estimated coefficients and standard errors are as we predicted before, as well as the estimated odds and odds ratios.

Residual deviance is the difference between −2 logL for the saturated model and −2 logL for the currently fit model. The high residual deviance shows that the model cannot be accepted. The null deviance is the difference between −2 logL for the saturated model and −2 logL for the intercept-only model. The high residual deviance shows that the intercept-only model does not fit.

Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept)  -1.82661    0.07858 -23.244  < 2e-16 ***
parentsmoke1  0.45918    0.08782   5.228 1.71e-07 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

In our $$2\times2$$ table smoking example, the residual deviance is almost 0 because the model we built is the saturated model. And notice that the degree of freedom is 0 too. Regarding the null deviance, we could see it equivalent to the section "Testing Global Null Hypothesis: Beta=0," by likelihood ratio in SAS output.

For our example, Null deviance = 29.1207 with df = 1. Notice that this matches the deviance we got in the earlier text above.

## The Homer-Lemeshow Statistic

An alternative statistic for measuring overall goodness-of-fit is the Hosmer-Lemeshow statistic.

NOTE! We use one predictor model here, that is, at least one parent smokes.

This is a Pearson-like chi-square statistic that is computed after the data are grouped by having similar predicted probabilities. It is more useful when there is more than one predictor and/or continuous predictors in the model too. We will see more on this later.

$$H_0$$: the current model fits well
$$H_A$$: the current model does not fit well

To calculate this statistic:

1. Group the observations according to model-predicted probabilities ( $$\hat{\pi}_i$$)
2. The number of groups is typically determined such that there is roughly an equal number of observations per group
3. The Hosmer-Lemeshow (HL) statistic, a Pearson-like chi-square statistic, is computed on the grouped data but does NOT have a limiting chi-square distribution because the observations in groups are not from identical trials. Simulations have shown that this statistic can be approximated by a chi-squared distribution with $$g − 2$$ degrees of freedom, where $$g$$ is the number of groups.

Warning about the Hosmer-Lemeshow goodness-of-fit test:

• It is a conservative statistic, i.e., its value is smaller than what it should be, and therefore the rejection probability of the null hypothesis is smaller.
• It has low power in predicting certain types of lack of fit such as nonlinearity in explanatory variables.
• It is highly dependent on how the observations are grouped.
• If too few groups are used (e.g., 5 or less), it almost always fails to reject the current model fit. This means that it's usually not a good measure if only one or two categorical predictor variables are involved, and it's best used for continuous predictors.

In the model statement, the option lackfit tells SAS to compute the HL statistic and print the partitioning. For our example, because we have a small number of groups (i.e., 2), this statistic gives a perfect fit (HL = 0, p-value = 1). Instead of deriving the diagnostics, we will look at them from a purely applied viewpoint. Recall the definitions and introductions to the regression residuals and Pearson and Deviance residuals.

## Residuals

The Pearson residuals are defined as

$$r_i=\dfrac{y_i-\hat{\mu}_i}{\sqrt{\hat{V}(\hat{\mu}_i)}}=\dfrac{y_i-n_i\hat{\pi}_i}{\sqrt{n_i\hat{\pi}_i(1-\hat{\pi}_i)}}$$

The contribution of the $$i$$th row to the Pearson statistic is

$$\dfrac{(y_i-\hat{\mu}_i)^2}{\hat{\mu}_i}+\dfrac{((n_i-y_i)-(n_i-\hat{\mu}_i))^2}{n_i-\hat{\mu}_i}=r^2_i$$

and the Pearson goodness-of fit statistic is

$$X^2=\sum\limits_{i=1}^N r^2_i$$

which we would compare to a $$\chi^2_{N-p}$$ distribution. The deviance test statistic is

$$G^2=2\sum\limits_{i=1}^N \left\{ y_i\text{log}\left(\dfrac{y_i}{\hat{\mu}_i}\right)+(n_i-y_i)\text{log}\left(\dfrac{n_i-y_i}{n_i-\hat{\mu}_i}\right)\right\}$$

which we would again compare to $$\chi^2_{N-p}$$, and the contribution of the $$i$$th row to the deviance is

$$2\left\{ y_i\log\left(\dfrac{y_i}{\hat{\mu}_i}\right)+(n_i-y_i)\log\left(\dfrac{n_i-y_i}{n_i-\hat{\mu}_i}\right)\right\}$$

We will note how these quantities are derived through appropriate software and how they provide useful information to understand and interpret the models.

# 6.2.4 - Multi-level Predictor

6.2.4 - Multi-level Predictor

The concepts discussed with binary predictors extend to predictors with multiple levels. In this lesson we consider $$Y_i$$ a binary response, $$x_i$$ a discrete explanatory variable (with $$k = 3$$ levels, and make connections to the analysis of $$2\times3$$ tables. But the basic ideas extend to any $$2\times J$$ table.

We begin by replicating the analysis of the original $$3\times2$$ table with logistic regression.

How many parents smoke? Student smokes?
Yes (Z = 1) No (Z = 2)
Both (Y = 1) 400 1380
One (Y = 2) 416 1823
Neither (Y = 3) 188 1168

First, we re-express the data in terms of $$Y_i=$$ number of smoking students, and $$n_i=$$ number of students for the three groups based on parents behavior (we can think of $$i$$ as an index for the rows in the table).

How many parents smoke? Student smokes?
$$y_i$$ $$n_i$$
Both 400 1780
One 416 2239
Neither 188 1356

Then we decide on a baseline level for the explanatory variable $$x$$ and create $$k − 1$$indicators if $$x$$ is a categorical variable with $$k$$ levels. For our example, we set "Neither" as the baseline for the parent smoking predictor and define a pair of indicators that take one of two values, specifically,

$$x_{1i}=1$$ if parent smoking = "One" ,
$$x_{1i}=0$$ otherwise

$$x_{2i}=1$$ if parent smoking = "Both",
$$x_{2i}=0$$ otherwise.

If these two indicator variables were added as columns in the table above, $$x_{1i}$$ would have values $$(0,1,0)$$, and $$x_{2i}$$ would have values $$(1,0,0)$$. Next, we let $$\pi_i$$ denote the probability of student smoking for the $$i$$th row group so that finally, the model is

$$\log\left(\dfrac{\pi_i}{1-\pi_i}\right)=\beta_0+\beta_1 x_{1i}+\beta_2 x_{2i}$$

for $$i=1,\ldots,3$$. Thus, the log-odds of a student smoking is $$\beta_0$$ for students whose parents don't smoke ("neither" group), $$\beta_0+\beta_1$$ for students with one smoking parent ("one" group), and $$\beta_0+\beta_2$$ for students with both smoking parents. In particular, $$\beta_1$$  is the difference in log odds or, equivalently, the log odds ratio for smoking when comparing students with one smoking parent against students with neither smoking parent. Similarly, $$\beta_2$$ represents that when comparing students with two smoking parents against students with neither smoking parent.

Based on the tabulated data, where we saw $$1.42=416 ( 1168)/(188( 1823))$$ and $$1.80=400(1168)/(188(1380))$$, we're not surprised to see $$\hat{\beta}_1=\log(1.42)=0.351$$ and $$\hat{\beta}_2=\log(1.80)=0.588$$. The estimated intercept should be $$\hat{\beta}_0=\log(188/1168)=-1.826$$.

## Fitting A Multi-level Predictor Model in SAS and R

There are different models fit within the smoke.sas SAS code. Here is one where '0' = neither parent smokes, '1' = one smokes, and '2' = both smoke, and we use PROC LOGISTIC; notice we could use proc GENMOD too.

data smoke;
input s $y n ; cards; 2 400 1780 1 416 2239 0 188 1356 ; proc logistic data=smoke descending; class s (ref=first)/ param=ref; model y/n = s / scale=none lackfit; output out=predict pred=prob reschi=pearson resdev=deviance; title 'Logistic regression fro 2x3 tables with residuals'; run; data diagnostics; set predict; shat = n*prob; fhat = n*(1-prob); run; proc print data=diagnostics; var s y n prob shat fhat pearson deviance; title 'Logistic regression diagnostics for 2x3 table'; run; The option param=ref tells SAS to create a set of two dummy variables to distinguish among the three categories, where '0'=neither is a baseline because of option descending and ref=first (see the previous section for details). Another way of doing the same in smoke.sas but using character labels (e.g, "both", "one", "neither") rather than numbers for the categories: data smoke; input s$ y n ;
cards;
both 400 1780
one 416 2239
neither 188 1356
;
proc logistic data=smoke descending;
class s (ref='neither') / order=data param=ref;
model y/n = s /scale=none lackfit;
output out=predict pred=prob;
title 'Logistic regression for 2x3 table';
run;
proc print data=predict;
run;

In the class statement, the option order=data tells SAS to sort the categories of S by the order in which they appear in the dataset rather than alphabetical order. The option ref='neither' makes neither the reference group (i.e. the group for which both dummy variables are zero). Let's look at some relevant portions of the output that differ from the analysis of the corresponding $$2\times2$$ table from the previous section of the notes.

Model Information
Data Set WORK.SMOKE
Response Variable (Events) y
Response Variable (Trials) n
Model binary logit
Optimization Technique Fisher's scoring

Fisher scoring is a variant of Newton-Raphson method for ML estimation. In logistic regression they are equivalent. Notice how there are 3 observations since we have 3 groupings by the levels of the explanatory variable.

Class Level Information
Class Value Design Variables
s 0 0 0
1 1 0
2 0 1

From an explanatory variable S with 3 levels (0,1,2), we created two indicator variables:

$$x_1=1$$ if parent smoking = One,
$$x_1=0$$ otherwise,

$$x_2=1$$ if parent smoking=Both,
$$x_2=0$$ otherwise.

Since parent smoking = Neither is equal to 0 for both indicator variables, it serves as the baseline.

Class Level Information
Class Value Design Variables
s both 1 0
one 0 1
neither 0 0

Here, we specified neither to be a reference variable. We used option data=order so that both take values (1,0), and one (0,1). If we didn't use that option, the other levels would be set based on alphabetical order, but in this case, they coincide.

The parameter estimates are reported in the output below.

Analysis of Maximum Likelihood Estimates
Parameter   DF Estimate Standard
Error
Wald
Chi-Square
Pr > ChiSq
Intercept   1 -1.8266 0.0786 540.2949 <.0001
s 1 1 0.3491 0.0955 13.3481 0.0003
s 2 1 0.5882 0.0970 36.8105 <.0001

Here, $$s_1$$ and $$s_2$$ correspond to $$x_1$$ and $$x_2$$. The saturated model is

$$\mbox{logit}(\pi)=-1.8266+0.3491 x_1+0.5882 x_2$$

and the estimated probability of a child smoking, given the explanatory variable is

$$\hat{\pi}_i=\dfrac{\exp(-1.8266+0.3491 x_1+0.5882 x_2)}{1+\exp(-1.8266+0.3491 x_1+0.5882 x_2)}$$

For example, the predicted probability of a student smoking, given that only one parent is smoking is

$$P(Y=1|x_1=1,x_2=0)= \dfrac{\exp(-1.8266+0.3491)}{1+\exp(-1.8266+0.3491)}= 0.1858$$

#### Residuals

In SAS, if we include the statement

output out=predict pred=phat reschi=pearson resdev=deviance;

then SAS creates a new dataset called "results" that includes all of the variables in the original dataset, the predicted probabilities $$\hat{\pi}_i$$, and the Pearson and deviance residuals. We can add some code to calculate and print out the estimated expected number of successes $$\hat{\mu}_i=n_i\hat{\pi}_i$$ (e.g., "s-hat") and failures $$n_i-\hat{\mu}_i=n_i(1-\hat{\pi}_i)$$ (e.g., "f-hat").

data diagnostics;
set predict;
shat = n*prob;
fhat = n*(1-prob);
run;

proc print data=diagnostics;
var s y n prob shat fhat pearson deviance;
title 'Logistic regression diagnostics for 2x3 table';
run;


Running this program gives a new output section:

Obs s y n prob shat fhat pearson deviance
1 2 400 1780 0.22472 400.000 1380.00 -.000000031 0
2 1 416 2239 0.18580 416.000 1823.00 -3.0886E-15 0
3 0 188 1356 0.13864 188.000 1168.00 -.000001291 -.000001196
.

Here "s" are the levels of the categorical predictor for parents' smoking behavior, "y" as before the number of students smoking for each level of the predictor, "n" the marginal counts for each level of the predictor", "prob" is the estimated probability of "success" (e.g. a student smoking given the level of the predictor), "s-hat" and "f-hat" expected number of successes and failures respectively, and "pearson" and "deviance" are Pearson and Deviance residuals.

All of the "s-hat" and "f-hat" values, that is predicted number of successes and failures are greater than 5.0, so the chi-square approximation is trustworthy.

Below is the R code (from smoke.R) that replicates the analysis of the original $$2\times3$$ table with logistic regression.

#### Fitting logistic regression for a 2x3 table #####
#### Here is one way to read the data from the table and use glm()
#### Notice how we need to use family=binomial (link=logit)
#### while with log-linear models we used family=poisson(link=log)

parentsmoke=as.factor(c(2,1,0))
response<-cbind(c(400,416,188),c(1380,1823,1168))
response


First, let’s see the table we created for the analysis.

> response
[,1] [,2]
[1,]  400 1380
[2,]  416 1823
[3,]  188 1168

You should again notice the difference in data input. In SAS, we input y and n as data columns; here we just input data columns as yes and no.

Next, the model information is displayed.

Call:
glm(formula = response ~ parentsmoke, family = binomial(link = logit))
Deviance Residuals:
[1]  0  0  0

Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept)  -1.82661    0.07858 -23.244  < 2e-16 ***
parentsmoke1  0.34905    0.09554   3.654 0.000259 ***
parentsmoke2  0.58823    0.09695   6.067  1.3e-09 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

Null deviance:  3.8366e+01  on 2  degrees of freedom
Residual deviance: -3.7215e-13  on 0  degrees of freedom
AIC: 28.165

Number of Fisher Scoring iterations: 2

If we don’t specify the baseline using the "class" option, R will set the first level as the default. Here, it’s "0". The parentsmoke1 and parentsmoke2 variables correspond to the $$x_1$$ and $$x_2$$ indicators. The saturated model is

$$\mbox{logit}(\pi)=-1.8266+0.3491 x_1+0.5882 x_2$$

and the estimated probability of a child smoking given the explanatory variable:

$$\hat{\pi}_i=\dfrac{\exp(-1.8266+0.3491 x_1+0.5882 x_2)}{1+\exp(-1.8266+0.3491 x_1+0.5882 x_2)}$$

For example, the predicted probability of a student smoking given that only one parent is smoking is

$$P(Y=1|x_1=1,x_2=0)= \dfrac{\exp(-1.8266+0.3491)}{1+\exp(-1.8266+0.3491)}= 0.1858$$

#### Residuals

In R, deviance residuals are given directly in the output by summary() function.

> smoke.logistic<-glm(response~parentsmoke, family=binomial(link=logit))
> summary(smoke.logistic)
Call:
glm(formula = response ~ parentsmoke, family = binomial(link = logit))
Deviance Residuals:
[1] 0 0 0
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.82661 0.07858 -23.244 < 2e-16 ***
parentsmoke1 0.34905 0.09554 3.654 0.000259 ***
parentsmoke2 0.58823 0.09695 6.067 1.3e-09 ***
---

We can also obtain the deviance and Pearson residuals by using residuals.glm() function.

To obtain deviance residuals

> residuals(smoke.logistic)
[1] 0 0 0

To obtain Person residuals:

> residuals(smoke.logistic, type="pearson")
1 2 3
-2.440787e-13 -4.355932e-13 -1.477321e-11

To obtain the predicted values, use the function predict.glm() with which we can specify the type of predicted values we want.

type is the type of prediction required. The default is on the scale of the linear predictors; the alternative "response" is on the scale of the response variable. Thus for a default binomial model, the default predictions are of log-odds (probabilities on logit scale) and type = "response" gives the predicted probabilities. The "terms" option returns a matrix giving the fitted values of each term in the model formula on the linear predictor scale.

For example, the code below gives predicted probabilities.

> predict(smoke.logistic, type="response")
1 2 3
0.2247191 0.1857972 0.1386431

All of the predicted number of successes and failures are greater than 5.0 so the chi-square approximation is trustworthy.

## Overall goodness-of-fit

The goodness-of-fit statistics $$X^2$$ and $$G^2$$ from this model are both zeroes because the model is saturated. Suppose that we fit the intercept-only model as before by removing the predictors from the model statement:

proc logistic data=smoke descending;
class s1 s2 (ref=first)/ param=ref;
model y/n = / scale=none lackfit;
output out=predict pred=prob reschi=pearson resdev=deviance;
title 'Logistic regression for intercept-only model';
run;

The goodness-of-fit statistics are shown below.

Deviance and Pearson Goodness-of-Fit Statistics
Criterion Value DF Value/DF Pr > ChiSq
Deviance 38.3658 2 19.1829 <.0001
Pearson 37.5663 2 18.7832 <.0001

Number of events/trials observations: 3

The Pearson statistic $$X^2= 37.5663$$ and the deviance $$G^2 = 38.3658$$ are precisely equal to the usual $$X^2$$ and $$G^2$$ for testing independence in the $$2\times 3$$ table. In this example, the saturated model fits perfectly (as always), but the independence model does not fit well.

We can use the Hosmer-Lemeshow test to assess the overall fit of the model. As in the previous example, the Hosmer and Lemeshow statistic is not very meaningful since the number of groups is small.

Partition for the Hosmer and Lemeshow Test
Group Total Event Nonevent
Observed Expected Observed Expected
1 1356 188 188.00 1168 1168.00
2 2239 416 416.00 1823 1823.00
3 1780 400 400.00 1380 1380.00

Hosmer and Lemeshow Goodness-of-Fit Test
Chi-Square DF Pr > ChiSq
0.0000 1 1.0000
    Null deviance:  3.8366e+01  on 2  degrees of freedom
Residual deviance: -3.7215e-13  on 0  degrees of freedom
AIC: 28.165

The residual deviance is almost 0 because the model is saturated. We can find $$G^2= 38.366$$ by null deviance, which is precisely equal to the usual $$G^2$$ for testing independence in the $$2\times 3$$ table. Since this statistic is large, it leads to a small p-value and provides significant evidence against the intercept-only model in favor of the current model. We can also find the same result in the output from the anova() part. Clearly, in this example, the saturated model fits perfectly (as always), but the independence model does not fit well.

We can use the Hosmer-Lemeshow test (available in the R file HosmerLemeshow.R) to assess the overall fit of the model. As in the previous example, the Hosmer and Lemeshow statistic is not very meaningful since the number of groups is small.

> ## hosmerlem() takes the vector of successes, predicted vector of success and g=# of groups as input
> ## produce the vector of predicted success "yhat"
> yhat=rowSums(response)*predict(smoke.logistic, type="response")
> yhat
1 2 3
400 416 188

> hosmerlem(response[,1], yhat, g=3) ## here run 3 groups
X^2 Df P(>Chi)
"-1.00593633284243e-24" "1" "."

We have shown that analyzing a $$2\times 3$$ table for associations is equivalent to a binary logistic regression with two dummy variables as predictors. For $$2\times J$$ tables, we would fit a binary logistic regression with $$J − 1$$ indicator variables.

## Testing the Joint Significance of All Predictors

Starting with the (full) model

$$\log\left(\dfrac{\pi}{1-\pi}\right)=\beta_0+\beta_1 x_1+\beta_2 x_2$$

the null hypothesis $$H_0\colon\beta_1=\beta_2=0$$ specifies the intercept-only (reduced) model:

$$\log\left(\dfrac{\pi}{1-\pi}\right)=\beta_0$$

In general, this test has degrees of freedom equal to the number of slope parameters, which is 2 in this case. Large chi-square statistics lead to small p-values and provide evidence to reject the intercept-only model in favor of the full model.

Testing Global Null Hypothesis: BETA=0
Test Chi-Square DF Pr > ChiSq
Likelihood Ratio 38.3658 2 <.0001
Score 37.5663 2 <.0001
Wald 37.0861 2 <.0001

## Testing for an Arbitrary Group of Coefficients

The likelihood-ratio statistic is

$$G^2 = −2 (\log \mbox{ likelihood from reduced model}−(−2 \log \mbox{ likelihood from full model}))$$

and the degrees of freedom is $$k=$$ the number of parameters differentiating the two models. The p-value is $$P(\chi^2_k \geq G^2)$$.

Model Fit Statistics
Criterion Intercept Only Intercept and Covariates
Log Likelihood Full Log Likelihood
AIC 5178.510 5144.144 28.165
SC 5185.100 5163.913 47.933
-2 Log L 5176.510 5138.144 22.165

For our example, $$G^2 = 5176.510 − 5138.144 = 38.3658$$ with $$3 − 1 = 2$$ degrees of freedom. Notice that this matches

Likelihood Ratio 38.3658 2 <.0001

from the "Testing Global Hypothesis: BETA=0" section. Here is the model we just looked at in SAS.

data smoke;
input s1 s2 \$ y n ;
cards;
0 1 400 1780
1 0 416 2239
0 0 188 1356
;
proc logistic data=smoke descending;
class s1 s2 (ref=first)/ param=ref;
model y/n = s1 s2 / scale=none lackfit;
output out=predict pred=prob reschi=pearson resdev=deviance;
run;

proc logistic data=smoke descending;
class s1 s2 (ref=first)/ param=ref;
model y/n = s1 / scale=none lackfit;
output out=predict pred=prob reschi=pearson resdev=deviance;
run;


Compare this to

model y/n = s1 /scale=none lackfit;

## Testing Individual Parameters

For the test of the significance of a single variable $$x_j$$

$$H_0\colon\beta_j=0$$ versus $$H_A\colon\beta_j\ne0$$

we can use the (Wald) test statistic and p-value. A large value of $$z$$ (relative to standard normal) or $$z^2$$ (relative to chi-square with 1 degree of freedom) indicates that we can reject the null hypothesis and conclude that $$\beta_j$$ is not 0.

From the second row of this part of the output,

Analysis of Maximum Likelihood Estimates
Parameter   DF Estimate Standard
Error
Wald
Chi-Square
Pr > ChiSq
Intercept   1 -1.8266 0.0786 540.2949 <.0001
s 1 1 0.3491 0.0955 13.3481 0.0003
s 2 1 0.5882 0.0970 36.8105 <.0001

$$z^2=\left(\dfrac{0.3491}{0.0955}\right)^2=13.3481$$

Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept)  -1.82661    0.07858 -23.244  < 2e-16 ***
parentsmoke1  0.34905    0.09554   3.654 0.000259 ***
parentsmoke2  0.58823    0.09695   6.067  1.3e-09 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

A large value of $$z$$ (relative to standard normal) indicates that we can reject the null hypothesis and conclude that $$\beta_j$$ is not 0.

Confidence Intervals: An approximate $$(1 − \alpha)100$$% confidence interval for $$\beta_j$$ is given by

$$\hat{\beta}_j \pm z_{(1-\alpha/2)} \times SE(\hat{\beta}_j)$$

For example, a 95% CI for $$\beta-1$$ is

$$0.3491 \pm 1.96 (0.0955) = (0.16192, 0.5368)$$

Then, the 95% CI for the odds-ratio of a student smoking, if one parent is smoking in comparison to neither smoking, is

$$(\exp(0.16192), \exp(0.5368)) = (1.176, 1.710)$$

Since this interval does not include the value 1, we can conclude that student and parents' smoking behaviors are associated. Furthermore,

Odds Ratio Estimates
Effect Point Estimate 95% Wald
Confidence Limits
s 1 vs 0 1.418 1.176 1.710
s 2 vs 0 1.801 1.489 2.178
• The estimated conditional odds ratio of a student smoking between one parent smoking and neither smoking is $$\exp(\beta_1) = \exp(0.3491) = 1.418$$.
• The estimated conditional odds ratio of a student smoking between both parents smoking and neither smoking is $$\exp(\beta_2) = \exp(0.5882) = 1.801$$.
• The estimated conditional odds ratio of a student smoking between both parents smoking and one smoking is $$\exp(\beta_2)/\exp(\beta_1) = 1.801/1.418 = 1.27 = \exp(\beta_2-\beta_1) = \exp(0.5882 − 0.3491) = 1.27$$. That is, compared with a student who has only one parent smoking, a student who has both parents smoking has an odds of smoking 1.27 times as high.

 [1] Link ↥ Has Tooltip/Popover Toggleable Visibility