将拟合回归线 (abline) 限制为模型中使用的数据范围
是否可以仅在特定的 x 值范围内绘制拟合的 abline
?
我有一个数据集,该数据集与该数据集的子集进行线性拟合:
# The dataset:
daten <- data.frame(x = c(0:6), y = c(0.3, 0.1, 0.9, 3.1, 5, 4.9, 6.2))
# make a linear fit for the datapoints 3, 4, 5
daten_fit <- lm(formula = y~x, data = daten, subset = 3:5)
当我绘制数据并绘制回归线时:
plot (y ~ x, data = daten)
abline(reg = daten_fit)
该线是针对原始数据中 x 值的全范围绘制的。但是,我只想为用于曲线拟合的数据子集绘制回归线。我想到了两个想法:
绘制第二条线,该线较粗,但仅在 3:5 范围内显示。我检查了
abline
、lines
和segments
的参数,但找不到任何内容添加小刻度到垂直于 abline 的相应位置。我现在知道如何做到这一点。这当然是更好的方式。
您有解决方案吗?
Is it possible to draw an abline
of a fit only in a certain range of x-values?
I have a dataset with a linear fit of a subset of that dataset:
# The dataset:
daten <- data.frame(x = c(0:6), y = c(0.3, 0.1, 0.9, 3.1, 5, 4.9, 6.2))
# make a linear fit for the datapoints 3, 4, 5
daten_fit <- lm(formula = y~x, data = daten, subset = 3:5)
When I plot the data and draw a regression line:
plot (y ~ x, data = daten)
abline(reg = daten_fit)
The line is drawn for the full range of x-values in the original data. But, I want to draw the regression line only for the subset of data that was used for curve fitting. There were 2 ideas that came to my mind:
Draw a second line that is thicker, but is only shown in the range 3:5. I checked the parameters for
abline
,lines
andsegments
but I could not find anythingAdd small ticks to the respective positions, that are perpendicular to the
abline
. I have now idea how I could do this. this would be the nicer way of course.
Do you have any idea for a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案是否定的,不可能让
abline()
仅在模型拟合的绘图区域的一部分上绘制拟合线。这是因为它仅使用模型系数来绘制线,而不是模型的预测。如果仔细观察,您会发现线条绘制实际上延伸到绘图区域之外,覆盖了绘图框所在的区域。此类问题的最简单解决方案是根据模型预测所需区域。
首先,我们获得要区分的 x 值的范围:
然后我们使用模型预测该范围上的一组均匀间隔的点:
现在使用 abline 绘制数据和模型():
然后添加您想要强调的区域:
这给了我们这个图:
如果您有一个比这更复杂的模型可以通过 abline() 处理,然后我们采取稍微不同的策略,预测可用的绘制数据的范围来绘制线条,然后选择我们想要突出显示的区间。以下代码执行此操作:
我们在这里所做的是使用
subset
参数从拟合中使用的区间内的预测中挑选出值,我们传递给subset
的向量code> 是一个由TRUE
和FALSE
值组成的逻辑向量,指示哪些数据位于感兴趣的区域中,并且lines()
仅沿着那些数据。人们可能想知道为什么我要对预测变量的 50 或 100 个均匀间隔的值进行预测,而在这种情况下,我们只需对数据或感兴趣区域的开始和结束进行预测并将两个点连接起来?好吧,并不是所有的建模练习都那么简单 - 上一个问题的双对数模型就是一个很好的例子 - 我上面概述的通用解决方案在所有情况下都适用,而简单地加入两个预测则不行。
@Andrie 为您提供了想法 2 的解决方案。
The answer is No, it is not possible to get
abline()
to draw the fitted line on only one part of the plot region where the model was fitted. This is because it uses only the model coefficients to draw the line, not predictions from the model. If you look closely, you'll see that the line draw actually extends outside the plot region, covering the plot frame where it exists the region.The simplest solution to such problems is to predict from the model for the regions you want.
First, we get the range of
x
values we want to differentiate:then we predict for a set of evenly spaced points on this range using the model:
Now plot the data and the model using
abline()
:then add in the region you want to emphasise:
Which gives us this plot:
If you have a model that is more complex than that which can be handled by
abline()
, then we take a slightly different strategy, predicting over the range of the available, plotted data to draw the line, and then pick out the interval we want to highlight. The following code does that:What we do here is use the
subset
argument to pick out the values from the predictions that are in the interval used in fitting, the vector we pass tosubset
is a logical vector ofTRUE
andFALSE
values indicating which data are in the region of interest andlines()
only plots a line along those data.One might wonder why I have done predictions over 50 or 100 evenly spaced values of the predictor variable when we could, in this case, just have done a prediction for the start and the end of the data or region of interest and join the two points? Well, not all modelling exercises are that simple - you double log model from a previous question is a case in point - and the generic solution I outline above will work in all cases whereas simply joining two predictions won't.
@Andrie has furnished you with a solution to Idea 2.
是使用颜色来区分已安装的点和未安装的点:
一种方法 第二种方法是在 x 轴上绘制刻度线。这些刻度线称为地毯,可以使用
rug
函数绘制。但首先您必须计算范围
:One way would be to use colours to distinguish between points that are fitted and those that aren't:
The second way is to plot the tick marks on the x-axis. These ticks are called rugs, and can be drawn using the
rug
function. But first you have to calculate therange
:这是一个有点基本的绘图问题 - 使用
ylim=c(low, high)
选项以及适合low
和high
的选项。您可能想阅读R 简介手册与您的 R 版本以及 优秀贡献文档 “http://cran.r-project.org” rel="nofollow">CRAN 网站。
This is a somewhat basic plotting question -- use the
ylim=c(low, high)
option with suitable options forlow
andhigh
.You may want to read then An Introduction to R manual that came with your R version, and the other fine contributed documentation on the CRAN site.