I would like to run many regression models automatically and test this and save fitted and residuals on the original file.
I mean that I would like to test all possible regression models over the data.
For example, X1=X2+X3...and X2=X1+X3...and X3=X1+X2...
And then add fitted values and residual values of each model.
I have a file like this.
test<-data.frame(X1=rnorm(50,mean=50,sd=10),
X2=rnorm(50,mean=5,sd=1.5),
X3=rnorm(50,mean=200,sd=25))
test$X1[10]<-5
test$X2[10]<-5
test$X3[10]<-530
I run all possible regression models.
varlist <- names(test)
models <- lapply(varlist, function(x) {
lm(substitute(i~., list(i = as.name(x))), data = data
})
I got fitted and residuals from each regression model.
lapply(models,residuals)
lapply(models, fitted)
However, I would like to save all residuals and fitted values on the original data. Is it possible to make the final data like this?
X1 X2 X3 Residual1 Residual2 Residual3 Fitted1 Fitted2 Fitted3
So that residual1
is from model1
, residual2
is from model2
, etc.