Reusing Results

In SAS, you can save the results of statistical analyses using the Output Delivery System (ODS). While ODS is a vast improvement over PROC PRINTO, it's sophistication can make some features very hard to learn (just try mastering PROC TEMPLATE). In SPSS you can do the same thing with the Output Management System (OMS). Again, not one of the easiest topics to learn.

One of the most useful design features of R is that the output of analyses can easily be saved and used as input to additional analyses. # Example 1
lm(mpg~wt, data=mtcars)

This will run a simple linear regression of miles per gallon on car weight using the data frame mtcars. Results are sent to the screen. Nothing is saved.

# Example 2
fit <- lm(mpg~wt, data=mtcars)

This time, the same regression is performed but the results are saved under the name fit. No output is sent to the screen. However, you now can manipulate the results.

# Example 2 (continued...)

str(fit) # view the contents/structure of "fit"

The assignment has actually created a list called "fit" that contains a wide range of information (including the predicted values, residuals, coefficients, and more.

# Example 2 (continued again)
# plot residuals by fitted values
plot(fit$residuals, fit$fitted.values)

To see what a function returns, look at the value section of the online help for that function. Here we would look at help(lm).

The results can also be used by a wide range of other functions.

# Example 2 (one last time, I promise)

# produce diagnostic plots

# predict mpg from wt in a new set of data
predict(fit, mynewdata)

# get and save influence statistics
cook <- cooks.distance(fit)

To Practice

To practice reusing results in variables, try this interactive course on the introduction to R programming from DataCamp.