【问题标题】:How to get the 95th and 5th percentiles separately?如何分别获得第 95 和第 5 个百分位数?
【发布时间】:2013-05-20 08:50:58
【问题描述】:

我有数据(例如 d 和 di)要进行一些计算。

数据:

d= 1:20
xi= 2:21

我想使用这个等式(d 和 xi 分别使用第 95 和第 5 个百分位数):

          res= d(95th)-d(5th)/xi(95th)+xi(5th)
          res2= res + xi(5th)

我们使用这个函数得到第 95 和第 5 个百分位数(由 stackoverflow 用户提供):

   fun <- function(x){
quantiles <- quantile( x, c(.05, .95 ) )
x[ x < quantiles[1] ] <- quantiles[1]
x[ x > quantiles[2] ] <- quantiles[2]
x
                  }

但这不适用于我的情况,因为我想在上面的等式中分别使用第 95 个和第 5 个百分位数。请有任何想法。

【问题讨论】:

    标签: r


    【解决方案1】:

    这是你要找的吗?

    your_fun <- function(x,y){
        qx <- quantile(x,c(.05,.95))
        qy <- quantile(y,c(.05,.95))
        out <- diff(qx)/diff(qy)
        out2 <- out + qy[1]
    
        names(out) <- NULL
        names(out2) <- NULL
        list(res=out,res2=out2)
    }
    
    your_fun(d,xi)
    

    这给了

    $res
    [1] 1
    
    $res2
    [1] 3.95
    

    【讨论】:

    • 非常感谢弗兰克。有没有办法只得到第 95 或第 5 个百分位数?请再看一下我的方程式
    • @ZadSim 好的,我已编辑我的问题以在列表中返回 resres2。对不起,如果我误解了。您可以通过将 res2 放在函数的最后一行来返回它。我想您可能很快就会想了解更多关于 R 基础知识的信息。这里有一个很好的资源列表:stackoverflow.com/tags/r/info
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-24
    • 2023-03-27
    • 2019-12-26
    • 2011-10-10
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多