R - apply lm on each data frame row

对着背影说爱祢 提交于 2019-12-02 20:15:52

问题


I am trying to apply a simple linear regression between two columns of a data frame, for every row. After some research I feel like I am almost there, but my function still doesn't work. Please take a look:

set.seed(1)
DF <- data.frame(A=rnorm(50, 100, 3),
                 B=rnorm(50, 100, 3))

resultlist   <- apply(DF, 1, function(y) lm(y ~ x))
resultcoeffs <- apply(DF, 1, function(y) lm(y ~ x)$coefficients)

Any tip on how to achieve that?

Thanks in advance.


回答1:


It is just one observation per row. Note that you get NA estimates as there are not enough degrees of freedom.

The idea would be:

 mapply(function(x,y) lm(y~x)$coefficients, DF[,1], DF[,2])

Or

 apply(DF1, 1, function(x) lm(x[2]~x[1])$coefficients)

EDIT

Suppose, you have many observations per row i.e. x and y variables span over many columns

 mapply(function(x,y) lm(y~x)$coefficients, as.data.frame(t(DFNew[1:3])),
                             as.data.frame(t(DFNew[4:6])))

Or

 apply(DFNew, 1, function(x) lm(x[4:6]~x[1:3])$coefficients)

data

set.seed(25)
DFNew <- as.data.frame(matrix(sample(1:50,10*6, replace=TRUE), ncol=6))


来源:https://stackoverflow.com/questions/27539033/r-apply-lm-on-each-data-frame-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!