Statistics 3N03 - Assignment #1 Hints

2002-09-30


Time Series Plots by Month

The simplest way to plot the time series for each month separately is to select the months one at a time and do a simple index plot.

> plot(chmp$sales[chmp$month=="JAN"], type ="l")

Putting the years on the x-axis and adding axis labels will improve the graph.

> plot(1962:1969,  chmp$sales[chmp$month=="JAN"],  type="l",  xlab="Year",  ylab="Sales")

You can use matplot() to put all the months on one graph:

> matplot(1962:1969, data.frame(split(chmp$sales,chmp$month)[chmp$month[1:12]]), xlab="Year", ylab="Sales")

To see how this works, build it up a bit at a time.

> split(chmp$sales,chmp$month)

> chmp$month[1:12]

> split(chmp$sales,chmp$month)[chmp$month[1:12]]

> data.frame(split(chmp$sales,chmp$month)[chmp$month[1:12]])

Interaction Plot

Here is a function I wrote to draw an interaction plot, given a column y of y-values and two columns facta, factb of factors. Note the finishing touches: if any of xlab or ylab or main title are not provided, the function uses deparse(substitute()) to pull out the names of the variables. It also adds a legend to show which line belongs to which level of the factor.

The function uses sapply() after splitting by facta:factb to compute the mean y at each combination of factor A and factor B, then matrix() to arrange the means into the columns of a matrix ready for matplot() to plot.

The ... in the function definition allows the user to pass additional arguments to any of the functions called. Its main use would be to send additional arguments to plot(), to give the user more control over the appearance of the plot.

> interactplot
function (y, facta, factb, xlab = deparse(substitute(factb)), 
    ylab = deparse(substitute(y)), main = paste("Interaction plot by", 
        deparse(substitute(facta))), ...) 
{
    values <- sapply(split(y, facta:factb), mean)
    matplot(matrix(values, ncol = nlevels(facta)), type = "l", 
        xlab = xlab, ylab = ylab, ...)
    title(main = main)
    legend(1, max(values), levels(facta), lty = 1:nlevels(facta), 
        col = 1:nlevels(facta))
    invisible()
}

Statistics 3N03