0

如何使用 lm(或 lsfit)将矩阵 A 的第 1 列与矩阵 B 的第 1 列相匹配。然后将矩阵 A 的第 2 列与矩阵 B 的第 2 列相匹配,依此类推。矩阵可能有 NA

4

1 回答 1

1

如果您强制使用 data.frame,则可以使用 mapply 将它们作为一系列列表处理:

> mapply(function(x,y) lm(y~x), as.data.frame(m1) , as.data.frame(m2) )
              V1         V2         V3        
coefficients  Numeric,2  Numeric,2  Numeric,2 
residuals     Numeric,4  Numeric,4  Numeric,4 
effects       Numeric,4  Numeric,4  Numeric,4 
rank          2          2          2         
fitted.values Numeric,4  Numeric,4  Numeric,4 
assign        Integer,2  Integer,2  Integer,2 
qr            List,5     List,5     List,5    
df.residual   2          2          2         
xlevels       List,0     List,0     List,0    
call          Expression Expression Expression
terms         Expression Expression Expression
model         List,2     List,2     List,2    

> ?lsfit
> mapply(function(x,y) lsfit(y,x), as.data.frame(m1) , as.data.frame(m2) )
             V1        V2        V3       
coefficients Numeric,2 Numeric,2 Numeric,2
residuals    Numeric,4 Numeric,4 Numeric,4
intercept    TRUE      TRUE      TRUE     
qr           List,6    List,6    List,6   
于 2013-03-26T19:29:35.433 回答