使用R进行描述性统计分析(连续性变量)
对于描述性统计来说,R可以实现的方法有很多,基础自带的有summary()
函数,还有其他packages,如Hmisc包,pastecs包,psych包提供了计算更多内容的函数。
基础函数
在R中,我们经常使用summary()
函数来计算最大值、最小值、四分位数、均值、频数等等。
data(mtcars)
myvars <- c("mpg", "hp", "wt")
summary(mtcars[myvars])
## --- output------
## NOT RUN:
> summary(mtcars[myvars])
mpg hp wt
Min. :10.40 Min. : 52.0 Min. :1.513
1st Qu.:15.43 1st Qu.: 96.5 1st Qu.:2.581
Median :19.20 Median :123.0 Median :3.325
Mean :20.09 Mean :146.7 Mean :3.217
3rd Qu.:22.80 3rd Qu.:180.0 3rd Qu.:3.610
Max. :33.90 Max. :335.0 Max. :5.424
一般而言,我们使用summary()
函数就可以得到我们想要的描述性统计量了。不过summary()
函数提供的统计量较少,有时候满足不了我们的需求,那么我们可以使用其他包中提供的函数来进行计算。
其他方法
Hmisc包
Hmisc包是一个包含了很多数据分析函数的包,包括样本量大小的计算,图标绘制,字符串操作,输出为LaTeX及HTML格式的文档等等。在这里可以查看更多详细的信息:
Contains many functions useful for data analysis, high-level graphics, utility operations, functions for computing sample size and power, importing and annotating datasets, imputing missing values, advanced table making, variable clustering, character string manipulation, conversion of R objects to LaTeX and html code, and recoding variables.
在Hmisc包中的describe()
函数提供了数量,缺失值,唯一值的数量,平均数,分位数,**基尼平均值(Geni mean difference, Gmd)**以及五个最大值和最小值:
library(Hmisc)
describe(mtcars[myvars])
## --- output---
## NOT RUN
> describe(mtcars[myvars])
mtcars[myvars]
3 Variables 32 Observations
--------------------------------------------------------------------------------
mpg
n missing distinct Info Mean Gmd .05 .10
32 0 25 0.999 20.09 6.796 12.00 14.34
.25 .50 .75 .90 .95
15.43 19.20 22.80 30.09 31.30
lowest : 10.4 13.3 14.3 14.7 15.0, highest: 26.0 27.3 30.4 32.4 33.9
--------------------------------------------------------------------------------
hp
n missing distinct Info Mean Gmd .05 .10
32 0 22 0.997 146.7 77.04 63.65 66.00
.25 .50 .75 .90 .95
96.50 123.00 180.00 243.50 253.55
lowest : 52 62 65 66 91, highest: 215 230 245 264 335
--------------------------------------------------------------------------------
wt
n missing distinct Info Mean Gmd .05 .10
32 0 29 0.999 3.217 1.089 1.736 1.956
.25 .50 .75 .90 .95
2.581 3.325 3.610 4.048 5.293
lowest : 1.513 1.615 1.835 1.935 2.140, highest: 3.845 4.070 5.250 5.345 5.424
--------------------------------------------------------------------------------
关于基尼平均值是什么,可以看这里的介绍了解更多相关的内容。
pastecs包
有时候我们想要知道标准差,值域