As pointed out by @FJCC , You need to provide the predictors in your new data frame. Please make a follow up down here;
library("wooldridge")
data("wage2")
model1 <- lm(wage ~ age + educ + IQ, data = wage2)
predict(model1, data.frame(age = median(wage2[['age']]), IQ = median(wage2[['age']]), educ = 12))
#> 1
#> 528.5071
# Alternatively, you can pass your dataframe with 'edu == 12'
df_new <- wage2[wage2$educ == 12, ]
predict(model1, newdata = df_new )
# Slice the data frame at 'edu = 12' for further plotting
df_new2 <- df_new[c('hours', 'wage', 'age', 'educ', 'IQ')]
new_wage <- predict(model1, newdata = df_new )
df_plt <- data.frame(df_new2[c('hours', 'wage')], 'new_wage' = new_wage) # df for plotting
# Let us do the plot. I assume a Time series of wages vs hours
df_plt |> reshape::melt(id = 'hours') |>
ggplot2::ggplot(aes(x = as.character(hours), y = value, group = variable, color = variable)) +
geom_line(size = 1.0, alpha = 0.75) + geom_point(size = 2.2, alpha = 0.75) +
labs(title = 'Wages at edu = 12', x = 'Hours', y = 'Wages')