Bare-Bones R.ppt
《Bare-Bones R.ppt》由会员分享,可在线阅读,更多相关《Bare-Bones R.ppt(84页珍藏版)》请在麦多课文档分享上搜索。
1、Bare-Bones R,A Brief Introductory GuideThomas P. Hogan University of Scranton 2010 All Rights Reserved,Citation and Usage,This set of PowerPoint slides is keyed to Bare-Bones R: A Brief Introductory Guide, by Thomas P. Hogan, SAGE Publications, 2010.All are welcome to use and/or adapt the slides wit
2、hout seeking further permission but with the usual professional acknowledgment of source.,Part 1: Base R,1-1 What is RA computer language, with orientation toward statistical applicationsRelatively newGrowing rapidly in use,1-2 Rs Ups and Downs,Plusses Completely free, just download from Internet Ma
3、ny add-on packages for specialized uses Open sourceMinuses Obscure terms, intimidating manuals, odd symbols, inelegant output (except graphics),1-3 Getting Started: Loading R,Have Internet connection Go to http:/cran.r-project/ R for Windows screen, click “base” Find, click on download R Click Run,
4、OK, or Next for all screens End up with R icon on desktop,At http:/cran.r-project.org/,Downloading Base R Figs 1.1 1.4,Click on WindowsThen in next screen, click on “base”Then screens for Run, OK, or NextAnd finally “Finish” will put R icon on desktop,What You Should Have when clicking on R icon: Rg
5、ui and R Console ending with R prompt () Fig 1.5,The R prompt (), This is the “R prompt.” It says R is ready to take your command.,1-4 Using R as Calculator,Enter these after the prompt,observe output 2+3 23+(5) 6/2+(8+5) 2 3 + (5),More as Calculator,You can copy and paste, but dont include the Use
6、# at end of command for notes, e.g. (22+ 34+ 18+ 29+ 36)/5 #Calculating the average, aka meanR as calculator: Not very useful,1-5 Creating a Data Set, Scores = c(22, 34, 18, 29, 36)c means “concatenate” in R in plain English “treat as data set”Now do:ScoresR will print the data set,Important Rules,W
7、e created a variable Variable names are case sensitive No blanks in name (can use _ or . to join words, but not -) Start with a letter (cap or lc) Can use - instead of =,Another variable,Create SCORES, using SCORESSCORES Scores,Non-numeric Data,Enclose in quotes, single or double Separate entries wi
8、th comma Example: names = c(“Mary”, “Tom”, “Ed”, “Dan”, “Meg”),Saving Stuff,To exit: either X or quit ( ) Brings up this screen:Do what you want: Yes or No Do Yes, then re-open R, get Scores & names,Special Note on Saving,Previous slide assumes you control computer If not, use File, Save Workspace,
9、name file, click Save Works much like saving a file in Microsoft To retrieve, do File, Load Workspace, find file, click Open,1-6 Using R Functions: Simple Stuff,Commands for mean, sd, summary (NB: function names case sensitive)mean(Scores) sd(Scores) summary(Scores)Command for correlation cor(Scores
10、,SCORES),R functions,A zillion of em Rs big strength, most common use For examples: Help R functions(text) Enter name of a function (e.g., sd) Yields lots (!) of information,1-7 Reading in Larger Data Sets,In Excel, enter (or download) the SATGPA20 fileSave as .xlsThen save as Text (tab delimited) f
11、ile Will have .txt extension, Larger Data Sets The read.table command,Now read into R like this:SATGPA20R=read.table(“E:/R/SATGPA20.txt“, header =T)Need exact path, in quotesheader = T T or TRUE, F or FALSE Depends on opening line of file,The file.choose ( ) command,At enter file.choose ( ) Accesses
12、 your systems files, much like Open in Microsoft Find the file, click on it R prints the exact path in R Console Can copy and paste into read.table,Checking what youve got:,Enter SATGPA20R Then mean (SATGPA20R) Try mean (GPA),The attach Command,To access individual variables, do this: attach(SATGPA2
13、0R)Now try: mean(GPA),The data.frame Command,Lets create these 3 variables with c IQ = c(110, 95, 140, 89, 102) CS = c(59, 40, 62, 40, 55) WQ = c(2, 4, 5, 1, 3) Then put them together with: All_Data = data.frame(IQ, CS, WQ) Check with: mean(All_Data),1-8 Getting Help,help(sd) example(sd) On R Consol
14、e:HelpR functions (text)Enter function name, click OK Reminder: function names case sensitive,Rs “function” terms,R language: function(arguments)Plain English: Do this (to this)or Do this (to this, with these conditions),1-9 Dealing with Missing Data,NB: Its a pain in R!Key items In data, enter NA f
15、or a missing value In (most) commands, use na.rm=T,Examples for missing data,Data=c(2,4,6,NA,10) mean(Data, na.rm=T)Add to the SATGPA20 file 21 1 NA NA NA 3.14 23 2 1 NA NA 2.86 Etc. and create new file SATGPA25R Then mean(SATGPA25R, na.rm=T) Note exception for cor function (use=complete),1-10 Using
16、 R Functions: Hypothesis tests,Be sure you have an active data set (SATGPA25R), using attach if neededThen, to test male vs. female on SATM: t.test(SATMSEX) # note tildeExamples of changing defaults: t.test(SATMSEX, var.equal=TRUE, conf.level=0.99),Hypothesis tests: Chi-square,Using SEX and State va
17、riables in SATGPA25Rchisq.test (SEX, State),1-11 R Functions for Commonly Used Statistics,function calculates this mean ( ) mean median ( ) medianmode ( ) mode sd ( ) standard deviation range ( ) range IQR ( ) interquartile rangemin ( ) minimum value max ( ) maximum value cor ( ) correlation quantil
18、e ( ) percentile t.test ( ) t-testchisq.test ( ) chi-sqaure NB1: See notes in text for details NB2: R contains many more functions,1-12 Two Commands for Managing Your Files, ls ( ) Will list your currently saved files rm (file) Insert file name; this will remove the fileNB: R has many such commands,
19、1-13 R Graphics,R graphs: good, simple Lets start with hist and boxplot with the SATGPA25R file hist(SATM) boxplot(SATM) boxplot(SATV, SATM) R Graphics window opens, need to minimize to get R Console,More Graphics: plot,Create these variables RS=c(12,14,16,18,25) MS=c(10,8,16,12,20)Now do this: plot
20、(RS, MS),Line of Best Fit,Do these for the RS and MS variables: lm(MSRS) # lm means linear model res=lm(MSRS) # res means residuals abline(res) # read as a-b line,Controlling Your Graphics: A Brief Look,R has many (often obscure) ways for controlling graphics; well look at a few Basically, well chan
21、ge “defaults”Examples (try each one): Limits (ranges) for X and Y axes plot(RS, MS, xlim = c(5,25), ylim = c(5,25),Controlling Graphs: More Examples,Plot characters: plot(RS, MS, pch=3)Line widths plot(RS, MS, pch=3, lwd=5)Axis labels plot(RS, MS, xlab = “Reading Score”, ylab = “Math Score”)You can
22、put them all together in one command,Part 2: R Commander,2-1 What is R Commander? Point and click version of R Uses (and prints) base R commandsLoading: Easy its just a package See next slide,Loading Rcmdr,On R Gui, top menu bar click Packages, then Install package(s).Pick a CRAN mirror site (nearby
23、), click OK.From the list of packages ,scroll to Rcmdr, highlight it, click OK After it loads, do these: Check with: library ( ) Activate with: library (Rcmdr),Rcmdrs extra packages,Scary message when first activating Rcmdr:Just click Yes and take a break,The R Commander Window,You get, R Commander
24、window withScript windowOutput window (incl Submit button)Message window,2-2 R Commander Windows and Menus,File Edit Data * Statistics * Most important for us Graphs * Models Distributions Tools Help,Our Lesser Used Menus,File Table 2.1 Much like in Microsoft Manage files Edit Table 2.2 Much like in
25、 Microsoft Can do with right click of mouse,Our Lesser Used Menus (cont),Models Mostly more advanced stats DistributionsTools Load packages Options change output defaults Help Searchable index R Commander manual,2-3 The Data Menu (very important) (Submenus for creating/getting data sets),New data se
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- BAREBONESRPPT
