Error in eval(predvars, data, env) : object ‘**‘ not found

created at 10-05-2021 views: 110

error

problem

The names of the variables in the new data are different from the names of the variables in the training model

#create data
df <- data.frame(x1=c(3, 4, 4, 5, 5, 6, 7, 8, 11, 12),
                 x2=c(6, 6, 7, 7, 8, 9, 11, 13, 14, 14),
                 y=c(22, 24, 24, 25, 25, 27, 29, 31, 32, 36))

#fit multiple linear regression model
model <- lm(y ~ x1 + x2, data=df)


#define new observation
new <- data.frame(x_1=c(5),
                  x_2=c(10))

#use the fitted model to predict the value for the new observation
predict(model, newdata = new)

solution

#create data
df <- data.frame(x1=c(3, 4, 4, 5, 5, 6, 7, 8, 11, 12),
                 x2=c(6, 6, 7, 7, 8, 9, 11, 13, 14, 14),
                 y=c(22, 24, 24, 25, 25, 27, 29, 31, 32, 36))

#fit multiple linear regression model
model <- lm(y ~ x1 + x2, data=df)


#define new observation
new <- data.frame(x1=c(5),
                  x2=c(10))

#use the fitted model to predict the value for the new observation
predict(model, newdata = new)
created at:10-05-2021
edited at: 10-05-2021: