如何从具有缩放响应的lmer()模型对系数进行缩放 [英] How to unscale the coefficients from an lmer()-model fitted with a scaled response

查看:163
本文介绍了如何从具有缩放响应的lmer()模型对系数进行缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中安装了具有lme4包中lmer()功能的模型.我缩放了因变量:

I fitted a model in R with the lmer()-function from the lme4 package. I scaled the dependent variable:

    mod <- lmer(scale(Y)
                ~ X
                + (X | Z),
                data = df,
                REML = FALSE)

我用fixef(mod)看固定效果系数:

    > fixef(mod)
    (Intercept)      X1          X2         X3           X4 
     0.08577525 -0.16450047 -0.15040043 -0.25380073  0.02350007

从固定效果系数中手动计算平均值非常容易.但是,我希望它们不缩放,并且不确定如何准确地执行此操作.我知道定标意味着从每个Y中减去均值并以标准差表示.但是,均值和标准差都是根据原始数据计算得出的.在使用原始数据的均值和标准差拟合lmer()模型后,是否可以简单地逆转此过程?

It is quite easy to calculate the means by hand from the fixed-effects coefficients. However, I want them to be unscaled and I am unsure how to do this exactly. I am aware that scaling means substracting the mean from every Y and deviding by the standard deviation. But both, mean and standard deviation, were calculated from the original data. Can I simply reverse this process after I fitted an lmer()-model by using the mean and standard deviation of the original data?

感谢您的帮助!

更新:我上面提出模型的方式似乎暗示因变量是通过取所有响应的均值并除以所有响应的标准差来缩放的.通常,它的执行方式有所不同.不是采用总体均值和标准差,而是通过使用该受试者反应的均值和标准差对每个受试者进行标准化. (在lmer()中,这很奇怪,我认为,随机截距应该解决这个问题……更不用说我们正在谈论按序规模计算均值的事实了……)但是问题仍然存在:一旦我拟合了这样的模型,是否有一种干净的方法可以重新调整拟合模型的系数?

Update: The way I presented the model above seems to imply that the dependent variable is scaled by taking the mean over all responses and dividing by the standard deviation of all the responses. Usually, it is done differently. Rather than taking the overall mean and standard deviation the responses are standardized per subject by using the mean and standard deviation of the responses of that subject. (This is odd in an lmer() I think as the random intercept should take care of that... Not to mention the fact that we are talking about calculating means on an ordinal scale...) The problem however stays the same: Once I fitted such a model, is there a clean way to rescale the coefficients of the fitted model?

推荐答案

已更新:一般化以允许对响应以及预测变量进行缩放.

Updated: generalized to allow for scaling of the response as well as the predictors.

这是一个相当粗糙的实现.

Here's a fairly crude implementation.

如果我们原来的(无标度)回归是

If our original (unscaled) regression is

Y = b0 + b1*x1 + b2*x2 ... 

那么我们的规模回归为

(Y0-mu0)/s0 = b0' + (b1'*(1/s1*(x1-mu1))) + b2'*(1/s2*(x2-mu2))+ ...

这等效于

Y0 = mu0 + s0((b0'-b1'/s1*mu1-b2'/s2*mu2 + ...) + b1'/s1*x1 + b2'/s2*x2 + ...)

所以bi = s0*bi'/si表示i>0

b0 = s0*b0'+mu0-sum(bi*mui)

实施此操作:

 rescale.coefs <- function(beta,mu,sigma) {
    beta2 <- beta ## inherit names etc.
    beta2[-1] <- sigma[1]*beta[-1]/sigma[-1]
    beta2[1]  <- sigma[1]*beta[1]+mu[1]-sum(beta2[-1]*mu[-1])
    beta2
 }

尝试一下线性模型:

m1 <- lm(Illiteracy~.,as.data.frame(state.x77))
b1 <- coef(m1)

制作数据的缩放版本:

ss <- scale(state.x77)

比例系数:

m1S <- update(m1,data=as.data.frame(ss))
b1S <- coef(m1S)

现在尝试重新缩放:

icol <- which(colnames(state.x77)=="Illiteracy")
p.order <- c(icol,(1:ncol(state.x77))[-icol])
m <- colMeans(state.x77)[p.order]
s <- apply(state.x77,2,sd)[p.order]
all.equal(b1,rescale.coefs(b1S,m,s))  ## TRUE

这假定响应和预测变量均已缩放.

This assumes that both the response and the predictors are scaled.

  • 如果仅缩放响应而不是预测变量,则应提交(mc(mean(response),rep(0,...))sc(sd(response),rep(1,...))(即,ms是变量进行了调整和缩放).
  • 如果仅缩放预测变量而不是响应,则对于m提交c(0,mean(predictors)),对于s提交c(1,sd(predictors)).
  • If you scale only the response and not the predictors, then you should submit (c(mean(response),rep(0,...)) for m and c(sd(response),rep(1,...)) for s (i.e., m and s are the values by which the variables were shifted and scaled).
  • If you scale only the predictors and not the response, then submit c(0,mean(predictors)) for m and c(1,sd(predictors)) for s.

这篇关于如何从具有缩放响应的lmer()模型对系数进行缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆