复旦R语言教学课件完整版本_第1页
复旦R语言教学课件完整版本_第2页
复旦R语言教学课件完整版本_第3页
复旦R语言教学课件完整版本_第4页
复旦R语言教学课件完整版本_第5页
已阅读5页,还剩47页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、R: Statistics? Programme?and Who are You?- An ABC introduction to RPresented byGuohui DingR&D, SIBS, CASFor Fudan University第1页,共52页。Main Topics Today What is R?How to administrate R?How does R work?How to apply R for statistical problem?How to program your R function?第2页,共52页。What is R?A brief hist

2、ory of R第3页,共52页。The legend of RR started in the early 1990s as a project by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, intended to provide a statistical environment in their teaching lab. The lab had Macintosh computers, for which no suitable commercial environment

3、was available.Robert GentlemanRoss Ihaka第4页,共52页。Rs Parents(1)The S languageS: an interactive environment for data analysis developed at Bell Laboratories since 1976Exclusively licensed by AT&T/Lucent to Insightful Corporation, Seattle WA. Product name: “S-plus”.You can learn more from:My father is

4、S, mother is Scheme, but why my name is “R”?第5页,共52页。The Scheme languageScheme is a statically scoped and properly tail-recursive dialect of the Lisp programming language invented by Guy Lewis Steele Jr. and Gerald Jay Sussman. Learn more: Schemes underlying semantics + Ssyntax = RRs Parents(2) “ We

5、 have named our language R in part to acknowledge the influence of S and in part to celebrate our own efforts.”- R. Ihaka R. Gentleman - Ihaka R. & Gentleman R., 1996第6页,共52页。R NowSince mid-1997 there has been a core group who can modify the R source code CVS archive.The R package system CRAN (the C

6、omprehensive R Archive Network )第7页,共52页。The characters of RR is “GNU S” A language and environment for data manipula-tion, calculation and graphical display. That is R is a Free Software (or Open source software). (Here, Free refers to freedom, not price, although R is free in that sense as well.)T

7、he core of R is an interpreted computer language.A mosaic of procedure-based programming and object-oriented programming Good interface to procedures written in C, C+, FORTRAN and other languagesA flexible data exchange mechanism accessingrelational databases -ODBC, PostgreSQL, MySQL and so on.小偷与强盗

8、的谈判第8页,共52页。R and StatisticsMost packages deal with statistics and data analysis.Powerful statistical graphics.Well crosstalking with other statistical softwares.Most R user are statistical experts. You can learn more modern analysis method from they by email.You can do it when you come across a thi

9、ng no body do it before.第9页,共52页。Install and administrate R Focus on Windows(MS)第10页,共52页。How do I get R?The informational web site CRAN - the Comprehensive R Archive Network. The primary site is .Mirror sites are available for many countries.CRAN sites have binary distributions for Windows 95, 98,

10、ME, NT4, 2000 and XP on Intel, for the Macintosh (System 8.6 to 9.1 and MacOS X), and for several Linux distributions.New releases occur frequently about every 3 months.Be prepared to re-install frequently.Also you can get it from your friends, teachers, etc.Down it!It is about 20.6M in size.Using P

11、recompiled Binary Distributions第11页,共52页。Installing RDouble click “rw1091.exe” using your mouse. That is OK. You can install it as all other standard MS softwares.第12页,共52页。R Console/RGui in Windows(MS)Command boxGraphics boxMenuIcons第13页,共52页。Several concepts in Administrating R Workspacexxx.RDataH

12、istoryxxx.RhistoryPackageObjectSession ConsoleRun your R codesLoad/save workspaceLoad/save HistoryChange your working directory- Ihaka R. & Gentleman R., 1996第14页,共52页。Add a new packageCommands:library()add a package in the librarydetach(package : xxx)detach a packageAll can do in the GUI (except de

13、tach()Load a local packageInstall packages frominternet or localUpdate the local package from internet第15页,共52页。Packages in R EnvironmentBasic packagespackage:methods package:stats package:graphics“ package:utils package:base Recommanded packagesgrid; lattice;e1071Contributed packages (more than 366

14、 packages nowadays)You can see what packages loaded now by the command search().第16页,共52页。Dont lose your way!Three useful system commandgetwd()Get Working Directorysetwd() Set Working Directorylist.files()List the Files in a Directory/Folder第17页,共52页。Show the Demonstrations of the Packages/Functions

15、Commandsdemo()Demonstrations of R Functionalityexample()Run an Examples Section from the Online Help第18页,共52页。Getting HelpsSeveral commandshelp.start()help() or ?()help.search()apropos()Internet searchingI like it verymuch. It seemsomnipotence.第19页,共52页。Quit RCommandq()Terminate an R Session第20页,共52

16、页。How does R work?Basic R Structure and data manipulation第21页,共52页。Basic R working flow(Object orientation)package- R for Beginners. Emmanuel Paradis第22页,共52页。Object orientationObject: a collection of atomic variables and/or other objects that belong togetherParlance:class: the “abstract” definition

17、 of itobject: a concrete instancemethod: other word for functionslot: a component of an object第23页,共52页。Types of Data in RThe basic data object is a vector of elements of type:numeric numbers - either floating point or integercharacter each element is a character stringlogical each element is TRUE o

18、r FALSElist elements can be any type of object, including other listsComponents of the S language, such as functions, are also vectors.Any vector can include the missing data marker NA as an element.All vectors have a length and a mode. The functions length and mode return this information as does t

19、he str function.A structure consists of a data object plus additional information. Matrices (or arrays, in general) and time series are examples of structures.第24页,共52页。Operators第25页,共52页。Vectors, Matrices and ArraysCommand: array(data = NA, dim = length(data), dimnames = NULL) matrix(data = NA, nro

20、w = 1, ncol = 1, byrow = FALSE, dimnames = NULL)第26页,共52页。ListsList vs. Vectorlist: an ordered collection of data of arbitrary types. vector: an ordered collection of data of the same type.Typically, vector elements are accessed by their index (an integer), list elements by their name (a character s

21、tring). But both types support both access methods.第27页,共52页。FactorsFactors: classification variablesIf the levels of a factor are numeric (e.g. the treatments are labelled“1”, “2”, and “3”) it is important to ensure that the data are ctually stored as a factor and not as numeric data. Always check

22、this by using summary.第28页,共52页。Data framesdata frame: is supposed to represent the typical data table that researchers come up with like a spreadsheet.It is a rectangular table with rows and columns; data within each column has the same type (e.g. number, text, logical), but different columns may h

23、ave different types. ( A list actually)第29页,共52页。Subsetting Individual elements of a vector, matrix, array or data frame are accessed with “ ” by specifying their index, or their name第30页,共52页。Using R on Windows(MS)Basic statistical analysis by R第31页,共52页。Data InputFrom the keyboard one by onec( );

24、scan( )From the fileread.table(); read.csv(); read.csv2(); read.dta(); read.spss(); By a spreadsheetdata.entry()edit()fix()第32页,共52页。Data EditCommandsedit()fix()Tips: edit() can invokean notepad in the RGui!第33页,共52页。Data DiscriptionCommandssummary()mean()sd()hist()boxplot()第34页,共52页。Probability Dis

25、tribution第35页,共52页。Three useful prefix in Probability Distribution Functiondxxx for the densitypxxx for the CDFqxxx for the quantile functionrxxx for the simulation(random deviates)They are different!The seed is set by the system. You can set seed yourselfby set.seed().第36页,共52页。Statistical Inferenc

26、eCommandsqxxx () for the quantile functiont.test()wilcox.test(stats)kruskal.test(stats)var.test(); shapiro.test();qqnorm(); qqline()- 第37页,共52页。Analysis of variance and Regression AnalysisCommandsanova()lm()第38页,共52页。Experiment DesignCommandssample()power.t.test()第39页,共52页。Save Object/DataEvery R ob

27、ject can be stored into and restored from a the commands “save” and “load”. save(x, file=“x.Rdata”) load(“x.Rdata”)Importing and exporting data with rectangular tables in the form of tab-delimited text files. write.table(x, file=“x.txt”, sep=“t”)第40页,共52页。Graphics with R第41页,共52页。A Friendly R Enviro

28、nment - RcmdrIf you dont like a command line environment, package Rcmdr may be a good choice!第42页,共52页。R programming (.R)Program your R code own第43页,共52页。Control Flow if(cond) expr if(cond) cons.expr else alt.expr for(var in seq) expr while(cond) expr repeat expr break next第44页,共52页。LoopsThe main lo

29、op construct in R is for. The commonest use, as in C and other languages, is to count from 1 to n.for (i in 1:n) # do something第45页,共52页。Leaving loopsThe break and next commands allow the flow of a loop to be alteredbreak jumps out the loopnext jumps to the next iteration of the loop第46页,共52页。Avoiding IterationThe canonical bad R program looks like this# multiply two vectorsfor(i in 1:n) di - ai * bi#compute the inner products - 0for (i in 1:n)s - s + diThe right way to do this iss-sum(a*b)apply(); lapply(); sapp

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论