I was recently asked by a coworker if there was an easy way to run multiple linear regression models on columns in a dataset. Basically, there were 4 predictor variables and then 199 columns of response variables, with each column being output from a sensor.
This is what I came up with (shown here with only 3 sensor response columns)
# Load packages
library(readr)
dataset <- read_csv("model-data.csv") # read in data
sensors <- names(dataset[5:7]) # Get list of sensor column names
fit.list <- list() # Initialize the fit output list.
for(sensor in 1:length(sensors)) {
y <- sensors[sensor]
lm.formula <- as.formula(paste(y, "F1+F2+F3+F4", sep="~"))
fit <- lm(lm.formula, data=dataset)
fit.list[[y]] <- fit
}
fit.list
## $S1
##
## Call:
## lm(formula = lm.formula, data = dataset)
##
## Coefficients:
## (Intercept) F1 F2 F3 F4
## 2033.5685 -3.2984 2.3454 0.6821 -1.4307
##
##
## $S2
##
## Call:
## lm(formula = lm.formula, data = dataset)
##
## Coefficients:
## (Intercept) F1 F2 F3 F4
## 2024.733 4.331 -1.898 2.837 -1.902
##
##
## $S3
##
## Call:
## lm(formula = lm.formula, data = dataset)
##
## Coefficients:
## (Intercept) F1 F2 F3 F4
## 2055.860 2.403 -2.897 -4.088 -1.809
If you have any ideas on how this could be improved, please let me know.