How to perform regressions taking the dependent variable fixed and changing only the dependent variable? That is, the data frame is something like
year a b c d..... z
1
2
3
4
I need the dependent variable to always be a
but the independent variable to be b
, then c
and so on and then give me the result in a list of the dependent variables that were significant, for example:
b
c
h
I tried to do the following
for (i in 1:40) {
modelo_prueba <- lm(data = ds, formula = a ~ ds[ ,i])
print(summary(modelo_prueba))
}
Define the formula like this: it
a ~ ds[ ,i]
is syntactically valid, but the problem is that in the context of evaluating the linear model, neither the objecti
nor the object existsds
.You can define the formula dynamically with each iteration as a simple string and then transform it into a real formula by
as_formula()
:In the example, we define what it is
var_dep
and then the independent variables, taking all the columns except the one of the dependent variable, in your case, you would have (I imagine) remove the columnyear
as well. We then iterate through the names of each independent variable, generate a string for the formula, and transform it into such.