5
$\begingroup$

I have analyzed some data (the exact nature of which, I assume, is irrelevant for this question) using linear mixed effects models with the lmer() function from lme4.

There has been at least one question about standardized regression coefficients previously, in which, obviously, there has been the recommendation to use the scale() function in order to obtain standardized predictors and outcome variables before entering these into a model.

Furthermore, in a comment on an answer, @crash posted a link to an email by Ben Bolker, in which the latter provided a function, applying lm.beta() to "lmer-land".

The function, as provided there, looks like this:

lm.beta.lmer <- function(mod) {
   b <- fixef(mod)[-1]            
   sd.x <- apply(mod at X[,-1],2,sd)                                 
   sd.y <- sd(mod at y)
   b*sd.x/sd.y
}

Yet, the "mod at X" and "mod at y" parts are hyperlinked and appear not to resemble executable R code. Specifically, when trying to define this function, R outputs

Error: unexpected symbol in:
"b <- fixef(mod)[-1]
sd.x <- apply(mod at"

so apparently R does not like the spaces.

I am interested in how I would need to change the code of lm.beta.lmer() in order to successfully apply this to my lmer models, but my nooby R skills forbid me to do so.

If anyone provided a useful answer to this, I would deeply appreciate this.

Cheers, bunsen

EDIT Thanks @Wolfgang, I was not aware of this. However, having changed the abovementioned code accordingly, i.e. writing mod@X and mod@y, I get the following output when applying the function to a model:

>lm.beta.lmer(mod)
Error in apply(mod@X[, -1], 2, sd) : 
no slot of name "X" for this object of class "lmerMod"

Apparently, the code fails to access the Field X in slot pp of the lmerMod object (of which the first column is labelled "(Intercept)", so I assume it is the one that should be deleted). Since I get this error, I do not know, whether the Field y of slot resp of this object will be accessed, but I guess not.

I have attempted various ways of changing the code in order to make it access the X and y fields of the respective slots, but failed. Which part am I doing incorrectly, or rather (since I do not tell the ways I've tried): which is a possible way to access these fields?

I have noticed, that most likely I will be faster using the scale() function, but I am eager to learn a solution to my question.

Thanks again!

$\endgroup$
2
  • 2
    $\begingroup$ The mod at X parts are supposed to be mod@X. Mailing lists often replace "@" with " at " to make it more difficult for spammers to farm e-mail addresses. $\endgroup$
    – Wolfgang
    Nov 10, 2014 at 11:05
  • $\begingroup$ Thanks @Wolfgang. However, there are problems that follow and I have edited my question accordingly. $\endgroup$
    – bunsenbaer
    Nov 10, 2014 at 17:10

1 Answer 1

5
$\begingroup$

You can get at X and y using the getME() function. So, this should work:

lm.beta.lmer <- function(mod) {
   b <- fixef(mod)[-1]
   sd.x <- apply(getME(mod,"X")[,-1],2,sd)
   sd.y <- sd(getME(mod,"y"))
   b*sd.x/sd.y
}
$\endgroup$
1
  • 2
    $\begingroup$ Minor fix for cases when there there's only a single predictor (in which case this throws an error as the dim of getME(mod,"X")[,-1] = NULL) would be to coerce getME(mod,"X")[,-1] into something with dimensions ~ e.g., (although I'm sure there's a more efficient version and this may cause later problems!): lm.beta.lmer <- function(mod) { b <- fixef(mod)[-1]; sd.x <- apply(matrix(getME(mod,"X")[,-1]),2,sd); sd.y <- sd(getME(mod,"y")); b*sd.x/sd.y; } $\endgroup$
    – FelixST
    Oct 8, 2018 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.