Re: [R] Nested and repeated effects together?

From: Spencer Graves <>
Date: Wed 07 Jun 2006 - 11:01:05 EST

  1. Am I correct that you are asking for a 4-factor interaction without a full set of 2- and 3-factor interactions? SAS may let you do that, but I'm not certain what that means. The standard treatment of a factor with k levels is to code it as (k-1) linearly independent "contrasts" that are also linearly independent of the constant term and then use ordinary least squares. As long as standard rules of hierarchy are observed, the resulting analyses of variance are independent of the specific set of contrasts chosen. However, when rules of hierarchy are broken, the answers may change with different choices for contrasts, and it's far from obvious what one is even testing.
  2. For these kinds of problems, I usually use "lme" in the "nlme" package. The function 'aov' is older, and its answers may not be as good if the design is not balanced. I've seen only one case where 'aov' produced an answer that I couldn't get out of "lme", and that was a saturated model in a perfectly balanced experiment where the noise was estimated from higher order interactions. For your case, I would consider variations on the following:

          lme(y~(Gr+Hemi+Region+Gender)^4, random=~1|ID)

          This assumes that each level of ID is unique. If not, I suggest you make it unique by pasting it together with Region and Gender.

          The construct (Gr+Hemi+Region+Gender)^4 indicates all main effects and interactions up to the fourth order. If you want, say, all main effects plus the three factor interaction Gr*Hemi*Region, I believe you could get that from "Gr*Hemi*Region+Gender": the term 'Gr*Hemi*Region' will force all the main effects an subordinate two-factor interactions into the model.

          If you don't already have Pinheiro and Bates (2000) Mixed-Effects Models in S and S-Plus (Springer), I suggest you get a copy. Bates is one of the leading contributors in nonlinear estimation and mixed models. The book contains numerous examples. Also, R script files are available for virtually everything discussed in the book. This will allow you to work the examples yourself one line at a time as you read the accompanying discussion in the book. To access them, find where R is installed on your hard drive, then find "~\library\nlme\scripts".

	  Hope this helps,
	  Spencer Graves

Stephan Kolassa wrote:
> Dear R people,
> I am having a problem with modeling the following SAS code in R:
> Class ID Gr Hemi Region Gender
> Model Y = Gr Region Hemi Gender Gr*Hemi Gr*Region Hemi*Region
> Gender*Region Gender*Hemi Gr*Hemi*Region Gender*Hemi*Region
> Gr*Gender*Hemi*Region
> Random Intercept Region Hemi /Subject = ID (Gr Gender)
> I.e., ID is a random effect nested in Gr and Gender, leading to
> ID-specific Intercept, Region and Hemi means. We have repeated
> measurements within each ID (one measurement each for the different
> combinations of Hemi and Region, i.e. 2 levels in Hemi: L vs. R; 4
> levels in Region: F vs. C vs. T vs. PO).
> I have been trying things like
> aov(y~Gr+Region+Hemi+Gender+
Gr:Hemi+Gr:Region+Hemi:Region+Gender:Region+Gender:Hemi+ Gr:Hemi:Region+Gender:Hemi:Region+Gr:Gender:Hemi:Region+
> Error(ID/(Gr+Gender))
> and do get results, but I am very unsure whether this implements the
> right model. An Error term like
> Error((1+Region+Hemi)/(ID_Vp/(Gender+Gr))), data=daten))
> in the above model looks intuitively better to me, but (1) again: I'm
> unsure about this, (2) this crashes my R.
> Of course, I have been googling for all permutations of "Nested
> effects", "repeated effects", "random effects" and digging through the
> R-help archives, but I can't seem to locate a similar question having
> been answered before.
> Thank you all for your time!
> Best regards,
> Stephan
> ______________________________________________
> mailing list
> PLEASE do read the posting guide! mailing list PLEASE do read the posting guide! Received on Wed Jun 07 11:05:14 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 07 Jun 2006 - 12:10:36 EST.

Mailing list information is available at Please read the posting guide before posting to the list.