R?? - PowerPoint PPT Presentation

1 / 63
About This Presentation
Title:

R??

Description:

Title: R Author: Last modified by: Stanley Tang Created Date: 2/16/2004 8:00:05 AM Document presentation format: – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 64
Provided by: 4518
Category:
Tags: brackets | math

less

Transcript and Presenter's Notes

Title: R??


1
R????
?????? ???
2
R??
  • ??????R??
  • R???
  • ?????
  • ??
  • ???????
  • R?????
  • R_GUI
  • R_Commander
  • R_WinEdt
  • R_ESSXEmacs

3
  • ??/??????
  • John Verzani, Simple R Using R for Introductory
    Statistics, 2002 http//www.math.csi.cuny.edu/Sta
    tistics/R/simpleR/index.html
  • J.H. Maindonald, Using R for Data Analysis and
    Graphics An Introduction, 2001
  • Julian J. Faraway, Practical Regression and ANOVA
    Uing R, 2002http//www.stat.lsa.umich.edu/farawa
    y/????http//www.stat.lsa.umich.edu/faraway/
    stat500/introR.html
  • S. Chatterjee, A.S. Hadi B. Price, Regression
    Analysis by Example, 3rd Ed., John Wiley Sons,
    2000http//www.ilr.cornell.edu/hadi/RABE/
  • (???,???????,2003)

4
R??????
Robert Gentleman
Ross Ihaka
Bill Venables
5
??????R??
  • R?
  • ????(GPL)???????
  • ????,?S??(?ATT Bell????Rick Becker, John
    Chambers,Allan Wilks??)?????(dialect)
    ??,????S-plus.
  • ????,?????????????????????
  • R??????(project),??(1995?)??Auckland??????Robert
    Gentleman?Ross Ihaka????,???R??????(R Development
    Core Team ???R DCT??)??,?????????????,??????????
    ?????????????????R?????(http//www.r-project.org)?
    ???R??????????,???????R?????R????????.

6
  • R??????!! ?S-Plus??????????????,?????????US .
  • R??????UNIX, Windows?Macintosh?????? .
  • R??????????????.
  • R?????????.
  • ???R????????S-Plus???,??S??????????????R.
  • ??R???????????,????????R?????.
  • ?????????????????R??(??????????????!!)
  • ....

7
??R???
  • R?? http//www.r-project.org
  • CRAN (Comprehensive R Archive Network),
  • http//cran.r-project.org
  • CRAN????? http//cran.r-project.org/mirrors.html
  • UCLA?????R?S-Plus???,??????
  • http//statcomp.ats.ucla.edu/splus/default.htm
  • ????????R?Windows?? http//cn.math.pku.edu.cn/tea
    chers/lidf/index.html
  • ????FTP??(?Cuteftp)?????(????)ftp.u-aizu.ac.jp

8
???????
  • CRAN??????????????http//cran.r-project.org/src/
    contrib/PACKAGES.html
  • stable -- ??(??)??????
  • tseries ??????
  • VaR ?????
  • matrix ????
  • cinterface C?R???
  • foreign ???S, Minitab, SAS, SPSS, Stata??????
  • normix ????????
  • nortest ?????Anderson-Darling??
  • MCMCpack ??Gibbs???MCMC????
  • fracdiff ?????????????
  • ????

9
  • ????
  • ?????pdf??(help-gtmanuals),?????
  • W.N. Venables, D.M. Smith and the R DCT
    Introduction to R -- Notes on R A Programming
    Environment for Data Analysis and Graphics,
    2003.http//bayes.math.montana.edu/Rweb/Rnotes/R.
    html
  • R DCT, The R Environment for Statistical
    Computing and Graphics -- Reference Index,2003.
  • R DCT, R Data Import/Export, 2003.
  • R DCT, R Language Definition,2003
  • R DCT, Writing R Extensions,2003

10
  • Kurt Hornik, R FAQ, Version 1.8-1, 2003-10-07
  • B. D. Ripley, R for Windows FAQ, Version for
    rw1080
  • R Html Help, Statistical Data Analysis
  • ??PDF/HTML??
  • Kickstarting R, http//cran.r-project.org/doc/cont
    rib/ Lemon-kickstart/
  • R examples, Alison Gibbs, http//www.utstat.toron
    to.edu/alisong/Teaching/ Winter04/Sta248/Rex.html

11
  • Ko-Kang Wang, Introduction to R for
    Statisticians,2004
  • J.H. Maindonald, Using R for Data Analysis and
    Graphics An Introduction, 2001
  • J.H. Maindonald, Using S-PLUS for Data Analysis
    and Graphics, 2001
  • Emmanuel Paradis, R for beginners
  • Jonathan Baron, R reference card
  • Bret Larget, R for Introductory Statistics, 2002
  • W. N. Venables and B. D. Ripley,Modern Applied
  • Statistics with S,4th Ed., 2002
    http//www.stats.ox.ac.uk/pub/MASS4/

12
  • ?????
  • Peter Dalgaard, Introductory Statistics with R,
    Springer, 2002
  • John Maindonald, John Braun, Data Analysis and
    Graphics Using R -- An Example-based Approach,
    Cambridge University Press,2003
  • John Fox, An R and S-Plus Companion to Applied
    Regression, Sage Publications, Inc., 2002

13
R?????-1
  • R_GUI
  • ??R,????R GUI (graphic users interface)????,
    ???????
  • ???
  • ???
  • R console (R?????)
  • R console
  • ????????????????????,????????,?????,???.
  • ????????????
  • help.start() HTML?????R?????
  • help() ?????????,??help(plot)
  • demo() ??R???????
  • q() ??R
  • ?Matlab??,??shift??????????

14
  • ?????

Source R code
Load image
Save image
Copy and Paste
Stop current computation
  • Source R code ??R??(.R?.r)
  • Save image ??????,????.RData
  • Load image ?????????
  • Stop current computation ??????(???????)

15
  • ?????

16
R?????-2
  • R_Commander (????)
  • ?? John Fox (jfox_at_mcmaster.ca)
  • ??Rcmdr Version 0.9-0
  • ??
  • Rcmdr?R?????,??????car???
  • ???Rcmdr????,???????car
  • Rcmdr?????RGui (SDI)???,????RGui?EditgtGUI
    preferences????(??????)
  • ??
  • ????car
  • ?RGui???PackagesgtLoad packages??Rcmdr??

17
(No Transcript)
18
  • ??
  • R Commander????????/?????(menu/dialog-box
    interfaces),?????????????????????.????????????????
    ??????.
  • ?????(???Rcmdr?Getting Started??)
  • R Commander????????????.
  • ????????log/script??
  • ??????????????RGUI?,??????????.???????log???????.?
    ??????????,????Rcmdr??????Submit????????R??
  • ?????????????????.???????????,??????.???DatagtImp
    ort data?????,????????????,??R GUI?????!!

19
  • R Commander???

R Commander ???(Menu Tree) (??)
  • R Commander?????(information field)

20
R?????-3
  • R_WinEdt ?????
  • ?? Uwe Ligges (ligges_at_statistik.uni-dortmund.de)
  • ??RWinEdt Version 1.6.0
  • ????
  • R http//cran.r-project.org/
  • WinEdt5.x http//www.winedt.com/ (?CTeX2.3.x)
  • R-WinEdt http//cran.r-project.org/contrib/extra/
    winedt/
  • R_WinEdt???
  • ?R_WinEdt???????WinEdt?plugins\R-WinEdt???
  • ??install.exe????????,????WinEdt???????R.ini(?????
    winedt.exe!)
  • ????????????CTeX2.3.x,???????,??WinEdt?
    C\CTeX\WinEdt ???

21
  • R_WinEdt???
  • ???????
  • ????????LaTeX/CTeX?R_WinEdt,????????WinEdt???,
    ????R_WinEdt
  • ??R_WinEdt???,????,?????(T)???
  • C\CTeX\WinEdt\WinEdt -C"R-WinEdt" -er.ini
  • (????)??????R??????/???????,???R????etc??.Rprofile
    ????,?????simple???????????
  • library(simple)
  • options(editor"\"c/program files/winedt/winedt\"
  • -c\"R-WinEdt-edit\" -er.ini -V")

22
R_WinEdt ??
23
R_WinEdt ??
24
  • R_WinEdt???
  • - ?RGui????
  • - ??WinEdt????? (?Delimiter??, ????, ??,?,
    ?????????)
  • - ??????(Syntax-Highlighting)
  • - ????????R??
  • - ?????????????
  • - ???????(script)???R???
  • - WinEdt??????(code)???R???
  • - ????(script)???R???
  • - ???????????? .Rhistory,??????????
  • - ??????,? for(_ in _)_

25
  • R_WinEdt ?????
  • Command Hot Key
    Menu Icon ??
  • -------------------------------------------------
    --------------------------------------------------
    ---
  • Brackets Check CtrlF12
    ??????
  • R History ALTH
    R HIST. ??????
  • R-line - and go ALTL
    ????
  • R-line CtrlALTL
    ???????
  • Rlt- R-paste - and go ALTP
    Rlt-PASTE ?????
  • Rlt- R-paste CtrlALTP
    ????????
  • Rlt- R-source - and go ALTS
    Rlt-SOURCE R????(???)
  • Rlt- R-script CtrlALTS
    R SCRIPT R???????
  • function CtrlAltF
    ??????
  • for CtrlAltO
    ??for????
  • if CtrlAltI
    ??if??
  • ifelse CtrlAltE
    ??ifesle??
  • lt- Ctrl-
    ??????

26
R?????-4
  • R_ESSXEmacs ????? (?? John Fox)
  • ??ESS?XEmacs
  • Emacs???????????????,????????,??????,??(debugging)
    ?.
  • For some Unix/Linux users, Emacs is more a
    way of life than an editor.
  • ESS(Emacs Speaks Statistics)??????????????????,??R
    , S-Pplus, SAS, Stata, Lisp-Stat?.
  • Emacs??????? GNU Emacs ? XEmacs.
  • ?????ESS,??????????????????.
  • John Fox?ESS??????????,???????
    ???????R????/??,?????????R??/???XEmacs???????????.
    (??????)
  • XEmacs?ESS?????!

27
(No Transcript)
28
  • ???????
  • R http//cran.r-project.org/
  • Xmacs with Ess http//www.xemacs.org/Download/win
    32/???Xemacs????(installer) setup.exe???,????net?
    ????Xemacs??????????????
  • ess mail-lib
  • dired pc
  • ediff speedbar
  • edit-utils vc
  • efs xemacs-base
  • fsf-compat xemacs-i586-pcwin32
  • ??????????,?????????(local directory)

29
  • XEmacs Netinstaller --- Install from the Internet
  • Please note that these screenshots do not contain
    the latest version numbers for XEmacs, its
    packages and the installer itself. They are meant
    to give you a general idea of what the
    download/installation process will look like.
  • An overview of existing packages and their
    purpose may be found at
  • http//www.xemacs.org/Develop/packages.html
  • and under the XEmacs source tree in etc/ PACKAGES.

????, ???New????????? ??????Skip???.
????????????????????.
????Native??(??Windows??Xemacs)
30
  • R_ESSXEmacs???
  • ????R?XEmacs??????
  • R C\Program Files\R\rwxxxx\ (xxxx????)
  • Xemacs C\Program Files\Xemacs
  • ?????home??(WinXP/2000/NT???DOS?????? set HOME
    ??. ?????c\
  • ?home????????(?DOS??mkdir) .xemacs
  • ?John Fox?????????fox-ess-config.zip ,
  • ???http//socserv.socsci.mcmaster.ca/jfox/Boo
    ks/Companion/ESS/index.html
  • ?????init.el???Rinit.el???? .xemacs?

31
  • ?????function.xpm, line.xpm, para.xpm,
    region.xpm, source.xpm?stop.xpm?????
  • c\Program Files\Xemacs\XEmacs-xx.y.z\etc\
    toolbar\
  • (xx.y.z?Xemacs????),???Xemacs???R????.
  • (???)???????? c\Program Files\R\rwxxxx\bin
  • (?WinXP/2000/NT???????gt??gt??gt???? ???/??).
  • ???? ????????Rini.el????R???,? (setq-default
    inferior-R-program-name c/Program Files/
    rwxxxx/bin/rterm.exe) ----???????????().
  • ??????XEmacs??? ???????????
  • -q -l c\.xemacs\Rinit.el, ??????????????.

32
  • ESSXEmacs?R??? ---??XEmacs??
  • ???????
  • ????R_XEmacs???????Initialization
    complete,????OK????.
  • ??????????????(minibuffer?)?????????? .Rhistory
    ???. ????????????????R?ESS???(???????gt????????????
    ????!)

33
(No Transcript)
34
  • ????
  • John Fox?Xemacs???(?????frame)???????,????(?????).
  • ?Xemacs?????????????(menu bar)??????(toolbar),????
    ????(buffer)???(major mode)???.???????????????????
    ????????.
  • J. Fox?R????????????,??????(????).
  • ????????scratch???,???????R??.????,
    scratch????????,???????????????.
  • ????????R?Emacs?????,??????????????R??.

35
  • ?????????????(mode line),????????,????1)?????
    2)??????(????) 3)?????????.
  • ????????scratch buffer,??ESSS???(?????????????
    . S?S??,?????? ? .r, .R, .s, .S,
    .q?S??????,???ESSS????),????(ALL)?????????.
  • ????????R buffer, ??iESS (inferior ESS
    ??????inferior,?????Emacs???!), ?????????(Bot).
    ???????????????.
  • ??(point) Emacs???,?????????????,??John
    Fox?ESS????Windows????????.
  • ???????????????,??minibuffer(???),????????,???????
    Emacs??,??????minibuffer?,?????(??????)???????.

36
  • ESSXEmacs?R???
  • --- R_XEmacs??
  • Paste to R(???R)?? --- ????????????????????
  • Paste line ???????
  • Paste paragraph ????????(??????)
  • Paste function ?????????????
  • Paste region ??????????
  • Source to R ???????(???)??????
  • ??R??????????????????
  • ??scratch buffer???????????
  • (?????????)R?????????????????????????!
    ????????minibuffer???Process to load into
    R,??????
  • Stop ??????(?????)

37
  • R???
  • ????/????R?????,????ESS??,??ESS???John
    Fox???R???R???.
  • R????????????
  • File ??
  • Packages ??
  • Misc ??
  • ??????R GUI???

38
  • ESSgtRgtFile??
  • Source R code ???????????
  • Save workspace ?R?????????
  • Load workspace ?????????
  • Change R directory ??????(?R??????,

    ??????????????)

39
  • ESSgtRgtpackages??
  • Load package ??R??,??????
  • Install package from CRAN
    ?CRAN???.??.????(??internet??!)
  • Install package from local zip file
    ???????????(zip????)
  • Update packages from CRAN
    ?CRAN????? ????????????, ?????

40
  • ESSgtRgtMisc??
  • Remove all objects ??R??????????
  • List objects
    ??R????????
  • Display path ??R?????
  • FilegtExit R/Xemacs??
  • Quit saving R workspace ????R?????????
  • Just quit
    ???R????????R,??? ???????????????

41
R????????
  • ??
  • ??
  • gt ????????
  • ???
  • ??????
  • ??
  • - ??
  • ??
  • / ??
  • ??
  • ???
  • ? lt-

42
  • ???
  • ?
  • help()
  • ??
  • gt35
  • gt3-5
  • gt3/5
  • gt35
  • gtx5
  • gt?plot
  • gthelp(plot)

43
  • ??
  • ???R????????
  • ??????????????,??
  • ???
  • ??
  • ?????
  • ?????
  • ???
  • ???
  • ???

44
  • ???????(??)
  • seq() ? ???(??)?????????
  • rep() ???(??)?????????
  • c() ???(??)??????
  • ??
  • gt110
  • gtseq(1,10,by0.5)
  • gtseq(1,10,length21)
  • gtrep(25,2)
  • gtrep(25,rep(2,4))
  • gtxc(42,7,64,9)
  • gtlength(x)

45
  • ??????????(recycling rule)
  • gt1214
  • gt1417

46
  • ?????(index)?????(??)???
  • ???? ??????????
  • ???? ??????????
  • ???? ????????????????
  • ?R???????1??,????????????
  • ????C?????????????????0??!
  • ??
  • gtx c(42,7,64,9)
  • gtx1
  • gtx-2
  • gtxc(1,4)

47
  • gtxgt10 ???10??????
  • 1 TRUE FALSE TRUE FALSE
  • gtxxgt10 ???10???
  • 1 42 64
  • gtxxlt40xgt10
  • gt ??(0,1)?100????????
  • gty runif(100, min 0, max 1)
  • gtsum(ylt0.5) ???0.5??????
  • 1 47
  • gtsum(yylt0.5) ???0.5???????
  • 1 10.84767

48
  • ????(data frame)
  • ???????????????
  • ??????????????????????,?????,????????,???????
  • ?????????(??)
  • ???????????????,?????
  • read.table() ?????????ASCII??
  • ????????????,??????????
  • read.csv() ?????????????(Comma
    Seperated values)
  • read.delim() ????????tab????????
  • ???????(?1,000,000??x200??),???
    ODBC????????.

49
  • ???R???????????????????,?????data.frame(),?????cbi
    nd().
  • ??
  • gtxc(42,7,64,9)
  • gty14
  • gtz.dfdata.frame(cbind(INDEX y, VALUE x))
  • gtz.df
  • INDEX VALUE
  • 1 1 42
  • 2 2 7
  • 3 3 64
  • 4 4 9
  • ? ? .df ????????z.df??????? ?
    INDEX?VALUE??????????

50
  • ??????????
  • ?? foorow, column?? foo ???????
    row ???????? column
    ????????
  • ??(????)

gt z.df1, ???????(???)
INDEX VALUE 1 1 42 gt z.df,1
?????(???????) 1 1 2 3 4 gt z.df2,1
??????????? 1 2
51
  • ??(list)
  • ????????,????????????
  • ???????????????
  • R???(list)???????????
  • ??
  • gtfoo list(x 16, y matrix(14, nrow 2))
  • gtfoo
  • x
  • 1 1 2 3 4 5 6
  • y
  • ,1 ,2
  • 1, 1 3
  • 2, 2 4

52
  • ????????
  • ????????foo?x,????????gt fooxgtfoo1gtfoo1
    ??
  • gtfooy
  • gtfoo2
  • gtfoo2
  • gtfoo12
  • gtfooy2
  • gtfooy4

53
  • ????
  • ?? ???????????????
  • ??1 if (??) ???1 else ???2
  • ??2 ?????1!ifelse(??, yes, no)
  • ??????????gtx c(6-4)
  • gt sqrt(ifelse(x gt 0, x, NA))
  • gt ifelse(x gt 0, sqrt(x), NA)
  • gtif (x gt 0) sqrt(x) else NA

54
  • ??(loops)
  • for() ???????for (?? in ??) ???
  • while() ?????????while(??) ???
  • ?????????1???????gtfor (i in 15) print
    (1i)gti1gtwhile(i lt 5) print(1i) i
    i1

55
  • ?2 ?Ko-Kang Wangs R Programming Workshop,
    pp6-8
  • Suppose we generate a pseudo DNA microarray and
    we want to do an ANOVA on it. First we generate
    some factors for Array (a), Treatments (t) and
    Genes (g). Then generate some normal random
    numbers for the logged foreground intensity. Then
    we put into an aov() function for each gene
    this is where the loop is good for. Note that you
    will get different answer when you try it,
    because of the random numbers generated.
  • ????(??for??,????while??)DNA_anova.R?R????????
    ?gt?,??????????!

56
  • gt n 3044
  • gt a c(rep(1, 2 n), rep(2, 2 n))
  • gt t c(rep(1, n), rep(2, n), rep(2, n), rep(1,
    n))
  • gt g rep(rep(11522, rep(2, 1522)), 4)
  • gt y rnorm(4 n, mean 4.13, sd 0.75)
  • gt ybar data.frame(A factor(a), G
    factor(g),
  • T factor(t), Intensity y)
  • gt attach(ybar)
  • gt ybar110, ??ybar??10?
  • gt res.mat matrix(0, 1522, 8, byrow TRUE)
  • gt coef.mat matrix(0, 1522, 4, byrow TRUE)
  • gt for(i in 11522)
  • gene.aov aov(Intensity A T A T,
  • sub G i)
  • res.mati, residuals(gene.aov)
    ??ANOVA?????
  • coef.mati, coef(gene.aov)
    ??ANOVA???????
  • gt res.mat110, ??????10?
  • gt coef.mat110, ????????10?

57
  • ???(vectorization)
  • ??(loops)???,????????????,????????,????
  • C???????,???????R?????????????,??C??R?100??
  • ?R????????,??R?????C????,???????????!
  • ?? --?Ko-Kang Wangs R Programming Workshop,
    pp9-11 (Gamma????)
  • Vector_1.R ?????????????
  • Vector_2.R ??for?????????
  • Vector_3.R ?????????

58
  • ??
  • ???????????,?R??????????
  • ?? ??? function( ???? ) ???
  • ???? ???(????)
  • ????????,????!
  • ?? ??gamma???n!gtfactorial function(n)
  • if (ngt0) gamma(n1)
  • else print(Please input a positive
    integer!")
  • gtfactorial(6)
  • gtfactorial(-6)

59
  • ????????? ??????????????????????
  • warning() ??????????????
  • stop() ???????????
  • print() ???????
  • formatC() ?????????
  • cat() ????,????\n(??)?\t(tab?)
  • paste() ????(????????)??gtcat("R",
    "is","a good", "software.\n")
  • gtformatC(1/3, format "f", digits 4)gt
    formatC(1/3, format "e", digits
    4)gtpaste(112) ?as.character(112)??gtpaste("A
    ", 16, sep "")gtpaste("today is", date())

60
R????
  • ??????
  • ??????(?)???????????????,???????????
  • ?????????!!
  • ??!!
  • ??!!
  • SAS???????????????????,?!!??SAS??????,?SPSS??!
  • ????????(??)????????????

61
  • ??????
  • ????,?????! ????????
  • ???(readability)
  • ????(understandability)
  • ????????(Indentation),????????
  • WinEdt gt ??????WinEdt?R????
  • (X)Emacs gt??????(X)Emacs?R????
  • UltraEdit
  • ????????(Commenting),??????
  • R????????????.
  • ?????????,?????????,???????????

62
  • ?? ??
  • gt for(i in 11522)
  • gene.aov lt- aov(Intensity A T A T,
  • sub G i)
  • res.mati, lt- residuals(gene.aov)
  • coef.mati, lt- coef(gene.aov)
  • ?
  • gtfor(i in 11522)
  • gt
  • gene.aov lt- aov(Intensity A T A T,
  • sub G i)
  • res.mati, lt- residuals(gene.aov)
  • coef.mati, lt- coef(gene.aov)

63
  • ????
  • 1.Julian Faraway??????????
  • Introduction to R
  • ?? R GUI
  • ?? R Commander
  • ?? R_XEmacs
  • Further reference Moore's The Basic Practice of
    Statistics, Second
    Edition (Freeman, 2000),
  • 2. John Verzani ???????? SimpleR --- Using R
    for Introductory Statistics
  • ??R_WinEdt
Write a Comment
User Comments (0)
About PowerShow.com