Skip to content

effect function (effects package)

4 messages · Hlavka, Eileen, Marc Girondot, Petr Savicky +1 more

#
Hi "the list" !

I would like to create a dataframe that answer to : "all the 
combinations of the different way to distribute n indistinguishable 
balls into k distinguishable boxes". Let take an example: 2 balls in 3 
boxes:
Box1 Box2 Box3
2       0       0
1       1       0
1       0       1
0       2       0
0       1       1
0       0       2

I have made a script (see script below) using expand.grid but it 
generates huge number of unnecessary solutions that I must filter. And 
when the number of balls or box is large, the size of the dataframe can 
be huge.

Has someone a solution for this problem ?

Thanks a lot

(I don't play with balls and box... it is a way to manage uncertainty. I 
know that 2 events have occured during a period of 3 days but I don't 
know when exactly)

Marc




Nballs<-2
nbbox<-3


# The number of different ways to distribute n indistinguishable balls
# into k distinguishable boxes is C(n+k-1,k-1).
# nb<-choose(Nballs+nbbox-1,nbbox-1)=dim(tb)[1]

if (Nballs==0) {
tb<-as.data.frame(matrix(rep(0, nbbox), ncol=nbbox))

} else {

es<-list(0:(Nballs-1))
es<-rep(es, nbbox)

tb<-expand.grid(es)
tb<-tb[apply(tb, 1, sum)==Nballs,1:nbbox]

# trick to have smaller tb after expand.grid
tbfull<- as.data.frame(matrix(rep(0, nbbox*nbbox), ncol=nbbox, 
dimnames=list(NULL, names(tb))))
for(i in 1:nbbox) {tbfull[i, i]<-Nballs}

tb<-rbind(tb, tbfull)

}

Result:

 > tb
    Var1 Var2 Var3
4     1    1    0
6     1    0    1
7     0    1    1
41    2    0    0
5     0    2    0
61    0    0    2
#
On Sat, Feb 04, 2012 at 08:45:20AM +0100, Marc Girondot wrote:
Hi.

The number of these placements is choose(n+k-1, k-1), which follows
from the representation of each placement by a sequence of balls and
boundaries between boxes, where "o" is a ball and "|" is a boundary.
For example (2, 0, 0) corresponds to "oo||" and
(1, 0, 1) is "o||o". Since this is a bijection, we can use it
for counting and also for generation.

  n <- 2
  k <- 3
  # generate all possible positions of the boundaries
  x <- combn(n+k-1, k-1)
  # compute the number of balls in each box
  a <- cbind(0, diag(k)) - cbind(diag(k), 0)
  t(a %*% rbind(0, x, n+k) - 1)

     [,1] [,2] [,3]
[1,]    0    0    2
[2,]    0    1    1
[3,]    0    2    0
[4,]    1    0    1
[5,]    1    1    0
[6,]    2    0    0

Hope this helps.

Petr Savicky.
#
Dear Eileen,

You apparently misunderstand what the effect() function does; it computes
fitted values and their standard errors under the model (by default in a GLM
on the scale of the response) at specific combinations of values of the
predictors (e.g., letting two interacting predictors range over their
combinations of values while others are held to their means). Details are
provided in the references given in ?effect; see in particular the 2003
Journal of Statistical Software article at
<http://www.jstatsoft.org/v08/i15>. 

I hope this helps,
 John

--------------------------------
John Fox
Senator William McMaster
  Professor of Social Statistics
Department of Sociology
McMaster University
Hamilton, Ontario, Canada
http://socserv.mcmaster.ca/jfox