Hello everybody, I'm writting some functions for experimental designs. The one I'm working on is similar to "fac.design" on Splus. The problem I have is with the form of the output : When the argument fraction is submitted, Splus gives something like : A B C 1 - - - 2 + + - 3 + - + 4 - + + Fraction : ~A:B:C The first part of this output is the design, and the second part is the formula which allows to fractionate the full design. This output isn't a list. My problem is the following : I can't manage to obtain such a composite output. The lonely way I've already found is to use : print(design) cat("Fraction: ") print(attr(design,"fraction")). This solution is only suitable for an output on screen, but it reveals bad as soon as I assign this function to an object (for ex: tmp <- fac.design( ....) ) since only the last command I performed (here print(attr(design,"fraction"))) is assigned to my object tmp. I hope someone can help me ... Regards, Isabelle Zabalza-Mezghani -- Isabelle Zabalza-Mezghani Tel : 01 47 52 61 99 Institut Fran?ais du P?trole E-mail : isabelle.zabalza-mezghani at ifp.fr 1-4 Av. Bois Preau - Bat Lauriers 92852 Rueil Malmaison Cedex, France -------------- next part -------------- An HTML attachment was scrubbed... URL: https://stat.ethz.ch/pipermail/r-help/attachments/20001019/4b7217a1/attachment.html
On Thu, 19 Oct 2000, isabelle Zabalza-Mezghani wrote:> Hello everybody, > > I'm writting some functions for experimental designs. The one I'm > working on is similar to "fac.design" on Splus. > The problem I have is with the form of the output : > > When the argument fraction is submitted, Splus gives something like : > > A B C > 1 - - - > 2 + + - > 3 + - + > 4 - + + > > Fraction : ~A:B:C > > The first part of this output is the design, and the second part is the > formula which allows to fractionate the full design. This output isn't a > list. > My problem is the following : > I can't manage to obtain such a composite output. The lonely way I've > already found is to use : > print(design) > cat("Fraction: ") > print(attr(design,"fraction")). > > This solution is only suitable for an output on screen, but it reveals > bad as soon as I assign this function > to an object (for ex: tmp <- fac.design( ....) ) since only the last > command I performed (here print(attr(design,"fraction"))) > is assigned to my object tmp. > > I hope someone can help me ...It's done via classes. fac.design returns an object of class c("design", "data.frame"), and that is printed by the method print.design for the generic function print. To see how this might be done in R, look at print.Anova in package MASS. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
isabelle Zabalza-Mezghani <isabelle.zabalza-mezghani at ifp.fr> writes:> Hello everybody, > > I'm writting some functions for experimental designs. The one I'm > working on is similar to "fac.design" on Splus. > The problem I have is with the form of the output : > > When the argument fraction is submitted, Splus gives something like > : > > ???? A? B? C > ?1?? -? -? - > ?2?? +? +? - > ?3?? +? -? + > ?4?? -? +? + > > Fraction :? ~A:B:C > > The first part of this output is the design, and the second part is > the formula which allows to fractionate the full design. This > output isn't a list. > My problem is the following : > I can't manage to obtain such a composite output. The lonely way > I've already found is to use : > print(design) > cat("Fraction:? ") > print(attr(design,"fraction")). > > This solution is only suitable for an output on screen, but it > reveals bad as soon as I assign this function > to an object (for ex: tmp <- fac.design( ....) ) since only the > last command I performed (here print(attr(design,"fraction"))) > is assigned to my object tmp. > > I hope someone can help me ...Return a list. For example, here is a simple function to calculate and return a risk ratio and 95% confidence from a 2-by-2 table rr22 <- function(exposure, outcome) { tab <- table(exposure, outcome) a <- tab[1,1]; b <- tab[1,2]; c <- tab[2,1]; d <- tab[2,2] rr <- (a / (a + b)) / (c / (c + d)) se.log.rr <- sqrt((b/a) / (a+b) + (d / c) / (c + d)) lci.rr <- exp(log(rr) - 1.96 * se.log.rr) uci.rr <- exp(log(rr) + 1.96 * se.log.rr) list(estimate = rr, conf.int = c(lci.rr, uci.rr)) } In use: > rr22(PASTA, ILL) $estimate [1] 1.682692 $conf.int [1] 1.255392 2.255433 > b <- rr22(PASTA, ILL) > b$estimate [1] 1.682692 > b$conf.int [1] 1.255392 2.255433 > b$conf.int[1] [1] 1.255392 > b$conf.int[2] [1] 2.255433 If you need the formatted output then you need to declare a class for the output list and create a print method for that class: rr22 <- function(exposure, outcome) { tab <- table(exposure, outcome) a <- tab[1,1]; b <- tab[1,2]; c <- tab[2,1]; d <- tab[2,2] rr <- (a / (a + b)) / (c / (c + d)) se.log.rr <- sqrt((b / a) / (a + b) + (d / c) / (c + d)) lci.rr <- exp(log(rr) - 1.96 * se.log.rr) uci.rr <- exp(log(rr) + 1.96 * se.log.rr) rr22.output <- list(estimate = rr, conf.int = c(lci.rr, uci.rr)) class(rr22.output) <- "rr22" rr22.output } print.rr22 <- function(x) { cat("RR : ", x$estimate, "\n", "95% CI : ", x$conf.int[1], "-", x$conf.int[2], "\n", sep = "") } In use: > b <- rr22(PASTA,ILL) > b RR : 1.682692 95% CI : 1.255392-2.255433 > b$estimate [1] 1.682692 > b$conf.int [1] 1.255392 2.255433 I hope that helps. Mark -- Mark Myatt -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._