Here's the Multiple regression model I'm running on the Boston data set. I would like to predict the medv for the case when


Within the predict(final.reg1, newdata) statement, what values should I assign to the other predictors in newdata=data.frame()?

I initially tried running:



But I get the following error: Error in eval(predvars, data, env) : object 'zn' not found


1 Answer 1


Your regression formula is medv ~ ., or medv regressed on all other variables. This means that medv is estimated as the sum of all other variables' effects times their values.

Let's say you have two variables:

$\hat{y} = \hat{\beta}_0 + \hat{\beta}_1 \cdot x_1 + \hat{\beta}_2 \cdot x_2$,

and you only know the value of $x_1$.

Of course you could express $\hat{y}$ in terms of $(\text{some number}) + \hat{\beta}_2 \cdot x_2$, but you can only answer a number by plugging in a value for $x_2$.

One thing you could do is give multiple answers, spanning the ranges of the remaining predictors. But you have only data for 5 variables, and there are 13 in your model, so the number of possible values for $\hat{y}$ will be large.

Maybe try running a second model that only uses the variables for which you have data. Then you can predict medv for your new observation using that model.

  • $\begingroup$ Thank you! I was going through the predict() definition and came across the argument descriptions for newdata as " An optional data frame in which to look for variables with which to predict. If omitted, the fitted values are used." and na.action as "function determining what should be done with missing values in newdata. The default is to predict NA." Could these two be used to only set values for a subset of the predictors? $\endgroup$
    – user209396
    Apr 23, 2019 at 7:08
  • $\begingroup$ You're welcome! What that means is that if you don't supply new data to the function, it will return the estimated values of $y$ for the data you used to train the model. $\endgroup$ Apr 23, 2019 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.