hands-on introduction to r. we live in oceans of data. computers are essential to record and help...

17
Hands-on Introduction to R 3 2 1 0 1 2 3

Upload: erik-simon

Post on 13-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hands-on Introduction to R. We live in oceans of data. Computers are essential to record and help analyse it. Competent scientists speak C/C++, Java,

Hands-on Introduction to R

3 2 1 0 1 2 3

Page 2: Hands-on Introduction to R. We live in oceans of data. Computers are essential to record and help analyse it. Competent scientists speak C/C++, Java,

• We live in oceans of data. Computers are essential to record and help analyse it.• Competent scientists speak C/C++, Java,

MATLAB, Python, Perl, R and/or Mathematica

• Data collection and analysis very important in Forensic Science since NAS 2009

• Using the above languages, codes can easily be made available for review/discovery

Why Leaning Programing?

Page 3: Hands-on Introduction to R. We live in oceans of data. Computers are essential to record and help analyse it. Competent scientists speak C/C++, Java,

• All machines understand is on/off!• High/low voltage

• High/low current

• High/low charge

• 1/0 binary digits (bits)

• To make a computer do anything, you have to speak machine language to it:

Getting a computer to do anything useful

000000 00001 00010 00110 00000 100000

Add 1 and 2. Store the result.Wikipedia

Page 4: Hands-on Introduction to R. We live in oceans of data. Computers are essential to record and help analyse it. Competent scientists speak C/C++, Java,

• Machine language is not intuitive and can vary a great deal over designs

• The basic operations operations however are the same, e.g.:• Move data here

• Combine these values

• Store this data

• Etc.

• “Human readable” language for basic machine operations: assembly language

Getting a computer to do anything useful

Page 5: Hands-on Introduction to R. We live in oceans of data. Computers are essential to record and help analyse it. Competent scientists speak C/C++, Java,

• Assembly is still cumbersome for (most) humans

Getting a computer to do anything useful

MOV AL, 61h

10110000 01100001

Assembly

A machine encoding

Move the number 97 over to “storage area” AL

Page 6: Hands-on Introduction to R. We live in oceans of data. Computers are essential to record and help analyse it. Competent scientists speak C/C++, Java,

• Better yet is a more “Englishy”, “high-level” language• Enter: C, C++, Fortran, Java, …

• Higher level languages like these are translated (“compiled”) to machine language• Not exactly true for Java, but it’s something

analogous…

Getting a computer to do anything useful

Page 7: Hands-on Introduction to R. We live in oceans of data. Computers are essential to record and help analyse it. Competent scientists speak C/C++, Java,

• Even more “Englishy” and “high-level” are interpreted languages• Enter: R MATLAB, Perl, Python, Mathematica,

Maple, …

• The “code” of these languages are “interpreted” as commands by a program that is already running• They make many assumptions behind the scenes

• Much easier to program with

• Much slower than compiled languages

Getting a computer to do anything useful

Page 8: Hands-on Introduction to R. We live in oceans of data. Computers are essential to record and help analyse it. Competent scientists speak C/C++, Java,

• R is not a black box!• Codes available for review; totally transparent!

• R maintained by a professional group of statisticians, and computational scientists• From very simple to state-of-the-art procedures

available

• Very good graphics for exhibits and papers

• R is extensible (it is a full scripting language)• Coding/syntax similar to Python and MATLAB

• Easy to link to C/C++ routines

Why ?

Page 9: Hands-on Introduction to R. We live in oceans of data. Computers are essential to record and help analyse it. Competent scientists speak C/C++, Java,

• Where to get information on R :• R: http://www.r-project.org/

• Just need the base

• RStudio: http://rstudio.org/

• A great IDE for R

• Work on all platforms

• Sometimes slows down performance…

• CRAN: http://cran.r-project.org/

• Library repository for R

• Click on Search on the left of the website to search for package/info on packages

Why ?

Page 10: Hands-on Introduction to R. We live in oceans of data. Computers are essential to record and help analyse it. Competent scientists speak C/C++, Java,

Finding our way around R/RStudio

Script Window

Command Line

Page 11: Hands-on Introduction to R. We live in oceans of data. Computers are essential to record and help analyse it. Competent scientists speak C/C++, Java,

• Basic Input and Output

Handy Commands:

x <- 4

x <- “text goes in quotes”

variables: store

information

Numeric input

Text (character) input

:Assignment operator

Page 12: Hands-on Introduction to R. We live in oceans of data. Computers are essential to record and help analyse it. Competent scientists speak C/C++, Java,

• Get help on an R command:• If you know the name: ?command name• ?plot brings up html on plot command

• If you don’t know the name:• Use Google (my favorite)• ??key word

Handy Commands:

Page 13: Hands-on Introduction to R. We live in oceans of data. Computers are essential to record and help analyse it. Competent scientists speak C/C++, Java,

• R is driven by functions:

Handy Commands:

func(arguement1, argument2)

x <- func(arg1, arg2)

function name input to function goes in parenthesis

function returns something; gets dumped into x

Page 14: Hands-on Introduction to R. We live in oceans of data. Computers are essential to record and help analyse it. Competent scientists speak C/C++, Java,

• Input from Excel• Save spreadsheet as a CSV file• Use read.csv function

• Needs the path to the file

Handy Commands:

"/Users/npetraco/latex/papers/data.csv”

Mac e.g.:

“C:\Users\npetraco\latex\papers\data.csv”

Windows e.g.:

*Exercise: basicIO.R

Page 15: Hands-on Introduction to R. We live in oceans of data. Computers are essential to record and help analyse it. Competent scientists speak C/C++, Java,

• Matrices: X• X[,1] returns column 1 of matrix X

• X[3,] returns row 3 of matrix X

• Handy functions for data frames and matrices:

• dim, nrow, ncol, rbind, cbind

• User defined functions syntax:• func.name <- function(arguements) {

do something

return(output)

}

• To use it: func.name(values)

Handy Commands:

Page 16: Hands-on Introduction to R. We live in oceans of data. Computers are essential to record and help analyse it. Competent scientists speak C/C++, Java,

• User defined function example: • Compute the intensities of the Planck distribution

• Let the user input a Temperature

• Let the user input endpoint. Assume it is in nm

• Careful here. Make sure wavelength units are consistent with the other constants.

• What is the “easiest” thing to do??

Handy Commands:

Page 17: Hands-on Introduction to R. We live in oceans of data. Computers are essential to record and help analyse it. Competent scientists speak C/C++, Java,

• Hints:• Have the user set the wavenumber range and generate a

sequence from it:

• The “wavenumber axis”

Handy Commands:

nut <- seq(from=nut.min, to=nut.max, length.out=2500)