R Code Error - Object Not Found

I am utilizing R to create a linear regression to predict population estimates increase. I created this code, however, I am receiving this error for the prediction. Does anyone have any insight on this? Thanks!

Estimates = read_excel("/Users/emily/Desktop/State_Population_Data_Clean.xlsx")

ggpairs(data=Estimates, columns=1:2, title = "Colorado Estimate")
fit_1 <- lm(PopEstYear ~ PopEstAmount, data = Estimates)
ggplot(data = Estimates, aes(x = PopEstYear, y = PopEstAmount)) +

  • geom_point() +
  • stat_smooth(method = "lm", formula = y~x, col = "dodgerblue3") +
  • theme(panel.background = element_rect(fill = "white"),
  • axis.line.x=element_line(),
  • axis.line.y=element_line()) +
  • ggtitle("Linear Model Fitted to Estimates")

predict(fit_1, data.frame(PopEstYear = 2021))
Error in eval(predvars, data, env) : object 'PopEstAmount' not found

Did you check your dataframe Estimates has the columns PopEstYear and PopEstAmount ? The names are case sensitive.

Also, your model fit_1 estimates PopEstYear from PopEstAmount. So when you do predict(fit_1, data.frame(PopEstYear = 2021)) you're providing the wrong variable.

Switching the variable values for fit_1 appears to have resolved the estimates. Thank you so much!!! :slight_smile:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.