---
title: "Beyond Ordinary Least Squares: The Generalized Linear Model (GLM)"
author: "Shane T. Mueller shanem@mtu.edu"
date: "`r Sys.Date()`"
output:
  pdf_document: default
  rmdformats::readthedown:
    gallery: yes
    highlight: kate
    self_contained: no
  word_document:
    reference_docx: ../template.docx
always_allow_html: yes
---


```{r knitr_init, echo=FALSE, cache=FALSE}
library(knitr)
library(rmdformats)

## Global options
options(max.print="75")
opts_chunk$set(echo=TRUE,
	             cache=TRUE,
               prompt=FALSE,
               tidy=TRUE,
               comment=NA,
               message=FALSE,
               warning=FALSE)
opts_knit$set(width=75)
```

# Generalized Linear Model (GLM)
The generalized linear model is a framework for fitting and testing versions of the linear regression model that are more flexible than traditional or "ordinary" least squares (OLS).  Unlike non-parametric models, GLM models do not "relax" assumptions--they make alternative assumptions, often permitting us to fit different noise distributions, and different non-linear relationships that we might have otherwise used a transformation to achieve.

Some suggested reading/resources include:

* R In action, Chapter 13
* Faraway's, "Extending the linear model with R" Chapter 6
* Modern Applied Statistics in S, Chapter 7 

# Assumptions of Ordinary Least Squares (OLS)

Ordinary Least Squares (OLS) regression makes a number of assumptions, which include:

* The effects are linear
* Error is normally distributed--i.e., the difference between the data and predicted values.
* Observations are independent
* Variance is uniform across different levels or magnitude of the IVs (no heteroscedascity)
* Each observation has equal value in estimating the parameters
* (Typically) the best-fitting model is one that minimizes least-squares error

Violations of these assumptions sometimes mean the model is a poor description of the data.  At other times, the model will still describe the general pattern of the data accurately, but even if the model is reasonable, violating these will impact our inferential statistics--the extent to which we believe our effects are statistically significant and did not arise from chance. Some of the approaches to this include using transformations to make effects more linear or error more normal.  If you have non-independent observations because of repeated measures, you can sometimes use error strata and repeated measures-ANOVA schemes to account for this, or mixed-effects models. But in generally, the fixes to the OLS model get increasingly ad hoc and unprincipled.  There are a number of ways that have been used to adopt alternate assumptions and processes we use to extend the model in more systematic ways:

* The **General Linear Model** typically refers to the entire set of linear regression techniques, including the potential for multiple DVS and multiple IVs, including categorical numerical predictors.  Thus, ANOVA, MANOVA, regression, t-tests, etc. are all subclasses of this method.

* **Weighted least-squares**, which permit giving different observations different weights.

* **Maximum likelihood estimation** (ML Estimation) is an alternative to least-squares that attempts to find the model parameters that maximize the likelihood of the model.  It is especially useful if you have assumed the error is not normal-especially if it is asymmetric.  In these cases, least-squares regression will be biased toward the tail of the distribution. Related methods include those that use Bayesian inference, because they also deal with likelihood, but don't try to maximize it.

* **Generalized Linear models** are extensions to linear regression that incorporate distinct error distributions and transformation functions (called linking functions). These include logistic regression, probit regression, poisson regression, log-linear models, and a handful of similar and related regression methods.

* **Robust regression** methods will attempt to fit models having conditional error distributions that are non-normal, often using alternative means for estimating predictors to avoid biases that might otherwise be introduced.



# Maximum Likelihood Estimation

To understand MLE, let's just remember what likelihood is. Likelihood is calculated for each observed point you are fitting, and it is the height of the error distribution you are fitting.  When we fit multiple points, we take the product of all the heights, and try to find a distribution that maximizes this product.

This sometimes leads to nearly identical estimates to the one we get in ordinary least squares. In OLS, we just calculate deviations from the model and try to minimize that, and estimate the error model AFTERWARD. MLE works on the likelihood of the data given the model, and tries to maximize that. Thus, unlike OLS, we need a probability distribution model for this to make sense.  

Suppose we observed 100 data points that were taken from a normal distribution.  I could make a guess at the distribution (the solid black line below) as a normal distribution with mean =0 and sd = 1.  Here, we are just fitting two parameters (mean and sd), but this could be integrated into a regression of arbitrary complexity.

```{r}
set.seed(111)
data <- rnorm(100)* 1.5+3
hist(data,col="gold",ylim=c(0,50))
mean=0
sd=1
points((-100:100)/10, dnorm((-100:100)/10,mean=mean,sd=sd)*100,type="l",lwd=3)
points(data,  dnorm(data,mean=mean,sd=sd)*100,type="p",cex=1.5)


```

I've plotted the distribution we guessed, along with where each point falls on that distribution. Note that the height of the density is scaled to match the histogram, and so they are not really on the same y-axis.

MLE maximizes the product of the likelihood value for each point.  If the parameters are $\mu$ and $\sigma$, we can represent the likelihood of point $i$ as $L(\mu,\sigma)_i$
$L(\mu,\sigma) = \prod_i { L(\mu,\sigma)_i}$. The values of $\mu$ and $\sigma$ that maximize $L(\mu,\sigma)$ also maximize $log(L(\mu,\sigma))$, which gives us $log(L(\mu,\sigma)) =\sum_i{log(L(\mu,\sigma)_i)})$.  Numerically, it is a lot easier to add together values, and so we generally use log(likelihood) because it is a faster and more precise calculation. So, calculating the log-likelihood of a model given data is just the sum of the log of the density function--the sum of the log of the curve at each point on the curve.
```{r}
sum(log(dnorm(data,mean=mean,sd=sd)))
```


We'd like to find the values of mean and sd that maximize log-like. let's do a simple grid search:

```{r}
library(tidyr)
means <- seq(-1,6,.5)
sds <- seq(1,2.5,.25)
loglikes <- matrix(NA, nrow=length(means),ncol=length(sds))
for(i in 1:length(means))
  for(j in 1:length(sds))
  {
    loglikes[i,j] <- sum(log(dnorm(data,mean=means[i],sd=sds[j])))
  }

maxMean <-means[which(rowSums(loglikes==max(loglikes))==1)]
maxSD <- sds[which(colSums(loglikes==max(loglikes))==1)]


data.frame(val=means,loglikes) %>% pivot_longer(cols=2:8) %>%
  mutate(sd=sds[as.numeric(as.factor(name))]) %>%
  mutate(mean=val) %>%
  ggplot(aes(x=mean,y=value,color=sd,group=sd))  + geom_line() +
  theme_bw()  + annotate("text", 1,0,label=paste("Max Mean: ",maxMean)) + 
  annotate("text", 1,-40,label=paste("Max SD: ",maxSD)) + 
  annotate("text",1,-80,label=paste("Max log(Like)= ",round(max(loglikes),2)))


```


We can easily find that the maximum likelihood combination occurred at mean=3 and sd=1.5, which is close to the values we used to generate the data.

If we happened to get the sd right, we can look at how the log-likelihood is impacted by moving the true mean of the distribution away (from 3 to 2.5 to 2):

```{r}
par(mfrow=c(1,3))
imean = 1
isd = 6
##the best one
hist(data,col="gold",ylim=c(0,50),main=paste("mean = ",means[imean],", sd =",sds[isd], "\nlog-like=",round(loglikes[imean,isd],2)))
points((-100:100)/10, dnorm((-100:100)/10,mean=means[imean],sd=sds[isd])*100,type="l",lwd=3)
points(data,  dnorm(data,mean=means[imean],sd=sds[isd])*100,type="p",cex=1.5)

imean = 9
isd = 6
##the best one
hist(data,col="gold",ylim=c(0,50),main=paste("mean = ",means[imean],", sd =",sds[isd], "\nlog-like=",round(loglikes[imean,isd],2)))
points((-100:100)/10, dnorm((-100:100)/10,mean=means[imean],sd=sds[isd])*100,type="l",lwd=3)
points(data,  dnorm(data,mean=means[imean],sd=sds[isd])*100,type="p",cex=1.5)


imean = 13
isd = 6
##the best one
hist(data,col="gold",ylim=c(0,50),main=paste("mean = ",means[imean],", sd =",sds[isd], "\nlog-like=",round(loglikes[imean,isd],2)))
points((-100:100)/10, dnorm((-100:100)/10,mean=means[imean],sd=sds[isd])*100,type="l",lwd=3)
points(data,  dnorm(data,mean=means[imean],sd=sds[isd])*100,type="p",cex=1.5)


```

As the mean gets further and further off, the likelihood goes down. Notice that when we get the mean right, more of the points fall at the peak of the distribution.  Similarly, suppose we got the mean right, but are searching for the right sd:



```{r}
par(mfrow=c(1,3))

imean = 9
isd = 1

##the best one
hist(data,col="gold",ylim=c(0,50),main=paste("mean = ",means[imean],", sd =",sds[isd], "\nlog-like=",round(loglikes[imean,isd],2)))
points((-100:100)/10, dnorm((-100:100)/10,mean=means[imean],sd=sds[isd])*100,type="l",lwd=3)
points(data,  dnorm(data,mean=means[imean],sd=sds[isd])*100,type="p",cex=1.5)


imean = 9
isd = 3

##the best one
hist(data,col="gold",ylim=c(0,50),main=paste("mean = ",means[imean],", sd =",sds[isd], "\nlog-like=",round(loglikes[imean,isd],2)))
points((-100:100)/10, dnorm((-100:100)/10,mean=means[imean],sd=sds[isd])*100,type="l",lwd=3)
points(data,  dnorm(data,mean=means[imean],sd=sds[isd])*100,type="p",cex=1.5)


imean = 9
isd = 7

##the best one
hist(data,col="gold",ylim=c(0,50),main=paste("mean = ",means[imean],", sd =",sds[isd], "\nlog-like=",round(loglikes[imean,isd],2)))
points((-100:100)/10, dnorm((-100:100)/10,mean=means[imean],sd=sds[isd])*100,type="l",lwd=3)
points(data,  dnorm(data,mean=means[imean],sd=sds[isd])*100,type="p",cex=1.5)





```

Notice that now, when we under-estimate the standard deviation (left), the likelihood values near the peak give greater values, but we underestimate those in the tails.  This can be very costly, because those values are near 0 and have a huge impact on the log-likelihood.  On the other hand, if we over-estimate the sd, the peak gets counted less--and this is where most of the points are.  The MLE is the point at which these balance out perfectly, which is close to what we see in the center.  

Note that the maximum likelihood finds a particular combination of parameters that maximize the likelihood. But as we saw in the likelihood graphs, there are large areas of parameters that are almost as likely, and could easily have generated the data we saw. Another approach is to identify the distribution of the parameters that could have generated the data---this is done with a Bayesian estimation approach, and the distributions are called posterior estimates (the distribution of values given data).


So, with MLE, we fit the entire distribution to the data. That means that if we have a data distribution that is not normal, we could fit a non-normal distribution.  Consider this skewed data, which we can fit with a weibull distribution, which has two parameters; scale and shape. we will start with a simple weibull distribution just to show basically how it looks.

```{r}
set.seed(101)
a <- exp(1.3+.9*rnorm(50))
hist(a,breaks=0:20,ylim=c(0,15),col="gold")
points(0:200/10,dweibull(0:200/10,.2,.3)*50,type="l")
points(a,dweibull(a,.2,.3)*50)
```
Using the same parameter search we did before, we can hone in on a region that maximimizes likelihood:
```{r}
shapes <- seq(1,2.5,.1)
scales <- seq(.5,10,.5)
loglikes <- matrix(NA, nrow=length(shapes),ncol=length(scales))
for(i in 1:length(shapes))
  for(j in 1:length(scales))
  {
    loglikes[i,j] <- sum(dweibull(a,shape=shapes[i],scale=scales[j],log=TRUE))
  }

matplot(shapes,loglikes,type="l",lty=1,col="black", ylim=c(-250,0))

maxshape <-shapes[which(rowSums(loglikes==max(loglikes))==1)]
maxscale <- scales[which(colSums(loglikes==max(loglikes))==1)]
print(paste("Shape: ",maxshape))
print(paste("scale: ",maxscale))

hist(a,breaks=0:20,ylim=c(0,15),col="gold",main=paste("Shape: ",maxshape,"\nscale:",maxscale))
points(0:200/10,dweibull(0:200/10,maxshape,maxscale)*50,type="l")
points(a,dweibull(a,maxshape,maxscale)*50)

```


For many distributions, there are better ways of estimating the maximum likelihood parameters than grid-search.  In fact, we tend to choose theoretical distributions whose maximum likelihood parameters are easy to estimate. However, the estimation process typically requires iterative numerical estimation of the values in a way that is proved to converge to the best estimate.  We don't usually have to worry about this-- it is  implemented behind the scenes in the glm() function in R generalized least models, using using an iterative weighted least squares process. So before we cover glm, let's briefly consider weighted least squares.


# Weighted Least Squares
One way we can extend the OLS model is to consider weighted regression.  Sometimes, to create a better, more predictive, or more representative model, we might want to weigh some values more than others.  Consider the state.x77 data set:

```{r}
head(state.x77)
```




Suppose we want to compute whether murder rate can be predicted based on income, illiteracy, and high school graduation rate

```{r}
state <- as.data.frame(state.x77)
colnames(state)[c(4,6)] <- c("LifeExp","HS") 
state$name <- rownames(state)
model1 <- lm(Murder~Income+Illiteracy+HS,data=state)
summary(model1)
        
```
In this model, illiteracy predicts murder rate, but income and HS does not.  But, states differ substantially in their population.  If we consider the larger states more strongly, we can downplay the small states, which might possibly be noisier observations. We can do this by providing the weights argument:


```{r}
model2 <- lm(Murder~Income+Illiteracy+HS,weights=Population,data=state)
summary(model2)
```
We can see that now, income matters.  It is hard to understand why, so let's look at some scatterplots:
```{r}
library(GGally)
library(dplyr)
ggpairs(dplyr::select(state,Murder,Population,Income, Illiteracy,HS),progress=F)
```


These show correlations between Murder rate  and income might be impacted by an outlier.  We can make the size of the point scale to population size, to see why the weighted model might be doing better:

```{r}
library(ggplot2)
library(ggrepel)

ggplot(state,aes(x=Income,y=Murder,size=Population)) + geom_point() + geom_text_repel(aes(label=name))
   
 cor(state$Income,state$Murder)
```

Notice how when all states are equally weighted, we don't see a significant relationship between income and Murder rate.  Maybe this is unfair because some states are much larger than others.  If we force the deviations for the larger states to have a larger impact, we see a significant (positive) relationship. Notice that the primary relationship between income and murder rate is negative, but this is fighting against a very strong impact of illiteracy--once illiteracy is considered, there was a positive relationship between murder rate and income.    This is difficult to interpret completely, because it might alternately be reasonable to include population as a predictor.




# Generalized Linear Regression

Many times, we can suspect from first principles that an ordinary linear regression model will be incorrect, mainly because of the type of outcome/response data we are trying to fit.  For example, if your outcome is all 'true' or 'false', regardless of how your model is made, the errors will all be one of two values--definitely not normally distributed.   If we adapt three things about the 'ordinary least squares' linear regression model, we can have a lot more flexibility while maintaining reasonable understanding of the relationship.

 1. Use Maximum likelihood instead of minimize residuals
 2. Permit transforms of the outcome variable (called a 'link' function)
 3. Allow exponential-family error distributions to permit non-normal residuals.


So, instead of the 'ordinary least squares' regression model:

$y = \beta_0 + \beta_1*x_1 + \beta_2*x_2 ... \beta_n*x_n + \epsilon$
where $\epsilon ~ N(0,\sigma^2)$ (i.e., the errors are normally distributed)


we have

$G(y) = \beta_0 + \beta_1*x_1 + \beta_2*x_2 ... \beta_n*x_n + \epsilon$

where $\epsilon$, the error distribution, can take be any exponential-family dsitribution (including normal, poisson, binomial, and others). Nelder and Wedderburn (1972), who demonstrated how #1 could be used to estimate parameters for when #2 and #3 are adopted, are often credited as introducing and popularizing the notion.  In fact, one of the major contributions was showing how weighted least squares could be used iteratively to obtain the maximum likelihood estimate.  An slight alternative used by glm in R is called Fisher's Scoring iterations, which are reported in the output.  So, the slight downside of the glm is it can no longer be estimated directly with simple statistics, but it can be estimated numerically using methods a few rounds of regression.  Thus, computationally it is typically a little slower than normal regression, but unless you have very large data sets it probably won't make much of a difference. 

McCullagh and Nelder (1989) are one of the most comprehensive books on the topic. Because the GLM uses Maximimum Likelihood, it adopts a a generalized notion of variance called `deviance' which is defined as $-2L_{max}$.  Nelder makes generalizations to the analysis of variance procedures  called analysis of deviance. However, you should recognize that deviance, if adjusted appropriately using the number of parameters to penalize for model complexity, becomes the AIC information metric (and the BIC if also adjusted for the number of observations.)


## Generalized Linear Regression as linear regression.

If the link function is the "identity" function $f(x)=x$ and the error distribution is guassian/normal, we get ordinary least squares (albeit estimated with maximum likelihood)


```{r}

x <-matrix(runif(40),ncol=4) 
y <-  3 + x %*% c(-1,3.5, 2.2, -4) + rnorm(10)*2

summary(lm(y~x))

summary(glm(y~x,family=gaussian()))

```

Notice how the estimates of parameter values are the same, but now we get 'deviance'. Deviance is related to the log-likelihood of the data, rather than the sum-squared error.


## Modeling counts data: Poisson Generalized Linear Regression

Consider data that are counts--usually small whole numbers.  For example, you may have a large epidemiological study with 10,000 enrollees; half of whom took a vaccine to prevent an infection that leads to cancer, and another half who did not. At the end of the day, you may have 25 people in the control group, in comparison to  13 in the vaccine group,  who were diagnosed with the cancer after 5 years. Did the treatment have an effect?  It appears to have reduced the incidence of a low-probability event by about half, which would be great if you could have identified who the twelve people were saved and just give the vaccine to them.

A standard regression model might be fine here, but there are a few reasons why you might entertain a glm.  First, counts have a lower value of 0, and can only take on whole numbers--two ways the normal error distribution may be violated.  However, the Poisson distribution is designed for this sort of data--it is a discrete distribution predicting the number of times an event would occur given a known rate of occurrence.  Also, counts are ratio-scaled, so that a value twice as large as another has a real meaning.  This implies a logarithmic transform (link) is reasonable.  For , example, suppose you found that women were twice as likely to get the disease as men. You might assume that the treatment would reduce the impact of the disease by 50% in both men and women, which is a linear effect on log(incidence), or a log-link function.

As a worked example, consider the FIFA World Cup data set described in the following paper:
http://www.public.iastate.edu/~curleyb/research.html

Here, they collected statistics from FIFA World Cup matches dating back to 1930.  Let's consider the total number of yellow and red cards issued over time.  This is the sum of three columns.

```{r}
wc <- read.csv("world_cup.csv")
head(wc)
wc$Cards <- wc$Y + wc$YCR + wc$R 
```

According to the data description, the columns are 

* Team
* MP = matches played,
* GF = goals for,
* GA = goals against,
* PEN = penalty goals
* SOG = shots on goal,
* SW = shots wide,
* FKR = free kicks received,
* OFF = offsides,
* CK = corner kicks,
* W = wins,
* D = draws,
* L = losses,
* Y = yellow cards,
* YCR = second yellow card and red card,
* R = red card,
* FC= fouls committed,
* FS = fouls suffered,
* Year =year of world cup,
* Loc =host nation.

To predict cards given, maybe we want to see whether players are more likely to behave badly in response to game situations and rule violations. We can compute the end total points and margin of victory, and also use number of offsides, fouls committed, and fouls against. We will start with a normal linear regression, using a polynomial of power 3 for to capture the non-linear increase over time. So far, this is just a standard linear regression. Because each team played different numbers of matches, we will include MP as a predictor as well.

```{r}

wc$margin <- wc$GF-wc$GA
wc$total <- wc$GF+wc$GA

model <- lm(Cards~poly(Year,3)+ margin + total+SOG+OFF+FC+FS+MP,data=wc)

wc$fit1 <- model$fitted.values

ggplot(wc,aes(x=Year,y=Cards))+geom_point()+geom_jitter() + 
   geom_point(aes(x=Year,y=fit1),colour="gold",size=2) + theme_bw()

summary(model)
```
Overall, the model fits reasonably well ($R^2=.73$).  Furthermore, most of the predictors are significant, indicating that total points and shots-on-goal are marginally predictive, but offsides and other fouls really are predictive of this 'bad behavior' warnings.

Next, let's fit this as a glm.  Here, the model is identical, but we specify family=poisson.  We don't need to specify the link as log here, because that is the default, but we did anyway.  By specifying a poisson/log link, we are trying to model a couple things:

* Note that in the original, predicted values are sometimes less than 0. A poisson model does not allow negative values.
* Note that the effect of any predictor on penalties is the same, regardless of era. So, the FC cost increases penalty cards the same amount (.07 per FC) as much in 1930 as it does in 2000, when 5 times as many cards are being given. A log-link will make this impact proportional instead of linear.




```{r}
model2 <- glm(Cards~poly(Year,3)+margin+total+SOG+OFF+FC+FS+ MP,
              family=poisson(link="log"),data=wc)
wc$fitted <- model2$fitted.values

ggplot(wc,aes(x=Year,y=Cards))+geom_point()+geom_jitter()+
   geom_point(aes(x=Year,y=fitted),colour="gold",size=2) + theme_bw()

summary(model2)

cor(wc$fitted,wc$Cards)^2

```
In this case, the results are fairly similar ($R^2=.78$), except SOG/OFF are no longer significant but margin and total are.  This happens in part because these predictors all impact the total number of cards as a proportion of baseline, not as an additive value. So, margin and total impact the proportion of red cards, allowing them to have very small impact back when no cards were given, but when 10-15 cards are given per team in the tournament, these might impact by a larger margin.  But the fit looks more reasonable, especially at the low end, where our normal regression made predictions of negative values.

## Deviance and Comparing Models

The Null deviance is like the total residual error for the intercept-only model, and "Residual deviance" is like the RSE for the current model. We can examine and compare models usung these deviance scores, or via the AIC (because this uses likelihood). Suppose we want to see if a 4th order polynomial is better:

```{r}
model2b <- glm(Cards~poly(Year,4)+margin+total+SOG+OFF+FC+FS+MP,
              family=poisson(link="log"),data=wc)
summary(model2b)

AIC(model2)
AIC(model2b)
anova(model2, model2b,test="Chisq")
```

Deviance is often compared using a Chi-squared test, although the AIC measure is also appropriate.  Here, the model2b has the lower AIC (just barely), but it is not significantly better than the smaller model according to a chi-squared test. Now, could we test specifically whether SOG matters? Let's remove it from the model and compare the two.

```{r}

model2c <- glm(Cards~poly(Year,4)+margin+total+OFF+FC+FS+MP,
              family=poisson(link="log"),data=wc)
summary(model2c)

AIC(model2b)
AIC(model2c)
anova(model2b,model2c,test="Chisq")
```
Removing SOG makes a significant difference according to the $\chi^2$ test, and the AIC value is larger/worse (1468 vs 1464). Both of these tell us that SOG matter, because removing it makes for a significantly worse model.


## Interpreting parameters
In the Poisson model, the parameter estimates are interpretable in terms of their impact on  the log-outcome.


$log(y) = \beta_0 + \beta_1 x_1 + \beta_2 x_2 ...$

Which implies:

$y = exp(\beta_0 + \beta_1 x_1 + \beta_2 x_2 ...)$

$y = exp(\beta_0) \exp({\beta_1 x_1}) \exp({\beta_2 x_2})...$

You can see how y is the product of each $\beta x$ term exponentiated. But since the $\beta$ values are available, we can re-write this as each $exp({\beta})$ value raised to the $x_i$ power. Focusing on just a few of the parameters:

$y = exp(.49) \times  exp(.02386)^{margin} \times exp(.0287)^{total}$
     
Calculating each exponential, we get:

$y = 1.63 \times 1.023^{margin} \times 1.029^{total} ...$

This suggests that for each unit of total, the card rate increases by 2.9\%; and for each point difference between winner and loser the card rate increases by 2.3\%.  Notice that prior to tranform, the beta weight is approximately equal to the the transformed rate -1.0.  For small values, this is a well-known engineering approximation; for large values such as the intercept (.49), this no longer holds well (.49 vs .63).


## Overdispersion

On important diagnostic of the GLM is to know whether the deviance is too large.  The rule of thumb is that deviance should generally be about the same as the number of the residual degrees of freedom.  If it is substantially larger, we should consider a better model, a different model, or accounting for the so-called over-dispersion directly.  There are many resources for determining how to deal with this.

In general, the quasi- models allow for fitting of an overdispersion parameter.  With the over-dispersion model, we can force the dispersion parameter to 1 (no overdispersion) to see if it makes a difference. A larger value means that the variance has more variability than you would expect if the assumptions were correct.

```{r}
model.od <- glm(Cards~poly(Year,3)+margin+total+FC+FS,
              family=quasipoisson(link="log"),data=wc)
summary(model.od)
anova(model.od,test="Chisq")
anova(model.od,dispersion=1,test="Chisq")
```


Here, the overdispersion parameter of 1.7 is a bit worrisome, but the signifance tests were all significant even when using the quasipoisson model.

Overdispersion usually indicates that you have additional variability in your data set than the GLM error model supposes.  This is a bit  strange thinking about when we considering a normal distribution, but for specific distributions, the variability of the data suggest essentially that the data could not have arisen from a single parameter value.  An example might be that a basketball team's number of three-point attempts leads to a poisson model that has over-dispersion, which may be because different members of the team are responsible for different proportions of the shots, and these players are impacted by different other factors, leading to a data distribution that does not fit the poisson model well.  A Bayesian inference approach is another alternative to handling this issue.

For example, in the world cup data, different teams played different numbers of matches.  In model.od, I did not  include MP, and that might be a source of variability that does not easily map onto tho poisson distribution. Let's add it back in and see what happens:


```{r}
model.od <- glm(Cards~poly(Year,3)+margin+total+FC+FS+MP,
              family=quasipoisson(link="log"),data=wc)
summary(model.od)
anova(model.od,test="Chisq")
anova(model.od,dispersion=1,test="Chisq")
```
Now, the dispersion parameter is close to 1 (1.196), which suggests that we have accounted for an important source of variability that was violating the poisson model.
