2014-02-13 37 views
0

我有三個目錄,每個目錄中有五個文件。這些文件是矩陣(柵格)。我想計算文件相應列之間的迴歸方程。爲什麼在R中使用lm.fit時,df.residual會返回「邏輯」?

fun <- function(x1, x2, y) { 
    keep <- !(is.na(x1) | is.na(x2) | is.na(y)) 
    if (sum(keep) > 1) { 
    res <- lm.fit(cbind(1,x1[keep], x2[keep]), y[keep])$df.residual 
    } else { 
    res <- c(NA, NA, NA) 
    } 
    res 
} 


res <- array(res, dim=c(1,dim(dat1)[2:3])); res <- aperm(res, c(2,3,1)) 
res 
# [1,] Logical,3 Logical,3 Logical,3 Logical,3 Logical,3 Logical,3 Logical,3 Logical,3 Logical,3  
# Logical,3 Logical,3 Logical,3 
+0

如果您不會說法語,您的下載鏈接將毫無用處。你應該教育自己如何創建一個**最小**可重現的例子。 – Roland

回答

1

試着改變你的函數

fun <- function(x1, x2, y) { 
    keep <- !(is.na(x1) | is.na(x2) | is.na(y)) 
    if (sum(keep) > 1) { 
    res <- lm.fit(cbind(1,x1[keep], x2[keep]), y[keep])$df.residual 
    } else { 
    res <- NA 
    } 
    res 
} 

df.residual爲每一個值合適,但您嘗試結合三個NA值的向量。因此,mapply不會調用simplify2array並最終生成一個列表。如果您想要適合三參數模型,則應該將if (sum(keep) > 1)更改爲更大的限制。兩個觀察結果不足以適應這個模型。

相關問題