mkshaw / r2mlm

r2mlm: R Package for Calculating R-Squared in Multilevel Models
27 stars 1 forks source link

clarify source of Tau values in documentation for r2mlm_manual #53

Closed samanthajoel closed 3 years ago

samanthajoel commented 3 years ago

The r2mlm_manual function requires Tau, defined as:

_Random effect covariance matrix; note that the first row/column denotes the intercept variance and covariances (if intercept is fixed, set all to 0) and each subsequent row/column denotes a given random slope<U+2019>s variance and covariances (to be entered in the order listed by randomcovs).

In the example provided, Tau is entered as:

Tau = matrix(c(18.548, -0.676,-0.396, -0.676, 1.064, -0.143, -0.396, -0.143, 3.612), 3, 3)

Where do those values come from? To look for them, I have tried the following: summary(model) vcov(model) VarCorr(model)

But the variances and covariances that these provide do not appear to match the example values. Is some sort of transformation required? Thank you!

mkshaw commented 3 years ago

Hi @samanthajoel! The way I've found to generate the Tau matrix is: as.matrix(Matrix::bdiag(VarCorr(model))). I find it the easiest to read, and found it through trial and error. Let me know if that works for you, too!

samanthajoel commented 3 years ago

Thank you for your reply @mkshaw!

When I run that code on your example model, I get this matrix:

         (Intercept)      salary_c     control_c

(Intercept) 3.867185e-01 6.461844e-05 0.0062518363 salary_c 6.461844e-05 2.766688e-03 -0.0003328784 control_c 6.251836e-03 -3.328784e-04 0.0284902495

But the code in the vignette, which lists the values manually, produces this matrix:

   [,1]   [,2]   [,3]

[1,] 18.548 -0.676 -0.396 [2,] -0.676 1.064 -0.143 [3,] -0.396 -0.143 3.612

I thought these would be equivalent so I must be missing something.

mkshaw commented 3 years ago

@samanthajoel Great catch! I changed the underlying dataset, teachsat, in the version 0.3.0 release of the package so the numbers made more sense for example purposes (no negative values of salary, for example), but I forgot to change the r2mlm_manual example accordingly and didn't realize until right now. So the matrix you got is correct!