How regression model loop

Hello, community!
A linear model, and one independent variable may have many different groups data(different measure methods), and put one group data at one time? How could i loop the regression model(like GLM or others)? iris data could be example data.
Any help would be appreciated.

Need a formal reproducible example here to work with but sounds like the map and walk families of functions from purrr could help here (i.e.purrr::imap_dfr(...))

1 Like

Here is an example for linear model using the iris dataset you suggested. I used a for loop:

Load packages

library(tidyverse)
library(agricolae)

iris data

head(iris)

Data Wrangling

iris <- iris %>%
relocate(Species,.before = Sepal.Length)

Fit the model with a for loop

for (i in 2:ncol(iris)) {
print(paste('---',names(iris[i]),'---')) # This adds the traits names

model <- lm(iris[[i]]~.,data=iris) # This fit the linear model

print(anova(model)) # This print the anova table

}

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.