Re: [R] Frequencies into Poisson responses

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Thu 18 May 2006 - 16:43:56 EST

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

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Thu May 18 16:52:51 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 18 May 2006 - 20:10:13 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.