ggplot2:如何绘制正交回归线?

use*_*247 4 regression r ggplot2

我已经在两种不同的视觉感知测试中测试了大量参与者——现在,我想看看这两种测试的表现在多大程度上相关。

为了可视化相关性,我使用 R 在 R 中绘制散点图ggplot()并拟合回归线(使用stat_smooth())。但是,由于 myxyvariable 都是性能度量,因此在拟合回归线时我需要同时考虑它们 - 因此,我不能使用简单的线性回归(使用stat_smooth(method="lm")),而是需要拟合正交回归(或 Total最小二乘法)。我该怎么做呢?

我知道我可以formula在中指定stat_smooth(),但我不知道要使用什么公式。据我了解,预设方法 ( lm, glm, gam, loess, rlm) 都不适用。

jlh*_*ard 6

事实证明,可以提取从主成分分析的斜率和截距上(X,Y),如图所示在这里。这只是稍微简单一点,在基本 R 中运行,并给出与使用Deming(...)in相同的结果MethComp

# same `x and `y` as @user20650's answer
df  <- data.frame(y, x)
pca <- prcomp(~x+y, df)
slp <- with(pca, rotation[2,1] / rotation[1,1])
int <- with(pca, center[2] - slp*center[1])

ggplot(df, aes(x,y)) + 
  geom_point() + 
  stat_smooth(method=lm, color="green", se=FALSE) +
  geom_abline(slope=slp, intercept=int, color="blue")
Run Code Online (Sandbox Code Playgroud)