R: numeric 'envir' arg not of length one in predict()
There are several problems here:
The
newdata
argument ofpredict()
needs a predictor variable. You should thus pass it values forCoupon
, instead ofTotal
, 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
df
in through thedata
argument, rather than using it directly in your formula. (This way, the name of the column innewdata
will 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")