R 數據分析方法(梅長林)exercise1-3

首先安裝幾個R數據分析的包

funModeling:探索性數據分析(EDA)、數據準備和模型性能評估

tidyverse:數據科學集成包

Hmisc:高級計算函數、繪圖功能等

matrixStats:數據分析

ggplot2:畫圖

載入需要的軟件包

# library(funModeling)
# library(tidyverse)
# library(Hmisc)
# library(matrixStats)
library(fBasics) ## 計算偏度峯度
library(ggplot2)
Loading required package: timeDate

Loading required package: timeSeries

讀取文件

source<-read.table("exersice1_3.txt", header = TRUE, fileEncoding = "UTF8");
#數據概覽
head(source)
A data.frame: 6 × 4
年份全國居民農村居民城鎮居民
<int><int><int><int>
11978184138405
21979207158434
31980236178496
41981262199562
51982284221576
61983311246603

(1)求均值、方差、標準差、變異係數、偏度、峯度

## 均值
n = dim(source)[1] # 行數
AA = source[, 2:4]
ColMean <- apply(AA,2,mean)
ColMean
全國居民
1117
農村居民
747.863636363636
城鎮居民
2336.40909090909
## 方差
ColVar <- apply(AA,2,var)
ColVar
全國居民
1031680.28571429
農村居民
399673.837662338
城鎮居民
4536136.44372294
## 標準差
ColStd <- apply(AA,2,sd)
ColStd
全國居民
1015.71663652531
農村居民
632.197625479832
城鎮居民
2129.82075389525
## 變異係數
ColCV <- ColStd/ColMean * 100
ColCV
全國居民
90.932554747118
農村居民
84.5338100076357
城鎮居民
91.157869663422

偏度公式:

g1=n(n1)(n2)1s3i=1n(xxˉ)3g1 =\frac{n}{(n-1)(n-2)} \frac{1}{s ^3}\sum _{i=1} ^n (x-\bar x)^3

峯度公式:
g2=n(n+1)(n1)(n2)(n3)1s4i=1n(xix)43(n1)2(n2)(n3)g2 =\frac{n(n+1)}{(n-1)(n-2)(n-3)}\frac{1}{s^4}\sum^n _{i=1} (x_i -x )^4- \frac{3(n-1)^2}{(n-2)(n-3)}

## 利用宏包fBasics計算偏度
skewness(AA)
全國居民
0.889330205728174
農村居民
0.878668808067427
城鎮居民
0.842138275058679
## 自定義函數計算公式
pianD<-function(xx=AA){
  yy=sum((xx-mean(xx))^3)/sd(xx)^3*n/(n-1)/(n-2);
  return(yy)
}
apply(AA, 2, pianD)
全國居民
1.02484718945818
農村居民
1.01256119786818
城鎮居民
0.970464107448573
## 利用宏包fBasics計算峯度
kurtosis(AA)
全國居民
-0.831996482068152
農村居民
-0.8278408069353
城鎮居民
-0.915094641514999
## 自定義函數計算峯度
fengD<-function(xx=AA){
  aFeng=n*(n+1)/(n-1)/(n-2)/(n-3);
  bFeng=3*(n-1)^2/(n-2)/(n-3);
  yy=sum((xx-mean(xx))^4)/sd(xx)^4*aFeng-bFeng;
  return(yy)
}
apply(AA, 2, fengD)
全國居民
-0.457241207817379
農村居民
-0.45144409308318
城鎮居民
-0.57316209891541

可以看出調用宏包計算結果與自定義函數計算結果不同,可能宏包不是無偏估計?

## 畫出核密度圖
ggplot(source, aes(x=全國居民)) + geom_density(fill="#f4876a",colour="black",alpha=0.6,size=1) + geom_vline(data=source, aes(xintercept=mean(source[,2])),
                linetype="dashed", size=1)
ggplot(source, aes(x=農村居民)) + geom_density(fill=9,colour="black",alpha=0.6,size=1) + geom_vline(data=source, aes(xintercept=mean(source[,3])),
                linetype="dashed", size=1)
ggplot(source, aes(x=城鎮居民)) + geom_density(fill=8,colour="black",alpha=0.6,size=1) + geom_vline(data=source, aes(xintercept=mean(source[,4])),
                linetype="dashed", size=1)

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-79LT0uqx-1586140673139)(output_16_0.png)]

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-CULlHxTE-1586140673141)(output_16_1.png)]

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-dImJ77Rd-1586140673143)(output_16_2.png)]

由圖可以看出均屬於左偏型的,即偏度大於零

## 畫出各列同方差正太分佈於0-1正太分佈的比較圖
Norm = data.frame('全國居民'= rnorm(50000, mean = 0, sd = sd(source[,1])), norm = rnorm(50000, mean=0, sd=1))
ggplot(Norm) + geom_density(aes(x=全國居民), fill="#f4876a",colour="black",alpha=0.6,size=1) + geom_density(aes(x=norm), fill=9,colour="black",alpha=0.6,size=1)
Norm = data.frame('農村居民'= rnorm(50000, mean = 0, sd = sd(source[,2])), norm = rnorm(50000, mean=0, sd=1))
ggplot(Norm, aes(x=農村居民)) + geom_density(fill="#f4876a",colour="black",alpha=0.6,size=1) + geom_density(aes(x=norm), fill=9,colour="black",alpha=0.6,size=1)
Norm = data.frame('城鎮居民'= rnorm(50000, mean = 0, sd = sd(source[,3])), norm = rnorm(50000, mean=0, sd=1))
ggplot(Norm, aes(x=城鎮居民)) + geom_density(fill="#f4876a",colour="black",alpha=0.6,size=1) + geom_density(aes(x=norm), fill=9,colour="black",alpha=0.6,size=1)

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-EvaVbT9u-1586140673146)(output_18_0.png)]

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-Uz83z9Ce-1586140673147)(output_18_1.png)]

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-E4hRODwD-1586140673148)(output_18_2.png)]

由圖可以看出均屬於細尾型的,即峯度小於零

(2)求中位數、上下四分位數、四分位極差、三均值

## 分位數
summary(AA)
    全國居民         農村居民         城鎮居民     
 Min.   : 184.0   Min.   : 138.0   Min.   : 405.0  
 1st Qu.: 321.8   1st Qu.: 255.2   1st Qu.: 617.8  
 Median : 727.5   Median : 530.5   Median :1499.5  
 Mean   :1117.0   Mean   : 747.9   Mean   :2336.4  
 3rd Qu.:1642.2   3rd Qu.:1052.2   3rd Qu.:3675.0  
 Max.   :3180.0   Max.   :1973.0   Max.   :6651.0  
## 四分位極差
Q1 <- unname(quantile(AA[,1], probs=c(0.25, 0.5, 0.75)))
Q2 <- unname(quantile(AA[,2], probs=c(0.25, 0.5, 0.75)))
Q3 <- unname(quantile(AA[,3], probs=c(0.25, 0.5, 0.75)))
R1 <- Q1[3] - Q1[1]
R2 <- Q2[3] - Q2[1]
R3 <- Q3[3] - Q3[1]
R1; R2; R3;

1320.5

797

3057.25

## 三均值
M1 = 1/4*Q1[1] + 1/2*Q1[2] + 1/4*Q1[3]
M2 = 1/4*Q2[1] + 1/2*Q2[2] + 1/4*Q2[3]
M3 = 1/4*Q3[1] + 1/2*Q3[2] + 1/4*Q3[3]
M1; M2; M3

854.75

592.125

1822.9375

(3)作出直方圖

## 直方圖
ggplot(source, aes(x=全國居民)) + geom_histogram(position= 'identity', stat="bin", binwidth=600) 
ggplot(source, aes(x=農村居民)) + geom_histogram(position= 'identity', stat="bin", binwidth=600) 
ggplot(source, aes(x=城鎮居民)) + geom_histogram(position= 'identity', stat="bin", binwidth=800) 

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-pbHcSIP2-1586140673149)(output_25_0.png)]

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-hbBlfpwH-1586140673150)(output_25_1.png)]

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-TP8mUMsK-1586140673152)(output_25_2.png)]

(4)作出莖葉圖

## 莖葉圖
stem(AA[,1],scale = 2,width=80,atom = 1e-08)
stem(AA[,2],scale = 2,width=80,atom = 1e-08)
stem(AA[,3],scale = 2,width=80,atom = 1e-08)
sink()
  The decimal point is 3 digit(s) to the right of the |

  0 | 22233344
  0 | 567889
  1 | 13
  1 | 7
  2 | 3
  2 | 68
  3 | 02


  The decimal point is 2 digit(s) to the right of the |

   0 | 468
   2 | 025858
   4 | 2157
   6 | 22
   8 | 6
  10 | 2
  12 | 
  14 | 3
  16 | 7
  18 | 807


  The decimal point is 3 digit(s) to the right of the |

  0 | 445666789
  1 | 14679
  2 | 4
  3 | 09
  4 | 9
  5 | 48
  6 | 27

(5)找出異常值

##計算上下截斷點
except1 = list(Q1[1] - 1.5*R1, Q1[3] + 1.5*R1)
except2 = list(Q2[1] - 1.5*R2, Q2[3] + 1.5*R2)
except3 = list(Q3[1] - 1.5*R3, Q3[3] + 1.5*R3)
except1; except2; except3
  1. -1659
  2. 3623
  1. -940.25
  2. 2247.75
  1. -3968.125
  2. 8260.875
AA[, 1][AA[, 1] > except1[2] || AA[, 1] < except1[1]] 
AA[, 2][AA[, 2] > except2[2] || AA[, 2] < except2[1]]
AA[, 3][AA[, 3] > except3[2] || AA[, 3] < except3[1]]

可以看到都爲空值,所以沒有異常值

boxplot(AA[, 1])$out

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-7BQjj2HO-1586140673153)(output_34_1.png)]

boxplot(AA[, 2])$out

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-TLBhv06X-1586140673154)(output_35_1.png)]

boxplot(AA[, 3])$out

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-oiH0RBFk-1586140673156)(output_36_1.png)]

由箱圖也可以看出沒有異常點

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章