[R] Frequencies into Poisson responses

Murray Jorgensen maj at stats.waikato.ac.nz
Thu May 18 03:48:17 CEST 2006


Suppose that one has several factors, all of the same length. These 
define a multi-way contingency table. Now suppose one wants to fit a 
Poisson GLM a.k.a. log-linear model to the frequencies in this table. 
How may we make the table into a data frame suitable for glm() ?
I have an answer to my own question below, but surely more elegant 
solutions exist?

set.seed(060518)
na <- nb <- 3
nc <- 4
n <- na*nb*nc
a <- round(runif(1000,0.5,na+0.5))
b <- round(runif(1000,0.5,nb+0.5))
cc <- round(runif(1000,0.5,nc+0.5))
A <- factor(a)
B <- factor(b)
CC <- factor(cc)
ftabc <- ftable(A,B,CC)
freqs <- as.vector(ftabc)
A1 <- gl(na,nb,n)
B1 <- gl(nb,1,n)
C1 <- gl(nc,na*nb,n)
required <- data.frame(A1,B1,C1,freqs)
required

Cheers,  Murray Jorgensen

-- 
Dr Murray Jorgensen      http://www.stats.waikato.ac.nz/Staff/maj.html
Department of Statistics, University of Waikato, Hamilton, New Zealand
Email: maj at waikato.ac.nz                                Fax 7 838 4155
Phone  +64 7 838 4773 wk    Home +64 7 825 0441    Mobile 021 1395 862




More information about the R-help mailing list