how to use loop to do linear regression in R
问题 I wonder if I can use such as for loop or apply function to do the linear regression in R. I have a data frame containing variables such as crim, rm, ad, wd. I want to do simple linear regression of crim on each of other variable. Thank you! 回答1: If you really want to do this, it's pretty trivial with lapply() , where we use it to "loop" over the other columns of df . A custom function takes each variable in turn as x and fits a model for that covariate. df <- data.frame(crim = rnorm(20), rm