[英]R best fit of 45 degree line
(i know it must be incredibely easy, but i'm strugling with it in R:) (我知道它一定很容易,但是我在R中苦苦挣扎:)
i have dataset of x and y values saved in X and Y vectors. 我有保存在X和Y向量中的x和y值数据集。 I know that plot of the data should follow exactly -45 degree line (see image below) 我知道数据的绘制应该精确地遵循-45度线(请参见下图)
How do i find such -45 degree line that best fits the data (+ all these statistics available from summary(lm(...))? I've tried lm, but i can't force it to abandon fitting the slope parameter 我如何找到最适合数据的-45度线(+可从summary(lm(...)获得的所有这些统计信息)?我尝试过lm,但是我不能强迫它放弃拟合斜率参数
Thank you 谢谢
After trying: lm(y~1,offset=-x)
and applying abline(coefficient, -1)
i obtain following plot (see below) 尝试后: lm(y~1,offset=-x)
abline(coefficient, -1)
lm(y~1,offset=-x)
并应用abline(coefficient, -1)
我得到以下曲线(请参见下文)
black line is abline plot, yellow one is mine guess of fit -- what's wrong with lm
or do i miss totally something? 黑线是abline情节,黄色为配合我的猜测-这有什么错lm
还是我错过完全的东西吗?
Since you state that: 由于您声明:
y = -1*x + b y = -1 * x + b
then 然后
y+x = b y + x = b
So calculate the mean of (y+x) and you get the average value of b 因此,计算(y + x)的平均值,即可得到b的平均值
mean(y+x)
I believe the solution from @BenBolker is correct and perhaps you are using the wrong coefficient: 我相信@BenBolker的解决方案是正确的,也许您使用了错误的系数:
lm1 <- lm(y~1,offset=-x,data=df)
plot(df)
abline(coefficients(lm1),-1)
This produces: 这将产生:
This fit looks like the correct fit to me. 这种合身看起来对我来说是正确的合身。 The intercept is -2.217. 截距是-2.217。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.