[R] Frequencies into Poisson responses

Murray Jorgensen maj at stats.waikato.ac.nz
Thu May 18 10:11:57 CEST 2006


Actually I had just meant that because I had some factors I had a 
conceptual table. Using ftable() was just my way of getting the factors 
into data frame form. But thank you for showing me that as.data.frame() 
does exactly what I want.

Murray Jorgensen

Prof Brian Ripley wrote:
> 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
>>
>>
> 

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