[Return to tutorial page]

Statistics 200: Lab 9 (Friday 27 March 1998)

Today's tasks:
Classes and methods.

Today's class will attempt to uncover some of the basics of how S-plus recognizes the type of objects that it is dealing with. To illustrate how S-plus does this, we shall concentrate primarily with the print, summary and plot functions.

You may have noticed that the plot, summary and print functions can deal with many different classes of objects and give appropriate output for the object class.

Problem 1 (How Splus recognises objects)

Generate a vector x  with 100 iid observations from a N(m,s2), you can choose the values for m and s2 yourself. (?rnorm).
Using these stored x values, generate y values, such that y = a + bx + e, where e ~ N(0, t2). (You are free to choose your own values of a, b, and t2).
Use the lm command to fit the model y = a + bx + e, store the output in an S-plus object called fit1.

Explain the output from the commands:

> fit1
> print(fit1)
> summary(fit1)
> plot(fit1)

> x
> print(x)
> summary(x)
> plot(x)

Think of what you don't like about the output from the plot, summary and print commands? How could they be improved? Keep a note of your comments, you will need to implement these improvements later.

Try these commands also:

> mode(fit1)
> class(fit1)
> attributes(fit1)

You should probably check the help files for the mode, class and attributes commands also.

> mode(x)
> class(x)
> attributes(x)

What do these functions tell you about the objects? What characteristics of these objects enables the functions print, summary and plot to recognise the type of object that it is dealing with?

Look up the help files for print, summary, and plot for some ideas. It might not be a bad idea to look at all the S-plus functions that start with the words print, summary and plot. Where does S-plus store its basic functions? (?search, ?ls and ?objects)

Problem 2 (Changing the class of an object)

You can now try and really confuse S-plus, and maybe yourself as well!

First, create a (20 x 2) dimensional matrix M whose elements are from a Binomial(5,0.62) distribution (?rbinom). Make a data frame DF that has the same terms as M (?as.data.frame). Plot both of these objects. Did you get the same result? Check the class of M, did you get anything surprising? How did the plot function recognize that M was a matrix? It might be worth looking at the help for plot.matrix. Can you change the class of DF so that you get the same plot as for M? Can you get this same plot without changing the class of DF? Is there a class "matrix"?

Have you any ideas what the "Matrix" is used for? (Hint look at the built in S-plus library contents)

Problem 3 (Changing how Splus deals classes of objects)

Please do (a) and/or (b).
By now you should have worked out how the print, summary and plot commands recognize the type of object that it is dealing with. You should also have found some improvements that you would like to make to these functions. Choose an object class ("lm", "aov", "princomp", "data.frame", "list" for example) and improve the operation of the print, summary and plot commands for this class of variables.
Alternatively, create your own new class of objects. Write a set of print, summary and plot commands for these objects. Make your function robust to receiving the wrong type of object.

Problem 4 (Multiple class types)

Can an object have more than one class? (Hint: Create data for an analysis of variance problem, and do the analysis of variance.) What is the effect of having more than one class?