下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【原创】【原创】R语言案例数据分析报告论文〔附代码数据〕有问题到淘宝找“大数据部落”就可以了RR语言线性回归案例数据分析可视化报告30个职业棒球大联盟球队的数据,并检查一个赛季的得分哪个变量〔假设有的话〕可以帮助我们最好地推想一个赛季中球队的得分状况。数据load(load(“more/mlb11.RData“)at_bats绘制这种关系作为推想。关系看起来是线性的吗?假设你知道一个团队的at_batsat_bats,你会习惯使用线性模型来推想运行次数吗?散点图plotplot(mlb11$at_bat,mlb11$runs,xlab=“at_bat“,ylab=“runs“,main=“at_bat“,frame.plot=TRUE,col=“red“)abline(lm(mlb11$runs~mlb11$at_bat))..假设关系看起来是线性的,我们可以用相关系数来量化关系的强度。corcor(mlb11$runs,mlb11$at_bats)##[1]0.610627.残差平方和.残差平方和回想一下我们描述单个变量分布的方式。能够描述两个数值变量〔例如上面的runandat_bats〕的关系也是有用的。度以及任何不寻常的观看。plot_ss(x=mlb11$at_bats,y=mlb11$runs)#Clicktwopointstomakealine.##Call:##lm(formula=y~x,data=pts)######Coefficients:(Intercept)x##-2789.24290.6305####SumofSquares:123721.9AfterrunningAfterrunningthiscommand,you’llbepromptedtoclicktwopointsontheplottodefinealine.Onceyou’vedonethat,thelineyouspecifiedwillbeshowninblackandtheresidualsinblue.Notethatthereare30residuals,oneforeachofthe30observations.Recallthattheresidualsarethedifferencebetweentheobservedvaluesandthevaluespredictedbytheline:ei=yiy^iei=yiy^iTheThemostcommonwaytodolinearregressionistoselectthelinethatminimizesthesumofsquaredresiduals.Tovisualizethesquaredresiduals,youcanreruntheplotcommandandaddaddtheargumentshowSquares=TRUE.plot_ss(x=mlb11$at_bats,y=mlb11$runs,showSquares=TRUE)##Clicktwopointstomakealine.##Call:####lm(formula=y~x,data=pts)######Coefficients:(Intercept)x##-2789.24290.6305####SumofSquares:123721.9NotethattheoutputNotethattheoutputfromtheplot_ssfunctionprovidesyouwiththeslopeandinterceptofyourlineaswellasthesumofsquares.3. Usingplot_ss,choosealinethatdoesagoodjobofminimizingthesumofsquares.Runthefunctionseveraltimes.Whatwasthesmallestsumofsquaresthatyougot?Howdoesitcomparetoyourneighbors?Answer:Thesmallestsumofsquaresis123721.9.Itexplainsthedispersionfrommean.ThelinearmodelItisrathercumbersometotrytogetthecorrectleastsquaresline,i.e.thelinethatminimizesthesumofsquaredresiduals,throughtrialanderror.InsteadwecanusethelmfunctioninRtofitthelinearmodel(a.k.a.regressionline).m1<-lm(runs~at_bats,data=mlb11)Thefirstargumentinthefunctionlmisaformulathattakestheformy~x.Hereitcanbereadthatthatwewanttomakealinearmodelofrunsasafunctionofat_bats.ThesecondargumentspecifiesspecifiesthatRshouldlookinthemlb11dataframetofindtherunsandat_batsvariables.TheTheoutputoflmisanobjectthatcontainsalloftheinformationweneedaboutthelinearmodelthatwasjustfit.Wecanaccessthisinformationusingthesummaryfunction.summary(m1)####Call:##lm(formula=runs~at_bats,data=mlb11)####Residuals:##Min1QMedian3QMax####-125.58-47.05-16.5954.40176.87##Coefficients:## EstimateStd.ErrortvaluePr(>|t|)##(Intercept)-2789.2429 853.6957-3.2670.002871**##at_bats 0.6305 0.1545 4.0800.000339***##---####Signif.codes:0”***”0.001”**”0.01”*”0.05”.”0.1””1####Residualstandarderror:66.47on28degreesoffreedom##MultipleR-squared:0.3729,AdjustedR-squared:0.3505##F-statistic:16.65on1and28DF,ue:0.0003388p-valLet’sconsiderLet’sconsiderthisoutputpiecebypiece.First,theformulausedtodescribethemodelisshownatthetop.Aftertheformulayoufindthefive-numbersummaryoftheresiduals.Thekey;itsfirstcolumndisplaysthelinearmodel’sy-interceptandandthecoefficientofat_bats.Withthistable,wecanwritedowntheleastsquaresregressionregressionlineforthelinearmodel:y^=2789.2429+0.6305∗atbatsy^=−2789.2429+0.6305∗atbatsOnelastpieceofinformationwewilldiscussfromthesummaryoutputistheMultipleR-squared,ormoresimply,R2R2.TheR2R2valuerepresentstheproportionofvariabilityintheresponsevariablethatisexplainedbytheexplanatoryvariable.Forthismodel,37.3%ofthevariabilityinrunsisexplainedbyat-bats.4. Fit4. Fitanewmodelthatuseshomerunstopredictruns.UsingtheestimatesfromtheRoutput,writeoutput,writetheequationoftheregressionline.Whatdoestheslopetellusinthecontextoftherelationshipbetweensuccessofateamanditshomeruns?Answer:homerunshaspositiverelationshipwithruns,whichmeans1homerunsincrease1.835timesruns.home.runs<-home.runs<-lm(runs~homeruns,data=mlb11)home.runs####Call:##lm(formula=runs~homeruns,data=mlb11)######Coefficients:(Intercept)homeruns##415.2391.835PredictionPredictionandpredictionerrorsLet’screateascatterplotwiththeleastsquareslinelaidontop.plot(mlb11$runs~mlb11$at_bats)abline(m1)Thefunctionablineplotsalinebasedonitsslopeandintercept.Here,weusedashortcutbyprovidingthemodelm1,whichcontainsbothparameterestimates.Thislinecanbeusedtopredictyyatanyvalueofxx.Whenpredictionsaremadeforvaluesofxxthatarebeyondtherangeoftheobserveddata,itisreferredtoasextrapolatioandisnotusuallyrecommended.However,predictionsmadewithintherangeofthedataaremorereliable.They’realsousedtocomputetheresiduals.5. Ifateammanagersawtheleastsquaresregressionlineandnottheactualdata,howmanyrunsmanyrunswouldheorshepredictforateamwith5,578at-bats?Isthisanoverestimateoranunderestimate,andbyhowmuch?Inotherwords,whatistheresidualforthisprediction?pred##[1]728.1323residual=residual=0.63058*(5578)residual##[1]3517.375ModelModeldiagnosticsToassesswhetherthelinearmodelisreliable,weneedtocheckfor(1)linearity,(2)nearlynormalresiduals,and(3)constantvariability.Linearity:Youalreadycheckediftherelationshipbetweenrunsandat-batsislinearusingascatterplot.Weshouldalsoverifythisconditionwithaplotoftheresidualsvs.at-bats.Recallthatanycodefollowinga#isintendedtobeacommentthathelpsunderstandthecodebutisignoredbyR.plot(m1$residuals~mlb11$at_bats)abline(h=0,lty=3)ashedlineaty=0#addsahorizontald6.6.Isthereanyapparentpatternintheresidualsplot?Whatdoesthisindicateaboutthelinearityoftherelationshipbetweenrunsandat-bats?Answer:Answer:theresidualshasnormallinearityoftherelationshipbetweenrunsansat-bats,whichmeanmeanis0.Nearlynormalresidua:Tocheckthiscondition,wecanlookatahistogramhist(m1$residuals)qqnormqqnorm(m1$residuals)qqline(m1$residuals)#addsdiagonallinetothenormalprobplot7.7.Basedonthehistogramandthenormalprobabilityplot,doesthenearlynormalresidualsconditionappeartobemet?Answer:Answer:Yes.It’snearlynormal.8. Based8. Basedontheplotin(1),doestheconstantvariabilityconditionappeartobemet?Answer:Yes,thepointsconstantlyaroundtheleastsquaresline.1. 1. Chooseanothertraditionalvariablefrommlb11thatyouthinkmightbeagoodpredictorpredictorofruns.Produceascatterplotofthetwovariablesandfitalinearmodel.Ataaglance,doesthereseemtobealinearrelationship?Answer:Answer:Yes,thescatterplotshowstheyhavealinearrelationship..1.1.Howdoesthisrelationshipcomparetotherelationshipbetweenrunsandat_bats?UsetheR22valuesfromthetwomodelsummariestocompare.Doesyourvariableseemtopredictrunsbetterthanat_bats?Howcanyoutell?plotplot(mlb11$hits,mlb11$runs,xlab=“hits“,ylab=“runs“,main=“hitsvsruns“,frame.plot=TRUE,col=“red“)abline(lm(mlb11$runs~mlb11$hits))m2m2<-lm(runs~hits,data=mlb11)summary(m2)####Call:##lm(formula=runs~hits,data=mlb11)####Residuals:##Min1QMedian3QMax##93-103.718-27.179-5.23319.322140.6####Coefficients:## EstimateStd.ErrortvaluePr(>|t|)##(Intercept)-375.5600 151.1806-2.4840.0192*##hits 0.7589 0.1071 7.0851.04e-07***##---##Signif.codes:0”***”0.001”**”0.01”*”0.05”.”0.1””1####Residualstandarderror:50.23on28degreesoffreedom####MultipleR-squared:0.6419,AdjustedR-squared:0.6292##F-statistic:ue:1.043e-0750.2on1and28DF,p-val1. Now1. Nowthatyoucansummarizethelinearrelationshipbetweentwovariables,investigatetherelationshipsbetweenrunsandeachoftheotherfivetraditionalvariables.Whichvariablevariablebestpredictsruns?Supportyourconclusionusingthegraphicalandnumericalmethodswe’vediscussed(forthesakeofconciseness,onlyincludeoutputforforthebestvariable,notallfive).Answer:TheAnswer:Thenew_obsisthebestpredictsrunssinceithassmallestStd.Error,whichthepointsareonorveryclosetotheline.par(mfrow=c(2,3))plot(mlb11$hits,mlb11$runs,xlab=“hits“,ylab=“runs“,frame.plot=TRUE,col=“blue“)plot(mlb11$bat_avg,mlb11$runs,xlab=“batblue“)plot(mlb11$new_slug,mlb11$runs,xlab=“new_slug“,ylab=“runs“,frame.plot=TRUE,col=“blue“)plot(mlb11$new_onbase,mlb11$runs,xlab=“new_onbase“,ylab=“runs“,frame.plot=TRUE,col=“blue“)plot(mlb11$new_obs,mlb11$runs,xlab=“newblue“)1$new_obs)for(iin1:5){print(summary(lms))}####Call:##l)=l[,i]~l[,i+1],data=######Min1QMedian3QMax##93-103.718-27.179-5.23319.322140.6####Coefficients:## EstimateStd.ErrortvaluePr(>|t|)##(Intercept)-375.5600 151.1806-2.4840.0192*##l[,i+1] 0.7589 0.1071 7.0851.04e-07***##---##Signif.codes:0”***”0.001”**”0.01”*”0.05”.”0.1””1####Residualstandarderror:50.23on28degreesoffreedom##MultipleR-squared:0.6419,AdjustedR-squared:0.6292##F-statistic:50.2on1and28DF,p-value:1.043e-07######Call:##lm(formula=l[,i]~l[,i+1],data=l)####Residuals:##Min1QMedian3QMax##-27.855-8.8401.14110.08621.899####Coefficients:##(>|t|)EstimateStd.ErrortvaluePr##(Intercept)32e-06***-312.151.0-6.121.##l[,i+1]6750.9199.833.79<2e-16***##---##Signif.codes:0”***”0.001”**”0.01”*”0.05”.”0.1””1####Residualstandarderror:13.71on28degreesoffreedom##MultipleR-squared:0.9761,AdjustedR-squared:0.9752##F-statistic:1142on1and28DF,p-value:<2.2e-16######Call:##l)##lm(formula=l[,i]~l[,i+1],data=##Residuals:##Min1QMedian3QMax##-0.0120811-0.0038072-0.00076230.00505690.0142072####Coefficients:## EstimateStd.ErrortvaluePr(>|t|)##(Intercept)0.11038 0.01851 5.9622.02e-06***##l[,i+1] 0.36244 0.04630 7.8281.58e-08***##---##Signif.codes:0”***”0.001”**”0.01”*”0.05”.”0.1””1####Residualstandarderror:0.007263on28degreesoffreedom##MultipleR-squared:0.6864,AdjustedR-squared:0.6752##F-statistic:61.28on1and28DF,p-value:1.582e-08######Call:##lm(formula=l[,i]~l[,i+1],data=l)####Residuals:## Min 1Q Median Max##-0.035295-0.0084810.0001560.010515####Coefficients:## EstimateStd.ErrortvaluePr(>|t|)##(Intercept)-0.20671 0.06434-3.2130.0033**##l[,i+1] 1.88957 0.20239 9.4203.54e-10***##---##Signif.codes:0”***”0.001”**”0.01”*”0.05”.”0.1””1####Residualstandarderror:0.01452on28degreesoffreedom##MultipleR-squared:0.7601,AdjustedR-squared:0.7516##F-statistic:88.74on1and28DF,p-value:3.538e-10######Call:##l)lm(formula=l[,i]~l[,i+1],data=####Residuals:1QMedian1QMedian3Q-0.00425990.0009995Max##-0.0074684180.0127444####Coefficients:## EstimateStd.ErrortvaluePr(>|t|)##(Intercept)0.10243 0.01535 6.6743.05e-07***##l[,i+1] 0.30321 0.0213114.2292.42e-14***##---##Signif.codes:0”***”0.001”**”0.01”*”0.05”.”0.1””1######Residualstandarderror:0.004768on28de
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高二道德与法治主题班会教学设计:识诈骗套路筑心理防线
- 高三地理第一轮复习阶段提升课教学设计-地理核心素养导向下的综合思维与区域认知进阶训练
- 初中七年级英语Unit 1 A new start基于主题意义探究的教学设计
- 八年级道德与法治上册 第九课第一课时 社会责任我担当 教学设计
- 九年级化学“人体健康与化学元素”跨学科融合教学设计
- 译林版七年级英语下册Unit 5-Unit 8期末复习教学设计
- 初中七年级硬笔书法复习教案:第七八课笔法结构精要
- 初中九年级历史《古代埃及》教学设计-基于大概念的文明探源课堂构建
- 高中地理选择性必修2 教学设计:资源跨区域调配的区域发展效应与协调机制
- 初中历史八年级近代经济社会生活与教育文化事业发展教学设计
- 2026事业单位财会岗面试历年真题题库及答案
- 2026版“优 质服务基层行”能力评价标准解读
- 职业生涯人物访谈
- 光伏发电项目档案分类、归档范围及保管期限划分表
- 矿山目视化管理制度
- DLT5210.1-2021电力建设施工质量验收规程第1部分-土建工程
- T/CCAS 007-2019水泥产能核定标准
- 傅佩荣论语三百讲(1-300讲)汇编
- 药食两用产品开发
- 慢阻肺合并心衰护理查房
- 传统礼仪文化:衣冠贵整
评论
0/150
提交评论