1 / 135

Math2411

Math2411. Introduction to R. Introduction to R. R is a free statistical programming language. Download at: http://www.r-project.org. Or, simply google [R], it should appear as the first hit. Introduction to R. R is a free statistical programming language.

newman
Télécharger la présentation

Math2411

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Math2411 Introduction to R Part II: Statistics

  2. Introduction to R R is a free statistical programming language. Download at: http://www.r-project.org. Or, simply google [R], it should appear as the first hit Part II: Statistics

  3. Part II: Statistics

  4. Part II: Statistics

  5. Part II: Statistics

  6. Introduction to R R is a free statistical programming language. Download at: http://www.r-project.org. Open R by double-clicking your desktop Icon : Part II: Statistics

  7. Create a vector in R Part II: Statistics

  8. Use a function c() “c” stands for concatenation most primitive way to create a column vector e.g. > c(1, 2, 3)numeric data [1] 1 2 3 - Integers > c(‘a’, ‘b’) or c(“a”, “b”) nonnumericdata [1] “a” “b” - Characters Creating Vectors Part II: Statistics

  9. Part II: Statistics

  10. Part II: Statistics

  11. 1 is not regarded as a number in R

  12. Part II: Statistics

  13. Creating Vectors Q : I want to use the same vector again… A : Give it a name and store it as a variable! Part II: Statistics

  14. Assignment operator <- OR = e.g. > myvect <- c(1, 2, 3, 4, 5, 6, 7) OR > myvect = c(1, 2, 3, 4, 5, 6, 7) Creating Vectors Part II: Statistics

  15. Now recall the variable myvect > myvect [1] 1 2 3 4 5 6 7 *Variable name starts with letters contains letters, numeric and period “.” only CASE SENSITIVE! Creating Vectors • Use t() to convert a column vector to a row vector For example: t(myvect) Part II: Statistics

  16. Creating Vectors The function c() can also link two vectors together to form a new vector.

  17. Part II: Statistics

  18. Use a function :to create a sequence x:y starts from x, increases by 1 up to y e.g. > myvect2 = 8:14 > myvect2 [1] 8 9 10 11 12 13 14 Step size = 1 Creating Vectors Part II: Statistics

  19. Use a function seq() to create a sequence in more flexible way seq(x, y, k) Starts from x Increase by k up to y e.g. > myvect3 = seq(1, 14, 2) > myvect3 [1] 1 3 5 7 9 11 13 Step size = 2 Creating Vectors Part II: Statistics

  20. “rep” stands for replicate rep(x, b) A vector consists of b replicates of x e.g. > rep(0, 5) [1] 0 0 0 0 0 > rep( c(1, 2), 2) [1] 1 2 1 2 > rep(‘a’, 3) [1] “a” “a” “a” Creating Vectors Part II: Statistics

  21. Vector manipulation Part II: Statistics

  22. Vector manipulation The square brackets [ ] are most commonly used e.g. To select the 2nd element in combine >combine [1] 8 9 10 11 12 13 14 1 2 3 4 5 6 7 >combine[2] [1] 9 Part II: Statistics

  23. e.g. To select from the 2nd up to the 5th elements >combine [1] 8 9 10 11 12 13 14 1 2 3 4 5 6 7 >combine[2:5] [1] 9 10 11 12 Vector manipulation Part II: Statistics

  24. e.g. To drop the 3rd element >combine [1] 8 9 10 11 12 13 14 1 2 3 4 5 6 7 >combine[ -3 ] [1] 8 9 11 12 13 14 1 2 3 4 5 6 7 Vector manipulation Part II: Statistics

  25. e.g. To select from the 1st, 5th and then 4th elements >combine [1] 8 9 10 11 12 13 14 1 2 3 4 5 6 7 >combine[ c(1, 5, 4) ] [1] 8 12 11 Vector manipulation Part II: Statistics

  26. e.g. To drop the 2nd, 3rd and then 5th elements >combine [1] 8 9 10 11 12 13 14 1 2 3 4 5 6 7 >combine[ -c(2, 3, 5) ] [1] 8 11 13 14 1 2 3 4 5 6 7 Vector manipulation Part II: Statistics

  27. Basic math functions 2 + 3 # addition 2 – 3 # substraction 2 * 3 # multiplication 2 / 3 # division 2 ^ 3 # power log( 5 ) # natural logarithms sqrt( 2 ) # square root exp( 0.2 ) # exponential function abs( -7 ) # absolute value Part II: Statistics

  28. Basic math functions a) Sum of a vector and a scalar x = c(11,8,3) x + 7 ? Each element of x will be added to 7 to form a new vector. x+7  (18, 15, 10) Part II: Statistics

  29. Basic math functions b) Multiplication of a vector by a scalar x = c(11,8,3) x*7 ? Each element of x will be multiplied by 7 to form a new vector. x*7  (77, 56, 21) c) Division of a vector by a scalar x/7  (11/7, 8/7, 3/7)

  30. d) Power Similarly, when applied to a vector, the values are evaluated element-wisely. e.g. >myvect [1] 1 2 3 4 5 6 7 >myvect^2 [1] 1 4 9 16 25 36 49 Basic math functions Part II: Statistics

  31. Basic math functions We can also add, subtract, or multiply two or more vectors of the same length (a) Vector Sum (sum elementwisely) x = c(11,8,3), and y = c(2,6,3) x + y (11+2, 8+6, 3+3)=(13, 14, 6) (b) Vector Subtraction x - y (11-2, 8-6, 3-3)=(9, 2, 0) Part II: Statistics

  32. Basic math functions Caution! Multiplying Vectors Part II: Statistics

  33. t(x)%*%y = sum(x*y) Basic math functions c1) Vector Multiplication (elementwisely) x = c(11,8,3), and y = c(2,6,3) x * y (11*2, 8*6, 3*3) c2) Vector Multiplication (usual),i.e. xTy t(x)%*%y11*2+8*6+3*3

  34. Basic math functions d) Vector Division (elementwisely) x = c(11,8,3), and y = c(2,6,3) x/y (11/2, 8/6, 3/3) Part II: Statistics

  35. Create a matrix and matrix manipulation Part II: Statistics

  36. ?matrix Or help(matrix) Create a Matrix Use matrix() Part II: Statistics

  37. Part II: Statistics

  38. Part II: Statistics

  39. ?matrix Or help(matrix) x = matrix(c(3,5,6,3,4,9,2,1,7), ncol=3, nrow=3, byrow=T) The matrix is filled by rows. Create a Matrix Use matrix() Part II: Statistics

  40. Part II: Statistics

  41. Part II: Statistics

  42. Part II: Statistics

  43. 1st 2nd 3rd column The (1,2)th element of x 1st row x[1,2]: the element in the 1st rowAND2nd column 2nd row 3rd row x[,1] x[3,] The 1st column vector The 3rd row vector Part II: Statistics

  44. Part II: Statistics

  45. Part II: Statistics

  46. Part II: Statistics

  47. Matrix Manipulation x[-i, ]: drop the ith row vector of x. x[-c(i,j), ]: drop the ith and jth row vectors of x. x[ ,-i]: drop the ith column vector of x. x[ ,c(i,j)]: keep the ith and jth column vectors of x. Part II: Statistics

  48. Part II: Statistics

  49. Part II: Statistics

More Related