I have a dataframe where I want to predict all variables from the other variables, so I construct a loop like this one:
df = iris
df$Species <- NULL
mods = list()
for (i in 1:ncol(df)) {
mods[[i]] <- lm(df[, i] ~ ., df)
}
But, to my surprise, each variable appears as it's own predictor; even if I do:
mods = list()
for (i in 1:ncol(df)) {
mods[[i]] = lm(df[, i] ~ . - df[, i], df)
}
The same happens.
I know I can create the correct formula expression on the side with the proper names and so on, but I feel like this shouldn't be the desired behaviour for lm.
The question is: Am I missing something? Is there a reason why this function has such an uncomfortable behaviour? In case the answer to the previous questions is "no", shouldn't it be improved?