title: Error in eval(expr, envir, enclos) : object 'mpg' not found
body: <p>I am trying to calculate groupwise regression coefficients for two variable by using data.table package.</p>
  
  <p>Here I have posted my sample code with dummy data.</p>
  
  <pre><code>#Model Dependent varaible
  reg_dep_vars&lt;-"mpg"
  #Model independent variable
  reg_ind_vars&lt;-c("cyl","drat")
  
  reg_data&lt;-as.data.table(mtcars)
  
  #creating a formula with depedent and independent variables which going to be used in the model.
  reg_formula&lt;-as.formula(paste(paste("reg_data$",reg_dep_vars,sep=""),"~",paste(paste("reg_data$",reg_ind_vars,sep=""),collapse="+")))
  
  OUT&lt;-reg_data[,.(intercept=coef(lm(reg_formula))[1],cyl=coef(lm(reg_formula))[2],drat=coef(lm(reg_formula))[3],P=glance(lm(reg_formula))$p.value,F=summary(lm(reg_formula))$fstatistic[1]),by=.(am,gear)]
  </code></pre>
  
  <p>In the above code, I am trying to find out estimates for cyl and drat variable and the by group is am and gear.</p>
  
  <p>If I use the above code, I am getting the following error.
  "Error in eval(expr, envir, enclos) : object 'mpg' not found"</p>
  
  <p>Can anyone help me on this? </p>