I have the following DataFrame
I run the regressions using ir_total as the dependent variable and 3-way combinations of municipalities as regressor variables.
combinaciones <- apply(combn(independientes, m=3), 2,
FUN=function(x){paste0("`", x, "`", collapse = ' + ')})
combinaciones
combinaciones <- sapply(paste("ir_total ~", combinaciones), as.formula)
modelos<-lapply(combinaciones, lm, df)
lapply(modelos, FUN=function(x) {cbind(r2=summary(x)$r.squared,
coef1=coef(x)[2],
coef2=coef(x)[3],
coef3=coef(x)[4])})->datos
datos <- do.call(rbind, datos)
rownames(datos) <- NULL
datos
The result I get is:
But I hope to get a column with the combinations of each regression and be able to identify them since I need to manipulate them datos
by applying the sort() function.
It is possible to extract the independent terms from a regression model obtained with
lm
the following way:atributes(modelo$terms)$term.labels
In this case, we can also obtain them using the function
coef()
as follows, omitting the first coefficient that corresponds to the intercept:names(coef(x))[-1]
In this way, we can make the following modification to obtain the name of all the independent variables in the same column.
Since in this case we know that we will always have three independent variables, we can get the name of each term in its own column.