I think what Greg say here is the "official wisdom" and it leads to more
future-proof code as the structure of various objects can change in new
versions of R.
OTOH the structure of a kind of object can always be explored with str()
but it may not be easy to find out what extractor functions are
available for the object.
Murray
On 25/09/2010 8:16 a.m., Greg Snow wrote:
I think that the reference to not using $ or [[ was meant for cases where there
is a proper extraction function, residuals being the example used.
If "fit" is an lm object then I could plot the fitted vs. residuals plot like
this:
plot( fit$fitted, fit$residuals )
Or like this:
plot( fitted(fit), resid(fit) )
The second one is preferred as it properly extracts the information without
needing to know the exact contents of fit (and the axis limits look a little
nicer).
With an lm object the 2 plots will be essentially the same, but what if fit is
a glm object? Then fit$residuals does give something that fits the definition
of residuals, but of the different types of residuals available for glm fits,
this gives the one that is least interesting/interpretable to humans (they were
useful to the program for getting the fit). Here the resid (or residuals)
function defaults to a more meaningful set of residuals and gives options for
other types.
If we have arbitrary objects without extractor functions then we need to use $
or [[ to extract the individual elements, but when working with fitted objects
it is much better to teach students to use the proper extractor functions
rather than directly working with elements of the object itself.
--
Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html
Department of Statistics, University of Waikato, Hamilton, New Zealand
Email: [email protected] [email protected] Fax 7 838 4155
Phone +64 7 838 4773 wk Home +64 7 825 0441 Mobile 021 0200 8350
_______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-teaching