1 introduction
play

1. Introduction excerpt from the lecture at ETHZ (1V + 1U) , Autumn - PowerPoint PPT Presentation

MAS Medizinphysik Using R MAS Medizinphysik Using R 1. Introduction excerpt from the lecture at ETHZ (1V + 1U) , Autumn Sem. 2010 In this Chapter you will ... Cornelia Schwierz, Andreas Papritz, ... learn what R is ... see a few first


  1. MAS Medizinphysik — Using R MAS Medizinphysik — Using R 1. Introduction excerpt from the lecture at ETHZ (1V + 1U) , Autumn Sem. 2010 In this Chapter you will ... Cornelia Schwierz, Andreas Papritz, ... learn what R is ... see a few first examples Martin M¨ achler <maechler@stat.math.ethz.ch> ... learn how to operate R Seminar f¨ ur Statistik, ETH Z¨ urich ... learn how to read in data ... learn how to quit an R session January 20, 2011 0partly based on work by Werner Stahel and Manuel Koller 0slides rendered (by L A T EX) on January 19, 2011 1 / 154 2 / 154 1.1 What is R? 1.2 Other Statistical Software ◮ S+ (formerly “S-PLUS”) same programming language, ◮ R is a software environment for statistical computing. commercial. Features a GUI. ◮ R is based on commands. Implements the S language. ◮ SPSS: good for standard procedures. ◮ There is an inofficial menu based interface called R-Commander. ◮ SAS: all-rounder, good for large data sets, complicated analyses. ◮ Drawbacks of menus: difficult to store what you do. A script of commands ◮ Systat: Analysis of Variance, easy-to-use graphics system. ◮ documents the analysis and ◮ Excel: Good for getting (a small!) dataset ready. Very limited ◮ allows for easy repetition with changed data, options, ... collection of statistical methods. ◮ R is free software. http://www.r-project.org Not for serious data analysis! Supported operating systems: Linux, Mac OS X, Windows ◮ Matlab: Mathematical methods. Statistical methods limited. ◮ Language for exchanging statistical methods among researchers Similar “paradigm”, less flexible structure. 3 / 154 4 / 154

  2. 1.3 Introductory examples A dataset that we have stored before in the system is called d.sport ◮ Scatter plot: type plot(d.sport[,"kugel"], d.sport[,"speer"]) weit kugel hoch disc stab speer punkte OBRIEN 7.57 15.66 207 48.78 500 66.90 8824 ◮ First argument: x coordinates; second: y coordinates BUSEMANN 8.07 13.60 204 45.04 480 66.86 8706 ◮ Many(!) optional arguments: DVORAK 7.60 15.82 198 46.28 470 70.16 8664 : : : : : : : : plot(d.sport[,"kugel"],d.sport[,"speer"], : : : : : : : : xlab="ball push",ylab="javelin",pch=7) : : : : : : : : CHMARA 7.75 14.51 210 42.60 490 54.84 8249 ◮ Scatter plot matrix Draw a histogram of the results of variable kugel : We type pairs(d.sport) hist(d.sport[,"kugel"]) Every column of d.sport is plotted against all other columns. The graphics window is opened automatically. We have called the function hist with argument d.sport[,"kugel"] . [, j] is used to select the column j . 5 / 154 6 / 154 1.4 Using R An R statement 1 is typically either ◮ a name of an object − → object is displayed ◮ Within a window running R, you will see the prompt ’ > ’. > d.sport You type a command and get a result and a new prompt. ◮ a call to a function − → graphical or numerical result > hist(d.sport[,"kugel"]) > hist(d.sport[,"kugel"]) > ◮ an assignment > a <- 2*pi/360 An incomplete statement can be continued on the next line > mn <- mean(d.sport[,"kugel"]) > plot(d.sport[,"kugel"], stores the mean of d.sport[,"kugel"] + d.sport[,"speer"]) under the name mn 1 R “statement”: more precisely R “function call” 7 / 154 8 / 154

  3. Reading and Writing Data Get a dataset from a text file on the internet and assign it to a name: > d.sport <- read.table( + "http://stat.ethz.ch/Teaching/Datasets/WBL/sport.dat") Read a file in table format and create a data frame from it. For data files with a one-line header, you need to set the option With cases corresponding to lines and variables to fields. header = TRUE , ◮ Text-files: > d... <- read.table(... , header = TRUE) > read.table(file, header = FALSE, sep = "", + dec = ".", row.names, col.names,...) To download the file first to the local computer, R provides the ◮ Excel-files: command > read.csv(file, sep = ",", dec=".",...) > download.url("http://...") > read.csv2(file, sep = ";", dec=",",...) Get all possible arguments and defaults with ?read.table Use file browser (of the underlying operating system) to open a file: > d.sport <- read.table(file.choose()) 9 / 154 10 / 154 To save or write data to a file: ◮ Text-files: > write.table(x, file = "", append = FALSE, Reading Data (ctd.) + sep = " ",eol = " \ n", na = "NA", dec = ".", + row.names = TRUE, col.names = TRUE, ...) ◮ Tabulator-separated files: where x is the data object to be stored. > read.delim(file, sep = " \ t", dec=".",...) ◮ Excel-files: > read.delim2(file, sep = " \ t", dec=",",...) > write.csv(...) ◮ R-Data: > write.csv2(...) > load(file=’’myanalysis.Rdata’’) ◮ R-Data files: > load(file=’’C:/myanalysis.Rdata’’) > save(..., file, ascii = FALSE,...) Example: > x <- c(1:20) > y <- d.sport$kugel > save(x, y, file = "xy.Rdata") 11 / 154 12 / 154

  4. Getting Help ◮ R stores all created “objects” in your workspace. List them by either ls() or equivalently, objects() : ◮ Documentation on the arguments etc. of a function > ls() (or dataset provided by the system): [1] "a" "d.sport" "mn" > help(hist) or ?hist ◮ Objects have names like a, fun, d.sport On the help page, the section “See Also...” contains related ◮ R provides a huge number of functions and other objects functions that could help you further. ◮ Arguments of functions are provided either by using their name, ◮ Search for a specific keyword: e.g. read.table(...,header=TRUE) , or by placing them > help.search("matrix") Lists packages and at their defined position (as defined in the help-pages). functions related to or using “matrix”. ◮ You can see the function definition (“source”) by typing its name Note: Takes a long time when you have many extra R packages installed without () : ◮ For many functions and data sets, examples are provided on the > read.table help page ( ?matrix ). You can execute them directly, ◮ Comments can be added using “#” : > example("matrix") > ls() ## Comments are ignored by R 13 / 154 14 / 154 Resources on the internet 1.5 Scripts and Editors ◮ R’s Project page http://www.r-project.org/ 2 Instead of typing commands into the R console, you can generate commands by an editor and then “send” them to R ... and later modify ◮ CRAN: use Swiss mirror 3 http://cran.CH.r-project.org/ : (correct, expand) and send again. Text Editors supporting R Links to Search (several search possibilites), Task Views (thematic collections of functions), Contributed (electronic ◮ WinEdt: http://www.winedt.com/ Documentation, Introductions) and FAQs . ◮ Emacs 4 with ESS: The following list could be extended “infinitely”: http://ESS.r-project.org/ 5 ◮ http://search.r-project.org/ : Search specific for R, also ◮ Tinn-R: http://www.sciviews.org/Tinn-R/ accessed via R function RSiteSearch() . Functions, Help, ◮ . . . and several more, partly depending on platform (Windows / etc. Mac / Linux) . . . . . . ◮ http://www.rseek.org/ : A “Google-type” search specific for R. Delivers Functions, Help Forums, etc. 4 http://www.gnu.org/software/emacs/ 2 all URLs on this page are “clickable” 5 For Windows and Mac, on the Downloads tab, look for the “All-in-one installation” 3 the Swiss CRAN mirror is at stat.ethz.ch by Vincent Goulet 15 / 154 16 / 154

  5. The Tinn-R Window Define Tinn-R Keyboard Shortcuts: Menu item R / Hotkeys of R 17 / 154 18 / 154 MAS Medizinphysik — Using R 2. Basics Leave the R session: > q() You get the question: In this Chapter you will ... Save workspace image? [y/n/c]: ... learn how to select elements from a data set If you answer ”y”, your objects will be available for your next session. ... find out about vectors (numerical, logical, Note that we usually answer “ n ” 6 , but always store the script (*.R) files. character) ... use R as a calculator ... learn how to create and manipulate matrices 6 and M.M. even eliminates that question by starting R as R --no-save 19 / 154 20 / 154

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend