Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error from mice.impute.2l.lmer() when specifying fixed effects only #384

Closed
cshawsome opened this issue Apr 13, 2021 · 8 comments
Closed
Labels

Comments

@cshawsome
Copy link

I'm having trouble imputing with 2l.lmer() when I only include fixed effects in the predictor matrix. I specify ID as the clustering variable by using -2 and specify the other 23 variables as fixed effects by using 1 in the predictor matrix. When I run the function I get this error message: Error in Xi %*% beta.star : non-conformable arguments

I thought this might be related to a previously closed issue about 2l.norm() where the solution at that time was to include every predictor as a random effect: #62

When I specify all predictors as random effects in 2l.lmer(), the function runs but my diagnostic plots are completely flat.

Screen Shot 2021-04-13 at 10 20 46 AM

Using 2l.norm() with fixed effects only with the same dataset runs and produces chains that mix, though. Is there something I'm missing about the 2l.lmer() function?

@stefvanbuuren
Copy link
Member

Hmm, that doesn't look OK.

Could you make a small reprex that shows the problem?

@cshawsome
Copy link
Author

Thank you for this quick response! I've created a synthetic dataset data_long.csv that can be plugged into the following code:

#---- read in data ----
data_long <- read_csv(here::here("data", "data_long.csv"))
time_updated_vars <- c("Ci", "age")
time_invariant_vars <- c("female")

#---- predictor matrix ----
blocks <- time_updated_vars
predict <- matrix(1, length(blocks), ncol = ncol(data_long)) %>% 
  set_rownames(blocks) %>% 
  set_colnames(colnames(data_long))

#Don't use these as predictors
predict[, c("wave")] <- 0

#Indicated cluster variable
predict[, "id"] <- -2

#Can't predict themselves
predict[time_updated_vars, time_updated_vars] <- 
  (diag(x = 1, nrow = length(time_updated_vars), 
        ncol = length(time_updated_vars)) == 0)*1

#---- impute ----
data_imputed <- mice(data = data_long, 
                     m = 2, maxit = 5, method = "2l.lmer", 
                     predictorMatrix = predict, where = is.na(data_long), 
                     blocks = as.list(rownames(predict)), 
                     seed = 20210126)

I checked and get the same behavior: Error for specifying everything as a fixed effect, Flat diagnostics when everything is a random effect. Please let me know if you have any trouble with the data file.

stefvanbuuren added a commit that referenced this issue Apr 14, 2021
…tead of row vector when the cluster contains only 1 observation (#384)
@stefvanbuuren
Copy link
Member

Thanks. I could reproduce and found the problem.

Cluster 14 in your data contains just one observation, which confused as.matrix() so that it returned a matrix of the wrong shape. I have now deleted as.matrix() in line 142 of mice.impute.2l.lmer(), and now your code runs.

mice 3.13.6 incorporates the fix. Could you check on your real data?

@stefvanbuuren stefvanbuuren self-assigned this Apr 14, 2021
@stefvanbuuren stefvanbuuren removed their assignment Apr 14, 2021
@stefvanbuuren
Copy link
Member

@shahabjolani Could you perhaps take a look at the flat lines plots, which look interesting/curious?

@shahabjolani
Copy link
Contributor

shahabjolani commented Apr 14, 2021

@stefvanbuuren @cshawsome Based on your toy example, it seems the imputation model is over-parameterised. I got the following warning for your example:

In mice.impute.2l.lmer(y = c(0.410241097375509, 0.502364878854811, :
lmer does not run. Simplify imputation model

This implies the multilevel imputation model (i.e., 2l.lmer) is not run so that no imputations should be produced. Nevertheless, when I look at the imputed data using the 'complete' function, the missing values are imputed! I have no idea what has been imputed but I am almost certain that the 2l.lmer is not used. @stefvanbuuren Do you have any idea?

Now, if you look at the mean of the imputed(!) values at each iteration, you realise the mean and sd of imputations are identical across iterations. This explains why you get the flat lines

@stefvanbuuren
Copy link
Member

OK, thanks.

I understand that the cause of the flat lines is that mice.impute.2l.lmer() returns the current imputations, and bypasses the update. I think we can leave it like that (what else to do?), but document the behaviour. The flat lines as well as the warning itself should then alert the user.

I'll open a new issue for this.

@shahabjolani
Copy link
Contributor

Indeed, the flat lines can act as an alert. However, this is not necessarily the behaviour of mice.impute.2l.lmer(), I think. If an imputation function (like this one) does not run, you will get such flat lines anyway...

@cshawsome
Copy link
Author

Thanks. I could reproduce and found the problem.

Cluster 14 in your data contains just one observation, which confused as.matrix() so that it returned a matrix of the wrong shape. I have now deleted as.matrix() in line 142 of mice.impute.2l.lmer(), and now your code runs.

mice 3.13.6 incorporates the fix. Could you check on your real data?

checked the fix on my real data and it works-- thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants