R - apply lm on each data frame row

2020-04-16 04:47发布

问题:

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))


标签: r apply lm