How do I calculate the relative importance using relaimpo package in R when I want to run it for several groups? As an example, in the mtcars dataframe I want to calculate the relative importance of several variables on mpg for every cyl. I calculated the relative importance of the variables on mpg, but I don't know how to make it per group. I tried to insert group_by(cyl) but I did not succeed. How would I do that in R?
library(relaimpo)
df <- mtcars
model <- lm(mpg ~ disp + hp + drat + wt, data=df)
rel_importance = calc.relimp(model, type = "lmg", rela=TRUE)
rel_importance