Skip to content
Prev 31544 / 398506 Next

expand.grid

I recently posted a question concerning an inconsistency of 
expand.grid in defining the reference level of the factors.
Thank you very much for the ready explanations and comments.

I found this inconsistency 
working with contingency tables and logistic models.  

I was working with a flat contingency table as for example,
low  0  1
race  smoke
white FALSE     40  4
      TRUE      33 19
black FALSE     11  5
      TRUE       4  6
other FALSE     35 20
      TRUE       7  5

(here bwt is a transformation of the dataframe birthwt
in library(MASS)). 

I wanted to analyse
this table by a logistic model with low as response. 
So it seems useful to have a function that extracts the 
factors race and smoke from the table:
race smoke
1 white FALSE
2 white  TRUE
3 black FALSE
4 black  TRUE
5 other FALSE
6 other  TRUE
  
in such a way that they can be directly used in glm:
...
Coefficients:
(Intercept)    raceblack    raceother    smokeTRUE
      1.841       -1.084       -1.109       -1.116
...
Note that the reference level for race is "white" (the first row). 

PS - Obviously, the same analysis is very easy from the original 
dataframe (which here is supposed to be missing):
except for the sign of the coefficients which are reversed
because the columns of ft are (failure, success) instead of
(success, failure). 

Thus, I wrote the function

"row.factors" <- function (ft)
{
# ft:    a flat table.
# Value: a data frame with the factors associated to the rows.
        vars <- attr(ft, "row.vars")
        k <- length(vars)
        expl <- expand.grid(vars[k:1])
        expl[,k:1, drop=FALSE]
}

The function worked pretty well except for the case of a simple 
contingency table.
low  0  1
race
white     73 23
black     15 11
other     42 25
race
1 white
2 black
3 other
[1] "black" "other" "white" 

Thus, here the reference level is "black" and this is a bit strange 
as the first row of the table is "white". This little "infelicity" 
is in fact caused by expand.grid. 


Thanks again


-- Giovanni