R语言之描述性统计量

来源:互联网 发布:湖南省软件协会 编辑:程序博客网 时间:2024/04/20 10:28

1.导入pastecs包

stat.desc(mtcars[vars])

                     mpg           hp          wt
nbr.val       32.0000000   32.0000000  32.0000000
nbr.null       0.0000000    0.0000000   0.0000000
nbr.na         0.0000000    0.0000000   0.0000000
min           10.4000000   52.0000000   1.5130000
max           33.9000000  335.0000000   5.4240000
range         23.5000000  283.0000000   3.9110000
sum          642.9000000 4694.0000000 102.9520000
median        19.2000000  123.0000000   3.3250000
mean          20.0906250  146.6875000   3.2172500
SE.mean        1.0654240   12.1203173   0.1729685
CI.mean.0.95   2.1729465   24.7195501   0.3527715
var           36.3241028 4700.8669355   0.9573790
std.dev        6.0269481   68.5628685   0.9784574
coef.var       0.2999881    0.4674077   0.3041285

所有值、空值、缺失值、最小、最大、值域(范围)、总和、中位数、均值、均值标准误、均值置信度95%的置信区间、方差、标准差及变异系数

也可以导入psych包用describe来查看更详细的描述性统计量

> describe(mtcars[vars])
    vars  n   mean    sd median trimmed   mad   min    max  range skew
mpg    1 32  20.09  6.03  19.20   19.70  5.41 10.40  33.90  23.50 0.61
hp     2 32 146.69 68.56 123.00  141.19 77.10 52.00 335.00 283.00 0.73
wt     3 32   3.22  0.98   3.33    3.15  0.77  1.51   5.42   3.91 0.42
    kurtosis    se
mpg    -0.37  1.07
hp     -0.14 12.12
wt     -0.02  0.17

2.分组计算描述性统计量

解释:一个变量取值在1,2,3,4,5可以根据这个变量分为5组进行描述性统计量可以值均值、方差,可以是多个变量进行分组

> aggregate(mtcars[vars],by=list(carb=mtcars$carb),mean)
  carb      mpg    hp     wt
1    1 25.34286  86.0 2.4900
2    2 22.40000 117.2 2.8628
3    3 16.30000 180.0 3.8600
4    4 15.79000 187.0 3.8974
5    6 19.70000 175.0 2.7700
6    8 15.00000 335.0 3.5700
> mtcars$carb
 [1] 4 4 1 1 2 1 4 2 2 4 4 3 3 3 4 4 4 1 2 1 1 2 2 4 2 1 2 2 4 6 8 2
> aggregate(mtcars[vars],by=list(carb=mtcars$carb,am=mtcars$am),mean)
  carb am      mpg       hp       wt
1    1  0 20.33333 104.0000 3.046667
2    2  0 19.30000 134.5000 3.430000
3    3  0 16.30000 180.0000 3.860000
4    4  0 14.30000 198.0000 4.329857
5    1  1 29.10000  72.5000 2.072500
6    2  1 27.05000  91.2500 2.012000
7    4  1 19.26667 161.3333 2.888333
8    6  1 19.70000 175.0000 2.770000
9    8  1 15.00000 335.0000 3.570000

导入reshape包

library("reshape", lib.loc="D:/rInstall/R-3.2.1/library")
> dstats <- function(x)(c(n=length(x),mean=mean(x),sd=sd(x)))
> dfm <- melt(mtcars,measure.vars = c("mpg","hp","wt"),id.vars = c("am","cyl"))
> cast(dfm,am+cyl+variable~.,dstats)
   am cyl variable  n       mean         sd
1   0   4      mpg  3  22.900000  1.4525839
2   0   4       hp  3  84.666667 19.6553640
3   0   4       wt  3   2.935000  0.4075230
4   0   6      mpg  4  19.125000  1.6317169
5   0   6       hp  4 115.250000  9.1787799
6   0   6       wt  4   3.388750  0.1162164
7   0   8      mpg 12  15.050000  2.7743959
8   0   8       hp 12 194.166667 33.3598379
9   0   8       wt 12   4.104083  0.7683069
10  1   4      mpg  8  28.075000  4.4838599
11  1   4       hp  8  81.875000 22.6554156
12  1   4       wt  8   2.042250  0.4093485
13  1   6      mpg  3  20.566667  0.7505553
14  1   6       hp  3 131.666667 37.5277675
15  1   6       wt  3   2.755000  0.1281601
16  1   8      mpg  2  15.400000  0.5656854
17  1   8       hp  2 299.500000 50.2045815
18  1   8       wt  2   3.370000  0.2828427

measure.vars = c("mpg","hp","wt")为变量,id.vars = c("am","cyl")为分组。

3.频数

table(),频数检测主要用于类别型变量(即名义型变量)

4.独立性检测

独立性主要用于检测变量之间是否存在独立性

当p-value<0.01说明变量之间存在某种关系,当p-value>0.05,不存在关系(即独立)

卡方独立性检测

> mytable <- xtabs(~Treatment+Improved,data=Arthritis)
> chisq.test(mytable)


Pearson's Chi-squared test


data:  mytable
X-squared = 13.055, df = 2, p-value = 0.001463


> mytable <- xtabs(~Improved+Sex,data=Arthritis)
> chisq.test(mytable)


Pearson's Chi-squared test


data:  mytable
X-squared = 4.8407, df = 2, p-value = 0.08889

Fisher精准检验

> mytable <- xtabs(~Treatment+Improved,data = Arthritis)
> fisher.test(mytable)

注意不能用2*2列联表

Cochran-Mantel-Haenszel检验

其原假设是2个名义变量在第三个变量的每一层都是条件独立的

> mytable <- xtabs(~Treatment+Improved+Sex,data = Arthritis)
> mantelhaen.test(mytable)


Cochran-Mantel-Haenszel test


data:  mytable
Cochran-Mantel-Haenszel M^2 = 14.632, df = 2, p-value = 0.0006647

0 0