I have a simple 1 way anova coded like summary(ANOVA1way <- aov(Value ~ WellID, data = welldata)) How can I use the BY function to do this ANOVA for each group using another variable in the dataset?? I tried coding it like this, but it doesn't seem to work. summary(ANOVA1way <- by(welldata, Analyte, function(x) aov(Value ~ WellID, data = welldata))) In SAS I would code it like this: Proc sort data=welldata; by analyte; run; Proc glm data=welldata; by analyte; class wellid; model value = wellid; run; Any suggestions??? -- View this message in context: http://www.nabble.com/Running-an-ANOVA-with-a-BY-tp25645413p25645413.html Sent from the R help mailing list archive at Nabble.com.
baxterj wrote:> I have a simple 1 way anova coded like > > summary(ANOVA1way <- aov(Value ~ WellID, data = welldata)) > > How can I use the BY function to do this ANOVA for each group using another > variable in the dataset?? I tried coding it like this, but it doesn't seem > to work. > > summary(ANOVA1way <- by(welldata, Analyte, function(x) aov(Value ~ WellID, > data = welldata))) > > In SAS I would code it like this: > Proc sort data=welldata; by analyte; run; > Proc glm data=welldata; > by analyte; > class wellid; > model value = wellid; > run;Look at the plyr package for a general solution to this type of problems: http://cran.r-project.org/web/packages/plyr/index.html and its introductory guide on the package home page: http://had.co.nz/plyr/ HTH, Tobias
On Sep 28, 2009, at 11:39 AM, baxterj wrote:> > I have a simple 1 way anova coded like > > summary(ANOVA1way <- aov(Value ~ WellID, data = welldata)) > > How can I use the BY function to do this ANOVA for each group using > another > variable in the dataset?? I tried coding it like this, but it > doesn't seem > to work. >Since you have not offered a reproducible example, we are left to choose our own. Checking the help page for "by" we see that a problem extremely similar to yours is already solved. I choose not to attach() the data for well-understood reasons: anova.LMH <- by(warpbreaks, warpbreaks$tension, function(x) aov(breaks ~ wool, data=x) )> summary(ANOVA1way <- by(welldata, Analyte, function(x) aov(Value ~ > WellID, > data = welldata))) > > In SAS I would code it like this: > Proc sort data=welldata; by analyte; run; > Proc glm data=welldata; > by analyte; > class wellid; > model value = wellid; > run;David Winsemius, MD Heritage Laboratories West Hartford, CT
baxterj wrote:> I have a simple 1 way anova coded like > > summary(ANOVA1way <- aov(Value ~ WellID, data = welldata)) > > How can I use the BY function to do this ANOVA for each group using another > variable in the dataset?? I tried coding it like this, but it doesn't seem > to work. > > summary(ANOVA1way <- by(welldata, Analyte, function(x) aov(Value ~ WellID, > data = welldata))) > > In SAS I would code it like this: > Proc sort data=welldata; by analyte; run; > Proc glm data=welldata; > by analyte; > class wellid; > model value = wellid; > run; > > > Any suggestions???Given you have asked two questions on translating SAS idioms to R, you might be a candidate for Bob Muenchen's book which some people have previously recommended: [3] Robert A. Muenchen. R for SAS and SPSS Users. Springer Series in Statistics and Computing. Springer, 2009. ISBN: 978-0-387-09417-5. [ bib | Discount Info | Publisher Info ] This book demonstrates which of the add-on packages are most like SAS and SPSS and compares them to R's built-in functions. It steps through over 30 programs written in all three packages, comparing and contrasting the packages' differing approaches. The programs and practice datasets are available for download. David Scott -- _________________________________________________________________ David Scott Department of Statistics The University of Auckland, PB 92019 Auckland 1142, NEW ZEALAND Phone: +64 9 923 5055, or +64 9 373 7599 ext 85055 Email: d.scott at auckland.ac.nz, Fax: +64 9 373 7018 Director of Consulting, Department of Statistics
Or with plyr there's a more flexible approach: res.aov <- dlply(warpbreaks, .(tension), function(x) aov(breaks ~ wool, data=x)) # aov results are stored in a list, you can directly extract what you want with l*ply l_ply(res.aov, function(x) print(summary(x))) ldply(res.aov, function(x) data.frame( fitted(x))) ldply(res.aov, coefficients) HTH, Xavier ----- Mail Original ----- De: "Tobias Verbeke" <tobias.verbeke at gmail.com> ?: "baxterj" <jdew at vt.edu> Cc: r-help at r-project.org Envoy?: Lundi 28 Septembre 2009 22h55:34 GMT +01:00 Amsterdam / Berlin / Berne / Rome / Stockholm / Vienne Objet: Re: [R] Running an ANOVA with a BY baxterj wrote:> I downloaded the package and got it to work with the coding: > > model <- function(df) {aov(values ~ WellID, data = twelldata)}Hmm.. I guess you mean to use 'data = df' instead of 'data = twelldata'> ANOVA1way <- dlply(twelldata, .(Analyte), model) > print(ANOVA1way) > > This gives me degrees of freedom and sum of squares for each anova per > analyte. However, I cant get the summary(ANOVA1way) to work so that I can > get p-values, etc... How can I do this?You need to extend your model function: - extract the bits you want - construct a result data frame - return it and use a ddply For example (non-tested): model <- function(df) { lmObj <- lm(values ~ WellID, data = df) summaryLmObj <- summary(lmObj) res <- data.frame(intercept = coef(lmObj)[1], adjr2 = summaryLmObj$adj.r.squared) # extract and insert anything return(res) } (ANOVA1way <- ddply(twelldata, .(Analyte), model)) HTH, Tobias> Tobias Verbeke-2 wrote: >> baxterj wrote: >>> I have a simple 1 way anova coded like >>> >>> summary(ANOVA1way <- aov(Value ~ WellID, data = welldata)) >>> >>> How can I use the BY function to do this ANOVA for each group using >>> another >>> variable in the dataset?? I tried coding it like this, but it doesn't >>> seem >>> to work. >>> >>> summary(ANOVA1way <- by(welldata, Analyte, function(x) aov(Value ~ >>> WellID, >>> data = welldata))) >>> >>> In SAS I would code it like this: >>> Proc sort data=welldata; by analyte; run; >>> Proc glm data=welldata; >>> by analyte; >>> class wellid; >>> model value = wellid; >>> run; >> Look at the plyr package for a general solution to >> this type of problems: >> >> http://cran.r-project.org/web/packages/plyr/index.html >> >> and its introductory guide on the package home page: >> >> http://had.co.nz/plyr/ >> >> HTH, >> Tobias >> >> ______________________________________________ >> 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. >> >> >______________________________________________ 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.