【问题标题】:Plotting empirical cumulative probability function and its inverse绘制经验累积概率函数及其反函数
【发布时间】:2016-07-23 15:25:26
【问题描述】:

我有数据cdecn

set.seed(0)
cdecn <- sample(1:10,570,replace=TRUE)
a <- rnorm(cdecn,mean(cdecn),sd(cdecn))

我创建了一个显示累积概率的图。

aprob <- ecdf(a)
plot(aprob)

我想知道如何切换 x 轴和 y 轴以获得新图,即 ECDF 的倒数。

另外,对于新的情节,有没有办法在我的曲线与 0 的交点处添加一条垂直线?

【问题讨论】:

    标签: r plot cdf


    【解决方案1】:

    我们可以做到以下几点。我的 cmets 沿用代码非常有解释性。

    ## reproducible example
    set.seed(0)
    cdecn <- sample(1:10,570,replace=TRUE)
    a <- rnorm(cdecn,mean(cdecn),sd(cdecn))  ## random samples
    
    a <- sort(a)  ## sort samples in ascending order
    e_cdf <- ecdf(a)  ## ecdf function
    e_cdf_val <- 1:length(a) / length(a)  ## the same as: e_cdf_val <- e_cdf(a)
    
    par(mfrow = c(1,2))
    
    ## ordinary ecdf plot
    plot(a, e_cdf_val, type = "s", xlab = "ordered samples", ylab = "ECDF",
         main = "ECDF")
    
    ## switch axises to get 'inverse' ECDF
    plot(e_cdf_val, a, type = "s", xlab = "ECDF", ylab = "ordered sample",
         main = "'inverse' ECDF")
    
    ## where the curve intersects 0
    p <- e_cdf(0)
    ## [1] 0.01578947
    
    ## highlight the intersection point
    points(p, 0, pch = 20, col = "red")
    
    ## add a dotted red vertical line through intersection
    abline(v = p, lty = 3, col = "red")
    
    ## display value p to the right of the intersection point
    ## round up to 4 digits
    text(p, 0, pos = 4, labels = round(p, 4), col = "red")
    

    【讨论】:

    • 不,不需要删除你的答案!我只是想知道您是否有更好的添加直线的方法。我终于找到了一些东西,但它可能不是很直接。
    • 带有切换轴的图是我想要的。我将在图中添加第二条曲线,我想说明曲线之间的差异。因此,我需要一条通过点 (x,0) 的垂直线。在这种情况下,我想要一条大约 x=0.05 的垂直线。所以我的问题是如何确定这一点在哪里。
    【解决方案2】:
    cdecn <- sample(1:10,570,replace=TRUE)
    a <- rnorm(cdecn,mean(cdecn),sd(cdecn))
    
    aprob <- ecdf(a)
    plot(aprob)
    
    # Switch the x and y axes
    x <- seq(0,1,0.001754386)
    plot(y=knots(aprob), x=x, ylab = "Fn(y)")
    

    # Add a 45 degree straight line at 0, 0
    my_line <- function(x,y,...){
      points(x,y,...)
      segments(min(x), y==0, max(x), max(y),...)
    }
    lines(my_line(x=x, y = knots(aprob)))
    

    【讨论】:

      【解决方案3】:

      “x==0 处的直线”位让我怀疑你想要一个 QQplot:

      qqnorm(a)
      qqline(a)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-20
        • 2014-09-15
        • 2020-08-05
        • 1970-01-01
        • 2021-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多