Hi Posit Community,
I am interested in using a linear mixed model, from the {lme4
} package. I would like to “manually” predict prices, extracting random effects, and the whole procedure that follows to predict the logarithm of price (\ln\,(\hat{p})) for all observations.
- However, for cars that don't have ex-ante prices, I can't estimate their random effects and I don't know how to do this on R. Does anyone have a solution to this, please?
My idea being, for example, to take the mean of the random effects for those observations that lack price, but again, I lack experience on R (I'm an avid stata user, but I want to make the change gradually on R).
Here's an example of my data:
X0_Car_Demand_Prices_Merge |>
filter(!is.na(ln_p)) |>
select(model, brand, brand_factor, brand_model_factor, fuel, engine_cap, power_kw, ev_type_fe, ln_p, price) |>
head(5)
# A tibble: 5 × 10
model brand brand_factor brand_model_factor fuel engine_cap power_kw ev_type_fe ln_p price
<chr> <chr> <fct> <fct> <chr> <dbl> <dbl> <fct> <dbl> <dbl>
1 4C ALFA ROMEO ALFA ROMEO ALFA ROMEO_4C 0 1742 177 6 11.0 62000
2 4C ALFA ROMEO ALFA ROMEO ALFA ROMEO_4C 0 1742 177 6 11.0 62000
3 4C ALFA ROMEO ALFA ROMEO ALFA ROMEO_4C 0 1742 177 6 11.0 62000
4 4C ALFA ROMEO ALFA ROMEO ALFA ROMEO_4C 0 1742 177 6 11.0 62000
5 GIULIA ALFA ROMEO ALFA ROMEO ALFA ROMEO_GIULIA 1 2143 100 6 10.2 27200
Here is what I used, but I would like to have predicted price for all observations, even if for the observations with missing prices the prices could not be exactly what they should, using a mean of the random effects. Then I tried to extract "manually" the random effects, because I wanted to compute the \ln\,(\hat{p}) for all observations. When I used the code below, some observations do not get a price \hat{p} and the subsequent \ln\,(\hat{p})
model_re <- lmer(ln_p ~ engine_cap + power_kw + ev_type_fe + (1 | brand_factor) + (1 | brand_model_factor),
data = X0_Car_Demand_Prices_Merge, REML = FALSE, na.action = na.exclude)
# predict
predict_with_re <- predict(model_re)
# Extranef_est_df# Extract random effects from lme4 fit
ranef_lme4 <- ranef(model_re)
Could someone please help me?
Many thanks in advance!
Have a nice day.
Edit: I could provide a sample dataset also with -dput()
- if needed.