Search code examples
riterationlinear-regressionprediction

Iteration for Simple Linear Regression and Prediction in R


Using R, I would like to run n iterations for generating n simple linear regression models using a training dataset to predict n sets of fitted values for the test data. This would involve storing the models and predictions in appropriate containers. I would also like to label the predictions using a vector of labels. The data are structured as follows: X = c(1.1,2.3,3.4,4.5,5.8), Y = c(1.0,2.4,3.3,4.7,6.0) and the model would be like lm(Y.train~X.train) and the predictions would be like predict(lm, data = test_set). How would I set this up? Is there a better approach?

Thanks!


Solution

  • Set up a list or two to store the fitted model and the predictions. Then loop.

    In the code below, you'll need to fill in the ...'s since I don't know what your data look like or how you are deciding what will differ between each iteration.

    model_list   <- vector(mode="list", length=N)
    predict_list <- vector(mode="list", length=N)
    
    for (i in 1:N) {
        # fit model
        model_list[[i]] <- lm(y ~ x, data=subset(...))
    
        # store predictions
        predict_list[[i]] <- predict(model_list[[i]], newdata=subset(...))
    }