[R] Three-dimensional contingency table

Cheng Peng cpeng at usm.maine.edu
Sun Aug 29 15:13:01 CEST 2010


I didn't see any error when I ran your code in my computer:

> numbers <- c(1134,956,328,529,435,599,27,99) 
> dim(numbers) <- c(2,2,2) 
> numbers 
, , 1

     [,1] [,2]
[1,] 1134  328
[2,]  956  529

, , 2

     [,1] [,2]
[1,]  435   27
[2,]  599   99

> dimnames(numbers)[[3]] <-list("Mussels", "No Mussels") 
> dimnames(numbers)[[2]] <- list("Flow", "No Flow") 
> dimnames(numbers)[[1]] <- list("Depth1", "Depth2") 
> ftable(numbers) 
                Mussels No Mussels
                                  
Depth1 Flow        1134        435
       No Flow      328         27
Depth2 Flow         956        599
       No Flow      529         99
> as.data.frame.table(numbers) 
    Var1    Var2       Var3 Freq
1 Depth1    Flow    Mussels 1134
2 Depth2    Flow    Mussels  956
3 Depth1 No Flow    Mussels  328
4 Depth2 No Flow    Mussels  529
5 Depth1    Flow No Mussels  435
6 Depth2    Flow No Mussels  599
7 Depth1 No Flow No Mussels   27
8 Depth2 No Flow No Mussels   99
> frame <- as.data.frame.table(numbers) 
> names(frame) <- c("wd", "wv", "MP", "Count") 
> frame 
      wd      wv         MP Count
1 Depth1    Flow    Mussels  1134
2 Depth2    Flow    Mussels   956
3 Depth1 No Flow    Mussels   328
4 Depth2 No Flow    Mussels   529
5 Depth1    Flow No Mussels   435
6 Depth2    Flow No Mussels   599
7 Depth1 No Flow No Mussels    27
8 Depth2 No Flow No Mussels    99
> attach(frame) 
> model1 <- glm(Count~MP+wd+wv,poisson) 
> summary(model1)

Call:
glm(formula = Count ~ MP + wd + wv, family = poisson)

Deviance Residuals: 
       1         2         3         4         5         6         7        
8  
  2.5545   -7.0682   -0.1343    7.4889    1.0555    5.7533  -11.0107  
-4.2551  

Coefficients:
             Estimate Std. Error z value Pr(>|z|)    
(Intercept)   6.95668    0.02631 264.414  < 2e-16 ***
MPNo Mussels -0.93237    0.03466 -26.900  < 2e-16 ***
wdDepth2      0.12629    0.03127   4.039 5.37e-05 ***
wvNo Flow    -1.15626    0.03657 -31.618  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 2279.76  on 7  degrees of freedom
Residual deviance:  286.14  on 4  degrees of freedom
AIC: 355.18

Number of Fisher Scoring iterations: 4


-- 
View this message in context: http://r.789695.n4.nabble.com/Three-dimensional-contingency-table-tp2398915p2399062.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list