I have three directories and there are five files in each directory. those files are matrix(rasters) . I want to compute the regression equation between the corresponding columns of the files.

  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
有帮助吗?

解决方案

Try changing your function to

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 is a single value for each fit, but you try to combine that with a vector of three NA values. Consequently, mapply doesn't invoke simplify2array and you end up with a list.

PS: You should change if (sum(keep) > 1) to a bigger limit if you want to fit a three parameter model. Two observations are not enough to fit this model.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top