IntroStats: 3d coffee example.R

From MathWiki

##
##  An R script to show the difference between simple regression and multiple regression
##  an example of Simpson's Paradox
##
##  If you haven't already, you need to install the packages 'spida' and 'p3d' from r-forge

install.packages('spida', repos = "http://r-forge.r-project.org")
install.packages('p3d', repos = "http://r-forge.r-project.org")

library(spida)
library(p3d)

data(coffee)
?coffee

summary( fit.simple <- lm( Heart ~ Coffee, coffee ))
summary( fit.mult   <- lm( Heart ~ Coffee + Stress, coffee))

Plot3d( Heart ~ Stress + Coffee, coffee)
Axes3d()

Id3d( coffee$Occ )  # identify by right clicking a rectangle around a point, exit by right-clicking empty space

Fit3d( fit.simple )
Fit3d( fit.mult, col = 'red')