将拟合回归线 (abline) 限制为模型中使用的数据范围

发布于 2024-11-14 21:17:14 字数 738 浏览 2 评论 0原文

是否可以仅在特定的 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 值的全范围绘制的。但是,我只想为用于曲线拟合的数据子集绘制回归线。我想到了两个想法:

  1. 绘制第二条线,该线较粗,但仅在 3:5 范围内显示。我检查了 ablinelinessegments 的参数,但找不到任何内容

  2. 添加小刻度到垂直于 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:

  1. Draw a second line that is thicker, but is only shown in the range 3:5. I checked the parameters for abline, lines and segments but I could not find anything

  2. Add 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倥絔 2024-11-21 21:17:14

答案是否定的,不可能让 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
mod <- lm(y~x, data = daten, subset = 3:5)

首先,我们获得要区分的 x 值的范围:

xr <- with(daten, range(x[3:5]))

然后我们使用模型预测该范围上的一组均匀间隔的点:

pred <- data.frame(x = seq(from = xr[1], to = xr[2], length = 50))
pred <- transform(pred, yhat = predict(mod, newdata = pred))

现在使用 abline 绘制数据和模型():

plot(y ~ x, data = daten)
abline(mod)

然后添加您想要强调的区域:

lines(yhat ~ x, data = pred, col = "red", lwd = 2)

这给了我们这个图:

在此处输入图像描述

如果您有一个比这更复杂的模型可以通过 abline() 处理,然后我们采取稍微不同的策略,预测可用的绘制数据的范围来绘制线条,然后选择我们想要突出显示的区间。以下代码执行此操作:

## range of all `x` data
xr2 <- with(daten, range(x))
## same as before
pred <- data.frame(x = seq(from = xr2[1], to = xr2[2], length = 100))
pred <- transform(pred, yhat = predict(mod, newdata = pred))

## plot the data and the fitted model line
plot(y ~ x, data = daten)
lines(yhat ~ x, data = pred)

## add emphasis to the interval used in fitting
with(pred, lines(yhat ~ x, data = pred, subset = x >= xr[1] & x <= xr[2],
                 lwd = 2, col = "red"))

我们在这里所做的是使用 subset 参数从拟合中使用的区间内的预测中挑选出值,我们传递给 subset 的向量code> 是一个由 TRUEFALSE 值组成的逻辑向量,指示哪些数据位于感兴趣的区域中,并且 lines() 仅沿着那些数据。

R> head(with(pred, x >= xr[1] & x <= xr[2]))
[1] FALSE FALSE FALSE FALSE FALSE FALSE

人们可能想知道为什么我要对预测变量的 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.

# 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
mod <- lm(y~x, data = daten, subset = 3:5)

First, we get the range of x values we want to differentiate:

xr <- with(daten, range(x[3:5]))

then we predict for a set of evenly spaced points on this range using the model:

pred <- data.frame(x = seq(from = xr[1], to = xr[2], length = 50))
pred <- transform(pred, yhat = predict(mod, newdata = pred))

Now plot the data and the model using abline():

plot(y ~ x, data = daten)
abline(mod)

then add in the region you want to emphasise:

lines(yhat ~ x, data = pred, col = "red", lwd = 2)

Which gives us this plot:

enter image description here

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:

## range of all `x` data
xr2 <- with(daten, range(x))
## same as before
pred <- data.frame(x = seq(from = xr2[1], to = xr2[2], length = 100))
pred <- transform(pred, yhat = predict(mod, newdata = pred))

## plot the data and the fitted model line
plot(y ~ x, data = daten)
lines(yhat ~ x, data = pred)

## add emphasis to the interval used in fitting
with(pred, lines(yhat ~ x, data = pred, subset = x >= xr[1] & x <= xr[2],
                 lwd = 2, col = "red"))

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 to subset is a logical vector of TRUE and FALSE values indicating which data are in the region of interest and lines() only plots a line along those data.

R> head(with(pred, x >= xr[1] & x <= xr[2]))
[1] FALSE FALSE FALSE FALSE FALSE FALSE

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.

人疚 2024-11-21 21:17:14

是使用颜色来区分已安装的点和未安装的点:

daten_fit <- lm(formula = y~x, data = daten[3:5, ])

plot(y ~ x, data = daten)
points(y ~ x, data = daten[3:5, ], col="red")
abline(reg=daten_fit, col="red")

在此处输入图像描述

一种方法 第二种方法是在 x 轴上绘制刻度线。这些刻度线称为地毯,可以使用 rug 函数绘制。但首先您必须计算范围

#points(y ~ x, data = daten[3:5, ], col="red")
abline(reg=daten_fit, col="red")
rug(range(daten[3:5, 1]), lwd=3, col="red")

在此处输入图像描述

One way would be to use colours to distinguish between points that are fitted and those that aren't:

daten_fit <- lm(formula = y~x, data = daten[3:5, ])

plot(y ~ x, data = daten)
points(y ~ x, data = daten[3:5, ], col="red")
abline(reg=daten_fit, col="red")

enter image description here

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 the range:

#points(y ~ x, data = daten[3:5, ], col="red")
abline(reg=daten_fit, col="red")
rug(range(daten[3:5, 1]), lwd=3, col="red")

enter image description here

墨落画卷 2024-11-21 21:17:14

这是一个有点基本的绘图问题 - 使用 ylim=c(low, high) 选项以及适合 lowhigh 的选项。

您可能想阅读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 for low and high.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文