加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

叙述性统计(DescriptiveStatistics)

(2018-05-14 01:26:06)
分类: R基礎

叙述性统计 (Descriptive Statistics)

 

1. 使用时机: 拿到数据时对数据的某些基本特征进行分析了解。

 

2. 分析类型: 数据基本特性分析。

 

3. 数据范例: 咪路调查淡水河口弹涂鱼的体长(cm),资料如下:

   14.3, 15.8, 14.6, 16.1, 12.9, 15.1, 17.3, 14.0, 14.5, 13.9, 16.2, 14.3, 14.6, 13.3, 15.5, 11.8, 14.8, 13.5, 16.3, 15.4, 15.5, 13.9, 10.7, 14.8, 12.9, 15.4

 

4. 输入数据放进R的基本数据结构:

步骤: 用小c将数据放入名称为lenvector (R最基本数据结构)

      len <- c(14.3, 15.8, 14.6, 16.1, 12.9, 15.1, 17.3, 14.0, 14.5, 13.9, 16.2, 14.3, 14.6, 13.3, 15.5, 11.8, 14.8, 13.5, 16.3, 15.4, 15.5, 13.9, 10.7, 14.8, 12.9, 15.4)

 

5. 数据的最大值、最小值及范围(maximum, minimum and Range):

步骤: 使用基础模块(base)maxminrange函数。

max(len)

[1] 17.3   # 最大值

min(len)

[1] 10.7   # 最小值

range(len)

[1] 10.7 17.3  # 范围

 

6. 资料的平均值、中数、众数(Measure of central tendency: mean, median, mode)

步骤一: 使用基础模块(base)meanmedian函数。

mean(len)

[1] 14.51538   # 平均值

median(len)

[1] 14.6    # 中数

       步骤二:

第一步: 安装modeest程序套件。

        第二步: 呼叫modeest程序套件备用。

         library(modeest)

        第三步: 阅读modeest程序套件的函数mvf的使用说明。

help(mfv)

        第四步: 使用modeest程序套件的函数mvf求众数。

mfv(len)

[1] 12.9 13.9 14.3 14.6 14.8 15.4 15.5   # 众数

 

7. 资料的四分位数(Quartiles and the Interquartile range (IQR)):

步骤: 使用基础模块(base)quantileIQR函数。

quantile(len)

              0%    25%    50%    75%   100%

10.700  13.900  14.600  15.475  17.300

  IQR(len)

[1] 1.575   # IQR

 

8. 数据的变异数、标准偏差及标准误差(variance, standard deviation, standard error):

步骤一: 使用基础模块(base)varsd函数。

var(len)

[1] 2.109354   # 变异数

sd(len)

[1] 1.452361    # 标准偏差

      步骤二: 计算标准误差储存到变量sem

sem <- sd(len)/sqrt(length(len))

sem

[1] 0.2848315   # 标准误差

# sqrt函数功能为开方(根号)

# length函数功能为取得len中数据个数(样本数)

步骤三: 计算平均值的95%信赖区间(95% confidence intervals of the mean)

 假设数据为常态分布: z = 1.96

ci95 <- c(mean(len) - 1.96 * sem, mean(len) + 1.96 * sem)

ci95

[1] 13.95711 15.07365  # 平均值的95%信赖区间

      步骤四: 计算变异系数(Coefficient of Variation; CV)

CV <- (sd(len) / mean(len)) * 100

CV

[1] 10.00567

 

9. 绝对中位差(median absolute deviation; MAD):

步骤: 使用基础模块(base)mad函数。

mad(len)

[1] 1.26021

 

10. 资料总整理:

步骤: 使用基础模块(base)summary函数。

summary(len)

     Min. 1st Qu.  Median    Mean 3rd Qu.    Max.

    10.70   13.90   14.60   14.52   15.47   17.30

 

11. 画图看数据分布方法一:

   第一步: 用小c将数据放入名称为Lenvector (R最基本数据结构)。用rep函数产生与数据相同数目的(26)大写F放入名称为Fishvector,再组合成名称为datdata frame

    Len <- c(14.3, 15.8, 14.6, 16.1, 12.9, 15.1, 17.3, 14.0, 14.5, 13.9, 16.2, 14.3, 14.6, 13.3, 15.5, 11.8, 14.8, 13.5, 16.3, 15.4, 15.5, 13.9, 10.7, 14.8, 12.9, 15.4)

    Fish <- rep("F", 26)

dat <- data.frame(Len, Fish)

   第二步: 安装ggplot2程序套件。

   第三步: 呼叫ggplot2程序套件备用。

    library(ggplot2)

   第四步: 画图。

    ggplot(dat, aes(x = Fish, y = Len)) +

     geom_boxplot(color = "red")+

     geom_jitter(position = position_jitter(0.05))

    # 同时画x-y散布(黑色点)图及盒图(红色box plot)

    # ggplot2程序套件geom_jitter函数让重迭(数值相同)的数据点错开,避免误判。

http://s9/mw690/0078lazCzy7krqiCw0E18&690

   # 如图所示这组资料有离群值(outlier)

 

12. 画图看数据分布方法二:

   第一步: 用小c将数据放入名称为Lenvector (R最基本数据结构)。用rep函数产生与数据相同数目的(26)大写F放入名称为Fishvector,再组合成名称为datdata frame

    Len <- c(14.3, 15.8, 14.6, 16.1, 12.9, 15.1, 17.3, 14.0, 14.5, 13.9, 16.2, 14.3, 14.6, 13.3, 15.5, 11.8, 14.8, 13.5, 16.3, 15.4, 15.5, 13.9, 10.7, 14.8, 12.9, 15.4)

    Fish <- rep("F", 26)

dat <- data.frame(Len, Fish)

   第二步: 安装ggpubr程序套件。

   第三步: 呼叫ggpubr程序套件备用。

    library(ggpubr)

   第四步: 使用ggpubr程序套件的gghistogram画分布(直方)图。

gghistogram(dat, x = "Len", bins = 9, add = "mean")

    # bins = 9是使用者自定。

    # add = "mean"加上平均值位置线。

http://s6/mw690/0078lazCzy7krqkVXJre5&690

第五步: 使用ggpubr程序套件的ggecdf画累加曲线(Empirical cumulative distribution curve)

ggecdf(dat, x = "Len")

http://s15/mw690/0078lazCzy7krqmcezs4e&690 

来劲了吗? 想知道更多?? 补充资料(链接):

1. Mean (https://en.wikipedia.org/wiki/Mean)

2. Median (https://en.wikipedia.org/wiki/Median)

3. Mode (https://en.wikipedia.org/wiki/Mode_(statistics))

4. Variance (https://en.wikipedia.org/wiki/Variance)

5. Standard deviation (https://en.wikipedia.org/wiki/Standard_deviation)

6. Standard error (https://en.wikipedia.org/wiki/Standard_error)

7. Coefficient of variation (https://en.wikipedia.org/wiki/Coefficient_of_variation)

8. Quantile (https://en.wikipedia.org/wiki/Quantile)

9. Box plot (https://en.wikipedia.org/wiki/Box_plot)

10. Confidence interval (https://en.wikipedia.org/wiki/Confidence_interval)

11. Median absolute deviation (https://en.wikipedia.org/wiki/Median_absolute_deviation)

12. Empirical distribution function (https://en.wikipedia.org/wiki/Empirical_distribution_function)

13. 关于R基础,R绘图及统计快速入门:

   a. R Tutorial: https://www.tutorialspoint.com/r/index.htm

   b. Cookbook for R: http://www.cookbook-r.com/

   c. Quick-R: https://www.statmethods.net/

   d. Statistical tools for high-throughput data analysis (STHDA): http://www.sthda.com/english/ 

   e. The Handbook of Biological Statistics: http://www.biostathandbook.com/ 

   f. An R Companion for the Handbook of Biological Statistics: http://rcompanion.org/rcompanion/index.html

14. Zar, JH. 2010. Biostatistical Analysis, Fifth Edition, Pearson.

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有