1 / 17

R Lecture 6

R Lecture 6. Naomi Altman Department of Statistics (Based on S Poetry http://www.burns-stat.com/pages/spoetry.html). Generic Functions. Functions that act on many different types of objects are termed "generic functions". Examples include: plot print

caesar
Télécharger la présentation

R Lecture 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. R Lecture 6 Naomi Altman Department of Statistics (Based on S Poetry http://www.burns-stat.com/pages/spoetry.html)

  2. Generic Functions Functions that act on many different types of objects are termed "generic functions". Examples include: plot print summary coefficients anova residuals

  3. Generic Functions We have already seen that generic functions behave differently for different classes. The idea is that the user should not have to remember a lot of different function names. Generic functions are a "good thing" when you want R to do what someone else thinks it should do and can be a "bad thing" when you are trying to do something else with your data.

  4. Generic Functions The form of the generic function "genfun" is genfun=function (object, ...) { UseMethod("genfun") }

  5. Generic Functions We can use UseMethod to give aliases to the same function. genfun=function (object, ...){ UseMethod("genfun")} gen=function (object, ...){ UseMethod("genfun")} gfun=function (object, ...){ UseMethod("genfun")}

  6. Generic Functions If you want an argument other than the first to be the one whose class controls the generic function, then the name of the argument must be sent to UseMethod genfun=function(x,y,z,...){ UseMethod("genfun",z) }

  7. Generic Functions If UseMethod finds that the calling object inherits from a class, it searches for a function "genfun.class". If there is no function that matches the class, it looks through the inheritance list. If there is no match, or no class, the function "genfun.default" is used.

  8. Generic Functions There is a lot more on this in the "S Poetry" manual - it looks very complete to me. I have been writing programs in S/R since 1981, and have not needed to create classes or methods but ...

  9. Generic Functions I have often used an existing function to create new functions - I have been confused by failing to understand generic functions (especially "summary" and "print"). One way to become well-known is to distribute your methodology as an R package. To be distributed from CRAN or other project repositories, your package must adhere to R programming standards.

  10. Generic Functions Some of the newer packages (particularly packages for bioinformatics) rely heavily on the use of Generic Functions, and you can never understand what they are doing without understanding at least the basics of this material.

  11. Slots I was not able to find an intuitive definition for "slot" so this is my own heuristic. An object is a list with a class. A slot is a function that extracts data from an object. It may be one of the elements stored in the object, or a derived data element.

  12. Slots For example: an lm object includes the list: "coefficients" "residuals" "effects" "rank" "fitted.values" "assign" "qr" "df.residual" "xlevels" "call" "terms" "model" We might build a new class, "Elm" (extended "lm")

  13. Slots Suppose we wanted to write a method that draws a histogram of any of dependent variable, residuals, studentized residuals, fitted values. We could have a method of the form: hist.Elm=function(object,slot) Our slots would be: dependent, residuals, student, fitted

  14. Slots If we set class(lm.out)=c("Elm","lm") then hist(lm.out,residual) would extract the residuals from the list and draw the histogram. hist(lm.out,student) would compute the studentized residuals (which are not stored) and draw the histogram.

  15. Slots By convention, the slots of an object can be extracted either by: objectname@slotname or slotname(objectname)

  16. Slots Again, I have used S/R for many years without writing or even encountering slots. But some of the recent packages use this programming concept, so it is important to understand it. My understanding is that slots are used primarily in areas like data-mining and microarrays, where the data storage requirements are large.

  17. Garbage Collection R does not always release memory after loading large data objects. Releasing memory not currently in use is called garbage collection. gc() will "do the trick". So will quitting and restarting R.

More Related