An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120204/8a4fddb8/attachment.pl>
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
__________________________________________________________ Marc Girondot, Pr Laboratoire Ecologie, Syst?matique et Evolution Equipe de Conservation des Populations et des Communaut?s CNRS, AgroParisTech et Universit? Paris-Sud 11 , UMR 8079 B?timent 362 91405 Orsay Cedex, France Tel: 33 1 (0)1.69.15.72.30 Fax: 33 1 (0)1.69.15.73.53 e-mail: marc.girondot at u-psud.fr Web: http://www.ese.u-psud.fr/epc/conservation/Marc.html Skype: girondot
On Sat, Feb 04, 2012 at 08:45:20AM +0100, Marc Girondot wrote:
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
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
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- project.org] On Behalf Of Hlavka, Eileen Sent: February-04-12 1:56 AM To: r-help at r-project.org Subject: [R] effect function (effects package) Dear all, How does the effect() function in the effects package calculate effects and standard errors for glm quasipoisson models? I was using effect() to calculate the impact of increasing x to e + epsilon, and then finding the expected percent change. I thought that this effect (as a percentage) should be exp(beta*epsilon), where beta is the appropriate coefficient from the model, but that's not what I'm getting using the effect() output. Sorry for the lack of example-it would require toy data etc. and seems unnecessary since my question is more conceptual. Thanks, Eileen
______________________________________________________________________
____
This email message is for the sole use of the intended
r...{{dropped:9}}
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-
guide.html
and provide commented, minimal, self-contained, reproducible code.