df <- data.frame(a=rnorm(20, 0,1), b=rnorm(20,1,2), c=rnorm(20, 2, 1), d=rnorm(20, 1,2))
a b c d
1 -0.114975143 2.8923335 2.9968141 1.68054858
2 -0.354557896 -2.3895332 3.7244438 1.99219906
3 0.271021912 -1.7725649 1.3278507 2.54157377
4 -1.981331306 2.7159506 3.4150731 3.57905491
5 0.003403129 -2.4912678 0.7665065 0.78206113
6 -0.843816087 0.4654837 2.2260497 5.16645779
7 0.091269485 2.6912003 2.1541783 -0.25013719
8 -0.221281275 1.6365565 -0.3745516 0.48183139
9 0.041891592 -1.1115767 1.2828104 0.07650962
10 1.182289018 -1.3741108 0.5180835 2.28457132
11 -0.498770473 1.3988605 1.3275124 -2.86340984
12 -0.277915549 -2.1960097 3.8331209 5.24206680
13 -0.522938649 0.9404911 3.0822281 -0.10335791
14 0.318142859 0.8651728 1.9794013 1.06888933
15 -0.870920037 3.9335164 4.5851187 0.37296019
16 -0.536834653 -0.2343890 1.5840454 0.83641016
17 1.369788371 -1.0971564 1.1922038 -0.91191447
18 0.355511037 1.1537255 1.2379696 -0.31673585
19 0.279638498 0.2543774 2.6568435 0.37737600
20 0.515248897 -4.3723985 2.5923414 2.60326350
I have 1000 data frames like this. I would like to apply linear regression (say for eg: y = column b, and x = column d) for each data frame separately to find out slopes. I would like to get a list of slopes and if possible regression equations for all 1000 models.
CodePudding user response:
Create the formula with reformulate
, loop over the list
of data.frames, create the model with lm
, extract the slope from the coefficients (coef
)
form1 <- reformulate('d', response = 'b')
sapply(lst1, function(x) coef(lm(form1, data = x))[[2]])