1、2 0 1 7课程汇报对R中的Faithful数据集作探索 性数据分析汇报人:王硕目 录01020304准备工作数据介绍数字化探索可视化探索1.准备工作#data()加载数据 data(faithful)#安装包#install.packages(Hmisc)#install.packages(fBasics)#install.packages(ggplot2)#install.packages(tidyr)#加载包require(Hmisc)#为了使用describe()函数 require(fBasics)#为了使用basicStats()函数 require(ggplot2)#绘图2.数
2、据介绍DescriptionWaiting time between eruptions and the duration of the eruption for the Old Faithful geyser in Yellowstone National Park,Wyoming,USA.A data frame with 272 observations on 2 variables.numeric Eruption time in mins numeric Waiting time to next,1 eruptions,2 waiting eruption(in mins)3.数字化
3、探索#获取faithful的属性列表attributes(faithful)其返回结果与下面三个函数一样rownames(faithful)colnames(faithful)class(faithful)3.数字化探索str(faithful)#str()查看数据集的内部结构 summary(faithful)#summary()得到数据集的一系列统计指标值#min.,1st Qu.,Median,Mean,3rd Qu.,Max.3.数字化探索describe(faithful)#describe()是Hmisc包中的函数#输出数据集的变量个数和观察样本数#样本的总数(n),缺失样本书(m
4、issing)不同值个数(distinct)#从0.05到0.95一系列的分位数取值,频率最低和最高的5个水平值3.数字化探索basicStats(faithful)#basicStats()是fBasics包中的函数#除了输出常规统计量外#还输出方差,标准差,偏度,峰度3.数字化探索#三种不同的方法计算相关性cor(faithful$eruptions,faithful$waiting,method=pearson)cor(faithful$eruptions,faithful$waiting,method=kendall)cor(faithful$eruptions,faithful$wa
5、iting,method=spearman)#可见pearson spearman kendall4.可视化探索#绘制散点图h-ggplot(data=faithful,aes(x=eruptions,y=waiting)+geom_point()+geom_smooth(method=lm)h#ggplot()是ggplot2包中的函数#添加拟合线#默认情况下,geom_smooth()函数会为回归拟合 线添加95%的置信域4.可视化探索#绘制箱线图h-ggplot(data=faithful)+geom_boxplot(aes(x=eruptions,y=eruptions,colou r
6、=eruptions)+geom_boxplot(aes(x=waiting,y=waiting/20,colour=waiting)+scale_y_continuous(sec.axis=sec_axis(.*20)+scale_colour_manual(values=c(blue,red),guide=FALSE)+theme(axis.title.y=element_blank()h#eruptions 看左纵轴,waiting 看右纵轴,轴的尺 度缩放不一致#箱线图能够很好地识别各分位数分布#sec.axis为了设置第二个轴,即右纵轴#scale_colour_manual 设置颜
7、色标度#theme设置主题,去掉左纵轴标题4.可视化探索#绘制直方图h-ggplot(data=faithful,aes(x=eruptions)+geom_histogram(binwidth=0.5,fill=white,colour=black)h#用来绘制eruptions的直方图h-ggplot(data=faithful,aes(x=waiting)+geom_histogram(binwidth=5,fill=white,colour=black)h#用来绘制waiting的直方图#binwidth 用来设置数据的组距,来调整数据的 分组数目,或者将数据切分为指定的分组数目#直方
8、图的外观非常依赖于组距与组边界,不同 的组距,差异会很大4.可视化探索#绘制密度曲线h-ggplot(faithful,aes(x=eruptions)+geom_line(stat=density)h#用来绘制eruptions的密度曲线h-ggplot(faithful,aes(x=waiting)+geom_line(stat=density,adjust=0.25,colour=red)+geom_line(stat=density)+geom_line(stat=density,adjust=2,colour=blue)h#用来绘制waiting的密度曲线#核密度曲线是基于样本数据对
9、总体分布做出的 一个估计#曲线的光滑程度取决于核函数的带宽#带宽越大,曲线越光滑4.可视化探索#绘制QQ图 qqnorm(faithful$eruptions)qqline(faithful$eruptions)#绘制eruptions的QQ图 qqnorm(faithful$waiting)qqline(faithful$waiting)#绘制waiting的QQ图#图中点越接近理论线,说明其分布越接近正态 分布#可见两个数据都不符合正态分布#相对而言,waiting更接近正态分布4.可视化探索#绘制经验累积分布函数图h-ggplot(data=faithful,aes(x=eruptions)+stat_ecdf()h#绘制eruptions经验累积分布函数图h-ggplot(data=faithful,aes(x=waiting)+stat_ecdf()h#绘制waiting经验累积分布函数图#ECDF表明了在观测数据中,小于或者等于给定x值的观测所占的比例#因为是经验的,所以累积分布线在每个有一个 或者更多观测值的x值出产生一个阶梯
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1