Statistics 200: Lab 9

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. 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

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 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)

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

Try these commands also:

> mode(fit1)        # Check the help file for mode
> class(fit1)       # Check the help file for class
> attributes(fit1)  # Check the help file for attributes

> 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

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 M so that you get the same plot as for DF? Can you get this same plot without changing the class of M? Is there a class "matrix"? Have you any ideas what it is used for? (Hint look at the built in S-plus library contents)

Problem 3 (Hand in the S-plus code and a sample output for this question)

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.

Problem 4 (If you have time! Please had in the code and a sample output for this question)

Some miscellaneous things to keep you occupied.

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? Create your own class of objects (Alternatively, create a new class type, for a type of S-plus object that does not have a class). Write suitable functions for dealing with the class of objects (in particular print, summary and plot). Make your function robust to receiving the wrong type of object.