It has long been a nuisance to me not being able to form margins on multiway tables in a simple fashion, so i wrote margins(). In my opinion it should go into the base package. The code and the documentation is in: http://www.biostat.ku.dk/~bxc/R/margins/ Please help yourself, and enhance and rename as you see fit. Bendix ---------------------- Bendix Carstensen Senior Statistician Steno Diabetes Center Niels Steensens Vej 2 DK-2820 Gentofte Denmark tel: +45 44 43 87 38 mob: +45 30 75 87 38 fax: +45 44 43 07 06 bxc@steno.dk www.biostat.ku.dk/~bxc
On Tue, 2 Mar 2004 14:20:01 +0100, "BXC (Bendix Carstensen)" <bxc@steno.dk> wrote :>It has long been a nuisance to me not being able to form margins on >multiway tables in a simple fashion, so i wrote margins(). > >In my opinion it should go into the base package. The code and the >documentation is in: > >http://www.biostat.ku.dk/~bxc/R/margins/ > >Please help yourself, and enhance and rename as you see fit.I really like the idea of this (and in fact was planning to work on something similar, when I had time). A couple of comments: It would be nice if the label for the added margin matched the name of the function, even when "FUN=fun" was used instead of "FUN list(name=fun)". It might be nice to be able to add some lines to the tables (though this is more of an ftable request than a margins request, the modifications will probably have to be done together). So for example the table Sea Black Dead Red White Min Max Aye Bee Oui Buzz 3 7 9 6 3 9 Hum 12 10 8 7 7 12 Si Buzz 10 9 7 9 7 10 Hum 5 9 3 6 3 9 Yes Buzz 0 10 5 5 0 10 Hum 12 8 9 8 8 12 Sum Buzz 13 26 21 20 13 26 Hum 29 27 20 21 20 29 could be displayed as Sea | Black Dead Red White | Min Max Aye Bee | | ---------------------------------------------- Oui Buzz | 3 7 9 6 | 3 9 Hum | 12 10 8 7 | 7 12 Si Buzz | 10 9 7 9 | 7 10 Hum | 5 9 3 6 | 3 9 Yes Buzz | 0 10 5 5 | 0 10 Hum | 12 8 9 8 | 8 12 ---------------------------------------------- Sum Buzz | 13 26 21 20 | 13 26 Hum | 29 27 20 21 | 20 29 (Fixed pitch fonts required!) Duncan
> -----Original Message----- > From: Duncan Murdoch [mailto:dmurdoch@pair.com] > Sent: Tuesday, March 02, 2004 3:13 PM > To: BXC (Bendix Carstensen) > Cc: r-devel@stat.math.ethz.ch > Subject: Re: [Rd] Margins on tables > > > On Tue, 2 Mar 2004 14:20:01 +0100, "BXC (Bendix Carstensen)" > <bxc@steno.dk> wrote : > > >It has long been a nuisance to me not being able to form margins on > >multiway tables in a simple fashion, so i wrote margins(). > > > >In my opinion it should go into the base package. The code and the > >documentation is in: > > > >http://www.biostat.ku.dk/~bxc/R/margins/ > > > >Please help yourself, and enhance and rename as you see fit. > > I really like the idea of this (and in fact was planning to > work on something similar, when I had time). > > A couple of comments: > > It would be nice if the label for the added margin matched > the name of the function, even when "FUN=fun" was used > instead of "FUN = list(name=fun)".I wanted to do this, but in R when you have a single object as argument you can retrieve its name by the deparse(substitute())-trick, but if the function is just given as part of a list element there is no way (thai I know of) to get at its name. You can of course do the deparse(substitute())-trick, and embark on major text-processing to get you the name.> > It might be nice to be able to add some lines to the tables > (though this is more of an ftable request than a margins > request, the modifications will probably have to be done > together). So for example the table > > Sea Black Dead Red White Min Max > Aye Bee > Oui Buzz 3 7 9 6 3 9 > Hum 12 10 8 7 7 12 > Si Buzz 10 9 7 9 7 10 > Hum 5 9 3 6 3 9 > Yes Buzz 0 10 5 5 0 10 > Hum 12 8 9 8 8 12 > Sum Buzz 13 26 21 20 13 26 > Hum 29 27 20 21 20 29 > > could be displayed as > > Sea | Black Dead Red White | Min Max > Aye Bee | | > ---------------------------------------------- > Oui Buzz | 3 7 9 6 | 3 9 > Hum | 12 10 8 7 | 7 12 > Si Buzz | 10 9 7 9 | 7 10 > Hum | 5 9 3 6 | 3 9 > Yes Buzz | 0 10 5 5 | 0 10 > Hum | 12 8 9 8 | 8 12 > ---------------------------------------------- > Sum Buzz | 13 26 21 20 | 13 26 > Hum | 29 27 20 21 | 20 29 > > (Fixed pitch fonts required!) >Good you did not ask for the Bee margins too... When I use the unsurpassed tabulation feature in SAS, proc tabulate, I always use the options: " formchar=' ' noseps ". If you know what they do you know why I did not implement your suggestion. The thing you want is an xtable function for an ftable object, so you can get a nice readable LaTeX table with all the \multicolumn and \cline bells and whistles. I'm sitting here for someone to produce it... Best, Bendix> Duncan
On Tue, 2 Mar 2004 16:56:58 +0100, you wrote:>I wanted to do this, but in R when you have a single object as argument >you can >retrieve its name by the deparse(substitute())-trick, but if the >function is just >given as part of a list element there is no way (thai I know of) to get >at its >name. You can of course do the deparse(substitute())-trick, and embark >on major >text-processing to get you the name.I was just thinking of the first case, where you had something like margins(A, FUN=mean) but I think you can handle the more complicated case using substitute(FUN), i.e. and using this function, which recursively goes through a list, and adds a name matching the value when the value is simple. add.names <- function(thelist) { n <- names(thelist) if (is.null(n)) n <- rep("", length(thelist)) for (i in seq(along=thelist)[-1]) { if (!is.call(thelist[[i]])) { if (n[i] == "") n[i] <- as.character(thelist[[i]]) } else if (as.character(thelist[[i]][[1]]) == "list") thelist[[i]] <- add.names(thelist[[i]]) } names(thelist) <- n thelist } I will add your function to the new "stats" package which is in the base R distribution (the same package holding ftable). I think it would be better to name it "add.margins", since "margins" sounds like a function to extract margins. What do you think of that change? Duncan Murdoch