Multiple testing in a loop for linear regression

Now I want to have a multiple testing for linear regression using a loop (select from column 80 to column 160 in mydata), when trying the code as below, it gets errors: invalid variable list for mydata[,i]. I'm a beginner for R, if someone knows how to fix this, please let me know, thanks in advance! This is an example of my test:
for (i in 80:160) {
mlrm <- lm(y~AGE+SEX+mydata[,i], data=mydata)
summary(mlrm)
}

This is working for me.

library(tidyverse)

# sample data
dat <- diamonds
# we want to use the last three columns which are 8 through 10
my_cols <- c(8:10)
# setup a list that will contain our results
results <- vector(mode = "list", length(my_cols))

for (i in seq_along(my_cols)) {
 # fill the results list one at a time
 results[[i]] <- lm(price ~ carat + dat[[my_cols[i]]], data = dat)
}

results

Thank you very much! It works for me, thanks!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.