Reputation: 163
I'm trying to predict the number of vehicles that have a V engine (vs. straight engine) given one specified x-variable wt=2100 (no restiction on disp). So I build the logit model as follow:
model <- glm(formula= vs ~ wt + disp, data=mtcars, family=binomial)
But I encounter a problem in using the prediction function. I tried to create a data frame
newdata = data.frame(wt = 2.1)
and then use the predict() function to calculate the predicted probability.
predict(model, newdata, type="response")
but I encounter an error: Error in eval(predvars, data, env) : object 'disp' not found. I know I didn't specify a disp value in the newdata, but my question, what if I only have a specified wt, but don't have a specified disp in this case?
Upvotes: 0
Views: 45
Reputation: 4414
The prediction function simply applies the formula for generating predictions, which in this case is
$$\hat{y} = \frac{1}{1+\exp(-\hat\beta_0 - \hat\beta_1 \text{wt} - \hat\beta_2 \text{disp})}$$
Without a provided value for disp
, how can the formula be applied? What should go in place of $disp$? It can't be 0; disp
may not even be able to take on a value of 0. If you build a model with a set of predictors, you need to supply all those predictors to get a predicted value. If you don't have disp
available, you can either fit a model without it and supply your desired value of wt
alone, or you can use some method to choose one or values of disp
to supply to the formula.
Upvotes: 0