您好,欢迎访问三七文档
1.读取数据:algae-read.table(C:/Users/shengjun/Desktop/Analysis.txt,,col.names=c('season','size','speed','mxPH','mnO2','Cl','NO3','NH4','oPO4','PO4','Chla','a1','a2','a3','a4','a5','a6','a7'),na.strings=c('XXXXXXX'))head(algae)2.获取数据描述性统计摘要:summary(algae)3.做变量间的相关程度图:mydata-algae[,c(4,5,6,7,8,9,10,11)]head(mydata)cormat-round(cor(mydata,use=na.or.complete),2)cormatinstall.packages(theme_minimal)install.packages(ggplot2)install.packages(reshape2)library(reshape2)melted_cormat-melt(cormat)head(melted_cormat)library(ggplot2)ggplot(data=melted_cormat,aes(x=X1,y=X2,fill=value))+geom_tile()由表和图比较直观看到但变量之间相关系数不大,但特殊的NH4和NO3相关程度有0.72、变量PO4、oPO4之间相关程度很大达到91.2%。这说明变量间从直观上也可以看出有可能存在着多重共线性的影响。4.判断数据是否服从正态:par(mfrow=c(2,4))library(car)qqPlot(algae$mxPH,main='NormalQQplotofmaximumpH')qqPlot(algae$mnO2,main='NormalQQplotofmnO2')qqPlot(algae$Cl,main='NormalQQplotofCl')qqPlot(algae$NO3,main='NormalQQplotofNO3')qqPlot(algae$NH4,main='NormalQQplotofNH4')qqPlot(algae$oPO4,main='NormalQQplotofoPO4')qqPlot(algae$Chla,main='NormalQQplotofChla')qqPlot(algae$PO4,main='NormalQQplotofPO4')从图上看到都不服从正态分布5.找数据确实值并填补:algae[!complete.cases(algae),]algae1-na.omit(algae)用书上第一种方法直接将确实部分剔除,因为200组数据剔除16组数据影响不大,用复杂的方法填补缺失值,反而可能导致较大偏差。6.建模:用处理后的训练数据集algae1作为最终待处理的数据集。建立用于预测海藻频率的线性回归模型:lm.a1-lm(a1~.,data=algae1[,1:12])summary(lm.a1)进行方差分析:anova(lm.a1)剔除对模型拟合优度贡献最小的因子seasonlm.a2-update(lm.a1,.~.-season)summary(lm.a2)比较a1,a2两个模型lm.a1的R2=0.3379,lm.a2的R2=0.3343相差不大,lm.a2和lm.a1的方差比较,anova(lm.a1,lm.a2)误差平方和减少了271.06,显著性0.8228,两个模型不同的可能性有17.72%,基本说明相同。用向后消元法:final.lm-step(lm.a1)得到新的线性模型为最后模型为:summary(final.lm)得到模型为:a1=24.74+3.77sizemedium+11.01sizesmall-0.04Cl-1.36NO3+0.0001NH4-0.06PO4(7.766)(1.139)(3.151)(-1.411)(-2.726)(1.487)(-5.355)R2=0.32P0.05通过检验,但是拟合效果欠佳。7.模型检验,看残差是否正态分布:shapiro.test(final.lm$residuals)par(mfrow=c(1,2))hist(final.lm$residuals)qqnorm(final.lm$residuals)看到残差图不是正态分布,有信息没有提取充分,回归方程不是很好。8.建立回归树建模:library(rpart)algaetree-algae1[-c(62,199),]rt.a1-rpart(a1~.,data=algaetree[,1:12])rt.a1printcp(rt.a1)n=183node),split,n,deviance,yval*denotesterminalnode1)root18375357.66015.3901602)Cl=7.291515634825.47010.0269204)PO4=43.81813921611.5107.5460438)oPO4=51.118823432.5493.896341*9)oPO451.1185715515.36012.79649018)mnO2=10.05241276.5106.770833*19)mnO210.053312733.70017.17879038)oPO424.9178382.8006.750000*39)oPO4=24.9172511202.39020.51600078)mnO29.4163486.95814.762500*79)mnO2=9.496244.20230.744440*5)PO443.818175363.37830.311760*3)Cl7.29152710118.69046.3777806)season=spring,summer164248.40036.850000*7)season=autumn,winter112305.16560.236360*printcp(rt.a1)Regressiontree:rpart(formula=a1~.,data=algaetree[,1:12])Variablesactuallyusedintreeconstruction:[1]ClmnO2oPO4PO4seasonRootnodeerror:75358/183=411.79n=183CPnsplitrelerrorxerrorxstd10.40358901.000001.005760.1477020.10417810.596410.736960.1295630.04730920.492230.662010.1230040.03534630.444920.597060.1176550.01997340.409580.549440.1151560.01738250.389600.561400.1126470.01000070.354840.569550.11371printcp(rt.a1)Regressiontree:rpart(formula=a1~.,data=algaetree[,1:12])Variablesactuallyusedintreeconstruction:[1]ClmnO2oPO4PO4seasonRootnodeerror:75358/183=411.79n=183CPnsplitrelerrorxerrorxstd10.40358901.000001.005760.1477020.10417810.596410.736960.1295630.04730920.492230.662010.1230040.03534630.444920.597060.1176550.01997340.409580.549440.1151560.01738250.389600.561400.1126470.01000070.354840.569550.11371作为最终模型。
本文标题:R语言预测海藻数量
链接地址:https://www.777doc.com/doc-4274076 .html