789372u - 1 year ago 83

R Question

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`

reg_dep_vars<-"mpg"

#Model independent variable

reg_ind_vars<-c("cyl","drat")

reg_data<-as.data.table(mtcars)

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

reg_formula<-as.formula(paste(paste("reg_data$",reg_dep_vars,sep=""),"~",paste(paste("reg_data$",reg_ind_vars,sep=""),collapse="+")))

OUT<-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)]

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?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Use

```
library(tidyverse)
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.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**