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
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 olsresThere 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.
![]()