Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

R lineal model function (lm) doesn't exclude predicted variable from predictors

$
0
0

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?


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>