[英]How do I output multiple linear regression slopes to new list?
I have a data.frame and a list of observation http://imgur.com/7hL2bDC 我有一个data.frame和观察列表http://imgur.com/7hL2bDC
and a list of observation times http://imgur.com/w1P0qsg 以及观察时间列表http://imgur.com/w1P0qsg
and I want to create a list with the linear regression line slopes from each column of that data frame and the observation times. 我想创建一个列表,其中包含该数据帧各列的线性回归线斜率和观察时间。
If i was doing it all manually, I'd do: 如果我手动完成所有操作,则可以执行以下操作:
raw_slope <- lm(score1$AKR ~ obs) raw_slope <-lm(score1 $ AKR〜obs)
slope <- raw_slope$coefficients[[2]] 斜率<-raw_slope $ coefficients [[2]]
and then "slope" is the value I want. 然后“坡度”就是我想要的值。 I'm trying to understand how i can run something and spit out the slope for each of the 19 company symbols in my data.frame. 我试图了解如何运行某些数据,并为data.frame中的19个公司符号中的每个符号吐出斜率。
I'm kind of a novice at R but I'm trying to learn it, I appreciate any help pointing me in the right direction. 我是R的新手,但我正在尝试学习它,我感谢为我指明正确方向的任何帮助。
EDIT2: I ran lm(score1$... ~ obs) for each of the rows manually so i know the calculations should all work out. EDIT2:我为每行手动运行了lm(score1 $ ...〜obs),所以我知道计算应该全部解决。
EDIT: I tried the suggestion from the comments 编辑:我从评论中尝试了建议
listslope<-lapply(score1, function(x) coef(lm(x~obs))[2]) listlope <-lapply(分数1,函数(x)coef(lm(x〜obs))[2])
and am getting 越来越
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases lm.fit(x,y,offset = offset,singular.ok = singular.ok,...)中的错误:0(非NA)
I'm going to try and figure out what's causing the issue, but I wanted to update my post and say thanks. 我将尝试找出导致此问题的原因,但我想更新我的帖子并表示感谢。
lapply(score1, function(x) coef(lm(x~obs))[2])
Should give it to you in a list. 应该在列表中给您。
Use sapply
if you'd prefer a vector. 如果您想使用向量,请使用sapply
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.