ggplot2:如何绘制正交回归线? [英] ggplot2: How to plot an orthogonal regression line?

查看:97
本文介绍了ggplot2:如何绘制正交回归线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过两种不同的视觉感知测试对大量参与者进行了测试-现在,我想看看两种测试的性能在多大程度上相关.

I have tested a large sample of participants on two different tests of visual perception – now, I'd like to see to what extent performance on both tests correlates.

为了可视化相关性,我使用ggplot()在R中绘制了一个散点图,并拟合了回归线(使用stat_smooth()).但是,由于我的xy变量都是性能指标,因此在拟合回归线时我需要将它们都考虑在内–因此,我不能使用简单的线性回归(使用stat_smooth(method="lm")),而是需要拟合正交回归(或总最小二乘法).我将如何去做呢?

To visualise the correlation, I plot a scatterplot in R using ggplot() and I fit a regression line (using stat_smooth()). However, since both my x and y variable are performance measures, I need to take both of them into account when fitting my regression line – thus, I cannot use a simple linear regression (using stat_smooth(method="lm")), but rather need to fit an orthogonal regression (or Total least squares). How would I go about doing this?

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

I know I can specify formula in stat_smooth(), but I wouldn't know what formula to use. From what I understand, none of the preset methods (lm, glm, gam, loess, rlm) are applicable.

推荐答案

事实证明,您可以提取斜率并从(x,y)的主成分分析中截取,如下所示

It turns out that you can extract the slope and intercept from principal components analysis on (x,y), as shown here. This is just a little simpler, runs in base R, and gives the identical result to using 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")

这篇关于ggplot2:如何绘制正交回归线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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