关闭

关闭

关闭

封号提示

内容

首页 Computing Primer for Linear Regression using R.…

Computing Primer for Linear Regression using R.pdf

Computing Primer for Linear Reg…

上传者: phoenixali 2013-12-28 评分 0 0 0 0 0 0 暂无简介 简介 举报

简介:本文档为《Computing Primer for Linear Regression using Rpdf》,可适用于高等教育领域,主题内容包含ComputingPrimerforAppliedLinearRegression,ThirdEditionUsingRSanfordWeisber符等。

ComputingPrimerforAppliedLinearRegression,ThirdEditionUsingRSanfordWeisbergUniversityofMinnesotaSchoolofStatisticsJuly,–,SanfordWeisbergIntroductionThealrpackageforusewithRwassubstantiallyrewritteninInparticular:()nearlyallfunctionnameshavechanged,andmosthavebeenmovedtoapackagecalledcar()theinstructionsforinstallingalrhavechangedslightlytoloadinstallcarandloaditwheneveralrisinstalledorloaded()thenewalrpackageworkswithRbutnotwithSPlustheoldversionofalrcontinuestoworkwithSPlusThiscomputerprimersupplementsthebookAppliedLinearRegression(alr),thirdedition,bySanfordWeisberg,publishedbyJohnWileySonsinItshowsyouhowtodotheanalysesdiscussedinalrusingoneofseveralgeneralpurposeprogramsthatarewidelyavailablethroughouttheworldAlltheprogramshavecapabilitieswellbeyondtheusesdescribedhereDifferentprogramsarelikelytosuitdifferentusersWeexpecttoupdatetheprimerperiodically,socheckwwwstatumnedualrtoseeifyouhavethemostrecentversionTheversionsareindicatedbythedateshownonthecoverpageoftheprimerOurpurposeislargelylimitedtousingthepackageswithalr,andwewillnotattempttoprovideacompleteintroductiontothepackagesIfyouarenewtothepackageyouareusingyouwillprobablyneedadditionalreferencematerialThereareanumberofmethodsdiscussedinalrthatarenotastandardpartofstatisticalanalysis,andsomemethodsarenotpossiblewithoutwritingINTRODUCTIONyourownprogramstosupplementthepackageyouchooseTheexceptionstothisruleisR,forwhichpackagesareavailabletodoeverythinginthebookHerearetheprogramsforwhichprimersareavailableRisacommandlinestatisticalpackage,whichmeansthattheusertypesastatementrequestingacomputationoragraph,anditisexecutedimmediatelyYouwillbeabletouseapackageoffunctionsforRthatwillletyouuseallthemethodsdiscussedinalrweusedRwhenwritingthebookRalsohasaprogramminglanguagethatallowsautomatingrepetitivetasksRisafavoriteprogramamongacademicstatisticiansbecauseitisfree,worksonWindows,LinuxUnixandMacintosh,andcanbeusedinagreatvarietyofproblemsThereisalsoalargeliteraturedevelopingonusingRforstatisticalproblemsThemainwebsiteforRiswwwrprojectorgFromthiswebsiteyoucangettothepagefordownloadingRbyclickingonthelinkforCRAN,or,intheUS,goingtocranusrprojectorgDocumentationisavailableforRonline,fromthewebsite,andinseveralbooksWecanstronglyrecommendtwobooksThebookAnRCompaniontoAppliedRegressionbyFoxandWeisberg()providesafairlygentleintroductiontoRwithemphasisonregressionTheCompanionalsoprovidesacomprehensivetreatmentofapackagecalledcarthatimplementsmostoftheideasinalrAmorecomprehensivethoughsomewhatdatedintroductiontoRisVenablesandRipley(),andwewillusethenotationvr,forexample,torefertoSectionofthatbookVenablesandRipleyhasmorecomputeresethandoestheCompanion,butitscoverageisgreaterOtherbooksonRincludeVerzani(),MaindonaldandBraun(),VenablesandSmith(),andDalgaard()WeusedRVersiononWindowsandLinuxtowriteupdatethisprimerAnewversionofRisreleasedtwiceayear,sotheversionyouusewillprobablybenewerIfyouhaveafastinternetconnection,downloadingandupgradingRiseasy,andyoushoulddoitregularlySASisthelargestandmostwidelydistributedstatisticalpackageinbothindustryandeducationSASalsohasaGUIWhileitispossibletodosomedataanalysisusingtheSASGUI,thestrengthofthisprogramisintheabilitytowriteSASprograms,intheeditorwindow,andthensubmitthemforexecution,withoutputreturnedinanoutputwindowWewillthereforeviewSASasabatchsystem,andconcentratemostlyonwritingSAScommandstobeexecutedThewebsiteforSASiswwwsascomSASisverywidelydocumented,includinghundredsofbooksavailablethroughamazoncomorfromtheSASInstitute,andextensiveonlinedocumentationMullerandFetterman()isdedicatedparticularlytoregressionWeusedVersionforWindowsWefindtheonlinedocumentationthataccompaniestheprogramtobeinvaluable,althoughlearningtoreadandunderstandSASdocumentationisn’teasyAlthoughSASisaprogramminglanguage,addingnewfunctionalitycanbeveryawkwardandrequirelong,confusingprogramsTheseprogramscould,however,beturnedintoSASmacrosthatcouldbereusedoverandover,soinprincipleSAScouldbemadeasusefulasRWehavenotdonethis,butwouldbedelightedifreaderswouldtakeonthechallengeofwritingmacrosformethodsthatareawkwardwithSASAnyonewhotakesthischallengecansendustheresults(sandystatumnedu)forinclusioninlaterrevisionsoftheprimerWehave,however,preparedscriptfilesthatgivetheprogramsthatwillproducealltheoutputdiscussedinthisprimeryoucangetthescriptsfromwwwstatumnedualrJMPisanotherproductofSASInstitute,andwasdesignedaroundacleverandusefulGUIAstudentversionofJMPisavailableThewebsiteiswwwjmpcomWeusedJMPVersiononWindowsDocumentationforthestudentversionofJMP,calledJMPIn,comeswiththebookwrittenbySall,CreightonandLehman(),andwewillwritejmpstartforChapterofthatbook,orjmpstartPforpageThefullversionofJMPincludesveryextensivemanualsthemanualsareavailableonCDonlywithJMPInFruend,LittellandCreighton()discussesJMPspecificallyforregressionJMPhasascriptinglanguagethatcouldbeusedtoaddfunctionalitytotheprogramWehavelittleexperienceusingit,andwouldbehappytohearfromreadersontheirexperienceusingthescriptinglanguagetoextendJMPtousesomeofthemethodsdiscussedinalrthatarenotpossibleinJMPwithoutscriptingSPSSevolvedfromabatchprogramtohaveaveryextensivegraphicaluserinterfaceIntheprimerweuseonlytheGUIforSPSS,whichlimitsthemethodsthatareavailableLikeSAS,SPSShasmanysophisticatedtoolsfordatabasemanagementAstudentversionisavailableThewebsiteforSPSSiswwwspsscomSPSSoffershundredsofpagesofdocumentation,includingSPSS(),withChapterdedicatedtoregressionmodelsInmid,amazoncomlistedmorethantwothousandbooksforwhich“SPSS”wasakeywordWeusedSPSSVersionforWindowsAnewerversionisavailableThisishardlyanexhaustivelistofprogramsthatcouldbeusedforregressionanalysisIfyourfavoritepackageismissing,pleasetakethisasachallenge:trytofigureouthowtodowhatissuggestedinthetext,andwriteyourownprimer!SendusaPDFfile(sandystatumnedu)andwewilladdittoourwebsite,orlinktoyoursINTRODUCTIONOneprogrammissingfromthelistofprogramsforregressionanalysisisMicrosoft’sspreadsheetprogramExcelWhileafewofthemethodsdescribedinthebookcanbecomputedorgraphedinExcel,mostwouldrequiregreatenduranceandpatienceonthepartoftheuserTherearemanyaddonstatisticsprogramsforExcel,andoneofthesemaybeusefulforcomprehensiveregressionanalysiswedon’tknowIfsomethingworksforyou,pleaseletusknow!AfinalpackageforregressionthatweshouldmentioniscalledArcLikeR,ArcisfreesoftwareItisavailablefromwwwstatumneduarcLikeJMPandSPSSitisbasedaroundagraphicaluserinterface,somostcomputationsaredoneviapointandclickArcalsoincludesaccesstoacompletecomputerlanguage,althoughthelanguage,lisp,isconsiderablyhardertolearnthantheSorSASlanguagesArcincludesallthemethodsdescribedinthebookTheuseofArcisdescribedinCookandWeisberg(),sowewillnotdiscussitfurtherhereseealsoWeisberg()ORGANIZATIONOFTHISPRIMERTheprimeroftenreferstospecificproblemsorsectionsinalrusingnotationlikealroralrA,forareferencetoSectionorAppendixA,alrPforProblem,alrFforFigure,alrEforanequationandalrTforatableReferenceto,forexample,“Figure,”wouldrefertoafigureinthisprimer,nottoalrChapters,sections,andhomeworkproblemsarenumberedinthisprimerastheyareinalrConsequently,thesectionheadingsinprimerreferstothematerialinalr,andnotnecessarilythematerialintheprimerManyofthesectionsinthisprimerdon’thaveanymaterialbecausethatsectiondoesn’tintroduceanynewissueswithregardtocomputingTheindexshouldhelpyounavigatethroughtheprimerTherearefourversionsofthisprimer,oneforR,andoneforeachoftheotherpackagesAllversionsareavailableforfreeasPDFfilesatwwwstatumnedualrAnythingyouneedtotypeintotheprogramwillalwaysbeinthisfontOutputfromaprogramdependsontheprogram,butshouldbeclearfromcontextWewillwriteFiletosuggestselectingthemenucalled“File,”andTransformRecodetosuggestselectinganitemcalled“Recode”fromamenucalled“Transform”Youwillsometimesneedtopushabuttoninadialog,andwewillwrite“pushok”tomean“clickonthebuttonmarked‘OK’”FornonEnglishversionsofsomeoftheprograms,themenusmayhavedifferentnames,andweapologizeinadvanceforanyconfusionthiscausesRMostofthegraphsandcomputeroutputinalrwereproducedwithRThecomputercodewegiveinthisprimermaynotreproducethegraphsexactly,sincewehavetweakedsomeofthegraphstomakethemlookprettierforpublication,andthetweakingargumentsworkalittledifferentlyinRIfyouwanttoseethetweaksweusedinR,lookatthescripts,SectionDATAFILESTableThedatafilehtwttxtHtWtDATAFILESDocumentationDocumentationfornearlyallofthedatafilesiscontainedinalrlookintheindexforthefirstreferencetoadatafileSeparatedocumentationcanbefoundinthefilealrdatapdfinPDFformatatthewebsitewwwstatumnedualrThedataareavailableinapackageforR,inalibraryforSAS,andasadirectoryoffilesinspecialformatforJMPandSPSSInaddition,thefilesareavailableasplaintextfilesthatcanbeusedwiththese,oranyother,programTableshowsacopyofoneofthesmallestdatafilescalledhtwttxt,anddescribedinalrPThisfilehastwovariables,namedHtandWt,andtencases,orrowsinthedatafileThelargestfileiswmtxtwith,casesandvariablesThislatterfileissolargethatitishandleddifferentlyfromtheothersseeSectionAfewofthedatafileshavemissingvalues,andthesearegenerallyindicatedinthefilebyaplaceholderintheplaceofthemissingvalueForexample,forR,theplaceholderisNA,whileforSASitisaperiod“”DifferentprogramshandlemissingvaluesalittledifferentlywewilldiscussthisfurtherwhenwegettothefirstdatasetwithamissingvalueinSectionRdatafilesandapackageTheinstructionsfordownloadingthealrpackagehavechangedasofDonotusethePackagesInstallpackage(s)menuitemasitwillnotloadalltheneededpackagesatonceAllthedatafilesarecollectedintoanRpackagenamedalrInaddition,manyfunctionsyouwillfindusefulforthetopicsofalrarenowinadifferentpackagecalledcarWhenyouinstallalronyourcomputerusingthefollowingcommand,carwillbedownloadedaswell:INTRODUCTION>installpackages("alr",dependencies=TRUE)Followtheonscreeninstructionstoselectamirrorsiteclosetoyourhome,andthenselectalrfromthelistofavailablepackagesBysettingdependencies=TRUEcarandseveralotherpackageswillbeobtainedandinstalledOnceyouhaveinstalledthepackage,allyouneedtoaccessadatafileistoloadthepackage,>library(alr)(orlibrary(alr,libloc="mylib")ifyouinstalledyourownLinuxUnixlibrary)Thealrpackageusessomethingcalled“lazyloading”ofdatasets,soallthedatasetsareimmediatelyavailableforyouruseForexample,simplytyping>forbesTempPressureLpresprintstheforbesdatasetDatafilesinthetextarealwaysnamedwithanendingof“txt”whichisnotusedinthealrpackageYoucanaddanewvariabletothedataframebytyping,forexample>forbes$logTemp<log(forbes$Temp)toaddthebasetwologarithmofTemperaturetothedataframeTherearesimilarfunctionslogfornaturallogsandlogforbasetenlogarithmsYoucanviewthedocumentationforthedatasetsonlineThemostelegantmethodistoenterthecommandhelpstart()intoRThiswillstartyourwebbrowser,ifitisnotalreadyrunningClickon“Packages,”andthenon“alr”Youwillthengetanalphabeticallistingofallthedatafilesandfunctionsinthealrpackage,andyoucanselecttheoneyouwanttoseeACOMPANIONTOAPPLIEDREGRESSIONTwofilesmissingfromthealrpackageThetwodatafilesanscombeandlongleyareavailable,withthosenames,inthedatasetslibrarythatispartofRForanscombethevariablenamesareasdescribedinthetextbookonpageForlongleythevariablenamesareslightlydifferentfromthenamesgivenonpagetypelongleytogetthevariablenamesGettingthedataintextfilesYoucandownloadthedataasadirectoryofplaintextfiles,orasindividualfilesseewwwstatumnedualrdataMissingvaluesonthesefilesareindicatedwithNAIfyourprogramdoesnotusethismissingvaluecharacter,youmayneedtosubstituteadifferentcharacterusinganeditorAnexceptionalfileThefilewmtxtisnotincludedinanyofthecompressedfiles,orinthelibrariesThisonefileisnearlyfivemegabyteslong,requiringasmuchspaceasalltheotherfilescombinedIfyouneedthisfile,foralrP,youcandownloaditseparatelyfromwwwstatumnedualrdataACOMPANIONTOAPPLIEDREGRESSIONThebookAnRCompaniontoAppliedRegressionbyFoxandWeisberg()providesadescriptionofthecarpackage,includingallthefunctionsthatwereintheearlyversionsofalrthathavebeenrenamed,improvedandmovedtocarThisbookprovidesaconsiderablymorecomprehensiveintroductiontoRthanisavailableinthisprimerWewillrefertotheCompanionwithreferenceslikeCompanionforSectionThroughoutthisPrimeryouwillbeusingfunctionsincarWhenyouloadthealrpackagecarisautomaticallyloadedaswellsoyoudon’tneedtodoanythingspecialtousecarSamplechaptersoftheCompanionareavailablefromtheSagewebsite,http:wwwsagepubcombooksBookSCRIPTSForRandSAS,wehavepreparedscriptfilesthatcanbeusedwhilereadingthisprimerForR,thescriptswillreproducenearlyeverycomputationshowninalrindeed,thesescriptswereusedtodothecalculationsinthefirstplaceForSAS,thescriptscorrespondtothediscussiongiveninthisprimer,butINTRODUCTIONwillnotreproduceeverythinginalrThescriptscanbedownloadedfromwwwstatumnedualrforRorSASForRusers,scriptscanbeobtainedwhileyouarerunningRandalsoconnectedtotheinternetTogetthescriptforChapterforthisprimer,forexample,youcouldtype>library(alr)>alrWeb(pscript="chapter")TogetthescriptforChapterofthetext,use>alrWeb(script="chapter")Ineithercase,thescriptwillopeninawindowinyourwebbrowser,andyoucanthensaveorcopyandpastethescriptAlthoughbothJMPandSPSShavescriptingorprogramminglanguages,wehavenotpreparedscriptsfortheseprogramsSomeofthemethodsdiscussedinalrarenotpossibleintheseprogramswithouttheuseofscripts,andsoweencouragereaderstowritescriptsintheselanguagesthatimplementtheseideasTopicsthatrequirescriptsincludebootstrappingandcomputerintensivemethods,alrpartialonedimensionalmodels,alr,inverseresponseplots,alr,,multivariateBoxCoxtransformations,alr,YeoJohnsontransformations,alr,andheteroscedasticitytests,alrThereareseveralotherplaceswhereusabilitycouldbeimprovedwithascriptIfyouwritescriptsyouwouldliketosharewithothers,letmeknow(sandystatumnedu)andI’llmakealinktothemoraddthemtothewebsiteTHEVERYBASICSBeforeyoucanbegindoinganyusefulcomputing,youneedtobeabletoreaddataintotheprogram,andafteryouaredoneyouneedtobeabletosaveandprintoutputandgraphsAlltheprogramsarealittledifferentinhowtheyhandleinputandoutput,andwegivesomeofthedetailshereReadingadatafileReadingdataintoaprogramissurprisinglydifficultWehavetriedtoeasethisburdenforyou,atleastwhenusingthedatafilessuppliedwithalr,byprovidingthedatainaspecialformatforeachoftheprogramsTherewillcomeatimewhenyouwanttoanalyzerealdata,andthenyouwillneedtobeabletogetyourdataintotheprogramHerearesomehintsonhowtodoitR(SeealsoCompanion)Ifyouhaveinstalledthealrpackageandwanttoreadoneofthedatafilesdescribedinalr,youcanfollowtheinTHEVERYBASICSstructionsinSectionIfyouhavenotinstalledthelibrary,oryouwanttoreadadifferentfile,usethecommandreadtabletoreadaplaindatafileThegeneralformofthiscommandis:>d<readtable("filename",header=TRUE,nastrings="")Thefilenameisaquotedstring,like"C:MyDocumentsdatatxt",givingthenameofthedatafileanditspathInplaceofthefilenameyoucanuse>d<readtable(filechoose(),header=TRUE,nastrings="")inwhichcaseastandardfiledialogwillopenandyoucanselectthefileyouwantintheusualwayTheargumentheader=TRUEindicatesthatthefirstlineofthefilehasvariablenames(youwouldsayheader=FALSEifthiswerenotso,andthentheprogramwouldassignvariablenameslikeX,Xandsoon),andthenastrings=""indicatesthatmissingvalues,ifany,areindicatedbyaquestionmarkratherthanthedefaultofNAusedbyRreadtablehasmanymoreoptionstypehelp(readtable)tolearnaboutthemRhasapackagecalledforeignthatcanbeusedtoreadfilesofothertypesSupposethatthefileC:MyDocumentsmydatahtwttxtgavethedatainTableThisfileisreadby>d<readtable("C:MyDocumentsmydatahtwttxt",header=TRUE)WithWindows,alwaysreplacethebackslashesbyforwardslashesWhilethisreplacementmaynotbenecessaryinallversionsofR,theforwardslashesalwaysworkThenastringsargumentcanbeomittedbecausethisfilehasnomissingdataAsaresultofthiscommand,adataframe,roughlylikeamatrix,iscreatednameddThetwocolumnsofdarecalledd$Htandd

用户评论(0)

0/200

精彩专题

上传我的资料

每篇奖励 +2积分

资料评价:

/24
仅支持在线阅读

意见
反馈

立即扫码关注

爱问共享资料微信公众号

返回
顶部