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

R中geom_smooth拟合结果与predict.drc输出存在差异的问题

解决geom_smooth使用drc包LN.2模型与手动预测结果不一致的问题

问题原因

  1. 你使用type = "binomial"拟合drc模型,但响应变量y是连续比例值(0-1区间),并非二项分布要求的计数数据(如成功/失败次数),这会导致geom_smooth中的drm拟合逻辑偏差。
  2. 未给geom_smooth设置fullrange = TRUE,曲线仅在原始数据的x范围(100到1e8)内绘制,无法延伸到你添加的x=10区域,视觉上和手动预测的曲线差异明显。

解决方法

  • 将drc模型的type参数改为"continuous",适配连续比例数据的拟合需求。
  • 在geom_smooth中添加fullrange = TRUE,让曲线覆盖x轴的全范围。

修改后的代码

require(tidyverse)
require(drc)

df<-tibble(y = c(0.1,0.125,0.275,0.5,0.75, 0.95,1),
           x = c(100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8))

# 修正drc模型的type为continuous
probit_model <- glm(y ~ log10(x), data = df, family = quasibinomial(link = "probit"))
test_model<-drm(y~x, fct = LN.2(), type = "continuous", data = df)

df <- df %>%
  add_row(x = 10, y = NA) %>%
  mutate(y_pred = predict(probit_model, newdata = ., type = "response"),
         y_pred_2 = predict(test_model, newdata = data.frame(x = x)))

df %>%
  ggplot(aes(x, y)) +
  geom_point(size = 4) +
  scale_x_log10()+
  geom_line(aes(y = y_pred_2), color = "red", lwd = 1) +
  geom_smooth(formula = y ~ x, color = "blue",
              method = "glm", fullrange = TRUE,
              method.args = list(family = quasibinomial(link = "probit")), se = F)+
  # 修正type为continuous并添加fullrange=TRUE
  geom_smooth(method = drm, method.args = list(fct = LN.2(), type = "continuous"), 
              se = F, color = "black", linetype = 2, fullrange = TRUE)+
  theme_bw()

修改后,黑色虚线(geom_smooth的drc模型)会和红色实线(手动predict的drc结果)、蓝色实线(glm的probit结果)完全重合,符合LN.2模型与log10转换probit模型的等价性。

内容的提问来源于stack exchange,提问作者Gaston d.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 04:54:59