R语言Faithful数据集作探索性数据分析附代码数据.pptx
《R语言Faithful数据集作探索性数据分析附代码数据.pptx》由会员分享,可在线阅读,更多相关《R语言Faithful数据集作探索性数据分析附代码数据.pptx(15页珍藏版)》请在冰豆网上搜索。
![R语言Faithful数据集作探索性数据分析附代码数据.pptx](https://file1.bdocx.com/fileroot1/2022-10/1/79731337-b4f6-423e-9ed8-c88064ca04a8/79731337-b4f6-423e-9ed8-c88064ca04a81.gif)
2017课程汇报对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.数据介绍DescriptionWaitingtimebetweeneruptionsandthedurationoftheeruptionfortheOldFaithfulgeyserinYellowstoneNationalPark,Wyoming,USA.Adataframewith272observationson2variables.numericEruptiontimeinminsnumericWaitingtimetonext,1eruptions,2waitingeruption(inmins)3.数字化探索#获取faithful的属性列表attributes(faithful)其返回结果与下面三个函数一样rownames(faithful)colnames(faithful)class(faithful)3.数字化探索str(faithful)#str()查看数据集的内部结构summary(faithful)#summary()得到数据集的一系列统计指标值#min.,1stQu.,Median,Mean,3rdQu.,Max.3.数字化探索describe(faithful)#describe()是Hmisc包中的函数#输出数据集的变量个数和观察样本数#样本的总数(n),缺失样本书(missing)不同值个数(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$waiting,method=spearman)#可见pearsonspearmankendall4.可视化探索#绘制散点图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,colour=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设置颜色标度#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用来设置数据的组距,来调整数据的分组数目,或者将数据切分为指定的分组数目#直方图的外观非常依赖于组距与组边界,不同的组距,差异会很大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的密度曲线#核密度曲线是基于样本数据对总体分布做出的一个估计#曲线的光滑程度取决于核函数的带宽#带宽越大,曲线越光滑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值出产生一个阶梯