source("http://www.uvm.edu/~rsingle/stat221/data/scripts-221.R") #The data give the speed of cars and the distances taken to stop. data(cars) plot(dist~speed, data=cars) #scatterplot mod <- lm(dist~speed, data=cars) #create a model using lm() mod mod$fitted #fitted or predicted values for the model predict(mod) #fitted values via the predict() function #predictions are based on the observed x-values #prediction & interval at a new x-values x.new.df <- data.frame(speed=12.5) #NOTE: the var.name must match (speed) x.new.df predict(mod, x.new.df, interval="confidence") predict(mod, x.new.df, interval="prediction") #prediction & interval at a new x-values x.new.df <- data.frame(x = seq(-3, 3, 0.5)) x.new.df predict(mod, x.new.df, interval = "confidence") predict(mod, x.new.df, interval = "prediction") #create a scatterplot with 95% (default) confidence or prediction bands conf.bands(cars$speed,cars$dist) #the syntax follows the plot() function: (X.var, Y.var) pred.bands(cars$speed,cars$dist) #customize the confidence band plot conf.bands(cars$speed,cars$dist) conf.bands(cars$speed,cars$dist, alpha=.01, xlab="speed", ylab="dist") title("99% Prediction Bands") #NOTE: although it is possible to fit a model using lm() as below #This syntax can be problematic when trying to use other functions such as predict() mod2 <- lm(cars$dist~cars$speed) mod2 #NOTE: the name of the variable for the slope coefficient (cars$speed) x.new.df <- data.frame(speed=12.5) x.new.df #NOTE: the var.name does not match above (it is 'speed' here, not cars$speed) predict(mod2, x.new.df, interval="confidence") #gives an error #It is not easy to name the variable on the new data.frame cars$speed to match (since $ is a reserved symbol)