熱線電話:13121318867

登錄
首頁精彩閱讀R語言用于數據分析的基本統計函數與基礎可視化
R語言用于數據分析的基本統計函數與基礎可視化
2017-12-17
收藏

R語言用于數據分析的基本統計函數與基礎可視化

1.函數的基本用法

#將數據導入R中
> data(iris)   
#平均數
> mean(iris$Sepal.Length)
[1] 5.843333
#標準差
> sd(iris$Sepal.Length)
[1] 0.8280661
#方差
> var(iris$Sepal.Length)
[1] 0.6856935
#最小值
> min(iris$Sepal.Length)
[1] 4.3
#最大值
> max(iris$Sepal.Length)
[1] 7.9
#中位數
> median(iris$Sepal.Length)
[1] 5.8
#值的范圍
> range(iris$Sepal.Length)
[1] 4.3 7.9
#分位點
> quantile(iris$Sepal.Length)
  0%  25%  50%  75% 100%
 4.3  5.1  5.8  6.4  7.9
sapply()函數

#忽略缺失值(na.rm=TRUE),求iris數據框前4個屬性的平均值
> sapply(iris[1:4],mean,na.rm=TRUE)
Sepal.Length  Sepal.Width Petal.Length  Petal.Width
    5.843333     3.057333     3.758000     1.199333
#此處也可以用summary(iris)
> summary(iris)
  Sepal.Length    Sepal.Width     Petal.Length    Petal.Width          Species 
 Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100   setosa    :50 
 1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300   versicolor:50 
 Median :5.800   Median :3.000   Median :4.350   Median :1.300   virginica :50 
 Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199                 
 3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800                 
 Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500
變量間的關聯進行分析

             Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length    1.0000000  -0.1175698    0.8717538   0.8179411
Sepal.Width    -0.1175698   1.0000000   -0.4284401  -0.3661259
Petal.Length    0.8717538  -0.4284401    1.0000000   0.9628654
Petal.Width     0.8179411  -0.3661259    0.9628654   1.0000000
每一個屬性對的協方差系數cov(iris[,1:4])

> cov(iris[,1:4])
             Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length    0.6856935  -0.0424340    1.2743154   0.5162707
Sepal.Width    -0.0424340   0.1899794   -0.3296564  -0.1216394
Petal.Length    1.2743154  -0.3296564    3.1162779   1.2956094
Petal.Width     0.5162707  -0.1216394    1.2956094   0.5810063
統計檢驗可用來評估結果的顯著性,下例用t檢驗來分析樣本之間的差異,具體任務是用t檢驗判斷setosa與versicolor兩類鳶尾花的花萼寬度的差別。如果p<0.5,說明這兩類花的花萼寬度差別非常明顯:

> t.test(iris$Petal.Width[iris$Species=="setosa"],
+ iris$Petal.Width[iris$Species=="versicolor"])

    Welch Two Sample t-test

data:  iris$Petal.Width[iris$Species == "setosa"] and iris$Petal.Width[iris$Species == "versicolor"]
t = -34.08, df = 74.755, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -1.143133 -1.016867
sample estimates:
mean of x mean of y
    0.246     1.326
同樣也可以對鳶尾花的花萼寬度與花萼長度進行進行關聯性檢驗,評分越接近1,正向相關越強,反之亦然。

> cor.test(iris$Sepal.Length[iris$Species=="setosa"],
+        + iris$Sepal.Width[iris$Species=="setosa"])

    Pearson's product-moment correlation

data:  iris$Sepal.Length[iris$Species == "setosa"] and +iris$Sepal.Width[iris$Species == "setosa"]
t = 7.6807, df = 48, p-value = 6.71e-10
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.5851391 0.8460314
sample estimates:
      cor
0.7425467
基礎可視化

> data(iris)
> table.iris = table(iris$Species)
> table.iris

    setosa versicolor  virginica
        50         50      50
> pie(table.iris)

花萼分布的直方圖

> hist(iris$Sepal.Length)

盒圖,也稱箱圖

boxplot(Petal.Width ~ Species,data = iris)

散點圖,用在同一個圖中比較兩個變量關系的圖

plot(x=iris$Petal.Length,y=iris$Ptal.Width,col=iris$Species)

繪制所有有兩兩之間的關系圖
> pairs(iris[1:4],main = "IRIS DATE",pch=21,bg=c("red","green","blue")[unclass(iris$Species)])

數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢