第3章:基本统计分析
案例引入
根据相关的行业报告,2018年中国在线短租用户规模达到1.47亿人,2020年有望突破3亿人,其中有大量租房需求的高校毕业生人数正在逐年上升。目前租赁房源出现向一线城市集中的趋势,随着90后“只租不买”的观念逐渐流行,以及租房政策的进一步完善,未来北上深等一线城市租房人群比例或将超过40%。在如此大规模的租房市场需求的驱动下,在线租房平台更是如雨后春笋般蓬勃发展。更多的年轻人不再通过中介租房,转而在线上平台选择房源。不仅如此,由于经济条件限制,租房的模式也发生了变化,由传统的整租转变为性价比更高的合租。
面对海量的房源,众多的合租房用户怎么才能找到物美价廉的合租房呢?这需要对租房市场深入调研。比如,需要了解:目前在租的房源价格一般水平是多少?不同城区之间差异如何?如果想租一间北京海淀区邻近地铁线的20平米左右的卧室,那么月租大概多少?这些问题都可以通过基本描述分析作答。
本案例的数据来源于某租房平台的租房数据集,共采集了北京市某年某月5149条合租房源的信息。具体变量说明表如下表所示:
| 变量类型 | 变量名 | 详细说明 | 取值范围 | |
|---|---|---|---|---|
| 月租金 | rent | 月租金 | [1150,6460] | |
| 租房信息 | 内部结构 | area | 租赁房间面积 | [5,30] | 
| room | 租赁房间类型 | 主卧、次卧 | ||
| bedroom | 卧室数 | [2,5] | ||
| livingroom | 厅数 | [1,2] | ||
| bathroom | 卫生间数 | [1,2] | ||
| heating | 供暖方式 | 集中供暖、自采暖 | ||
| 外部条件 | floor_grp | 所在楼层分组 | 高楼层、中楼层、低楼层 | |
| subway | 邻近地铁 | 是、否 | ||
| region | 城区 | 朝阳、海淀、东城、西城、昌平、大兴、通州、石景山、丰台、顺义、房山 | 
rm(list = ls())#清理工作空间
tenement = read.csv("./data/rent.csv",fileEncoding = "gbk",stringsAsFactors = FALSE)#导入数据3.1 基本描述统计量
以下从频数统计开始,一一介绍均值、分位数、方差、标准差、最大最小值、峰度、偏度等基本统计量。
3.1.1 频数统计
频数(freguency)统计是对定性变量的各水平计数进行统计的描述统计量。
(type<- table(tenement$region))#频数统计## 
##   昌平   朝阳   大兴   东城   房山   丰台   海淀 石景山   顺义   通州   西城 
##    702   1317    361     94    183    581    424    258    314    819     96tenement$regiontype=tenement$region#设置新变量
tenement[!is.element(tenement$region,c("朝阳","通州","昌平","丰台")),]$regiontype="其他"#将非朝阳、通州、昌平和丰台的租赁房间合并为为其他
sort(table(tenement$regiontype))## 
## 丰台 昌平 通州 朝阳 其他 
##  581  702  819 1317 17303.1.2 均值
均值(mean),即一组数值型数据的平均数,是最常用的统计量之一。
mean(tenement$rent)#求房租均值## [1] 2917.9tenement_num=tenement[c("rent","area")]#选取数值型子集
colMeans(tenement_num)#同时计算列和##       rent       area 
## 2917.89989   12.851433.1.3 分位数
在数据分布偏度较大的情况下,均值并不能很好地反映数据的一般水平。此时,可使用中位数(median)刻画数据的一般情形。中位数是分位数(quantile)的一个特殊情况。
median(tenement$rent)#求租金的中位数## [1] 2700quantile(tenement$rent, probs = 0.5)#利用quantile()函数求中位数##  50% 
## 2700quantile(tenement$rent, probs = seq(0, 1, 0.25))#求向量最小值、最大值、四分位数##       0%      25%      50%      75%     100% 
## 1343.001 2448.000 2700.000 3327.316 6547.0423.1.4 方差和标准差
方差(variance)和标准差(standard deviation)是描述一组数值型数据的分散程度的描述统计量。协方差(covariance)和相关系数(correlation coefficient)是描述变量相关程度的描述统计量。
mean(tenement$rent[tenement$regiontype == "昌平"])#昌平区租金均值## [1] 2801.377var(tenement$rent[tenement$regiontype == "昌平"])#昌平区租金方差## [1] 292458.5mean(tenement$rent[tenement$regiontype == "其他"])#其他区租金均值## [1] 2835.275var(tenement$rent[tenement$regiontype == "其他"])#其他区租金方差## [1] 636694.4sd(tenement$rent[tenement$regiontype == "昌平"])#昌平区租金标准差## [1] 540.7943sd(tenement$rent[tenement$regiontype == "其他"])#其他区租金标准差## [1] 797.9314cov(tenement$rent, tenement$area)#协方差## [1] 1366.097cor(tenement$rent, tenement$area)#相关系数## [1] 0.4474291cov(tenement_num)#协方差矩阵##            rent       area
## rent 536375.336 1366.09651
## area   1366.097   17.37983cor(tenement_num)#相关系数矩阵##           rent      area
## rent 1.0000000 0.4474291
## area 0.4474291 1.00000003.1.5 最大值、最小值
最大值(maximum value)、最小值(minimum value)描述一组数值型变量由高到低排序后最大和最小的元素。
max(tenement$rent)#租金最高值## [1] 6547.042min(tenement$rent)#租金最低值## [1] 1343.001a=which.max(tenement$rent)#找出租金最高的房间对应数据所在行
tenement[a,]#输出这一行的内容##         X     rent bedroom livingroom bathroom area room floor_grp subway region  heating regiontype
## 4821 4821 6547.042       4          1        1   27 主卧    中楼层     是   朝阳 集中供暖       朝阳3.1.6 峰度和偏度
偏度(skewness)和峰度(kurtosis)是一组用于描述数据分布形态的统计量。
library(e1071)#加载包skewness(tenement$rent)#用e1071包求租金的偏度## [1] 0.9925526
## attr(,"method")
## [1] "moment"kurtosis(tenement$rent) #用e1071包求租金的峰度## [1] 1.08197
## attr(,"method")
## [1] "excess"3.2 交叉分组下的频数分析
rm(list = ls())#清理工作空间
tenement = read.csv("./data/rent.csv",fileEncoding = "gbk",stringsAsFactors = FALSE)#导入数据3.2.1 交叉列联表
交叉列联表(contingency table)是对两个类别变量每个交叉水平下的频数统计。交叉列联表可以展示两个类别变量之间的相关关系。
tenement$regiontype=tenement$region#设置新变量
tenement[!is.element(tenement$region,c("朝阳","通州","昌平","丰台")),]$regiontype="其他"#将非朝阳、通州、昌平和丰台的租赁房间合并为为其他
(tab <- table(tenement$regiontype,tenement$floor_grp))#建立城区和楼层的交叉列联表##       
##        低楼层 高楼层 中楼层
##   昌平    208    215    279
##   朝阳    477    398    442
##   丰台    184    185    212
##   其他    549    555    626
##   通州    261    239    319prop.table(tab)#频率形式交叉列联表##       
##            低楼层     高楼层     中楼层
##   昌平 0.04039619 0.04175568 0.05418528
##   朝阳 0.09263935 0.07729656 0.08584191
##   丰台 0.03573509 0.03592931 0.04117304
##   其他 0.10662265 0.10778792 0.12157701
##   通州 0.05068945 0.04641678 0.06195378margin.table(tab,1)#求行变量边际函数## 
## 昌平 朝阳 丰台 其他 通州 
##  702 1317  581 1730  819margin.table(tab,2)#求列变量边际函数## 
## 低楼层 高楼层 中楼层 
##   1679   1592   1878addmargins(tab)#添加边际和##       
##        低楼层 高楼层 中楼层  Sum
##   昌平    208    215    279  702
##   朝阳    477    398    442 1317
##   丰台    184    185    212  581
##   其他    549    555    626 1730
##   通州    261    239    319  819
##   Sum    1679   1592   1878 51493.2.2 描述统计量的分组统计
描述统计量的分组统计是指对定量变量按照类别变量各取值水平分组后,进行描述统计的操作。
library(dplyr)
tenement_group<-group_by(tenement,regiontype,floor_grp)
summarise(tenement_group,
          meanrent=mean(rent),#分组计算平均平均租金
          maxrent=max(rent),#分组计算最高租金
          minrent=min(rent))#分组计算最低租金## # A tibble: 15 x 5
## # Groups:   regiontype [5]
##    regiontype floor_grp meanrent maxrent minrent
##    <chr>      <chr>        <dbl>   <dbl>   <dbl>
##  1 昌平       低楼层       2922.   4659    1575.
##  2 昌平       高楼层       2800.   4644    1814.
##  3 昌平       中楼层       2712.   4613.   1703.
##  4 朝阳       低楼层       3370.   5681.   2023.
##  5 朝阳       高楼层       3443.   5939.   2219.
##  6 朝阳       中楼层       3379.   6547.   2119.
##  7 丰台       低楼层       2834.   4488    1849 
##  8 丰台       高楼层       2857.   4688.   1778.
##  9 丰台       中楼层       2827.   4524    1712.
## 10 其他       低楼层       2838.   5828.   1364.
## 11 其他       高楼层       2799.   5388    1369.
## 12 其他       中楼层       2865.   5595.   1343.
## 13 通州       低楼层       2508.   4144    1730.
## 14 通州       高楼层       2501.   4729.   1669 
## 15 通州       中楼层       2444.   4125    1800.习题答案
tenement = read.csv("./data/rent.csv",fileEncoding = "gbk",stringsAsFactors = FALSE)#导入数据题目 3.1
请求出租房数据集中租赁房间面积(area)的偏度和峰值,并作出简要分析。
library(fBasics)
skewness(tenement$area) #用fBasics包求评分的偏度## [1] 1.025208
## attr(,"method")
## [1] "moment"kurtosis(tenement$area) #用fBasics包求评分的峰度## [1] 1.066606
## attr(,"method")
## [1] "excess"分析:面积的偏度为1.03,说明面积是一个右偏分布;面积的峰度为1.07,说明标准化后的面积分布比正态分布更尖锐,尾部更粗。
题目 3.2
请求出向量城区(region)和供暖方式(heating)的交叉列联表的频率形式,并添加边际和,给出简要解读。
tab <- table(tenement$region,tenement$heating)#建立城区和供暖方式的交叉列联表
p_tab <- prop.table(tab)#频率形式交叉列联表
addmargins(p_tab)#添加边际和##         
##              集中供暖       自采暖          Sum
##   昌平   0.1062342202 0.0301029326 0.1363371528
##   朝阳   0.1899397941 0.0658380268 0.2557778209
##   大兴   0.0642843271 0.0058263741 0.0701107011
##   东城   0.0157312099 0.0025247621 0.0182559720
##   房山   0.0178675471 0.0176733346 0.0355408817
##   丰台   0.0912798602 0.0215575840 0.1128374442
##   海淀   0.0563216158 0.0260244708 0.0823460866
##   石景山 0.0489415420 0.0011652748 0.0501068169
##   顺义   0.0576811031 0.0033016120 0.0609827151
##   通州   0.1491551758 0.0099048359 0.1590600117
##   西城   0.0176733346 0.0009710623 0.0186443970
##   Sum    0.8151097300 0.1848902700 1.0000000000分析:租赁房间的供暖方式以集中供暖为主,自采暖为辅。不同城区中,朝阳区房源数量最多。朝阳区的集中供暖房源占比最大。
题目 3.3
利用租房数据集,通过分组统计求出每个城区的平均租金并简要解读。
library(dplyr)tenement%>%group_by(region)%>%
  summarise(meanrent=mean(rent))#平均租金## # A tibble: 11 x 2
##    region meanrent
##    <chr>     <dbl>
##  1 昌平      2801.
##  2 朝阳      3395.
##  3 大兴      2411.
##  4 东城      3348.
##  5 房山      1957.
##  6 丰台      2839.
##  7 海淀      3582.
##  8 石景山    2917.
##  9 顺义      2288.
## 10 通州      2481.
## 11 西城      3878.分析:西城区的房源平均租金最高,为3785元;房山区的房源平均租金最低,为1752元。
题目 3.4
利用租房数据集,通过分组统计计算出不同城区房源的平均租赁面积、最大租赁面积和最小租赁面积并简要解读。
library(dplyr)
tenement%>%group_by(region)%>%
  summarise(meanarea=mean(area),
            maxarea=max(area),
            minarea=min(area))## # A tibble: 11 x 4
##    region meanarea maxarea minarea
##    <chr>     <dbl>   <int>   <int>
##  1 昌平       13.2      30       6
##  2 朝阳       13.0      29       5
##  3 大兴       12.5      29       6
##  4 东城       12.8      27       6
##  5 房山       11.9      25       6
##  6 丰台       12.3      29       6
##  7 海淀       12.5      29       6
##  8 石景山     13.7      30       6
##  9 顺义       12.1      28       6
## 10 通州       13.1      30       5
## 11 西城       14.2      28       7分析:石景山区房源的平均租赁面积最大,为13.7平米,房山区房源的平均租赁面积最小,为11.9平米;最大租赁面积为30平米,分别在昌平区和通州区;最小租赁面积为5平米,分别在朝阳区和通州区。