统计学第二章 计量资料的统计描述

作者: x2yline | 来源:发表于2017-10-16 17:34 被阅读20次

    第二章 计量资料的统计描述

    知识清单:

    求极差(range),做频数分布表和频数分布图(graph of frequency distribution),算术平均数(mean),几何均数(geometric mean),中位数与百分位数(median and percentile),四分位间距(quartile range),方差(variance),标准差(standard deviation),变异系数(coefficient of variance),正态分布(normal distribution),标准正态分布(standard normal distribution)

    图形美观,5:7或7:5

    使用R语言的内建实例数据框:faithful

    > head(faithful)
    
    eruptions waiting
    
    1     3.600      79
    
    2     1.800      54
    
    3     3.333      74
    
    4     2.283      62
    
    5     4.533      85
    
    6     2.883      55
    

    第一列eruptions代表火山喷发的持续时间,第二列代表距离下一次喷发的间隔时间

    1. 计算极差(range)

    > duration = faithful$eruptions     # the eruption durations
    
    > max(duration)−min(duration)     # apply the max and min functions
    
    [1] 3.5
    
    > range(duration)
    
    [1] 1.6 5.1
    

    2. 频数分布

    > duration = faithful$eruptions
    
    > breaks = seq(1.5, 5.2, length.out = 12)
    
    > duration.cut = cut(duration, breaks, right=FALSE)
    

    right属性默认为TRUE,表示每个组段为右闭左开的一个区间

    duration.cut为一个factor变量

    > duration.freq = table(duration.cut)
    
    > duration.freq
    
    > cbind(duration.freq)
    

    计算频率

    > duration.relfreq = duration.freq / nrow(faithful)
    

    展示为列的形式

    > cbind(duration.freq)
    
    >hist(duration, right=FALSE, breaks = breaks, labels =TRUE, freq = FALSE, col = "lightgray", border = "white")
    

    tips: 控制输出小数点位数使用

    > old = options(digits=1)
    
    > options(old)    # restore the old option
    

    3. 算术平均

    > mean(faithful$eruptions)
    

    4. 几何平均

    > exp(mean(log(faithful$eruptions)))
    
    > psych::geometric.mean(faithful$eruptions)
    

    5. 中位数与百分位数

    > quantile(faithful$eruptions, c(0.5, 0.6))
    
    50%  60%
    
    4.000 4.167
    
    > median(faithful$eruptions)
    
    [1] 4
    
    > quantile(faithful$eruptions)
    
    0%    25%    50%    75%    100%
    
    1.60000 2.16275 4.00000 4.45425 5.10000
    

    6. 四分位间距 interquartile range

    > IQR(faithful$eruptions)
    
    [1] 2.2915
    

    7. 方差与标准差

    > var(faithful$eruptions)
    
    [1] 1.302728
    
    > (sum((faithfuleruptions-mean(faithfuleruptions))^2))/(nrow(faithful)-1)
    
    [1] 1.302728
    
    > sd(faithful$eruptions)
    
    [1] 1.141371
    
    > sd(faithful$eruptions)^2
    
    [1] 1.302728
    
    

    8. 变异系数

    > raster::cv(faithful$eruptions)
    
    [1] 32.72483
    
    > sd(faithfuleruptions)/mean(faithfuleruptions)*100
    
    [1] 32.72483
    

    9. 正态分布和标准正态分布

    dnorm() 的返回值是正态分布概率密度函数,pnorm()返回值是正态分布的分布函数。函数qnorm()的返回值是给定概率p后的下分位点,rnorm()的返回值是n个正态分布随机数构成的向量。

    已知某正态分布均值为72,标准差为15.2,求在72出的概率密度:

    > dnorm(72, mean=72, sd=15.2)
    
    [1] 0.0262462
    

    已知某正态分布均值为72,标准差为15.2,求大于84的概率:

    > pnorm(84, mean=72, sd=15.2, lower.tail=FALSE)
    
    [1] 0.21492
    

    已知某正态分布均值为0,标准差为1,求小于多少值时,其概率大于0.975

    > qnorm(0.975, mean=0, sd=1, lower.tail=TRUE)
    
    [1] 1.959964
    

    生成服从正态分布,均值为0,标准差异1的100个数:

    > rnorm(100, mean=0, sd=1)
    

    参考:

    http://www.r-tutor.com/elementary-statistics

    相关文章

      网友评论

        本文标题:统计学第二章 计量资料的统计描述

        本文链接:https://www.haomeiwen.com/subject/tbhluxtx.html