如何使用彩色置信区间带绘制回归原始尺度的回归图? [英] How to plot regression transformed back on original scale with colored confidence interval bands?

查看:128
本文介绍了如何使用彩色置信区间带绘制回归原始尺度的回归图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制线性模型的线和 95% 置信区间,其中响应已被 logit 转换回数据的原始尺度.所以结果应该是一条曲线,包括原始尺度上的置信区间,在 logit 变换尺度上它将是一条直线.见代码:

I would like to plot the line and the 95% confidence interval from a linear model where the response has been logit transformed back on the original scale of the data. So the result should be a curved line including the confidence intervals on the original scale, where it would be a straight line on the logit transformed scale. See code:

# Data
dat <- data.frame(c(45,75,14,45,45,55,65,15,3,85),
                  c(.37, .45, .24, .16, .46, .89, .16, .24, .23, .49))
colnames(dat) <- c("age", "bil.")               


# Logit transformation
dat$bb_logit <- log(dat$bil./(1-dat$bil.))

# Model
modelbb <- lm(bb_logit ~ age + I(age^2), data=dat)
summary(modelbb)

# Backtranform
dat$bb_back <- exp(predict.lm(modelbb))/ (1 + exp(predict.lm(modelbb)))

# Plot
plot(dat$age, dat$bb_back)
abline(modelbb)

我在这里尝试的是绘制曲线回归线并添加置信区间.在 ggplot2 中有 geom_smooth 函数,可以在其中指定线性模型,但我找不到从 predict.lm(我的模型).

What do I try here is to plot the curved regression line and add the confidence interval. Within ggplot2 there is the geom_smooth function where the the linear model can be specified, but I could not find a way of plotting the predictions from the predict.lm(my model).

我还想知道如何添加一个彩色多边形来表示如下图所示的置信区间.我知道我必须使用函数 polygon 和坐标,但我不知道如何使用.

I would also like to know how to add a coloured polygon which will represent the confidence interval as in the image below. I know I have to use function polygon and coordinates but I do not know how.

推荐答案

You may use predict on a age range say 1:100,指定interval= 选项用于 CI.使用 type="l" 绘图将平滑一条漂亮的曲线.然后可以使用 lines 添加置信区间.

You may use predict on an age range say 1:100, specify interval= option for the CIs. Plotting with type="l" will smooth a nice curve. Confidence intervals then can be added using lines.

p <- predict(modelbb, data.frame(age=1:100), interval="confidence")
# Backtransform
p.tr <- exp(p) / (1 + exp(p))

plot(1:100, p.tr[,1], type="l", ylim=range(p.tr), xlab="age", ylab="bil.")
sapply(2:3, function(i) lines(1:100, p.tr[,i], lty=2))
legend("topleft", legend=c("fit", "95%-CI"), lty=1:2)

产量

要获得阴影置信带,请使用 polygon.由于您想要两个置信度,您可能需要为每个置信度做出一个预测ion.该线将被 polygon 覆盖,因此最好先使用 type="n" 制作一个空的 plot 并绘制 <代码>行在最后.(请注意,我还将向您展示自定义轴标签的一些提示.)polygons 的技巧是使用 rev 来回表达值.

To get shaded confidence bands use polygon. Since you want two confidence levels you probably need to make one prediction for each. The line will get covered by the polygons, so it's better to make an empty plot first using type="n" and draw the lines at the end. (Note that I'll also show you some hints for custom axis labeling.) The trick for the polygons is to express the values back and forth using rev.

p.95 <- predict(modelbb, data.frame(age=1:100), interval="confidence", level=.95)
p.99 <- predict(modelbb, data.frame(age=1:100), interval="confidence", level=.99)
# Backtransform
p.95.tr <- exp(p.95) / (1 + exp(p.95))
p.99.tr <- exp(p.99) / (1 + exp(p.99))

plot(1:100, p.99.tr[,1], type="n", ylim=range(p.99.tr), xlab="Age", ylab="",
     main="", yaxt="n")
mtext("Tree biomass production", 3, .5)
mtext("a", 2, 2, at=1.17, xpd=TRUE, las=2, cex=3)
axis(2, (1:5)*.2, labels=FALSE)
mtext((1:5)*2, 2, 1, at=(1:5)*.2, las=2)
mtext(bquote(Production ~(kg~m^-2~year^-1)), 2, 2)
# CIs
polygon(c(1:100, 100:1), c(p.99.tr[,2], rev(p.99.tr[,3])), col=rgb(.5, 1, .2),
        border=NA)
polygon(c(1:100, 100:1), c(p.95.tr[,2], rev(p.95.tr[,3])), col=rgb(0, .8, .5),
        border=NA)
# fit
lines(1:100, p.99.tr[,1], ylim=range(p.99.tr), lwd=2)
#legend
legend("topleft", legend=c("fit", "99%-CI", "95%-CI"), lty=c(1, NA, NA), lwd=2,
       pch=c(NA, 15, 15), bty="n",
       col=c("#000000", rgb(.5, 1, .2), rgb(0, .8, .5)))

产量

这篇关于如何使用彩色置信区间带绘制回归原始尺度的回归图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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