【问题标题】:Back-transform coefficients from glmer with scaled independent variables for prediction来自 glmer 的反向变换系数与用于预测的缩放自变量
【发布时间】:2019-04-18 21:09:20
【问题描述】:

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

简化示例:

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

我尝试使用this example 对预测进行反向转换:

# 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"

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

我也查看了许多类似的问题,但没有设法将任何问题应用于我的案例(How to unscale the coefficients from an lmer()-model fitted with a scaled responseunscale and uncenter glmer parametersScale back linear regression coefficients in R from scaled and centered datahttps://stats.stackexchange.com/questions/302448/back-transform-mixed-effects-models-regression-coefficients-for-fixed-effects-f)。

【问题讨论】:

    标签: r regression scale lme4


    【解决方案1】:

    您的方法的问题在于它仅根据wt 变量“取消缩放”,而您缩放了回归模型中的所有变量。一种可行的方法是使用原始数据帧上使用的居中/缩放值调整新(预测)数据帧中的所有变量:

    ## 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))的预测进行比较,结果为真。

    另一种合理的方法是

    • 使用链接问题之一(例如this one)中提供的“取消缩放”方法取消缩放/取消居中所有参数,生成系数向量beta_unsc
    • 根据您的预测框架构建适当的模型矩阵:
    X <- model.matrix(formula(model,fixed.only=TRUE), 
             newdata=pred_frame)
    
    • 计算线性预测器和反向变换:
    pred <- plogis(X %*% beta_unsc)
    

    【讨论】:

    • 谢谢@BenBolker,太棒了。我想取消缩放数据的原因是将其显示在绘图中,但我仍然遇到问题。我不确定错误是在我在这个问题中使用的代码中还是在我用来绘图的代码中。我在另一篇文章 (stackoverflow.com/questions/53193940/…) 中提出了这个问题,现在已更新以包含您的未缩放修复。任何想法我做错了什么?
    猜你喜欢
    • 1970-01-01
    • 2019-11-07
    • 2023-03-31
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多