计算R中表的每一行的线性趋势线 [英] Calculating a linear trend line for every row of a table in R
问题描述
是否有可能在不使用循环的情况下对数据帧的每一行进行线性回归?趋势线的输出(截距+斜率)应作为新列添加到原始数据框中.
is it somehow possible to conduct a linear regression for every single row of a data frame without using a loop? The output (intercept + slope) of the trend line should be added to the original data frame as new columns.
为了更清楚地说明我的意图,我准备了一个非常小的数据示例:
To make my intention more clearly, I have prepared a very small data example:
day1 <- c(1,3,1)
day2 <- c(2,2,1)
day3 <- c(3,1,5)
output.intercept <- c(0,4,-1.66667)
output.slope <- c(1,-1,2)
data <- data.frame(day1,day2,day3,output.intercept,output.slope)
输入变量为第1-3天;假设这些是连续3天不同商店的销售额.我要做的是计算3行的线性趋势线,并将输出参数作为新列添加到原始表中(请参见output.intercept + output.slope).
Input variables are day1-3; let's say those are the sales for different shops on 3 consecutive days. What I want to do is to calculate a linear trend line for the 3 rows and add the output parameters to the origin table (see output.intercept + output.slope) as new columns.
由于实际数据帧具有许多100k行,因此该解决方案在计算时间方面应该非常有效.
The solution should be very efficient in terms of calculation time since the real data frame has many 100k's of rows.
最好,克里斯多夫
推荐答案
design.mat <- cbind(1,1:3)
response.mat <- t(data[,1:3])
reg <- lm.fit(design.mat, response.mat)$coefficients
data <- cbind(data, t(reg))
# day1 day2 day3 output.intercept output.slope x1 x2
#1 1 2 3 0.00000 1 0.000000 1
#2 3 2 1 4.00000 -1 4.000000 -1
#3 1 1 5 -1.66667 2 -1.666667 2
但是,如果您有大量数据,由于内存限制,可能有必要进行循环.如果是这种情况,我将使用长格式的data.table并使用包的by
语法进行循环.
However, if you have massive data, it might be necessary to loop due to memory restrictions. If that's the case I would use a long format data.table and use the package's by
syntax to loop.
这篇关于计算R中表的每一行的线性趋势线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!