Generalized+Linear+Model+(GLM)+Randomization

 GLM – Regression models that allow us to generalize the linear regression approach to accommodate many types of response variables including count, binary, proportions and positive valued continuous distributions (Nelder and Wedderburn 1972, Hilbe 1994, Hoffman 2004).  Randomization – Randomization (or permutation) tests involve reordering observed data values (see Crowley [1992], Edgington [1995], and Manly [2007] for detailed coverage of random resampling methods). Permutation tests provide for the fewest assumptions to be met by a dataset when explanatory variables are fixed.
 * Applications: **

 GLM – Homogeneity, Normality, and Independence of Residuals; Variance equal to Mean  Randomization – effectively removes assumptions above (Crowley 1992, Manly 2007).
 * Assumptions: **

** Package: ** car (1.2-9) ** Calls: ** Anova # Data must be in tab delimited text file to follow this routine. # Import data to R: //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">data.name //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><- read.delim(" //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">filename //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">.txt") <span style="font-family: Arial,Helvetica,sans-serif;">﻿# Check that column headers are correct:
 * <span style="font-family: Arial,Helvetica,sans-serif; font-size: 150%;">Program: ** <span style="font-family: Arial,Helvetica,sans-serif; font-size: 125%;">R (2.8)
 * <span style="font-family: Arial,Helvetica,sans-serif; font-size: 125%;"> Package: ** <span style="font-family: Arial,Helvetica,sans-serif; font-size: 110%;">stats
 * <span style="font-family: Arial,Helvetica,sans-serif; font-size: 110%;"> Calls: **<span style="font-family: Arial,Helvetica,sans-serif;"> glm + fitted, resid, plot, lag.plot, qqnorm, qqline, abline, coef, deviance, df.residual
 * <span style="font-family: Arial,Helvetica,sans-serif; font-size: 125%;"> Generic Implementation: **

<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">names(// data.name //) <span style="color: #000000; font-family: 'Courier New',Courier,monospace;">## [1] " //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">var.1 //<span style="color: #000000; font-family: 'Courier New',Courier,monospace;">" " //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">var.2 //<span style="color: #000000; font-family: 'Courier New',Courier,monospace;">" " //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">... //<span style="font-family: 'Courier New',Courier,monospace;">" <span style="font-family: Arial,Helvetica,sans-serif;"># Construct the model:

<span style="font-family: Arial,Helvetica,sans-serif;"># Independence – 1. colinearty of explanatory variables, remove data replication, re-run

<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">pairs(with(// data.name //, cbind( <span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">//var.1//, //var.2// <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">, //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">var.3 //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">, //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">var.4 //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">, //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">... //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">))) //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><- with(// data.name //,glm(response.variable ~ //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">var.1 //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">+ //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">var.2 //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">+ //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">var.3 //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">+ //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">var.4 //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">+ <span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">... <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">, family= //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">family //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">)) <span style="font-family: Arial,Helvetica,sans-serif;"># <span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">family <span style="font-family: Arial,Helvetica,sans-serif;"> can be: <span style="font-family: 'Courier New',Courier,monospace;">gaussian, poisson, Gamma(link="identity"), binomial, quasi, quasipossion, <span style="font-family: Arial,Helvetica,sans-serif;"> or <span style="font-family: 'Courier New',Courier,monospace;">quasibinomial <span style="font-family: Arial,Helvetica,sans-serif;">. <span style="font-family: Arial,Helvetica,sans-serif;"># All columns of data in number format are assumed to be ratio data in R. <span style="font-family: Arial,Helvetica,sans-serif;"># To coerce to categorical use <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">factor( <span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">var.name <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">) <span style="font-family: Arial,Helvetica,sans-serif;"> as the variable. <span style="font-family: Arial,Helvetica,sans-serif;"># Interaction terms may be entered as <span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">var.name*var.name <span style="font-family: Arial,Helvetica,sans-serif;"> to include interaction and main terms, or as <span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">var.name:var.name <span style="font-family: Arial,Helvetica,sans-serif;"> for only the interaction term. <span style="font-family: Arial,Helvetica,sans-serif;"># Check assumptions with plots (keep plot window open, R will automatically replace each time) <span style="font-family: Arial,Helvetica,sans-serif;"># Straight Line (if you have a regression) & Homogeneity

<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">plot(fitted(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name // ), resid(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name // )) <span style="font-family: Arial,Helvetica,sans-serif;"># Independence – 2. lagged residuals <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">lag.plot(resid(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name // )) <span style="font-family: Arial,Helvetica,sans-serif;"># Normality <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">qqnorm(resid(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name // )) <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">qqline(resid(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name // )) <span style="font-family: Arial,Helvetica,sans-serif;"># Linear model fit (link function check) <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">plot(fitted(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name // ), with(// data.name //, //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">response.variable //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">)) <span style="font-family: Arial,Helvetica,sans-serif;"># Fit regression line to the plot <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">abline(lm(with(<span style="color: #000000; font-family: arial,helvetica,sans-serif;"><span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">// data.name //, //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">response.variable // ) ~ fitted(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name // ))) <span style="font-family: Arial,Helvetica,sans-serif;"># Extract regression coefficients <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">coef(lm(with(<span style="color: #000000; font-family: arial,helvetica,sans-serif;"><span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">// data.name //, //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">response.variable // ) ~ fitted(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name // ))) <span style="color: #000000; font-family: 'Courier New',Courier,monospace;">## (Intercept) fitted( //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">model.name //) <span style="color: #000000; font-family: 'Courier New',Courier,monospace;">## //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.######## #.######## // <span style="font-family: Arial,Helvetica,sans-serif;"><-this value is slope of importance <span style="font-family: Arial,Helvetica,sans-serif;"># Dispersion <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">deviance(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name // ) <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">df.residual(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name // ) <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">deviance(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name // ) / df.residual(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name // ) <span style="font-family: Arial,Helvetica,sans-serif;"># Print ANODEV table:

<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">Anova(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">model.name //, type="III") <span style="font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">## Anova Table (Type III tests) <span style="font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">## <span style="font-family: 'Courier New',Courier,monospace;">## Response: //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">response.variable // <span style="color: #000000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">## LR Chisq Df Pr(>Chisq) //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"> ## var.1 #.#### # #.###### // //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"> ## var.2 #.#### # #.###### // //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;"> ## ... #.#### # #.###### // <span style="font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">## --- <span style="font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">## Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 <span style="font-family: Arial,Helvetica,sans-serif;"># Assign experimental <span style="font-family: 'Courier New',Courier,monospace;">LR Chisq <span style="font-family: Arial,Helvetica,sans-serif;"> to a vector: //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">exp.chi //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><- data.frame(data.frame(Anova(// model.name //, type="III")) [,1]) <span style="font-family: Arial,Helvetica,sans-serif;"># Check that assignment was correct: //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">exp.chi // <span style="font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">## data.frame.Anova.model2..type....III......1. <span style="font-family: 'Courier New',Courier,monospace;">## 1 //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.###### // <span style="font-family: 'Courier New',Courier,monospace;">## 2 //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.###### // <span style="font-family: 'Courier New',Courier,monospace;">## ... //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.###### // <span style="font-family: Arial,Helvetica,sans-serif;"># Check that individual value within vector is correct: <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">exp.chi // [1,] <span style="color: #000000; font-family: 'Courier New',Courier,monospace;">## [1] //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">#.###### // <span style="font-family: Arial,Helvetica,sans-serif;"># Assign randomized <span style="font-family: 'Courier New',Courier,monospace;">LR Chisq <span style="font-family: Arial,Helvetica,sans-serif;"> values to a matrix: <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;"><span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">rand.chi // <- data.frame(rbind(replicate( //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">#### //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">, c(data.frame(with(// data.name //, Anova(glm(sample( //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">response.variable //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">, <span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">## //, FALSE) ~ <span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">var.1 //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">+ //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">var.2 //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">+ //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">var.3 //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">+ //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">var.4 //<span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">+ <span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">... <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace;">, family= //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">family // ), type="III"))) [,1])))) <span style="font-family: Arial,Helvetica,sans-serif;"># //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">#### //<span style="font-family: Arial,Helvetica,sans-serif;"> is number of randomizations. <span style="font-family: Arial,Helvetica,sans-serif;"># //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">## //<span style="font-family: Arial,Helvetica,sans-serif;"> must equal number of observations of response.variable. <span style="font-family: Arial,Helvetica,sans-serif;"># Calculate randomized p-value for each term of interest: <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">summary(c(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">rand.chi // [1,]) > <span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">exp.chi // [1,]) <span style="color: #000000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">## Mode FALSE TRUE NA's <span style="color: #000000; font-family: 'Courier New',Courier,monospace;">## logical <span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">### ### // <span style="color: #000000; font-family: 'Courier New',Courier,monospace;"> 0 <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">summary(c(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">rand.chi // [2,]) > <span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">exp.chi // [2,]) <span style="color: #000000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">## Mode FALSE TRUE NA's <span style="color: #000000; font-family: 'Courier New',Courier,monospace;">## logical <span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">### ### // <span style="color: #000000; font-family: 'Courier New',Courier,monospace;"> 0 <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">summary(c(<span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">rand.chi // [...,]) > <span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">exp.chi // [...,])

<span style="color: #000000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;">## Mode FALSE TRUE NA's <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; margin: 0px; padding: 0px;"><span style="color: #000000; font-family: 'Courier New',Courier,monospace;">## logical <span style="color: #000000; font-family: arial,helvetica,sans-serif;">//<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">### ### // <span style="color: #000000; font-family: 'Courier New',Courier,monospace;"> 0

<span style="font-family: Arial,Helvetica,sans-serif;"># //<span style="color: #0000ff; font-family: 'Courier New',Courier,monospace;">### //<span style="font-family: Arial,Helvetica,sans-serif;"> true divided by //<span style="color: #00ff00; font-family: 'Courier New',Courier,monospace;">#### // randomizations is the p-value for each term.

**Weblinks:** N/A


 * References: **

=
<span style="font-family: Arial,Helvetica,sans-serif;">Carroll RJ, Spiegelman CH. 1992. Diagnostics for nonlinearity and heteroscedasticity in errors-in-variables regression. Technometrics 34(2):186-196. ======

=
<span style="font-family: Arial,Helvetica,sans-serif;">Crowley PH. 1992. Resampling methods for computation-intensive data analysis in ecology and evolution. Annual Review of Ecology and Systematics 23:405-447. ======

=
<span style="font-family: Arial,Helvetica,sans-serif;">Guisan A, Edwards TC, Hastie T. 2002. Generalized linear and generalized additive models in studies of species distributions: setting the scene. Ecological Modelling 157(2-3):89-100. ======

=
<span style="font-family: Arial,Helvetica,sans-serif;">Petraitis PS, Beaupre SJ, Dunham AE. 2001. ANCOVA: nonparametric and randomization approaches. In: Scheiner SM, Gurevitch J, editors. Design and analysis of ecological experiments. 2nd ed. Oxford: Oxford University Press. p. 116-133. ======

=
<span style="font-family: Arial,Helvetica,sans-serif;">Potvin C, Roff DA. 1993. Distribution-free and robust statistical methods: viable alternatives to parametric statistics? Ecology 74(6):1617-1628. ======

=
<span style="font-family: Arial,Helvetica,sans-serif;">R Development Core Team. 2008. R: A Language and Environment for Statistical Computing. Vienna: R Foundation for Statistical Computing. Version 2.8.0. Available from http://www.r-project.org/ ======