789372u 789372u - 1 month ago 8
R Question

Error in eval(expr, envir, enclos) : object 'mpg' not found

I am trying to calculate groupwise regression coefficients for two variable by using data.table package.

Here I have posted my sample code with dummy data.

#Model Dependent varaible
#Model independent variable


#creating a formula with depedent and independent variables which going to be used in the model.


In the above code, I am trying to find out estimates for cyl and drat variable and the by group is am and gear.

If I use the above code, I am getting the following error.
"Error in eval(expr, envir, enclos) : object 'mpg' not found"

Can anyone help me on this?

Answer Source



mtcars_model <- function(df) {
  lm.fit(y = df[[reg_dep_vars]], x = as.matrix(df[reg_ind_vars]))

test <- mtcars %>% 
  group_by(am, gear) %>%
  nest() %>%
  mutate(model = map(data, mtcars_model))

Learn more at http://r4ds.had.co.nz/many-models.html.

PS: sorry, I can't stand formulas.