glmer的反变换系数和可缩放的自变量,用于预测 [英] Back-transform coefficients from glmer with scaled independent variables for prediction

查看:202
本文介绍了glmer的反变换系数和可缩放的自变量,用于预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用lme4软件包安装了混合模型.在拟合模型之前,我用scale()函数转换了自变量.现在,我想使用predict()在图表上显示结果,因此我需要将预测数据恢复到原始比例.我该怎么做?

I've fitted a mixed model using the lme4 package. I transformed my independent variables with the scale() function prior to fitting the model. I now want to display my results on a graph using predict(), so I need the predicted data to be back on the original scale. How do I do this?

简化示例:

database <- mtcars

# Scale data
database$wt <- scale(mtcars$wt)
database$am <- scale(mtcars$am)

# Make model
model.1 <- glmer(vs ~ scale(wt) + scale(am) + (1|carb), database, family = binomial, na.action = "na.fail")

# make new data frame with all values set to their mean
xweight <- as.data.frame(lapply(lapply(database[, -1], mean), rep, 100))

# make new values for wt
xweight$wt <- (wt = seq(min(database$wt), max(database$wt), length = 100))

#  predict from new values
a <- predict(model.1, newdata = xweight, type="response", re.form=NA)

# returns scaled prediction

我尝试使用此示例对预测进行反变换:

I've tried using this example to back-transform the predictions:

# save scale and center values
scaleList <- list(scale = attr(database$wt, "scaled:scale"),
              center = attr(database$wt, "scaled:center"))

# back-transform predictions
a.unscaled <- a * scaleList$scale + scaleList$center

# Make model with unscaled data to compare
un.model.1 <- glmer(vs ~ wt + am + (1|carb), mtcars, family = binomial, na.action = "na.fail")

# make new data frame with all values set to their mean
un.xweight <- as.data.frame(lapply(lapply(mtcars[, -1], mean), rep, 100))

# make new values for wt
un.xweight$wt <- (wt = seq(min(mtcars$wt), max(mtcars$wt), length = 100))

#  predict from new values
b <- predict(un.model.1, newdata = xweight, type="response", re.form=NA)

all.equal(a.unscaled,b)
# [1] "Mean relative difference: 0.7223061"

这不起作用-不应有任何区别. 我做错了什么?

This doesn't work - there shouldn't be any difference. What have I done wrong?

我也研究了许多类似的问题,但是没有设法将其应用于我的案例(不缩放和不居中的glmer参数 https://stats.stackexchange.com/questions /302448/back-transform-mixed-effects-models-regression-coefficients-for-fixed-effects-f ).

I've also looked at a number of similar questions but not managed to apply any to my case (How to unscale the coefficients from an lmer()-model fitted with a scaled response, unscale and uncenter glmer parameters, Scale back linear regression coefficients in R from scaled and centered data, https://stats.stackexchange.com/questions/302448/back-transform-mixed-effects-models-regression-coefficients-for-fixed-effects-f).

推荐答案

您的方法存在的问题是,它仅基于wt变量未缩放",而您缩放了回归模型中的所有变量.一种有效的方法是使用在原始数据帧上使用的居中/缩放值来调整新(预测)数据帧中的所有变量:

The problem with your approach is that it only "unscales" based on the wt variable, whereas you scaled all of the variables in your regression model. One approach that works is to adjust all of the variables in your new (prediction) data frame using the centering/scaling values that were used on the original data frame:

## scale variable x using center/scale attributes
## of variable y
scfun <- function(x,y) {
    scale(x, 
          center=attr(y,"scaled:center"), 
          scale=attr(y,"scaled:scale"))
}
## scale prediction frame
xweight_sc <- transform(xweight,
                        wt = scfun(wt, database$wt),
                        am = scfun(am, database$am))
## predict
p_unsc <- predict(model.1, 
                  newdata=xweight_sc, 
                  type="response", re.form=NA)

将此p_unsc与未缩放模型(代码中的b)(即all.equal(b,p_unsc))的预测进行比较,得出TRUE.

Comparing this p_unsc to your predictions from the unscaled model (b in your code), i.e. all.equal(b,p_unsc), gives TRUE.

另一种合理的方法是

    使用链接问题之一(例如
  • unscale/uncenter all of your parameters using the "unscaling" approaches presented in one of the linked question (such as this one), generating a coefficient vector beta_unsc
  • construct the appropriate model matrix from your prediction frame:
X <- model.matrix(formula(model,fixed.only=TRUE), 
         newdata=pred_frame)

  • 计算线性预测变量并进行逆变换:
  • pred <- plogis(X %*% beta_unsc)
    

    这篇关于glmer的反变换系数和可缩放的自变量,用于预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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