ebook img

Вступ до R на прикладах PDF

107 Pages·1.334 MB·Russian
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Вступ до R на прикладах

‚÷ªâ®à ƒ âîª ‚áâ㯠¤® R ¯à¨ª« ¤ å • àª÷¢á쪨© (cid:141) æ÷® «ì ¨© …ª® ®¬÷ç ¨© “ ÷¢¥àá¨â¥â • àª÷¢ 2010 (cid:13) Copyright 2010 ‚÷ªâ®à ƒ âîª. „ ¨© ¤®ªã¬¥ â ¤®§¢®«ïõâìáï ª®¯÷î¢ â¨ ÷ ஧¯®¢áî¤㢠⨠¢ ¥§¬÷ ÷© ä®à¬÷, ¢¨ª«îç ® ¢ ¥¯à¨¡ã⪮¢¨å æ÷«ïå, ÷§ §¡¥à¥¥ ï¬ ÷ ä®à¬ æ÷ù ¯à® €¢â®à â 㬮¢¨ ஧¯®¢á ï. ‡¬÷áâ 1 ‡ ©®¬á⢮ § R ........................................... 1 1.1 Ǒ®ç ⮪ ஡®â¨ ........................................ 2 1.2 Ǒਪ« ¤¨ ¢ R .......................................... 2 1.3 Žâਬ ï ¤®¯®¬÷ ®ù ÷ ä®à¬ æ÷ù. ...................... 5 1.4 Ǒਪ« ¤¨ áâ â¨áâ¨ç ¨å ¤ ¨å........................... 6 1.5 „¥¬® áâà æ÷ï ¬®«¨¢®á⥩ R ........................... 7 1.6 Ǒ ª¥â¨ ¢ R............................................. 8 1.6.1 (cid:129) §®¢¨© ¡÷à ¯ ª¥â÷¢ R........................... 8 1.6.2 ö áâ «ïæ÷ï ¤®¤ ⪮¢¨å ¯ ª¥â÷¢ ..................... 8 1.6.3 Ǒ÷¤ª«îç¥ ï ¤®¤ ⪮¢¨å ¯ ª¥â÷¢ .................. 9 2 Ž¡'õªâ¨ ÷ ⨯¨ ¤ ¨å ¢ R................................. 10 2.1 ‚¥ªâ®à¨ ............................................... 10 2.2 ” ªâ®à¨ ............................................... 11 2.3 — ᮢ÷ à廊/á¥à÷ù ....................................... 13 2.4 Œ âà¨æ÷ ÷ ¬ ᨢ¨ ....................................... 13 2.5 (cid:129)«®ª¨ ¤ ¨å ¡® ¤ â ä३¬¨............................ 16 2.6 ‘¯¨áª¨ ................................................ 18 2.7 ‹®£÷ç ÷ ⨯¨ ¤ ¨å ÷ ®¯¥à â®à¨ .......................... 19 3 …ªá¯®àâ/ö¬¯®àâ ¤ ¨å ¢ R ............................... 21 3.1 …ªá¯®àâ ¤ ¨å ......................................... 21 3.2 ‡ ¯¨á ¤ ¨å ¢ ä®à¬ â÷ Ex el ............................ 22 3.3 Ǒ¥à¥ ¯à ¢«¥ ï ¤ ¨å § ¥ªà ã ¢ ä ©« ................. 22 3.4 ö¬¯®àâ ¤ ¨å........................................... 23 3.5 ö¬¯®àâ ¤ ¨å § ä®à¬ ⮢ ®£® ⥪á⮢®£® ä ©«ã.......... 23 3.6 ”ã ªæ÷ù read.table(),read. sv()÷ read.delim()......... 24 3.7 ”ã ªæ÷ï read.fwf() .................................... 25 3.8 ”ã ªæ÷ï s an() ......................................... 25 3.9 ö¬¯®àâ ¤ ¨å § äa©«÷¢ EXCEL (*.xls ä ©«¨).............. 26 3.10 ö¬¯®àâ ¤ ¨å § äa©«÷¢ SPSS ............................ 26 3.11 ‚¢¥¤¥ ï ¤ ¨å § ª« ¢÷ âãਠ............................ 27 3.12 Žâਬ ï ÷ ä®à¬ æ÷ù ¯à® ®¡'õªâ¨....................... 28 3.13 ‘¯¥æ÷ «ì ÷ § ç¥ ï .................................... 30 4 ‡¬÷áâ 3.13.1 NA ÷ NaN ........................................ 30 3.13.2 (cid:141)¥áª÷ ç¥ ÷áâì Inf ................................. 30 3.13.3 ‡ ç¥ ï NULL .................................. 31 3.14 Š®¤ã¢ ï § ç¥ ì §¬÷ ¨å ............................. 31 3.15 ‚¨ª«îç¥ ï ¢÷¤áãâ ÷å § ç¥ ì § «÷§ã.................. 31 4 ”ã ªæ÷ù ÷ ª® áâàãªæ÷ù ¢ R ................................ 32 4.1 ‚¡ã¤®¢ ÷ äã ªæ÷ù ...................................... 32 4.1.1 €à¨ä¬¥â¨ç ÷ äã ªæ÷ù ............................ 32 4.1.2 ”ã ªæ÷ù ¤«ï ஡®â¨ § ᨬ¢®«ì ¨¬¨ ⨯ ¬¨ ¤ ¨å .. 33 4.2 (cid:141) ¯¨á ï ¢« á ¨å äã ªæ÷© ............................ 34 4.2.1 €à£ã¬¥ ⨠÷ §¬÷ ÷ äã ªæ÷ù........................ 35 4.3 “¯à ¢«÷ ï ¯®â®ª ¬¨ - â¥á⨠÷ 横«¨..................... 37 4.3.1 ”ã ªæ÷ù if ÷ swit h ................................ 37 4.3.2 –¨ª«¨ § ¢¨ª®à¨áâ ï¬ for, while ÷ repeat........... 39 4.4 ‘÷¬¥©á⢮ apply äã ªæ÷© ................................ 41 4.4.1 ”ã ªæ÷ï apply() .................................. 41 4.4.2 ”ã ªæ÷ù lapply(),sapply() ÷ repli ate() ............... 42 4.4.3 ”ã ªæ÷ï rapply() ................................. 43 4.4.4 ”ã ªæ÷ï tapply() ................................. 44 4.4.5 ”ã ªæ÷ï by() ..................................... 45 4.4.6 ”ã ªæ÷ï outer() .................................. 45 5 ‘â â¨á⨪ ................................................ 47 5.1 Žá ®¢ ÷ áâ â¨áâ¨ç ÷ äã ªæ÷ù............................. 47 5.2 ”ã ªæ÷ù ஧¯®¤÷«ã ©¬®¢÷à ®á⥩ ......................... 49 5.3 (cid:144)¥£à¥á÷© ¨© «÷§...................................... 54 5.3.1 ‹÷ ÷© ॣà¥á÷ï.................................. 54 5.3.2 (cid:141)¥«÷ ÷© ॣà¥á÷ï................................ 59 6 ƒà ä÷ª¨ ÷ £à ä÷ç ÷ ¯ à ¬¥âà¨............................ 63 6.1 ’¨¯¨ £à ä÷ª÷¢.......................................... 63 6.1.1 ”ã ªæ÷ï plot() .................................. 63 6.1.2 ‹÷ ÷© ÷ £à ä÷ª¨ .................................. 64 6.1.3 ƒ÷áâ®£à ¬¨ ÷ £à ä÷ª¨ £ãá⨠¨ ஧¯®¤÷«ã ............ 65 6.1.4 Q-Q(Š¢ ⨫ì-Š¢ â¨«ì ¨©) £à ä÷ª............... 69 6.1.5 ’®çª®¢÷ £à ä÷ª¨ .................................. 70 6.1.6 C⮢¯ç¨ª®¢÷ ¤÷ £à ¬¨............................. 71 6.1.7 Šà㣮¢÷ ¤÷ £à ¬¨ ................................. 72 6.1.8 (cid:129)®ªá¯«®â¨ ¡® áªà¨ 쪮¢÷ ¤÷ £à ¬¨ ................ 73 6.1.9 Ǒ®à÷¢ ï«ì ÷ ¤÷ £à ¬¨ ............................. 74 6.1.10 Œ âà¨æ÷ ¤÷ £à ¬ ஧á÷î¢ ï...................... 75 6.1.11 ’®çª®¢÷ £à ä÷ª¨ ¢¨á®ª®ù é÷«ì ®áâ÷ ................. 76 6.1.12 ƒà ä÷ª¨ 㬮¢ ¨å ஧¯®¤÷«÷¢....................... 76 6.1.13 3D £à ä÷ª¨ ...................................... 77 6.2 ‡¡¥à¥¥ ï £à ä÷ª÷¢ ã ä ©« ............................ 78 6.3 ƒà ä÷ç ÷ ¯ à ¬¥âਠ.................................... 81 6.3.1 ƒ«®¡ «ì ÷ ÷ «®ª «ì ÷ ãáâ ®¢ª¨ .................... 81 ‡¬÷áâ 5 6.3.2 Œã«ì⨣à ä÷ª¨................................... 83 6.3.3 Š®«÷à............................................ 84 6.3.4 ‘¨¬¢®«¨......................................... 85 6.3.5 ‹÷ ÷ù............................................. 87 6.3.6 (cid:144)®§¬÷à ᨬ¢®«÷¢, «÷ ÷© â âਡãâ÷¢ £à ä÷ª ........ 87 6.3.7 (cid:141) §¢¨ ÷ ¯÷¤¯¨á¨ .................................. 88 6.3.8 ’¥ªáâ £à ä÷ªã ................................. 89 6.3.9 ‹¥£¥ ¤ ......................................... 91 7 „®¤ ⮪ € ................................................ 94 7.1 ö áâ «ïæ÷ï R ........................................... 94 7.2 ‡ ¯ã᪠R............................................... 94 7.3 ‡ ¯ã᪠áªà¨¯â®¢®£® ä ©«ã *.R........................... 95 7.4 ‡ ¯ã᪠R ã ä® ®¢®¬ã २¬÷............................ 96 8 „®¤ ⮪ (cid:129) ................................................ 97 8.1 Ž¡'õªâ formula.......................................... 97 ‹÷â¥à âãà .................................................... 99 Ǒ®ª 稪 ....................................................100 1 ‡ ©®¬á⢮ § R R - ¬®¢ ÷ á¥à¥¤®¢¨é¥ ¯à®£à ¬ã¢ ï ®à÷õ ⮢ ÷, ¢ ¯¥àèã ç¥à£ã, áâ â¨áâ¨ç ÷®¡à åã ª¨, ¯¨á ïà÷§ ®£®à®¤ã¯à®£à ¬®¡à®¡ª¨, «÷§ã ¤ ¨å ⠯।áâ ¢«¥ ÷ १ã«ìâ â÷¢ ¢ £à ä÷ç ®¬ã ¢¨£«ï¤÷. R õ ¡¥§ª®è- ⮢ ¨¬¯à®£à ¬ ¨¬á¥à¥¤®¢¨é¥¬§¢÷¤ªà¨â¨¬ª®¤®¬,é®à®§¯®¢áî¤ãõâì- áï ®á ®¢÷«÷æ¥ §÷ùGNUGeneralPubli Li ense(§ á ®¢ ®îFreeSoftware 1 Foundation) ÷ § 室¨âìáï ã ¢÷«ì ®¬ã ¤®áâã¯÷. Ǒà®£à ¬¨ ¯¨á ÷ R § ¯ã᪠îâìáï ¡÷«ìè®áâ÷¯« âä®à¬ ÷ ®¯¥à æ÷© ¨åá¨á⥬ - FreeBSD,Li- nux, Ma OS, Windows. Ǒ஥ªâR¡ã¢÷ ÷æ÷©®¢ ¨©¯à æ÷¢ ¨ª ¬¨Žãª«¥ ¤á쪮£®ã ÷¢¥àá¨â¥âã (cid:144)®á®¬ öå ª®î â (cid:144)®¡¥à⮬ „¥ â«¥¬¥ ®¬ (Ross Ihaka, Robert Gentleman University of Au kland, New Zealand) ¯®ç âªã 90-x ÷ õ ¤÷ «¥ªâ®¬ ¡÷«ìè à ì®ù ¬®¢¨ ¯à®£à ¬ã¢ ï S ஧஡«¥ ®î Bell Laboratories 箫÷ § „® ®¬ —¥¬¡¥àᮬ (John Chambers) â ª®«¥£ ¬¨. öá ãõ ¯¥¢ ¢÷¬÷ ÷- áâì ¬÷ ¯à®£à ¬ ¨¬¨ á¥à¥¤®¢¨é ¬¨, ®¤ ª¯à®£à ¬ ¨© ª®¤ ¯¨á ¨© ¢ S, ¢ ¯¥à¥¢  ÷© ¡÷«ìè®áâ÷ ¡¥§ §¬÷ ¡ã¤¥ ¢¨ª® 㢠â¨áï ¢ R. ‘¥à¥¤®¢¨é¥ R ¬÷áâ¨âì è¨à®ªã £ ¬ã áâ â¨áâ¨ç ¨å ¬¥â®¤÷¢ â äã ªæ÷© («÷ ÷© ¨© ÷ ¥«÷ ÷© ¨© ॣà¥á÷© ¨© «÷§, áâ â¨áâ¨ç ÷ â¥áâ¨, «÷§ ç ᮢ¨å àï¤÷¢, ª« áâ¥à¨§ æ÷ù÷¡ £ â®÷ 讣®),£à ä÷ç ¨å÷ áâà㬥 â÷¢÷õ§ ç ®£ ãçª÷è- ¨¬ ÷ ÷ è÷ áâ â¨áâ¨ç ÷ ¯à®£à ¬ ÷ ¯à®¤ãªâ¨, ®áª÷«ìª¨ ª®à¨áâ㢠ç÷ ¯®áâ- ÷© ®¬®ãâì஧è¨àî¢ â¨äã ªæ÷® «§ à åã ®ª ¯¨á ï ®¢¨åäã ªæ- ÷©. ‚÷¤¯®¢÷¤ ÷ ¯ ª¥â¨, é® à¥ «÷§ãîâì ®¢÷ äã ªæ÷ù ÷ ஧è¨àîîâì ¬®«¨- ¢®áâ÷ R ஧¬÷éãîâìáï ¢ ® « © ª®«¥ªæ÷ù ¯ ª¥â÷¢ R. ‚ ¬¥à¥÷ Internet 2 á ©â÷ Comprehensive R Ar hive Network ÷á ãõ ¢¥«¨ç¥§ ª®«¥ªæ÷ï ¯ ª¥â- ÷¢ § äã ªæ÷ﬨ, é® ¢¥ ¢¨ª®à¨á⮢ãîâìáï ¢ à÷§ ®¬ ÷â ¨å ¯àשׁ å, ¢÷¤ âà ¤¨æ÷© ® áâ â¨á⨪¨ ¤® £¥®ä÷§¨ª¨, ¡÷®÷ ä®à¬ ⨪¨, ¥ª® ®¬¥âà÷ù, á®æ÷®«®£÷ù â ÷ è¨å áãá¯÷«ì ® ¢ «¨¢¨å ¤¨á樯«÷ å. ‚ æ쮬ã ᥠá÷ R § ¢¤¨ § 室¨âìáï ¯®¯¥à¥¤ã ¢ ¯®à÷¢ ï ÷ § ¯à®¯÷õâ à ¨¬¨ ¯à®£à ¬ ¨- ¬¨ á¥à¥¤®¢¨é ¬¨ ¯à¨§ ç¥ ¨¬¨ ¤«ï áâ â¨áâ¨ç ¨å ®¡à åã ª÷¢ ÷ «÷§ã ¤ ¨å. 1 2 http://www.gnu.org/li enses/ http:// ran.r-proje t.org/ 2 1 ‡ ©®¬á⢮ § R ö è®î á¨«ì ®î áâ®à® ®î R õ ¬®«¨¢÷áâì ¯à¨£®â㢠ï in situ ¢¨- ᮪®ïª÷á ¨å ÷ ÷ ä®à¬ ⨢ ¨å £à ä÷ª÷¢ ¤«ï ¯ã¡«÷ª æ÷© ¢ 㪮¢¨å ¢¨- ¤ ïå, §¢÷â å â web áâ®à÷ ª å. 1.1 Ǒ®ç ⮪ ஡®â¨ R ¤®áâ㯠¨© á ©â÷ Comprehensive R Ar hive Network (CRAN) ¡® 3 ®¤ ®¬ã § ©®£® ¤§¥àª « § ¢÷¤¯®¢÷¤ ¨¬¨ ¯®á¨« ﬨ . Ǒ÷á«ï ÷ áâ «ïæ÷ù 4 â § ¯ãáªã (¤¨¢. „®¤ ⮪ € ) ¢÷¤¡ã¢ õâìáï ÷ ÷æ÷ «÷§ æ÷ï á¥à¥¤®¢¨é R R version 2.11.1 (2010-05-31) Copyright (C) 2010 The R Foundation for Statisti al Computing ISBN 3-900051-07-0 R is free software and omes with ABSOLUTELY NO WARRANTY. You are wel ome to redistribute it under ertain onditions. Type 'li ense()' or 'li en e()' for distribution details. Natural language support but running in an English lo ale R is a ollaborative proje t with many ontributors. Type ' ontributors()' for more information and ' itation()' on how to ite R or R pa kages in publi ations. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interfa e to help. Type 'q()' to quit R. > ¯÷á«ï箣® ¨á⥬ £®â®¢ ¤®à®¡®â¨.‡ ª>®§ ç õ£®â®¢ ÷áâ줮¢¢¥¤¥ ï ÷ ¢¨ª® ï ª®¬ ¤. (cid:141) ¡÷à ª®¬ ¤ â ª® ¬® § ¯ãáâ¨â¨ ®ªà¥¬® § ä ©«ã-áªà¨¯âã. 1.2 Ǒਪ« ¤¨ ¢ R C¥à¥¤®¢¨é¥ R ªâ¨¢ ® ¢¨ª®à¨á⮢ãõâìáï ¢ § ¤ ç å, ¯®¢ï§ ¨å § ®¡à- ®¡ª®î, «÷§®¬ ÷ ¢÷§ã «÷§ æ÷õî áâ â¨áâ¨ç ¨å ¤ ¨å. Ž¤¨ § ¯à¨ª« ¤÷¢ ¤¥¬® áâàãõ, ïª §£¥ ¥à㢠⨠¢¨¯ ¤ª®¢÷ç¨á« ÷ ¯à¥¤áâ ¢¨â¨ùå ÷© ஧¯®¤÷« ã ¢¨£«ï¤÷ £÷áâ®£à ¬¨ 3 4 http:// ran.r-proje t.org/mirrors.html ‚¥àá÷ïR¬®¥¢÷¤à÷§ ïâ¨áï.(cid:141) ¬®¬¥ â ¯¨á ï®ä÷æ÷© ¨©à¥«÷§-Rversion 2.11.1 (2010-05-31) 1.2 Ǒਪ« ¤¨¢ R 3 > x <- rnorm(1000) # £¥ ¥à æ÷ï 1000 ¢¨¯ ¤ª®¢¨å ç¨á¥« # § ஧¯®¤÷«ã ƒ ãá # ஧à åã ®ª £÷áâ®£à ¬¨ ¤«ï §¬÷ ®ù x, ª÷«ìª÷áâì # ÷ â¥à¢ «÷¢ 50 > histogram <- hist(x, breaks=50, plot=FALSE) # à¨áã ®ª £÷áâ®£à ¬¨ § ¤®¯®¬®£®î äã ªæ÷ù plot() > plot(histogram, ol="blue",border="red") Histogram of x 80 60 Frequency 40 20 0 −2 0 2 4 x (cid:144)¨á. 1.1. ƒ÷áâ®£à ¬ ¯®¡ã¤®¢ ®á ®¢÷ ¢¨é¥§£ ¤ ®£® ¯à¨ª« ¤ã. R¬® ¢¨ª®à¨á⮢㢠â¨ïªª «ìªã«ïâ®à(¢ ©¯à®áâ÷讬㢨¯ ¤ªã) > 1+1 [1℄ 2 > sqrt(81) # ª®¬¥ â à÷©, [1℄ 9 # äã ªæ÷ï sqrt() ¢¨ª® ãõ à åã ®ª ª®à¥ ï ª¢ ¤à â ®£® > os(pi/3) [1℄ 0.5 4 1 ‡ ©®¬á⢮ § R â ª ÷ «÷§ã¢ ⨠¤ ÷, é® § 室ïâìáï ¢ ¬¥à¥÷ Internet. (cid:141) ¯à¨ª« ¤ & ¯à¥¤áâ ¢¨â¨ ÷áâ®à÷î §¬÷ ¨ ä® ¤®¢®£® ÷ ¤¥ªáã S P500 ¬®¬¥ â § ªà- 1500 1000 Close data$ 500 0 1950 1960 1970 1980 1990 2000 2010 & (cid:144)¨á.1.2.öáâ®à÷ï§ ç¥ ì÷ ¤¥ªáãS P500®âਬ ÷§á ©âãfinan e.yahoo. om ÷ ¯à¥¤áâ ¢«¥ ÷¢ £à ä÷ç ®¬ã ¢¨£«ï¤÷ § ¤®¯®¬®£®î R. 5 ¨ââï ¢ £à ä÷ç ®¬ã ¢¨£«ï¤÷ # ®§ ç¥ ï ¤à¥á¨ ¤¥à¥« ¢ ¬¥à¥÷ internet > address = "http://i hart.finan e.yahoo. om/table. sv? s=%5EGSPC&d=10&e=30&f=2010&g=d&a=0&b=3& =1950&ignore=. sv" # §ç¨âã¢ ï ¤ ¨å ÷ ¯à¨á¢®õ ï ùå §¬÷ ÷© data > data <- read. sv(file=url(address)) # ª® ¢¥àâ æ÷ï ç ᮢ®£® ä®à¬ âã > time <- strptime(data$Date,"%Y-%m-%d") # ¯à¥¤áâ ¢«¥ ï ¤ ¨å ¢ £à ä÷ç ®¬ã ¢¨£«ï¤÷ > plot(time,data$Close,type='l') öá ãîâì ¯¥¢ ÷ ®¡¬¥¥ ï ¯à¨á¢®õ ï §¢ ®¡'õªâ ¬ ¢ R: • # ‚ §¢ å ®¡'õªâ÷¢ ¥ ¬®ãâì ¡ã⨠ᯥæ÷ «ì ÷ ᨬ¢®«¨ !, +, -, . 5 €¤à¥á ¤¥à¥« ¤ ¨å (¯ à ¬¥âà address) § ç ᮬ ¬®¥ áâ ⨠¥ ªâ- ã «ì ®î - ¢ â ª®¬ã à §÷ ¥®¡å÷¤ ® ®¡ ®¢¨â¨ ¯®á¨« ï § á ©âã & http://(cid:12)nan e.yahoo. om/indi es ¢ ç á⨠÷ S P500 (ᨬ¢®« ÷ ¤¥ªáã ^GSPC) Histori al Pri es

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.