logit原油和调整后的GAM模型的赔率比插值

时间:2018-11-12 21:59:45

标签: r interpolation logistic-regression gam mgcv

我有一些数据可以拟合粗略和调整后的logit GAM:

library(mgcv)
## Simulate some data...
set.seed(3);n<-400
dat <- gamSim(1,n=n)
mu <- binomial()$linkinv(dat$f/4-2)
phi <- .5
a <- mu*phi;b <- phi - a;
dat$y <- rbeta(n,a,b) 

## Fitting GAMs
crude <- gam(y~s(x0),family=binomial(link="logit"),data=dat)
adj <- gam(y~s(x0)+s(x1)+s(x2)+s(x3),family=binomial(link="logit"),data=dat)

现在,我将以比值比(OR)1.00(即概率0.50)截获x0的值。为此,我将visreg与参数plot = FALSE一起使用。

## Prepare data for ggplotting 
library(visreg)
p.crude <- visreg(crude, "x0",  plot = FALSE)
p.adj <- visreg(adj, "x0",  plot = FALSE)

library(dplyr)
bind_rows(
mutate(p.crude$fit, Model = "crude"),
mutate(p.adj$fit, Model = "adj")
) -> fits

好的。我将从LogOR计算OR。以下代码正确吗?

# Compute ORs and CI from LogOR
fits$or <- exp(fits$visregFit)  
fits$ci.low <- exp(fits$visregLwr) 
fits$ci.up <- exp(fits$visregUpr) 

现在我使用approx以便用OR 1.00插值x0的值

## Interpolate x0 which give OR 1.00 (or 50% of probability)
x.crude <- round(approx(x = crude$fitted.values, y=crude$model$x0, xout = .5)$y, 1)
x.adj <- round(approx(x = adj$fitted.values, y=adj$model$x0, xout = .5)$y, 1)

最后,我将两个模型绘制在一张图中:

## Plotting using ggplot
library(ggplot2)
ggplot(data = fits) +
  geom_vline(aes(xintercept = x.crude), size=.2, color="black")+
  geom_vline(aes(xintercept = x.adj), size=.2, color="red")+
  annotate(geom ="text", x= x.crude - 0.05, y=.5, label = x.crude, size=3.5) +
  annotate(geom ="text", x= x.adj - 0.05, y=.5, label = x.adj, size=3.5, color="red") +
  geom_ribbon(aes(x0, ymin=ci.low, ymax=ci.up, group=Model, fill=Model), alpha=.05) +
  geom_line(aes(x0, or, group=Model, color=Model)) +
  labs(x="X0", y="Odds ratio")+
  theme_bw(16) 

enter image description here

如您所见,只有粗略模型显示OR几乎等于1.00(x0 = 0.9)的截距,而对于adj模型则永远不会发生。 首先,我怎么能得到精确等于1的OR插值? 第二...由于我的统计知识的局限,据我了解,我也应该根据该模型对x0的观察值,观察到adj模型的OR = 1的截距。为什么相对曲线向上设置?

0 个答案:

没有答案