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
我有 1000 個這樣的資料框。我想分別對每個資料框應用線性回歸(例如:y = b 列和 x = d 列)以找出斜率。我想獲得所有 1000 個模型的斜率串列以及可能的回歸方程。
uj5u.com熱心網友回復:
用 建立公式reformulate,遍歷listdata.frames,用 建立模型lm,從系數中提取斜率 ( coef)
form1 <- reformulate('d', response = 'b')
sapply(lst1, function(x) coef(lm(form1, data = x))[[2]])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/524540.html
標籤:r线性回归
