将R中的方差描述为惯性矩

时间:2013-08-23 10:47:34

标签: r graphics

分布的方差具有物理解释,即围绕分布均值的惯性矩。我想通过一个模式来表示R中的方差,如下所示:像一个带箭头的开放椭圆,这是表示惯性矩的常用方法。你有什么建议可以方便地完成这项工作吗?我希望能够通过指定中心的坐标和两个“半径”的长度(以及可选的“中断”的长度)将此椭圆添加到基本图形。

enter image description here

1 个答案:

答案 0 :(得分:1)

以下是我的解决方案的最终代码:

inertia <- function(x0=0, y0=0, a, b, r=1/5, l=0.25, d=3, s=0, w=NULL, cols="black", npoints=101, ...){
  if(length(cols==1)) cols <- rep(cols,3)
  # ellipse:
  f <- function(x) sqrt(round(b^2*(1-(x-x0)^2/a^2),14)) 
  curve(y0 + f(x), from=x0-a, to=x0-a*r, add=TRUE, n=npoints, col=cols[1], ...)
  curve(y0 + f(x), from=x0+a*r, to=x0+a, add=TRUE, n=npoints, col=cols[2], ...)
  curve(y0 - f(x), from=x0-a, to=x0+a, add=TRUE, n=npoints, col=cols[3], ...)
  # arrow:
  segments(x0-s, y0-b, x0-l, y0-b+l/d, col=cols[3], ...)
  segments(x0-s, y0-b, x0-l, y0-b-l/d, col=cols[3], ...)
  if(!is.null(w)){
    segments(x0-s+w, y0-b, x0-l, y0-b+l/d, col=cols[3], ...)
    segments(x0-s+w, y0-b, x0-l, y0-b-l/d, col=cols[3], ...)
  }
}

enter image description here

有关说明,请参阅http://stla.overblog.com/schematizing-the-variance-as-a-moment-of-inertia