Hi:

One approach would be to use dlply() from the plyr package to generate the
models and assign the results to a list, something like the following:

library(plyr)
# function to run the GLM in each data subset - the argument is a generic
data subset d
gfun <- function(d) glm(Stems ~ Time, data = d, family = poisson)
mlist <- dlply(stems139, .(SizeClass, Species), gfun)

To see the result, try mlist[[1]] or summary(mlist[[1]]) to execute the
print and summary methods on the first fitted model. Each output list object
from glm() is a list component of mlist, so mlist is actually a list of
lists.

You can extract various pieces from mlist by using ldply() with a suitable
extraction function or by use of the do.call/lapply combination.

All of this is untested since no minimal example was provided per
instructions in the Posting Guide...

HTH,
Dennis


On Tue, Feb 8, 2011 at 11:54 AM, Christopher R. Dolanc <crdol...@ucdavis.edu
> wrote:

> I'm having a hard time figuring out how to group results by certain factors
> in R.  I have data with the following headings:
>
> [1] "Time"      "Plot"      "LatCat"    "Elevation" "ElevCat"   "Aspect"
>  "AspCat"    "Slope"
> [9] "SlopeCat"  "Species"   "SizeClass" "Stems"
>
> and I'm trying to use a GLM to test differences in "Stems" for different
> categories/factors - most importantly, I want to group things so that I see
> results by "SizeClass" and then by "Species".  This is pretty easy in SAS
> using the "Group By" command, but in R, I haven't figured it out.
>
> I've tried using the following code:
>
> > stems139GLM <- glm(Stems ~ Time | SizeClass | Species, family=poisson,
> data=stems139)
>
> but R gives me this message:
>
> Error in pmax(exp(eta), .Machine$double.eps) :
>  cannot mix 0-length vectors with others
> In addition: Warning messages:
> 1: In Ops.factor(Time, SizeClass) : | not meaningful for factors
> 2: In Ops.factor(Time | SizeClass, Species) : | not meaningful for factors
>
> I'd appreciate any help.
>
> Thanks.
>
> --
> Christopher R. Dolanc
> PhD Candidate
> Ecology Graduate Group
> University of California, Davis
> Lab Phone: (530) 752-2644 (Barbour lab)un
>
> ______________________________________________
> R-help@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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to