第二章 計量資料的統(tǒng)計描述
知識清單:
求極差(range),做頻數(shù)分布表和頻數(shù)分布圖(graph of frequency distribution),算術(shù)平均數(shù)(mean),幾何均數(shù)(geometric mean)粉楚,中位數(shù)與百分位數(shù)(median and percentile),四分位間距(quartile range),方差(variance)徘跪,標準差(standard deviation),變異系數(shù)(coefficient of variance)琅攘,正態(tài)分布(normal distribution)垮庐,標準正態(tài)分布(standard normal distribution)
圖形美觀,5:7或7:5
使用R語言的內(nèi)建實例數(shù)據(jù)框: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代表火山噴發(fā)的持續(xù)時間坞琴,第二列代表距離下一次噴發(fā)的間隔時間
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. 頻數(shù)分布
>?duration?=?faithful$eruptions
>?breaks?=?seq(1.5,?5.2,?length.out = 12)
>?duration.cut?=?cut(duration,?breaks,?right=FALSE)
right屬性默認為TRUE哨查,表示每個組段為右閉左開的一個區(qū)間
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: 控制輸出小數(shù)點位數(shù)使用
> old?=?options(digits=1)
>?options(old)????#?restore?the?old?option
3. 算術(shù)平均
> mean(faithful$eruptions)
4. 幾何平均
> exp(mean(log(faithful$eruptions)))
> psych::geometric.mean(faithful$eruptions)
5. 中位數(shù)與百分位數(shù)
> 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((faithful$eruptions-mean(faithful$eruptions))^2))/(nrow(faithful)-1)
[1] 1.302728
> sd(faithful$eruptions)
[1] 1.141371
> sd(faithful$eruptions)^2
[1] 1.302728
8. 變異系數(shù)
> raster::cv(faithful$eruptions)
[1] 32.72483
> sd(faithful$eruptions)/mean(faithful$eruptions)*100
[1] 32.72483
9. 正態(tài)分布和標準正態(tài)分布
dnorm() 的返回值是正態(tài)分布概率密度函數(shù),pnorm()返回值是正態(tài)分布的分布函數(shù)剧辐。函數(shù)qnorm()的返回值是給定概率p后的下分位點寒亥,rnorm()的返回值是n個正態(tài)分布隨機數(shù)構(gòu)成的向量。
已知某正態(tài)分布均值為72荧关,標準差為15.2溉奕,求在72出的概率密度:
> dnorm(72, mean=72, sd=15.2)
[1] 0.0262462
已知某正態(tài)分布均值為72,標準差為15.2忍啤,求大于84的概率:
> pnorm(84, mean=72, sd=15.2, lower.tail=FALSE)
[1] 0.21492
已知某正態(tài)分布均值為0腐宋,標準差為1,求小于多少值時檀轨,其概率大于0.975
> qnorm(0.975, mean=0, sd=1, lower.tail=TRUE)
[1] 1.959964
生成服從正態(tài)分布胸竞,均值為0,標準差異1的100個數(shù):
> rnorm(100, mean=0, sd=1)
參考: