如何将CDF中的百分位数转换为R脚本中的独占?

问题描述 投票:0回答:0

我正在使用 R 脚本创建包含 P97、P97.5、P98、P98.5、P99、P99.5 的各种百分位数的 CDF。

我想知道是否希望将百分位数转换为独占(这类似于Excel中的PERCENTILERANK.EXC函数),我尝试将百分位数[i] / 100更改为(百分位数[i] - 0.0001)/ 100?,但看起来是一样的。

R 中是否内置了一个函数来计算这个?否则,我怎么办?我将不胜感激任何意见或建议。

# define a list of percentile values

percentiles = c(97, 97.5, 98, 98.5, 99, 99.5)

 

# extract unique values of grouping column

groups = unique(df[,groupingColumnName])

 

# loop through groups

for (gg in groups) {

 

 # subset the data frame to include only the rows corresponding to the current group

 x = df[df[groupingColumnName] == gg, analysisColumnName]

 

 # sort the values and remove missing values

 x = sort(x, decreasing = FALSE, na.last = NA)

 N = length(x)

 

 # loop through the list of percentiles and calculate the corresponding percentile values and store in a list

 percentile_values = lapply(percentiles, function(p) quantile(x, p/100))

 

 # create a new data frame containing the sorted values and corresponding cumulative probabilities and percentile values

 cdfTable_g = data.frame(

  group = rep(gg, N),

  prob = ((1:N) - 1) / (N - 1),

  value = x

 )

 

 for (i in 1:length(percentiles)){

  cdfTable_g[paste0("percentile_",percentiles[i])] = percentile_values[[i]]

 }

 

 # modify the cumulative probabilities based on the current percentile values

 for (i in 1:length(percentiles)){

  cdfTable_g$prob[cdfTable_g$value > percentile_values[[i]]] = percentiles[i] / 100

 }

 

 # append the data frame to the master cdfTable using rbind

 if (gg == groups[1]) {

  cdfTable = cdfTable_g

 } else {

  cdfTable = rbind(cdfTable, cdfTable_g)

 }

}
r percentile
© www.soinside.com 2019 - 2024. All rights reserved.