mmeierer / REndo

REndo - A R package to control for endogeneity by using internal instrumental variable models
15 stars 4 forks source link

Non-matching matrix dimensions #63

Closed pschil closed 3 years ago

pschil commented 3 years ago

Discussed in https://github.com/mmeierer/REndo/discussions/62

Originally posted by **pinson06** August 18, 2021 Hi everyone, I'm trying to use this very nice package but I obtain a error message for which I have no clue how to solve. Here's what i obtain in R when using the MultiLevelIV with my dataset and this code : formula <- y ~ e + a + f + w + l + w2 + w_l + f_l + e_l + e_w + a_w + a_l + a_e + e_w_l + f_w_l + f_a_l + f_e_l + a_w_l + f_a_e_l + v + pr + t + (1 | album_id) | endo(e,a,f) multilevel <- multilevelIV(formula = formula, data = data, verbose=TRUE) Message obtained via R's console: "Detected multilevel model with 2 levels. For album_id (Level 2), 181 groups were found. Error: Matrices must have same dimensions in diag2Tsmart(e1, e2, "d") - e2" Any ideas?
pschil commented 3 years ago

We are using mapply() in various places to loop over the elements of two lists and expect a list to be returned. However, because the default SIMPLIFY=TRUE was not set to FALSE, mapply() may return an array instead of a list, if dimensions allow. Example:

l.Q <- mapply(l.Z2, l.W, FUN = function(g.z2, g.w){
    g.w %*% g.z2 %*% corpcor::pseudoinverse(Matrix::crossprod(g.z2, g.w) %*% g.w %*% g.z2) %*%
      Matrix::crossprod(g.z2, g.w)
  })

l.Z2and l.W are lists of matrices, and l.Q is expected to be a list of matrices as well but may be a single array in some cases.

Fix: Pass SIMPLIFY=FALSE to every call of mapply().