R语言ablone数据集数据挖掘预测分析报告●介绍●数据集描述●检测异常值并构建清洁数据集●清洁数据分析●结论介绍鲍鱼是铁和泛酸的极佳来源,是澳大利亚,美国和东亚地区的营养食品资源和农业。
100克鲍鱼每日摄取这些营养素的量超过20%。
鲍鱼的经济价值与年龄呈正相关。
因此,准确检测鲍鱼的年龄对于农民和消费者确定其价格非常重要。
然而,目前用来决定年龄的技术是相当昂贵和低效的。
农民通常通过显微镜切割贝壳并计数环以估计鲍鱼的年龄。
这种复杂的方法增加了成本并限制了它的普及。
我们的目标是找出预测戒指的最佳指标,然后找出鲍鱼的年龄。
数据集描述数据集描述在这个项目中,数据集Abalone是从UCI Machine Learning Repository(1995)获得的。
该数据集包含1995年12月由澳大利亚塔斯马尼亚州主要工业和渔业部海洋研究实验室Taroona记录的4177只鲍鱼的物理测量结果。
有9个变量,分别是性别,长度,直径和身高,体重,体重,内脏重量,外壳重量和戒指。
随着年龄等于戒指数量,变量戒指与鲍鱼年龄呈线性相关加1.5。
检测异常值并构建清洁数据集library(ggplot2)library(plyr)library(nnet)library(MASS)library(gridExtra)## Loading required package: gridlibrary(lattice)library(RColorBrewer)library(xtable)Data = read.csv("abalone.csv")# Import Dataprint(str(Data))# Structure of the Data## 'data.frame': 4177 obs. of 9 variables:## $ Sex : Factor w/ 3 levels "F","I","M": 3 3 1 3 2 2 1 1 3 1 ...## $ Length : num 0.455 0.35 0.53 0.44 0.33 0.425 0.53 0.545 0.475 0.55 ...## $ Diameter : num 0.365 0.265 0.42 0.365 0.255 0.3 0.415 0.425 0.37 0.44 ...## $ Height : num 0.095 0.09 0.135 0.125 0.08 0.095 0.15 0.125 0.125 0.15 ...## $ Whole.weight : num 0.514 0.226 0.677 0.516 0.205 ...## $ Shucked.weight: num 0.2245 0.0995 0.2565 0.2155 0.0895 ...## $ Viscera.weight: num 0.101 0.0485 0.1415 0.114 0.0395 ...## $ Shell.weight : num 0.15 0.07 0.21 0.155 0.055 0.12 0.33 0.26 0.165 0.32 ...## $ Rings : int 15 7 9 10 7 8 20 16 9 19 ...## NULL有4种不同的体重衡量标准,即Whole.weight,Shucked.weight,Viscera.weight和Shell.weight。
Whole.weight应该是最容易测量的。
所以我放弃了所有其他措施。
Data = subset(Data, select =-c(Shucked.weight, Viscera.weight, Shell.weight))绘制不同戒指的鲍鱼数量。
ggplot(Data, aes(x = Rings, fill = Sex))+ geom_bar(binwidth =1, color ="blue",origin = min(Data$Rings), position ="identity")+ scale_x_continuous(name ="Rings", breaks = seq(0, 30, by =2))+ theme(axis.text.x = element_text(angle =90))+scale_fill_brewer("Location", type ="qual", palette =3)+ ylab("Number of abalone")+ggtitle("Number of abalone with different Rings")+ facet_wrap(~Sex, ncol =3)从上面的图表中,我们可以看到戒指的范围是从1到29,这可能太多以至于无法衡量。
实际上,人们可能不需要这么详细的类别。
因此,我们将小于6环(<7.5岁),6至13环(7.5至14.5岁)和13环(> 14.5岁)的鲍鱼进行分组,相应地表明年轻,成年和老年鲍鱼,将它们标记为1,2,3。
Age = c(rep(0, nrow(Data)))for(i in1:nrow(Data)){if(Data[i, ]$Rings <7)Age[i]=1if(Data[i, ]$Rings >=7& Data[i, ]$Rings <=13)Age[i]=2if(Data[i, ]$Rings >13)Age[i]=3}gData = cbind(Data, Age)粗略地绘制图表,并获得关于数据的小尝试ggplot(gData, aes(x = Height, y = factor(Rings), colour = factor(Sex)))+geom_jitter(position = position_jitter(width =0.3))+geom_point()+ ggtitle("Original Data: Height vs Rings")+ scale_colour_brewer(type = "seq",palette ="Set1")从上面的图表我们可以看到女性中有一些轮廓。
我想摆脱他们。
jData = subset(gData, Height <0.4)ggplot(jData, aes(x = Height, y = factor(Rings), colour = factor(Sex)))+geom_jitter(position = position_jitter(width =0.1))+geom_point()+ ggtitle("Reduced Data: Height vs Rings")+ scale_colour_brewer(type = "seq",palette ="Set1")根据性别和戒指重新排序数据Order_Data = arrange(jData, Sex, Rings, Length)write.table(Order_Data, "abalone_clean.csv", quote =FALSE, sep =",", s =FALSE)清洁数据分析Data = read.csv("abalone_clean.csv")# Import Datahead(Data)## Sex Length Diameter Height Whole.weight Rings Age## 1 F 0.275 0.195 0.070 0.0800 5 1## 2 F 0.290 0.225 0.075 0.1400 5 1## 3 F 0.360 0.270 0.090 0.1885 5 1## 4 F 0.370 0.275 0.085 0.2405 5 1## 5 F 0.290 0.210 0.075 0.2750 6 1## 6 F 0.335 0.220 0.070 0.1700 6 1不同年龄段不同性别的鲍鱼数量。
with(Data, table(Sex, Age))## Age## Sex 1 2 3## F 20 1067 219## I 381 913 48## M 47 1257 223首先,我想测试一下观察数据如Height,Whole.weight是否有助于确定鲍鱼对不同年龄组的影响。
应用Logistic回归分析。
ggplot(Data, aes(x = Whole.weight, y = Height))+ geom_point(aes(colour = Rings))+scale_colour_gradient(low ="purple")+ stat_smooth(colour ="red")+ggtitle("Whole.weight vs Height")## geom_smooth: method="auto" and size of largest group is >=1000, so using gam with formula: y ~ s(x, bs = "cs"). Use 'method = x' to change the smoothing method.ggplot(Data, aes(x = Length, y = Height))+ geom_point(aes(colour = Rings))+scale_colour_gradient(low ="purple")+ stat_smooth(colour ="red")+ ggtitle("Length vs Height")## geom_smooth: method="auto" and size of largest group is >=1000, so using gam with formula: y ~ s(x, bs = "cs"). Use 'method = x' to change the smoothing method.ggplot(Data, aes(x = Length, y = Diameter))+ geom_point(aes(colour = Rings))+scale_colour_gradient(low ="purple")+ stat_smooth(colour ="red")+ ggtitle("Length vs Diameter")## geom_smooth: method="auto" and size of largest group is >=1000, so using gam with formula: y ~ s(x, bs = "cs"). Use 'method = x' to change the smoothing method.。