R - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

R

Description:

R has a very successful package ... Lots of computer exercises. Lots of work ... Excercises: work alone, work hard. 11. Using R. R is a scripting language ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 21
Provided by: hanvand
Category:
Tags: excercises

less

Transcript and Presenter's Notes

Title: R


1
R
  • Basics
  • Based on sheets provided by
  • prof. dr. Han van der Maas (UvA)

2
Part 1 What is R?
  • a programming environment
  • powerful calculator
  • excellent graphics capabilities
  • excellent statistical procedures (advanced SPSS)
  • Replaces C, Excel and SPSS
  • Computer simulations
  • Data management
  • Statistical analysis
  • Figures for papers, talks etc.

3
Why R?
  • easy to learn
  • powerful
  • Freeware http//cran.r-project.org/
  • platform independent
  • supported by a large user network
  • R has a very successful package system for
    distributing code and data
  • can be used in combination with C, Fortran
  • object-oriented

4
Disadvantages of R
  • some libraries are not available in R
  • R (and Matlab) can not be used for
    experimentation (Authorware Presentation)
  • R is slow and uses lots of memory
  • But computers are fast and have lots of memory
  • In extreme cases we combine R and C

5
What about Matlab?
  • Matlab is very expensive (student version already
    hundreds of dollars)
  • R and Matlab are very similar, but some things
    are easier in Matlab, and other things are easier
    in R
  • Psychologists will learn Matlab

6
Demo 1
  • 34
  • a lt- 3
  • a4
  • a lt- c(3,1,2,5,6, 8,6,9,11)
  • a4
  • B lt- matrix(a,3,3)
  • B4
  • matplot(B,type'b')
  • C lt- matrix( rnorm(200),40,5)
  • matplot(C,type'b')
  • pairs(C)
  • t.test(C)
  • cor(C,1,C,2)

7
Demo 2
  • data
  • xseq(-5,3,length100)y23xx2-2x3rnorm(100
    ,0,20)
  • fit models
  • m1lm(yx)
  • m2lm(ypoly(x,2))
  • m3lm(ypoly(x,3))
  • m4lm(ypoly(x,4))
  • summary(m3) coefficients and statistics
  • summary(lm(yxI(x2)I(x3)))
  • compare models
  • anova(m2,m3)anova(m3,m4)
  • compute predicted dvalues
  • pred1predict(m1,interval"conf",newdatadata.fram
    e(x))
  • pred2predict(m2,interval"conf",newdatadata.fram
    e(x))
  • pred3predict(m3,interval"conf",newdatadata.fram
    e(x))
  • pred4predict(m4,interval"conf",newdatadata.fram
    e(x))
  • and plot
  • plot(x,y)matlines(x,pred3,ltyc(1,2,2))
  • lines(x,pred1,1,lty3) lines(x,pred2,1,lty4)

8
Demo 3
o p t i o n a l
  • help.start()
  • Choose packages
  • Try nnet
  • Copy example
  • Go to R
  • library(nnet)
  • Paste example
  • You have run a neural network simulation !

9
Learning strategy
  • Lectures
  • Main texts Introduction to R
  • Lots of computer exercises
  • Lots of work
  • You will really learn it only if you use it in
    all your subsequent work!
  • Have patience.

10
Overview
  • Meeting 1 Basics
  • Meeting 2 Data
  • Meeting 3 Functions
  • Meeting 4 Programming I
  • Meeting 5 Programming II
  • Meeting 6 Graphics
  • Meeting 7 Statistical
  • Meeting 8 Extra
  • Meeting 9 Exam on October 28 10.00h-17.00h
  • Excercises work alone, work hard.

11
Using R
  • R is a scripting language
  • Read, evaluate, print result
  • Write your code in an .R file
  • Tabs File, Open script
  • You can also save the history of your commands
  • Use arrow keys to scroll to previous commands
  • Use for comments

12
Help!
  • There are many ways to get help
  • help(lm) or ?lm (check examples)
  • apropos(lm)
  • help.start() to open browser
  • On the internet
  • http//cran.r-project.org/
  • Search
  • R manuals
  • R wiki
  • R reference card

13
R basics
  • Basically two types of objects
  • Data (variables, vectors, matrices, lists)
  • Functions (operators, plot, statistical,
    mathematical functions)
  • In R there are hundreds of functions available
    (and you can make new ones yourself)
  • functions always have the form f(), e.g.,
    mean(x), plot(x,y)
  • This is also true for operators like , you can
    write 34, but this is short hand for ''(3,4)

14
Data
  • Information
  • Variables
  • Matrices with numbers
  • Datasets
  • Strings
  • ...
  • For example
  • alt-2
  • Then a is data object

15
Workspace
  • Data exist in the workspace (active memory)
  • With ls() you can see your objects
  • With rm() you can remove them

16
Assignment (function)
  • a 1
  • a lt- 1
  • 1 -gt a
  • assign(a,1)
  • a b 1 or a lt- b lt- 1
  • Note test equality with (or all.equal())
  • Separate commands with a newline or

17
Operators (function)
  • - Minus, can be unary or binary
  • Plus, can be unary or binary
  • ! Unary not
  • Tilde, used for model formulae, can be either
    unary or binary
  • ? Help
  • Sequence, binary (in model formulae
    interaction)
  • Multiplication, binary
  • / Division, binary
  • Exponentiation, binary
  • any Special binary operators, any can be
    replaced by any valid name
  • Modulus, binary
  • / Integer divide, binary
  • Matrix product, binary
  • o Outer product, binary
  • x Kronecker product, binary
  • in Matching operator, binary (in model
    formulae nesting)
  • lt Less than, binary
  • gt Greater than, binary
  • Equal to, binary
  • ! Unequal to, binary
  • gt Greater than or equal to, binary
  • lt Less than or equal to, binary
  • And, binary, vectorized
  • And, binary, not vectorized
  • Or, binary, vectorized
  • Or, binary, not vectorized
  • lt- Left assignment, binary
  • Left assignment, binary
  • -gt Right assignment, binary
  • List subset, binary

18
Precedence of operators(from highest to lowest)
o p t i o n a l
  • ( Indexing
  • access variables in a name space
  • _at_ component
  • / slot extraction
  • exponentiation
  • - unary minus and plus
  • sequence operator
  • any special operators
  • / multiply, divide
  • - (binary) add, subtract
  • lt gt lt gt ! ordering and comparison
  • ! negation
  • and
  • or
  • as in formulae
  • -gt -gtgt assignment
  • assignment
  • lt- ltlt- assignment
  • ? help

19
Simple math
  • x lt- .5
  • sin(x) cos(x) tan(x)
  • asin(x) acos(x) atan(x)
  • log(x) log10(x) exp(x) sqrt(x)
  • xx
  • Use parentheses to be sure
  • -22 is how much?
  • And 2-2 ?

20
So you can do things like...
  • a lt- 3 b lt- 5
  • y lt- sqrt(a2b2)
  • !(alt1blt1)
  • But there is much more...
  • More complex variables
  • More interesting operators (functions)
  • A lot of functions are always available (base
    package), others can be added or created
Write a Comment
User Comments (0)
About PowerShow.com