You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ggpredict()无法保留log-link尺度的技术问题咨询

问题:ggpredict()无法保留log-link尺度的预测结果

我尝试使用ggpredict()函数获取log-link尺度的预测值,但修改back_transform = FALSE参数后没有效果。我的模型是负二项GAM,链接函数为log,希望单独查看交互项CYR * fSeason的效应,但发现back_transform = TRUE和back_transform = FALSE两种参数设置输出的预测值完全一致。

相关文档说明:

back_transform: 逻辑值,若为TRUE(默认值),针对log、log-log、exp、sqrt及类似变换响应的预测值将被反变换至原始响应尺度。

附带代码及输出:

library(mgcv)
library(ggeffects)

gam_lmre <- gam(num ~ 
                  
                  CYR * fSeason +
                  
                  s(DO) +
                  s(ave_tt) +
                  s(sal) +
                  
                  s(fSite, bs="re"),
                
                offset(log(area_sampled)), 
                
                data = toad2, 
                method = 'REML',
                select = TRUE,
                family = nb(link = "log"),
                control = list(trace = TRUE),
                drop.unused.levels=FALSE)

> summary(gam_lmre)$family

Family: Negative Binomial(0.326) 
Link function: log 

# 这应该将结果置于log尺度,不是吗?
> ggpredict(gam_lmre, terms = c("CYR", "fSeason"), back_transform = FALSE)
# Predicted counts of num

fSeason: DRY

 CYR | Predicted |     95% CI
-----------------------------
2008 |      0.11 | 0.04, 0.30
2011 |      0.12 | 0.05, 0.31
2013 |      0.13 | 0.05, 0.32
2016 |      0.14 | 0.06, 0.34
2019 |      0.16 | 0.06, 0.38
2024 |      0.18 | 0.07, 0.48

fSeason: WET

 CYR | Predicted |     95% CI
-----------------------------
2008 |      0.90 | 0.36, 2.24
2011 |      0.59 | 0.24, 1.43
2013 |      0.44 | 0.18, 1.07
2016 |      0.29 | 0.12, 0.70
2019 |      0.19 | 0.08, 0.47
2024 |      0.09 | 0.04, 0.24

> ggpredict(gam_lmre, terms = c("CYR", "fSeason"), back_transform = TRUE)
# Predicted counts of num

fSeason: DRY

 CYR | Predicted |     95% CI
-----------------------------
2008 |      0.11 | 0.04, 0.30
2011 |      0.12 | 0.05, 0.31
2013 |      0.13 | 0.05, 0.32
2016 |      0.14 | 0.06, 0.34
2019 |      0.16 | 0.06, 0.38
2024 |      0.18 | 0.07, 0.48

fSeason: WET

 CYR | Predicted |     95% CI
-----------------------------
2008 |      0.90 | 0.36, 2.24
2011 |      0.59 | 0.24, 1.43
2013 |      0.44 | 0.18, 1.07
2016 |      0.29 | 0.12, 0.70
2019 |      0.19 | 0.08, 0.47
2024 |      0.09 | 0.04, 0.24

原因分析

问题根源在于模型中包含的offset项(offset(log(area_sampled)))。ggpredict()默认会自动应用offset值:当设置back_transform = FALSE时,函数仍会将offset加到log尺度的线性预测器上,随后自动执行指数变换以匹配原始响应的计数尺度,导致两种参数设置输出结果一致。

解决方案

要获取纯log-link尺度(即不含offset的线性预测器)的预测值,需显式指定offset = 0,同时保留back_transform = FALSE:

ggpredict(gam_lmre, terms = c("CYR", "fSeason"), back_transform = FALSE, offset = 0)

补充说明

  • 如果需要查看包含offset的log尺度结果,可以先获取不含offset的log预测值,再手动加上mean(log(area_sampled))(或对应分组的offset均值);
  • 若仅需观察交互项CYR * fSeason的相对效应,不含offset的log尺度结果已足够反映趋势差异。

内容的提问来源于stack exchange,提问作者Nate

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 23:29:54