【问题标题】:Residuals from 1:1 line1:1 线的残差
【发布时间】:2014-10-31 11:33:05
【问题描述】:

我想测量一组点与 1:1 线之间的距离。我可以建立一个线性模型并从最佳拟合中获得残差,但我无法从 1:1 的线中获得测量值。有什么有用的提示吗?

#build a df of random numbers     
x=runif(100, 0, 100)
    y=runif (100, 0, 100)
    df=cbind (x,y)
    df=as.data.frame(df)
#build a linear model    
lm1<-lm(y~x, data=df)
    summary (lm1)
#plot the data, lm best fit and 1:1 (red) line)    
    plot (y~x, data=df, pch=16)
    line (lm1)
    abline abline(0,1, col="red")
#get residulas for the linear model 
y.resid= resid (lm1)

【问题讨论】:

  • 按 1:1 行,你的意思是 y = 1 * x + 0 吗?
  • 是的,那将是情节上的红线
  • 请记住,对于 OLS 回归,残差不会给出(最小)距离(正交),而是给定 x 值的预期 y 值和测量 y 值之间的差异。
  • 你能澄清一下这个问题吗,你问的是点到 1:1 线的距离,还是残差?

标签: r scatter-plot linear-regression variance


【解决方案1】:

我建议使用y-x,就像@vpipkt 建议的那样。只是为了完整起见:您还可以创建一个具有固定系数y-x ~ 0 的线性模型并在那里取残差。

resid(lm(y-x ~ 0))

当然,这只是更复杂,并给出与y-x 相同的结果,但它明确指出您正在计算残差而不是计算到直线的最小距离(参见@user3969377 的答案)。

【讨论】:

    【解决方案2】:

    要确定一组点与 1:1 线之间的距离,请使用

    dist[x-y=0; (x0,y0)] = abs(x0 - y0) / sqrt(2)
    

    参考http://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line

    以你的为例,

    par(pty="s")
    plot (y~x, data=df, pch=16)
    line (lm1)
    abline(0,1, col="red")
    #get residulas for the linear model 
    y.resid= resid (lm1)
    a=1;b=-1;c=0
    xi = (b*(b*x-a*y)-a*c) / (a^2+b^2)
    yi = (a*(-b*x+a*y)-b*c) / (a^2+b^2)
    segments(x,y,xi,yi,col="blue")
    yr = abs(a*x+b*y+c)/sqrt(a^2+b^2)
    hist(yr)
    

    【讨论】:

      【解决方案3】:

      就模型 y=x 的残差而言,距离只是 `y-x'。

      r = y-x
      plot(r~x)
      abline(h=0)
      

      您可以将其扩展为更一般的线性模型 y = ax + b。残差是

      r = y - ax - b
      

      【讨论】:

        猜你喜欢
        • 2015-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-22
        • 2020-03-25
        • 2011-10-03
        • 2017-03-02
        相关资源
        最近更新 更多