next up previous contents
Next: Calculating the variogram Up: First analyses Previous: Folding vectors into balanced

The saturated groups-by-times model

One of the main reasons for fitting an Ordinary Least Squares model to longitudinal data is to take the residuals of a groups-by-times fit and use these to estimate the variogram (see below). While this can be done using lm as in the example above, it is such a common procedure that the function olsres has been created specifically to do this task. The S command

olsres(obj)
performs an OLS fit on the data contained in the balanced object obj, and returns the residuals from the fit. The actual model fitted is (in GLIM notation) group * time, where where group is taken from the groups attribute of obj, and time is considered a factor. Thus one mean is fitted per time point in each group. The result of olsres is a balanced object of the same dimensions as obj containing the residuals from the fit (there is no need to use fold.balanced in this case), and is suitable for plotting and calculating its variogram.

If obj does not have a groups attribute, or you wish to use a different set of groups, you can provide the groups to be used as the second argument of olsres.

Example 5.2: groups attribute of the cows dataset actually represents the interaction of two separate treatment effects. The command olsres(cows) will calculate the OLS fit with respect to this ``combined'' groups attribute. If, however, we wish to calculate the fit with respect to just one of the treatments, all that is required is to create the appropriate grouping factor and supply that as the second argument of olsres
\begin{Example}
\gt cows.I <- as.factor(rep(c(''Control'',''Infection''),c(8,19)))
\gt cows.Ires <- olsres(cows, cows.I)\end{Example}
There is a companion function to olsres called olsfit which calculates the fitted values for the same groups-by-times model. This is useful for getting a picture of the behaviour for each group by averaging over the non-missing values for each individual within the group at each time point. For example, plot(olsfit(milk)) is a good way to get a quick picture of the behaviour of the groups in the milk data.


next up previous contents
Next: Calculating the variogram Up: First analyses Previous: Folding vectors into balanced
David Smith
4/2/1998