首页 > 解决方案 > 如何在 R 中实现 SAS 百分位语句?

问题描述

我有这样的 SAS 声明:

proc univariate data = df noprint;
class &var1. &var2.;
var &var3.;
output out = STAT PCTLPTS = 2 5 98 99 95 PCTLPRE = P;

我有这样的输出SAS proc

在此处输入图像描述

如何在 R 中获得相同的结果?(每行有5个P列和值)

标签: rsasstatisticsquantilepercentile

解决方案


查看quantile()功能。https://stat.ethz.ch/R-manual/R-devel/library/stats/html/quantile.html

有一个type基于该SAS方法的潜在分布分位数计算。

类型 3 - SAS 定义:最接近偶数阶统计。如果 g = 0 且 j 为偶数,则 γ = 0,否则为 1。

像这样使用函数:

quantile(iris$Sepal.Length, probs = c(.02, .05, .98, .99, .95), type=3)
 2%  5% 98% 99% 95% 
4.4 4.6 7.7 7.7 7.2 

您可以使用各种方法,例如对多个变量sapply()执行。quantile()前任。

 t(sapply(iris[1:4], quantile, probs = c(.02, .05, .98, .99, .95), type=3))
              2%  5% 98% 99% 95%
Sepal.Length 4.4 4.6 7.7 7.7 7.2
Sepal.Width  2.2 2.3 4.0 4.1 3.8
Petal.Length 1.2 1.3 6.6 6.7 6.1
Petal.Width  0.1 0.2 2.4 2.5 2.3

推荐阅读