使用`matlines`进行预测图时,如何更改置信区间线的颜色? [英] How do I change colours of confidence interval lines when using `matlines` for prediction plot?

查看:1250
本文介绍了使用`matlines`进行预测图时,如何更改置信区间线的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制对数回归的最佳拟合线以及该线周围的置信区间.我使用的代码运行良好,但我希望置信区间都为灰色"(而不是默认的红色"和绿色").不幸的是,在指定颜色更改时,我没有找到一种隔离它们的方法.我想要

I'm plotting a logarithmic regression's line of best fit as well as the confidence intervals around that line. The code I'm using works well enough, except I'd rather that the confidence intervals both be "gray" (rather than the default "red" and "green"). Unfortunately, I'm not seeing a way to isolate them when specifying colour changes. I'd like

  • 回归线:lty = 1, col = "black";
  • 要使置信区间具有:lty=2, col = "gray".
  • for the regression line: lty = 1, col = "black";
  • for confidence intervals to have: lty=2, col = "gray".

我该如何实现?我的代码是这样的:

How can I achieve this? my code is of the sort:

R6cl <- lm(log(R6$y) ~ R6$x)
pR6cl <- predict(R6cl, interval="confidence")
plot(R6$x, log(R6$y), type = "p") 
matlines(x = R6$x, y = log(R6$y), lwd = 2, lty = 1, col = "black")

产生:

推荐答案

colltylwd是矢量化的.您可以使用

col, lty and lwd are vectorized. You can use

R6cl <- lm(log(y) ~ x, data = R6)  ## don't use $ in formula
pR6cl <- predict(R6cl, interval = "confidence")
plot(log(y) ~ x, data = R6)  ## Read `?plot.formula`
matlines(R6$x, pR6cl, lwd = 2, lty = c(1, 2, 2), col = c(1, 2, 2))

您可以查看逐次回归,它具有二次多项式和在断点处平滑连接的直线这段代码会产生什么.

You can check the last figure in Piecewise regression with a quadratic polynomial and a straight line joining smoothly at a break point for what this code would produce.

如果您不清楚我为什么不建议在模型公式中使用$,请阅读 Predict()-也许我是不了解.

If you are unclear why I advise against the use of $ in model formula, read Predict() - Maybe I'm not understanding it.

其他读者的注意事项

OP具有对x进行排序的数据集.如果您的x未排序,请确保先对其进行排序.有关更多信息,请参见在R中使用lm()绘制多项式回归的预测时的杂乱图.

OP has a dataset where x is sorted. If your x is not sorted, make sure you sort it first. See Messy plot when plotting predictions of a polynomial regression using lm() in R for more.

这篇关于使用`matlines`进行预测图时,如何更改置信区间线的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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