R: numeric 'envir' arg not of length one in predict()

后端 未结 1 460
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 18:21

I\'m trying to predict a value in R using the predict() function, by passing along variables into the model.

I am getting the following e

1条回答
  •  暖寄归人
    2020-12-08 18:49

    There are several problems here:

    1. The newdata argument of predict() needs a predictor variable. You should thus pass it values for Coupon, instead of Total, which is the response variable in your model.

    2. 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).

    3. For this to work, your original call should pass df in through the data argument, rather than using it directly in your formula. (This way, the name of the column in newdata 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")
    

    0 讨论(0)
提交回复
热议问题