[R] Frequencies into Poisson responses

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu May 18 08:43:56 CEST 2006


In fact you have an ftable, not a multi-way table, but as.data.frame works 
for multi-way tables, and here as.data.frame(as.table(ftabc)) works:

> as.data.frame(as.table(ftabc))
    A B CC Freq
1  1 1  1   20
2  2 1  1   38
3  3 1  1   20
4  1 2  1   22
5  2 2  1   25
6  3 2  1   23
...

It would be more usual to start with a table and use ftable to display it.
It might just be worth adding as.data.frame.ftable.


On Thu, 18 May 2006, Murray Jorgensen wrote:

> 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
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list