计算几个文件的线性回归?

时间:2013-11-20 12:35:49

标签: r linear-regression

我在一个文件夹中有3个文件(矩阵有200列和6行)

mat1 <- matrix(seq(1:1200), ncol = 200)
mat2 <- matrix(seq(1:1200), ncol = 200)
mat3 <- matrix(seq(1:1200), ncol = 200)

我在另一个文件夹中有另外3个文件(矩阵有200列和6行)

at1 <- matrix(seq(1:1200), ncol = 200)
at2 <- matrix(seq(1:1200), ncol = 200)
at3 <- matrix(seq(1:1200), ncol = 200)

我想计算线性回归方程:

mat=a + b * at
例如,我们采用

中的第一个像素
mat1[1,1]........until mat3[1,1]  and regress this with 
at1[1,1]........until at3[1,1] 

然后写输出(截距和b系数......)

做同样的事情:

mat1[1,2]........until mat3[1,2]  and regress this with 
at1[1,2]........until at3[1,2] 

因此对于mat1中的每个像素,我将有截距和系数b 最后将得到一个截距矩阵和一个b系数矩阵。

我知道我们只使用一个简单的矩阵:

model=lm(mat1~at1)

但对于时态数据,我不知道。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

这是一个开始:

myfits<-list()

for (j in 1:600) {
   for(k in 1:6) {
       ins <- c(at1[k,j],at2[k,j],at3[k,j])
       outs <- c(mat1[k,j],mat2[k,j],mat3[k,j])
       lmfit <-lm(outs~ins)
       myfits[[( k + (j-1)*6)]]<-lmfit
       }
   }

这将为您提供所有linfits的列表;然后,您可以在类似的循环中提取系数(list[[n]]$coefficients)。有更简洁的方法可以做到这一点,但我想说清楚发生了什么。

相关问题