I have a dataset that I'll call dataset1 with a predictor variable (e.g. Price). I'm hoping to get a nice single predictor of price based on the n other predictors that exist in the dataset. But if n is large, I can't manually make and examine all these models, so I was hoping to use something like this:
for (i in names(dataset1)) {
model = lm(Price~i, dataset1)
# Do stuff here with model, such as analyze R^2 values.
}
(I thought this would work since replacing the inside of the for loop with print(i) results in the correct names.) The error is as follows:
Error in model.frame.default(formula = Price ~ i, data = dataset1, drop.unused.levels = TRUE) :
variable lengths differ (found for 'i')
Does anyone have advice for dealing with the problem regarding how R reads in the i variable? I know how to approach this problem using other software, but I would like to get a sense of how R works.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…