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.