There are several problems here:
predict()needs a predictor variable. You should thus pass it values for
Coupon, instead of
Total, which is the response variable in your model.
- The predictor variable needs to be passed in as a named column in a data frame, so that
predict()knows what the numbers its been handed represent. (The need for this becomes clear when you consider more complicated models, having more than one predictor variable).
- For this to work, your original call should pass
dfin through the
dataargument, rather than using it directly in your formula. (This way, the name of the column in
newdatawill be able to match the name on the RHS of the formula).
With those changes incorporated, this will work:
model <- lm(Total ~ Coupon, data=df) new <- data.frame(Coupon = df$Coupon) predict(model, newdata = new, interval="confidence")