简体   繁体   English

从R中的实际点到建模点丢弃线

[英]Drop lines from actual to modeled points in R

Yesterday I worked up an example of the difference between Ordinary Least Squares (OLS) vs. Principal Components Analysis (PCA). 昨天我研究了普通最小二乘法(OLS)与主成分分析(PCA)之间差异的一个例子 For that illustration I wanted to show the errors minimized by OLS and PCA so I plotted the actuals, the predicted line and then I manually (with GIMP) drew in a drop line to illustrate a couple of the error terms. 为了说明我想要显示由OLS和PCA最小化的错误,所以我绘制了实际值,预测线,然后我手动(使用GIMP)画了一条下拉线来说明几个错误术语。 How can I code the creation of the error lines in R? 如何编写R中错误行的创建? Here's the code I used for my example: 这是我用于示例的代码:

set.seed(2)
x <- 1:100

y <- 20 + 3 * x
e <- rnorm(100, 0, 60)
y <- 20 + 3 * x + e

plot(x,y)
yx.lm <- lm(y ~ x)
lines(x, predict(yx.lm), col="red")

Then I manually added the yellow lines to produce the following: 然后我手动添加黄线以产生以下内容:

替代文字

?segments

I'd provide an example, but I'm pretty busy today and it's not that complicated to pick the points. 我举了一个例子,但今天我很忙,挑选积分并不复杂。 ;-) ;-)

Okay, so I'm not that busy... 好的,所以我不是那么忙......

n=58; segments(x[n],y[n],x[n],predict(yx.lm)[n])
n=65; segments(x[n],y[n],x[n],predict(yx.lm)[n])

As Joshua mentioned, segments() is the way to go here. 正如约书亚所提到的, segments()是去这里的方式。 And as it is totally vectorised, we can add in all the errors at once, following on from your example 由于它是完全矢量化的,我们可以一次性添加所有错误,继续您的示例

set.seed(2)
x <- 1:100

y <- 20 + 3 * x
e <- rnorm(100, 0, 60)
y <- 20 + 3 * x + e

plot(x,y)
yx.lm <- lm(y ~ x)
lines(x, predict(yx.lm), col="red")
## Add segments
segments(x, y, x, fitted(yx.lm), col = "blue")

If you only want to highlight a couple of the errors, then to modify the example Joshua gave: 如果您只想突出显示几个错误,那么要修改Joshua给出的示例:

n <- c(58,65)
segments(x[n], y[n], x[n], fitted(yx.lm)[n], col = "orange", lwd = 3)

HTH HTH

G G

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM