SlideShare ist ein Scribd-Unternehmen logo
1 von 72
Downloaden Sie, um offline zu lesen
R

          R         :
                              1.7



    R Development Core Team

         June 10, 2006
Contents

1                                                                                                                                       1
    1.1    R          . . . . . .   .   .   .   .   .   . . .
                                                            .       .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   1
    1.2                         .   .   .   .   .   .   . . .
                                                            .       .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   1
    1.3    R        . . . . . . .   .   .   .   .   .   . . .
                                                            .       .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   2
    1.4    R              . . . .   .   .   .   .   .   . . .
                                                            .       .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   2
    1.5    R              . . . .   .   .   .   .   .   . . .
                                                            .       .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   2
    1.6               . . . . . .   .   .   .   .   .   . . .
                                                            .       .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   3
    1.7                                             .   . . .
                                                            .       .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   3
    1.8    R                                              . .       .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   3
    1.9                                             . . . . .       .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   4
    1.10                                                    .               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   4
    1.11                                        . . . . . . .               .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   4

2                                                                                                                                       5
    2.1                  . . . . . . .      .   .   .   .   .   .   .   .
                                                                        .   .   .   .   .   .   .       .   .   .   .   .   .   .   .   5
    2.2                . . . . . . . .      .   .   .   .   .   .   .   .
                                                                        .   .   .   .   .   .   .       .   .   .   .   .   .   .   .   5
    2.3                      . . . . .      .   .   .   .   .   .   .   .
                                                                        .   .   .   .   .   .   .       .   .   .   .   .   .   .   .   6
    2.4                . . . . . . . .      .   .   .   .   .   .   .   .
                                                                        .   .   .   .   .   .   .       .   .   .   .   .   .   .   .   7
    2.5           . . . . . . . . . .       .   .   .   .   .   .   .   .
                                                                        .   .   .   .   .   .   .       .   .   .   .   .   .   .   .   7
    2.6                . . . . . . . .      .   .   .   .   .   .   .   .
                                                                        .   .   .   .   .   .   .       .   .   .   .   .   .   .   .   7
    2.7             (index vector);                                     .                               .   .   .   .   .   .   .   .   8
    2.8                      . . . . .      . . . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   9

3                                                                                                                                       10
    3.1                             .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   10
    3.2                     . . . . .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   11
    3.3                         . . .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   11
    3.4                 . . . . . . .       .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   11

4                                                                                                                                       12
    4.1               . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                     12
    4.2        tapply() ragged          . . . . . . . . . . . . . . . . . . . . . .                                                     12
    4.3               . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                     13

5                                                                                                                                       14
    5.1         . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                     14
    5.2                                 . . . . . . . . . . . . . . . . . . . . . .                                                     14
    5.3               . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .                                                     15


                                                        i
CONTENTS                                                                                                                                  ii

     5.4         array() . . .   . . . . . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   16
            5.4.1                                                .                               .   .   .   .   .   .   .   .   .   .   16
     5.5                     .   . . . . . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   17
     5.6                     .   . . . . . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   17
     5.7                     .   . . . . . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   18
            5.7.1                . . . . . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   18
            5.7.2                            . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   18
            5.7.3                          . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   19
     5.8                             . . . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   19
     5.9                       QR        . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   19
     5.10                      cbind() rbind() . . . . . . . .                                   .   .   .   .   .   .   .   .   .   .   19
     5.11              c()                     . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   19
     5.12                          . . . . . . . . . . . . . . . .                               .   .   .   .   .   .   .   .   .   .   20

6                                                                                                                                        21
     6.1            . . . . . . . . . . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   21
     6.2                        . . . . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   22
            6.2.1                 . . . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   22
     6.3              . . . . . . . . . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   22
            6.3.1                   . . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   22
            6.3.2     attach() detach()          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   23
            6.3.3                   . . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   23
            6.3.4                     . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   24
            6.3.5                     . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   24

7                                                                                                                                        25
     7.1         read.table() . .     . . . . . . . . . .                    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   25
     7.2         scan() . . . . . .   . . . . . . . . . .                    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   26
     7.3                          .   . . . . . . . . . .                    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   26
            7.3.1          R                          . .                    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   27
     7.4                . . . . . .   . . . . . . . . . .                    .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   27

8                                                                                                                                        28
     8.1    R—                              . . . . . . . . . . . . . . . . . . . . . .                                                  28
     8.2                              . . . . . . . . . . . . . . . . . . . . . . . . .                                                  29
     8.3                              . . . . . . . . . . . . . . . . . . . . . . . . .                                                  32

9                                                                                                                                        35
     9.1                      . . . . . . . . . . . . . . . . .                          .   .   .   .   .   .   .   .   .   .   .   .   35
     9.2                  . . . . . . . . . . . . . . . . . . .                          .   .   .   .   .   .   .   .   .   .   .   .   35
            9.2.1                 if      . . . . . . . . . . .                          .   .   .   .   .   .   .   .   .   .   .   .   35
            9.2.2                 for         repeat while                               .   .   .   .   .   .   .   .   .   .   .   .   35

10                                                                                                                                       37
     10.1                . . . . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   37
     10.2                            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   38
     10.3                            .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   38
     10.4        ’...’ . . . . . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   39
     10.5                    . . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   39
     10.6                    . . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   39
CONTENTS                                                                                                                     iii

          10.6.1                                (Efficiency factors)                   .   .   .   .   .   .   .   .   .   .   39
          10.6.2                                         . . . . . . .               .   .   .   .   .   .   .   .   .   .   40
          10.6.3                      .        . . . . . . . . . . . .               .   .   .   .   .   .   .   .   .   .   41
     10.7      (scope) . . . . . . . .         . . . . . . . . . . . .               .   .   .   .   .   .   .   .   .   .   41
     10.8             . . . . . . . . .        . . . . . . . . . . . .               .   .   .   .   .   .   .   .   .   .   43
     10.9                                        . . . . . . . . . . .               .   .   .   .   .   .   .   .   .   .   44

11 R                                                                                                                         45
   11.1                              . . . . . . . .     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   45
            11.1.1        (contrasts) . . . . . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   48
     11.2                . . . . . . . . . . . . . .     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   48
     11.3                                      . . .     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   48
     11.4                            . . . . . . . .     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   49
            11.4.1               (ANOVA tables)          .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   49
     11.5                    . . . . . . . . . . . .     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   50
     11.6                    . . . . . . . . . . . .     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   50
            11.6.1     (families) . . . . . . . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   51
            11.6.2        glm() . . . . . . . . . .      .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   51
     11.7                                        . .     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   53
            11.7.1               . . . . . . . . . .     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   53
            11.7.2               . . . . . . . . . .     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   54
     11.8                          . . . . . . . . .     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   55

12                                                                                                                           56
     12.1                    . . . . . .   .   .
                                             . . . . . . .           . . . .         .   .   .   .   .   .   .   .   .   .   56
            12.1.1        plot() . . . .   .   .
                                             . . . . . . .           . . . .         .   .   .   .   .   .   .   .   .   .   56
            12.1.2                   . .   .   .
                                             . . . . . . .           . . . .         .   .   .   .   .   .   .   .   .   .   57
            12.1.3               . . . .   .   .
                                             . . . . . . .           . . . .         .   .   .   .   .   .   .   .   .   .   58
            12.1.4                           . . . . . . .           . . . .         .   .   .   .   .   .   .   .   .   .   58
     12.2                    . . . . . . . . . . . . . . .           . . . .         .   .   .   .   .   .   .   .   .   .   59
            12.2.1               . . . . . . . . . . . . .           . . . .         .   .   .   .   .   .   .   .   .   .   61
            12.2.2   Hershey               . . . . . . . .           . . . .         .   .   .   .   .   .   .   .   .   .   61
     12.3                  . . . . . . . . . . . . . . . .           . . . .         .   .   .   .   .   .   .   .   .   .   61
     12.4                    . . . . . . . . . . . . . . .           . . . .         .   .   .   .   .   .   .   .   .   .   62
            12.4.1               (Permanent changes):                par()                   .   .   .   .   .   .   .   .   62
            12.4.2                                     . .           . . . .         .   .   .   .   .   .   .   .   .   .   63
     12.5                    . . . . . . . . . . . . . . .           . . . .         .   .   .   .   .   .   .   .   .   .   63
            12.5.1               . . . . . . . . . . . . .           . . . .         .   .   .   .   .   .   .   .   .   .   63
            12.5.2                   . . . . . . . . . . .           . . . .         .   .   .   .   .   .   .   .   .   .   64
            12.5.3          (Figure margins) . . . . .               . . . .         .   .   .   .   .   .   .   .   .   .   65
            12.5.4               . . . . . . . . . . . . .           . . . .         .   .   .   .   .   .   .   .   .   .   65
     12.6                . . . . . . . . . . . . . . . . .           . . . .         .   .   .   .   .   .   .   .   .   .   67
            12.6.1               PostScript        . . . .           . . . .         .   .   .   .   .   .   .   .   .   .   67
            12.6.2                   . . . . . . . . . . .           . . . .         .   .   .   .   .   .   .   .   .   .   67
     12.7                . . . . . . . . . . . . . . . . .           . . . .         .   .   .   .   .   .   .   .   .   .   68
Chapter 1




1.1     R
R
    •
    •
    •
    •
    •               S


            ”       ” environment           R

    R                                                        R


                R                                                 R



1.2
R               S     Rick Becker,John Chambers AllanWilks Bell
                       S-Plus
    S                         John Chambers
  R     ,               The New S Language: A Programming Environment
for Data Analysis and Graphics Richard A. Becker, John M. Chambers and
Allan R. Wilks          1991          S (S version 3)        Statistical
Models in S edited by John M. Chambers and Trevor J. Hastie

         S-Plus                         R           R S




                                    1
CHAPTER 1.                                                                                2

1.3           R
              R                                                               R

                                               R
                         8            R
     CRAN                             http://cran.r-project.org
        R

     S        R                                                                   S

      SAS         SPSS                                                            R
                                                                      R



1.4           R
      R
                                                                          R
                                      R X window

              UNIX                            Windows     R




1.5           R
R                                                             >,       UNIX shell
                                                                      R
                    UNIX shell                 $   UNIX           R
                   1.                                                                 ,
                  work.                                       .
                  $ mkdir work
                  $ cd work
2.        R
                  $ R
     3.       R
     4.       R
              > q()
                                          R                            yes,no     cancel(
                         )                                                        R
                                 R
              R                      1.   work                            R
                  $ cd work
                  $ R
CHAPTER 1.                                                                 3

     2.      R                  q()
          Windows         R
                 R               ”       ”                    R


1.6
                                                                       R



1.7
 UNIX        man               R
              solve
     >help(solve)

     >?solve

                                        if,for   function

            > help("[[")
                                                                  ”It’s im-
portant”
                              HTML

            > help.start()
               Web       (UNIX    netscape        )
                UNIX                             HTML
              ’Search Engine and Keywords’
                                                 R
       help.search
 ?help.search
       > example(topic)
  Windows       R
       > ?help



1.8        R
                     R
 UNIX                            R                          A a
                                R
 (         locale     )                                       (
                )              . ***note 1***



          (;)                                        ( { })
                                                     (#)
CHAPTER 1.                                                                      4

                                                           R

        +
                                    R



1.9
                       UNIX Windows              R


                       (       <DEL>                           )
                                                       UNIX
                readline                                     Emacs
                                             (  ESS Emacs Speaks Statistics)
    R                                      R Emacs


1.10
                                                        work       commands.R
                  R
              > source("commands.R")
            Windows Source          File                       sink
              > sink("record.lis")
                                                           record.lis
                 > sink()



1.11
R
                                             R
    (                                            )
                 > objects()
        (              ls()  )                         R
                                        workspace
                           rm
                 > rm(x, y, z, ink, junk, temp, foo, bar)
                      R
            R                       R
                                                                           .RData
***Note3***                     R            R                     workspace

                               R
                   x       y
Chapter 2




2.1
R
                                       R                                        x       5
    (10.4, 5.6, 3.1, 6.4 21.7)
            > x <- c(10.4, 5.6, 3.1, 6.4, 21.7)
                               c()                             c()

              1                 ( ¡- )                     =                    =
                                                 ¡ (       )      - (   )
                     ¡ -
      ***note 5***                                     assign()

          > assign("x", c(10.4, 5.6, 3.1, 6.4, 21.7))
                     ¡-             assign()


          > c(10.4, 5.6, 3.1, 6.4, 21.7) -> x


          > 1/x
                                 (       x        )
          > y <- c(x, 0, x)
                  y        11                x                              0


2.2

                                                                                    ,

                            (                         )

          > v <- 2*x + y + 1



                                     5
CHAPTER 2.                                                                              6

                           11          v                              2*x       2.2   y
         1             1          11                                       + - * /
    ^                                                    log, exp, sin, cos, tan,sqrt
                                           max min
               range                            2              c(min(x),max(x))
length(x)                   x                              x        sum(x)       x
                       prod(x)
                         x                                                     mean(x)
  var(x)                                                                   sum(x)/length(x)
sum((x-mean(x))^2)/(length(x)-1)                         var()            n*p
                  p*p                                                   p
    sort(x)          x
                      (   order() sort.list())
            max min
          pmax pmin
                                   (     )



              sqrt(-17)
                 NaN(             )
              sqrt(-17+0i)



2.3
R                                                 1:30         c(1,2,. . . ,29,30)
                (:)                         (                   )               2*1:15
        c(2,4, ...,28,30)              n<-10                               1:n-1 1:(n-1) 30:1
                                                           seq()
               5

                seq(2,10)                  2:10
             seq()                R
                                                from=value               to=value
    seq(1,30), seq(from=1, to=30) seq(to=30, from=1) 1:30                       seq()
                       by=value length=value
                                        by=1

              > seq(-5, 5, by=.2) -> s3
               s3          c(-5.0, -4.8, -4.6, ..., 4.6, 4.8, 5.0)

              > s4 <- seq(length=51, from=-5, by=.2)
        s4
                                 along=vector
                                rep()

              > s5 <- rep(x, times=5)
               x s5                5
CHAPTER 2.                                                                               7

2.4
                R                                                                     TRUE,
FALSE, NA (not available           )                      T     F            T    F
            TRUE FALSE
                         TRUE FALSE
          > temp <- x > 13
       temp          x                                                    TRUE FALSE
                         <, <=, >, >=,                    ==           !=
      c1 c2                   c1 & c2                          (”and”) c1 | c2
            (”or”)    !c1 c1
                                 FALSE             0 TRUE           1



2.5

        ”         ”(”not available”)    ”        ”(”missing value”)
                                                   NA                        NA
             NA
                                        is.na(x)
  x                                                  NA
                TRUE   FALSE
            > z <- c(1:3,NA); ind <- is.na(z)
                         x==NA     is.na(x)                             NA

      x==NA           x                                    NA
  ”      ”                             Not a Number       NaN
           > 0/0

          > Inf - Inf
                NaN                                         is.na(xx)   NA NaN
        TRUE                                is.nan(xx)     NaN     TRUE


2.6
                     R
                               e.g. "x-values" "New iteration results"
                                   (’)       (”)                 (
       ) R       C                                 backslash          
           ,     ”                       ".                  n,    ,
t, tab b,                                  c()
         paste()


                                   sep=string

            > labs <- paste(c("X","Y"), 1:10, sep="")
CHAPTER 2.                                                                             8

        labs
       c("X1", "Y2", "X3", "Y4", "X5", "Y6", "X7", "Y8", "X9", "Y10")
                                                 |c("X","Y")|
   5           1:10


2.7                       (index vector);




 1.
                                                      TRUE
               FALSE
                 > y <- x[!is.na(x)]
                          y                              x
                x                            y                x
                 > (x+1)[(!is.na(x)) & x>0] -> z
                                     z                             x+1   x


 2.                                                                      {1, 2, ...,length(x)}


                              x[6]           x
                 > x[1:10]
           x     10            (         x                   10)
                 > c("x","y")[rep(c(1,2,2,1), times=4)]
                                                 16     "x", "y", "y", "x"       4


 3.

                > y <- x[-(1:5)]
       y              5
 4.                                                          names



                 > fruit <- c(5, 10, 1, 20)
                 > names(fruit) <- c("orange", "banana", "apple", "peach")
                 > lunch <- fruit[c("apple","orange")]|
CHAPTER 2.                                           9

                            (names)

                              vector[index_vector]
 vector
        > x[is.na(x)] <- 0
  replaces any missing values in x by zeros and
        > y[y < 0] <- -y[y < 0]
  has the same effect as
        > y <- abs(y)


2.8
      R
  •       (matrices)



  •       (factors)
  •       (lists)



  •         (data frames)
                                             ”   ”
               (      )
               :
  •       (functions)        R   workspace
              R
Chapter 3




3.1
R

                           (   ) (numeric)            (complex)              (logical)
            (character)


    NA
            character(0)                              numeric(0)
        R                                                (list)
                                                                                         ”
    ”
                                              (function)             (expression)
                       R


                                  mode(object)    length(object)

                                 attributes(object)
                                      ”         ”
                   z             100                          mode(z)             ”complex”   length(z)
        100
                           (                      ) R

                 > z <- 0:9

                 > digits <- as.character(z)
            z                c("0", "1", "2", ..., "9")                     digits
                               (coercion)                          digits
    :
                 > d <- as.integer(digits)
                d z             R                                  as.something()



                                             10
CHAPTER 3.                                                          11




3.2
   ” ”
          > e <- numeric()
                                         character()


          > e[3] <- 17
      e             3                                    NA

                                                       scan()


     alpha           10
  > alpha <- alpha[2 * 1:5]
                   5



3.3
   attributes(object)
                                 attr(object,name)

                         R

                                                        R

          attr()

           > attr(z,"dim") <- c(10,10)
      R   z        10 × 10


3.4
                                           R
                               "data.frame"
                plot()
                  summary()
                                         unclass()              winter
       "data.frame"
          > winter

           > unclass(winter)


      sec10.9
Chapter 4



      1

                                 R



4.1
                            30


> state <- c("tas", "sa", "qld", "nsw", "nsw", "nt", "wa", "wa",
             "qld", "vic", "nsw", "vic", "qld", "qld", "sa", "tas",
             "sa", "nt", "wa", "vic", "qld", "nsw", "nsw", "wa",
             "sa", "act", "nsw", "vic", "vic", "act")


                factor()
            > statef <- factor(state)
          print()

> statef
[1] tas sa qld nsw nsw nt wa wa qld vic nsw vic qld qld sa
[16] tas sa nt wa vic qld nsw nsw wa sa act nsw vic vic act
Levels: act nsw nt qld sa tas vic wa

          levels()


4.2             tapply()     ragged


> incomes <-     c(60, 49, 40, 61, 64, 60, 59, 54, 62, 69, 70, 42, 56,
                  61, 61, 61, 58, 51, 48, 65, 49, 49, 41, 48, 52, 46,
                  59, 46, 58, 43)
  1




                                     12
CHAPTER 4.                                                               13

              tapply()
       > incmeans <- tapply(incomes, statef, mean)


   act    nsw     nt    qld     sa    tas    vic     wa
44.500 57.333 55.500 53.600 55.000 60.500 56.000 52.250

       tapply()
                  incomes       mean()         incomes        tapply()
         statef
                            incomes statef                        tapply()


               R                                         R
           var()

         > stderr <- function(x) sqrt(var(x)/length(x))


         > incster <- tapply(incomes, statef, stderr)


> incster
act    nsw nt     qld     sa tas   vic     wa
1.5 4.3102 4.5 4.1061 2.7386 0.5 5.244 2.6575

                                         95%
             tapply()         length()                               qt()
   t
       tapply()




                                                   ragged array




4.3


                                                             ordered()
Chapter 5




5.1
                                                                 R

                     2                                     k
   1                         R
          dim
                z       1500
          > dim(z) <- c(3,5,100)
            dim                         3 × 5 × 100
                                                matrix() array()

                                                      FORTRAN
 —”          ”
                 a               c(3,4,2)          24
                         a[1,1,1], a[2,1,1],..., a[2,4,2], a[3,4,2]


5.2




                     a[2,,]       4×2                          c(4,2)


c(a[2,1,1], a[2,2,1], a[2,3,1], a[2,4,1],
  a[2,1,2], a[2,2,2], a[2,3,2], a[2,4,2])


  a[,,]                                                a
                         Z                    dim()



                                   14
CHAPTER 5.                                                           15




5.3




                                   4×5      X


   •                      X[1,3], X[2,2]   X[3,1]
   •                      0

                    3×2

> x <- array(1:20,dim=c(4,5)) # Generate a 4 by 5 array.
> x
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    5    9   13   17
[2,]    2    6   10   14   18
[3,]    3    7   11   15   19
[4,]    4    8   12   16   20
> i <- array(c(1:3,3:1),dim=c(3,2))
> i
     [,1] [,2]
[1,]    1    3
[2,]    2    2
[3,]    3    1
> x[i]
[1] 9 6 3
> x[i] <- 0 # Replace those elements by zeros.
> x
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    5    0   13   17
[2,]    2    0   10   14   18
[3,]    0    7   11   15   19
[4,]    4    8   12   16   20
>

                                                blocks(b levels)   varieties
(v levels)
  n          plot

> Xb <- matrix(0, n, b)
> Xv <- matrix(0, n, v)
CHAPTER 5.                                                                   16

>   ib <- cbind(1:n, blocks)
>   iv <- cbind(1:n, varieties)
>   Xb[ib] <- 1
>   Xv[iv] <- 1
>   X <- cbind(Xb, Xv)

                    N
    > N <- crossprod(Xb, Xv)
                                                       table()
    > N <- table(blocks, varieties)


5.4              array()
                            dim                          array

    > Z <- array(data_vector, dim_vector)
             h   24
    > Z <- array(h, dim=c(3,4,2))
     h         Z        3×4×2                      h               24

    > dim(Z) <- c(3,4,2)
           h          24                                     24

     > Z <- array(0, c(3,4,2))
    E
           dim(Z)            c(3,4,2) Z[1:24]                           Z[] Z


                                                                  dim
                                          A,B,C
     > D <- 2*A*B + C + 1
    D




5.4.1



    •
    •


    •
            dim                            (As long as short vectors and arrays
        only are encountered, the arrays must all have the same dim attribute or
        an error results.)
CHAPTER 5.                                                                            17

   •


   •
                                                     dim


5.5
                                          a,b

          a              b
          %o%:
   > ab <- a %o% b

   > ab <- outer(a, b, "*")
                                                                               f (x; y) =
cos(y)/(1 + x2)          x, y                   R     x,y


> f <- function(x, y) cos(y)/(1 + x^2)
> z <- outer(x, y, f)


  1
                  chapter10                                                R

             2×2
                                                2×2         [a, b; c, d]
                  0∼9
                                          ad − bc


                                outer()

> d <- outer(0:9, 0:9)
> fr <- table(outer(d, d, "-"))
> plot(as.numeric(names(fr)), fr, type="h",
       xlab="Determinant", ylab="Frequency")

                                    names
                                                            for
  for                                               for                    chapter9



5.6
      aperm(a, perm)                                            perm              1, . . . , k
                k a                                         a
          perm[j]
CHAPTER 5.                                                        18

                             A

  > B <- aperm(A, c(2,1))
     B       A                                                 t()
              B <- t(A)


5.7

                         R
                                 t(X)    nrow(A) ncol(A)
 A

5.7.1
                   %*%       n×1 1×n
        n


        A,B
  > A * B

  > A %*% B
              X
  > x %*% A %*% x

      crossprod()                  cross product      crossprod(X, y)
 t(X) %*% y                   crossprod()
                                     crossprod(X)        crossprod(X, X)
        diag()                        v          diag(v)          v
                            M        diag(M)            M
                   diag() Matlab
            k             diag(k)      k×k

5.7.2

  > b <- A %*% x
           A b                   x               R
  > solve(A,b)

                      x
           solve(A) A     A−1                   x = A−1 b
 solve(A,b)    x <- solve(A) %*% b
                           x A−1 x          x %*% solve(A,x)
             A
CHAPTER 5.                                                                              19

5.7.3
         eigen(Sm)
         values vectors
        > ev <- eigen(Sm)
                    ev    ev$val ev$vec                  Sm

        > evals <- eigen(Sm)$values
         evals
        > eigen(Sm)
                      > eigen(Sm)


5.8
1




5.9                                     QR
         lsfit()
        > ans <- lsfit(X, y)
                                                     y                  X
                                                                            ls.diag()
         ls.diag()
    X                      X
                                     qr()

>   Xplus <- qr(X)
>   b <- qr.coef(Xplus, y)
>   fit <- qr.fitted(Xplus, y)
>   res <- qr.resid(Xplus, y)

                               y x


5.10                                        cbind()      rbind()
                                                                       cbind() rbind()
                     cbind()
        rbind()

        > X <- cbind(arg 1, arg 2, arg 3, ...)
         cbind()


5.11                           c()
                                  cbind() rbind()             dim
                     c()                                            dim,dimnames

    1                           http://mathworld.wolfram.com/SingularValueDecomposition.html
CHAPTER 5.                                                                  20

                                                              as.vector()

                            c()




5.12

                                                    table()
                                  k                              k

             statef
                                      statefr
  level



                  incomef                       cut()                ”income
class”
Chapter 6




6.1
R




> Lst <- list(name="Fred", wife="Mary", no.children=3,
child.ages=c(4,7,9))|
                                                               Lst
                                                        Lst[[1]], Lst[[2]],
Lst[[3]]      Lst[[4]]                  Lst[[4]]                       Lst[[4]][1]

        Lst                     length(Lst)


    ¿ name$component name




    Lst[[1]]       Lst$name          "Fred"
    Lst[[2]]       Lst$wife          "Mary"
    Lst[[4]][1]       Lst$child.ages[1]           4
                                                              Lst[["name"]] Lst$name

    > x <- "name"; Lst[[x]]
               Lst[[1]] Lst[1] ’[[...]]’
       ’[...]’                                               Lst

     Lst


         Lst$coefficients               Lst$coe       Lst$covariance
     Lst$cov

                                   21
CHAPTER 6.                                                    22



          names


6.2
              list()
  > Lst <- list(name_1=object_1, . . ., name_m=object_m)
                      m




  > Lst[5] <- list(matrix=Mat)

6.2.1
        c()

  > list.ABC <- c(list.A, list.B, list.C)

                      dim


6.3
              ”data.frame”
  •


  •


  •


  •




6.3.1
                                                data.frame

  > accountants <- data.frame(home=statef, loot=income, shot=incomef)
                                                           as.data.frame()

                                 read.table()
                  chapter 7
CHAPTER 6.                                                                      23

6.3.2       attach()   detach()
      $      account$statef


         attach()                         directory name
      lentils                     lentils$u,lentils$v,lentils$w

  > attach(lentils)
                                      2           1            u,v,w

  > u <- v+w                                               u
         1                        u
                              $
  > lentils$u <- v+w                      u                            detach
       attach

  > detach()
                                                 2
 u,v,w                                        lentils$u                         2
                        detach
                    detach(lentils) detach("lentils")

NOTE                     R                                20


NOTE                     R                                 2




6.3.3


  •


  •                                                   2         1


  •                                                                      $
                                      detach()
  •



          x,y,z
CHAPTER 6.                                              24

6.3.4
attach()
                                               "list"

      > attach(any.old.list)
                               detach


6.3.5
      search



> search()
[1] ".GlobalEnv" "Autoloads" "package:base"
                                               1
            .GlobalEnv             workspace
       lentils

> search()
[1] ".GlobalEnv" "lentils" "Autoloads" "package:base"
> ls(2)
[1] "u" "v" "w"

                 ls      objects


> detach("lentils")
> search()
[1] ".GlobalEnv" "Autoloads" "package:base"




  1                                autoload
Chapter 7



                                                                            R
                 R                                                                 R
                                                                        1
                                                                 Perl
            R

 read.table()                                                                           scan()

           R                                          R data Import/Export


7.1               read.table()


     •
     •




                             Price     Floor   Area     Rooms    Age        Cent.heat
                     01      52.00     111.0    830       5      6.2           no
                     02      54.75     128.0    710       5      7.5           no
                     03      57.50     101.0   1000       5      4.2           no
                     04      57.50     131.0    690       6      8.8           no
                     05      59.75      93.0    900       5      1.9           yes
                     ...


                Cent.heat

                  read.table()
 1       Unix              Sed   Awk


                                                25
CHAPTER 7.                                                                     26

    > HousePrice <- read.table("houses.data")




                      Price   Floor   Area     Rooms       Age   Cent.heat
                      52.00   111.0    830       5         6.2      no
                      54.75   128.0    710       5         7.5      no
                      57.50   101.0   1000       5         4.2      no
                      57.50   131.0    690       6         8.8      no
                      59.75   93.0     900       5         1.9      yes
                      ...

    > HousePrice <- read.table("houses.data", header=TRUE)
        header=TRUE



7.2             scan()
                                                             3
                                             ’input.dat’              scan()

    > inp <- scan("input.dat", list("",0,0))

       inp

    > label <- inp[[1]]; x <- inp[[2]]; y <- inp[[3]]


    > inp <- scan("input.dat", list(id="", x=0, y=0))

    > label <- in$id; x <- in$x; y <- in$y
                                   2      chapter 6


    > X <- matrix(scan("light.dat", 0), ncol=5, byrow=TRUE)
    R


7.3
R                50
             S-Plus                                data
    data()

    data(infert)
              infert
CHAPTER 7.                                                   27

                                  R




7.3.1               R
                                 package

data(package="nls")
data(Puromycin, package="nls")

                         library()


library(nls)
data()
data(Puromycin)

                                           base nls
        Puromycin



7.4
                         edit

  > xnew <- edit(xold)
               xold                                   xnew

  > xnew <- edit(data.frame())
Chapter 8




8.1      R—
R
    P (X ≤ x)                                         q        P (X ≤ x) > q
       x
       Distribution     R name       additional arguments
       beta             beta         shape1, shape2, ncp
       binomial         binom        size, prob
       Cauchy           cauchy       location, scale
       chi-squared      chisq        df, ncp
       exponential      exp          rate
       F                f            df1, df1, ncp
       gamma            gamma        shape, scale
       geometric        geom         prob
       hypergeometric   hyper        m, n, k
       log-normal       lnorm        meanlog, sdlog
       logistic         logis        location, scale
       negative         binomial     nbinom size, prob
       normal           norm         mean, sd
       Poisson          pois         lambda
       Student s        t            t df, ncp
       uniform          unif         min, max
       Weibull          weibull      shape, scale
       Wilcoxon         wilcox       m, n
                               ’d’           (density) ’p’    CDF ’q’
        ’r’
  dxxx          x pxxx               q qxxx          p rxxx          n rhyper
  rwilcox                                 nn                            (non-
centrality parameter)  CDF

         pxxx qxxx                    lower.tail log.p                dxxx
            log
     - pxxx(t, ..., lower.tail = FALSE, log.p = TRUE)
                                cumulative/integrated hazard function    H(t) =


                                         28
CHAPTER 8.                                                            29

− log(1 − F (t))                            dxxx(..., log = TRUE)
                                                         ptukey qtukey


>   ## 2-tailed    p-value for t distribution
>   2*pt(-2.43,    df = 13)
>   ## upper 1%    point for an F(2, 7) distribution
>   qf(0.99, 2,    7)


8.2

                       summary fivenum
                   stem                        ”stem and leaf” plot

> data(faithful)
> attach(faithful)
> summary(eruptions)
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.600 2.163 4.000 3.488 4.454 5.100
> fivenum(eruptions)
[1] 1.6000 2.1585 4.0000 4.4585 5.1000
> stem(eruptions)
The decimal point is 1 digit(s) to the left of the |
16 | 070355555588
18 | 000022233333335577777777888822335777888
20 | 00002223378800035778
22 | 0002335578023578
24 | 00228
26 | 23
28 | 080
30 | 7
32 | 2337
34 | 250077
36 | 0000823577
38 | 2333335582225577
40 | 0000003357788888002233555577778
42 | 03335555778800233333555577778
44 | 02222335557780000000023333357778888
46 | 0000233357700000023578
48 | 00000022335800333
50 | 0370

                                R                      hist

> hist(eruptions)
## make the bins smaller, make a plot of density
> hist(eruptions, seq(1.6, 5.2, 0.2), prob=TRUE)
> lines(density(eruptions, bw=0.1))
> rug(eruptions) # show the actual data points
CHAPTER 8.                                                                                                          30

                   density
         density
                   1
                                               bw                  (trial-and-error)                              MASS KernSmooth



                                                          Histogram of eruptions
                                 0.7
                                 0.6
                                 0.5
                                 0.4
                       Density

                                 0.3
                                 0.2
                                 0.1
                                 0.0




                                         1.5        2.0   2.5    3.0             3.5    4.0    4.5    5.0

                                                                   eruptions




                                                      stepfun                            ecdf

> library(stepfun)
> plot(ecdf(eruptions), do.points=FALSE, verticals=TRUE)


    eruption           3                                                                                    CDF

>   long <- eruptions[eruptions > 3]
>   plot(ecdf(long), do.points=FALSE, verticals=TRUE)
>   x <- seq(3, 5.4, 0.01)
>   lines(x, pnorm(x, mean=mean(long), sd=sqrt(var(long))), lty=3)

                                                                   ecdf(long)
                                         1.0
                                         0.8
                                         0.6
                                 Fn(x)

                                         0.4
                                         0.2
                                         0.0




                                                3.0        3.5         4.0             4.5      5.0

                                                                             x




        Quantile-quantile (Q-Q)
    1               it usually does for                    interesting             densities
CHAPTER 8.                                                                        31

par(pty="s")
qqnorm(long); qqline(long)

 t

                                                  Normal Q−Q Plot




                                       5.0
                                       4.5
                    Sample Quantiles

                                       4.0
                                       3.5
                                       3.0




                                             −2   −1       0        1     2

                                                  Theoretical Quantiles




x <- rt(250, df = 5)
qqnorm(x); qqline(x)


qqplot(qt(ppoints(250), df=5), x, xlab="Q-Q plot for t dsn")
qqline(x)
                Q-Q
                                                                              ctest
     Shapiro-Wilk
> library(ctest)
> shapiro.test(long)

        Shapiro-Wilk normality test

data: long
W = 0.9793, p-value = 0.01052

     Kolmogorov-Smirnov
> ks.test(long, "pnorm", mean=mean(long), sd=sqrt(var(long)))

        One-sample Kolmogorov-Smirnov test

data: long
D = 0.0661, p-value = 0.4284
alternative hypothesis: two.sided
CHAPTER 8.                                                             32

8.3

                                            R
                                    ctest
 library(ctest)
                               (cal/gm)             Rice(1995,P.490)

Method A: 79.98 80.04 80.02 80.04 80.03 80.03 80.04 79.97
          80.05 80.03 80.02 80.00 80.02
Method B: 80.02 79.94 79.98 79.97 79.97 80.03 79.95 79.97

                  box-plot

A <- scan()
79.98 80.04 80.02 80.04 80.03 80.03 80.04 79.97
80.05 80.03 80.02 80.00 80.02

B <- scan()
80.02 79.94 79.98 79.97 79.97 80.03 79.95 79.97

boxplot(A, B)
                       80.04
                       80.02
                       80.00
                       79.98
                       79.96
                       79.94




                                    1           2




                                                      t-

> t.test(A, B)

        Welch Two Sample t-test

data: A and B
t = 3.2499, df = 12.027, p-value = 0.00694
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
CHAPTER 8.                                                       33

 0.01385526 0.07018320
sample estimates:
mean of x mean of y
 80.02077 79.97875

                                                      R
                         S-Plus     t.test
             F

> var.test(A, B)

          F test to compare two variances

data: A and B
F = 0.5837, num df = 12, denom df = 7, p-value = 0.3938
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
 0.1251097 2.1052687
sample estimates:
ratio of variances
         0.5837405




                 t-

> t.test(A, B, var.equal=TRUE)

          Two Sample t-test

data: A and B
t = 3.4722, df = 19, p-value = 0.002551
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 0.01669058 0.06734788
sample estimates:
mean of x mean of y
 80.02077 79.97875

                                                  Wilcoxon    Mann-
Whitney               (null hypothesis)

> wilcox.test(A, B)

          Wilcoxon rank sum test with continuity correction

data: A and B
W = 89, p-value = 0.007497
alternative hypothesis: true mu is not equal to 0
CHAPTER 8.                                                     34

Warning message:
Cannot compute exact p-value with ties in: wilcox.test.default(A, B)




> library(stepfun)
> plot(ecdf(A), do.points=FALSE, verticals=TRUE, xlim=range(A, B))
> plot(ecdf(B), do.points=FALSE, verticals=TRUE, add=TRUE)

               CDF      qqplot                Q-Q
  Kolmogorov-Smirnov                                ecdf


> ks.test(A, B)

        Two-sample Kolmogorov-Smirnov test

data: A and B
D = 0.5962, p-value = 0.05919
alternative hypothesis: two.sided

Warning message:
cannot compute correct p-values with ties in: ks.test(A, B)
Chapter 9




9.1
R


                               expr 1; . . . ; expr m




9.2
9.2.1               if
R
    > if (expr_1) expr_2 else expr_3
        expr 1

          && ||           if
        & | && ||               & |                                     && ||

    if/else                   ifelse                    ifelse (condition,a,b)
                          condition[i]                    a[i]
    b[i]                              a,b


9.2.2               for           repeat        while
loop
   > for (name in expr_1) expr_2
        name            expr 1                                   1:20
          expr 2
  dummy name                                                  expr 2    name
     expr 1



                                    35
CHAPTER 9.                                                  36

                           ind                      x
        y x                              coplot()



> xc <- split(x, ind)
> yc <- split(y, ind)
> for (i in 1:length(yc)) {
    plot(xc[[i]], yc[[i]]);
    abline(lsfit(xc[[i]], yc[[i]]))
}

                   split()

  help
           for()          R
  R        ’            ’ whole object


     > repeat (expr )


     > while (condition) expr

   break
  repeat
   next
                                                        chapter
10
Chapter 10



                            R                        function
                        R
                             R
                                  R

                                      R                          mean(),
var(),postscript()           R


  > name <- function(arg_1, arg_2, ...) expression
  expression    R                                               arg i

                        name(expr 1,expr 2,...)



10.1
                                          t




> twosam <- function(y1, y2) {
n1 <- length(y1); n2 <- length(y2)
yb1 <- mean(y1); yb2 <- mean(y2)
s1 <- var(y1); s2 <- var(y2)
s <- ((n1-1)*s1 + (n2-1)*s2)/(n1+n2-2)
tst <- (yb1 - yb2)/sqrt(s2*(1/n1 + 1/n2))
tst
}

                             t           Z
  > tstat <- twosam(data$male, data$female); tstat
                                    Matlab backslash
     y      X

                                 37
CHAPTER 10.                                                                        38

                               qr()

                     n                y   n×p       X   Xy           (X X)−1 X y
          −1
 (X X)           (X X)

> bslash <- function(X, y) {
X <- qr(X)
qr.coef(X, y)
}


      > regcoeff <- bslash(Xmat, yvar)

                                                                     1
            R        lsfit()                                               qr() qr.coef()




10.2
             bslash()
      %anything%

                !
      > "%!%" <- function(X, y) { ... }
                                                             x%!%y
                                      (backslash)

                         %*%                        %o%


10.3
               section 2.3                                           "name = obj"


                            fun1
      > fun1 <- function(data, data.frame, graph, limit) {
      [function body omitted]
      }


> ans <- fun1(d, df, TRUE, 20)
> ans <- fun1(d, df, graph=TRUE, limit=20)
> ans <- fun1(data=d, limit=20, graph=TRUE, data.frame=df)
  1      chapter11
CHAPTER 10.                                                           39



                                                               fun1

  > fun1 <- function(data, data.frame, graph=TRUE, limit=20) { ... }

  > ans <- fun1(d, df)

  > ans <- fun1(d, df, limit=10)




10.4           ’...’

                         par()         plot()                   par()
                                                       "..."

  fun1 <- function(data, data.frame, graph=TRUE, limit=20, ...) {
  [omitted statements]

       if (graph)
       par(pch="*", ...)

  [more omissions]
  }


10.5
                                                   (local)     (temporary)
                            X<-qr(x)

               R


                           (global)      (permanent)
               , <<-            assign()                        S-PLUS
            <<- R                            10.7


10.6
10.6.1                            (Efficiency factors)

                          sect
                              blocks(b levels) varieties(v levels)
    R K         v×v           (replications matrix) b × b
 (block size matrix)   N b×b                (incidence matrix)
CHAPTER 10.                                                    40

  E = Iv R1/2 N K −1 N R1/2 = Iv A A
      A = K 1/2 N R−1/2

> bdeff <- function(blocks, varieties) {
blocks <- as.factor(blocks) # minor safety move
b <- length(levels(blocks))
varieties <- as.factor(varieties) # minor safety move
v <- length(levels(varieties))
K <- as.vector(table(blocks)) # remove dim attr
R <- as.vector(table(varieties)) # remove dim attr
N <- table(blocks, varieties)
A <- 1/sqrt(K) * N * rep(1/sqrt(R), rep(b, v))
sv <- svd(A)
list(eff=1 - sv$d^2, blockcv=sv$u, varietycv=sv$v)
}



                   (block and variety canonical contrasts)


10.6.2

                                  dimnames
         dimnames                                      X

> temp <- X
> dimnames(temp) <- list(rep("", nrow(X)), rep("", ncol(X))
> temp; rm(temp)

               no.dimnames()
                               ”wrap around”


no.dimnames <- function(a) {
## Remove all dimension names from an array for compact printing.
d <- list()
l <- 0
for(i in dim(a)) {
d[[l <- l + 1]] <- rep("", i)
}
dimnames(a) <- d
a
}



  > no.dimnames(X)
CHAPTER 10.                                                        41

10.6.3



            —
                one panel trapizum rule       tow panel trapizum
                                                    panel



                         R

area <- function(f, a, b, eps = 1.0e-06, lim = 10) {
fun1 <- function(f, a, b, fa, fb, a0, eps, lim, fun) {
## function   fun1   is only visible inside   area
d <- (a + b)/2
h <- (b - a)/4
fd <- f(d)
a1 <- h * (fa + fd)
a2 <- h * (fd + fb)
if(abs(a0 - a1 - a2) < eps || lim == 0)
return(a1 + a2)
else {
return(fun(f, a, d, fa, fd, a1, eps, lim - 1, fun) +
fun(f, d, b, fd, fb, a2, eps, lim - 1, fun))
}
}
fa <- f(a)
fb <- f(b)
a0 <- ((fa + fb) * (b - a))/2
fun1(f, a, b, fa, fb, a0, eps, lim, fun1)
}


10.7          (scope)

 S-Plus R
                       (symbol)           3

                             binding



f <- function(x) {
y <- 2*x
print(x)
print(y)
print(z)
}
CHAPTER 10.                                                                         42

                   x                    y               z
      R
                                             (lexical scope)
 cube

cube <- function(n) {
sq <- function() n*n
n*sq()
}

          sq           n
                                                                    (static scope) S-
Plus                                                    n
  (R)                       sq                  n           cube
    n                      sq               R S-Plus                     S-Plus
        n                  R                  cube                                      n


## first evaluation in S
S> cube(2)
Error in sq(): Object "n" not found
Dumped
S> n <- 3
S> cube(2)
[1] 18
## then the same function evaluated in R
R> cube(2)
[1] 8

                                                   (mutable state)
                           R

               account
 account                                                    total
                                                total
        total
                       <<-                      total
                                  total
                           2
                                                                                total
   R
 <<-                                                                      <<-



open.account <- function(total) {
list(
deposit = function(amount) {
if(amount <= 0)
stop("Deposits must be positive!n")
  2                            S-Plus               S-Plus
CHAPTER 10.                                                          43

total <<- total + amount
cat(amount, "deposited. Your balance is", total, "nn")
},
withdraw = function(amount) {
if(amount > total)
stop("You don t have that much money!n")
total <<- total - amount
cat(amount, "withdrawn. Your balance is", total, "nn")
},
balance = function() {
cat("Your balance is", total, "nn")
}
)
}
ross <- open.account(100)
robert <- open.account(200)
ross$withdraw(30)
ross$balance()
robert$balance()
ross$deposit(50)
ross$balance()
ross$withdraw(500)


10.8
                                                R                  (site
initialization file)
                       .First .Last
                            R_PROFILE
                 R                      ’etc’
         R                                          ’.Rprofile’3
                                         R

                                                     ’.Rprofile’
  R                           ’.Rprofile’

                             ’.RData’                 .First()
                            R

                       ’Rprofile.site’, ’.Rprofile’,’.RData’       .First()


> .First <- function() {
options(prompt="$ ", continue="+t") # $ is the prompt
options(digits=5, length=999) # custom numbers and printout
x11() # for graphics
par(pch = "+") # plotting character
source(file.path(Sys.getenv("HOME"), "R", "mystuff.R"))
  3   UNIX
CHAPTER 10.                                                       44

# my personal package
library(stepfun) # attach the step function tools
}

                            .Last()


> .Last <- function() {
graphics.off() # a small safety measure.
cat(paste(date(),"nAdiosn")) # Is it time for lunch?
}


10.9
                  (class)                    (generic function)




                                        plot()
 summary(),                   anova()
                                                           ’’data.frame’’


    [     [[<-     any      as.matrix
    [<-   model    plot     summary

      method()
  > methods(class="data.frame")
                                                    ’’data.frame’’
,’’density’’,’’factor’’—                  plot()
                    method()
   > methods(plot)
Chapter 11

R



  R




11.1

            p
  yi =      j=0   βj xij + ei ,   ei ∼ N ID(0, σ 2 ),   i = 1, . . . , n

  y = Xβ + e
      y                     X                                       x0 , x1 , . . . , xp
       x0                   1

Examples

         y,x,x0,x1,x2,...                       X             A,B,C,...


      y~x
      y~1+x
                                                y   x


      y~0+x
      y~-1+x
      y~x-1
            y   x

                                           45
CHAPTER 11. R                                                                  46

   log(y)~x1+x2
                              log(y) x1 x2


   y~poly(x,2)
   y~1+x+I(x^2)
         y   x
   y~X+poly(x,2)
         y                                 X       x
   y~A
         y                                 A
   y~A+x
         y                                     A               x
   y~A*B
   y~A+B+A:B
   y~B%in%A
   y~A/B
         y   A B                    (non-additive)
                 (crossed classification)
       (nested classification)


   y~(A+B+C)^2
   y~A*B*C-A:B:C



   y~A*x
   y~A/x
   y~A/(1+x)-1
         y   x A
                     A
   y~A*B+Error(C)
                                A B                    C            (error strata)
                                                                      C
   R                ~

             response ˜ op 1 term 1 op 2 term 2 op 3 term 3 . . .
CHAPTER 11. R                                                          47

response


op i                    + −


term i
           •                       1
           •
           •


                    1

                         Wilkinson Rogers Glim   Genstat
                          R                            ’.’    ’:’
                                 C&H 1992,p29
       Y˜M
       Y       M
       M 1+M 2
               M1 M2
       M 1-M 2
               M1       M2
       M 1:M 2
       M1 M2                                                 (subclasses)


       M 1 %in% M 2
           M1 M2
       M1*M2
       M 1+M 2+M 1:M 2
       M 1/M 2
       M 1 + M 2 %in% M 1
       Mˆn
       M                  n
       I(M)
               M                       M



           I()
CHAPTER 11. R                                                                      48

11.1.1            (contrasts)


                                         1
                      k             A
                                          2,. . .,k            k−1
                                                                             k−1
 1,. . .,k

                                                                       k
                                contrasts              (options)
 R
  options(contrasts = c("contr.treatment", "contr.poly"))
                   R S                                        S
 Helmert Contrasts                      R                S-Plus

  options(contrasts = c("contr.helmert", "contr.poly"))
                          treatment contrast R

                                                      contrasts C


                                    R




11.2
                                        lm()
> fitted.model <- lm(formula, data = data.frame)


> fm2 <- lm(y ~ x1 + x2, data = production)
             y   x1       x2
                                                       data = production
                                   production                                 production



11.3
    lm()                                                     "lm"
                                                      "lm"


add1         coef              effects       kappa       predict residuals
alias        deviance          family        labels      print   step
anova        drop1             formula       plot        proj    summary
CHAPTER 11. R                                                           49




anova(object 1, object 2)


coefficients(object)                                      coef(object)
deviance(object)
formula(object)
plot(object)
predict(object, newdata=data.frame)
                                               data.frame


print(object)
residuals(object)
         resid(object)
step(object)                                                (hierarchies)
                             AIC
summary(object)


11.4
               aov(formula, data=data.frame)                  lm()     Sect11.3

                    aov()                  (multiple error strata)
                     (split plot experiments)
            (balanced incomplete block designs with recovery of
inter-block information)
   response mean.formula + Error(strata.formula)
                            strata.formula                      strata.formula


   > fm <- aov(yield ~ v + n*p*k + Error(farms/blocks), data=farm.data)
                       v + n*p*k
    ”      ” ”              ” ”       ” (”between farms”, ”within
farms, between blocks” and ”within blocks”)

11.4.1                  (ANOVA tables)
CHAPTER 11. R                                                                            50

11.5
      update()

  > new.model <- update(old.model, new.formula)
    new.formula         ’.’                                               ”
       ”

> fm05 <- lm(y ~ x1 + x2 + x3 + x4 + x5, data = production)
> fm6 <- update(fm05, . ~ . + x6)
> smf6 <- update(fm6, sqrt(.) ~ .)

                 5                                                            production


                                             data=
                                                 update()
      ’.’
  > fmfull <- lm(y ~ . , data = production)
                               y                                          production

                                                   add1(),drop1() step()



11.6


  •                  y              x1 , x2 , . . . ,
  •                                                          y
                      (linear predictor)                         η = β1 x1 + β2 x2 + · · · +
      βp xp ,
                     βi = 0         xi   y
  • y                fY (y; µ, ϕ) = exp[ A yλ(µ) − γ(λ(µ)) + τ (y, ϕ)]
                                         ϕ                                          ϕ
                                                      A
                                                 µ y
        y
  •          µ                                               µ = m(η)         η = m−1 (µ) =
      l(µ)                    l()                       (link function)
CHAPTER 11. R                                                              51

11.6.1          (families)
R
                                         (quasi-likelihood )




11.6.2             glm()
                                   (stimulus variables)


                              R      glm()
     > fitted.model <- glm(formula, family=family.generator, data=data.frame)
                  family.generator


                                 (family generators)           Sect 11.6   ”Family
Name”                                    xxxxxxxxx




     > fm <- glm(y ~ x1 + x2, family = gaussian, data = sales)

     > fm <- lm(y ~ x1+x2, data=sales)


                           quasi



                                               Silvey
                 Kalythos


      Age             20    35     45    55 70
      No.tested       50    50     50    50 50
      No.blind        6     17     26    37 44
                                          logistic   probit
    LD50                                50
         y x                   n
     y ∼ B(n, F (β0 + β1 x))           probit         F (z) = Φ(z)
    logit                    F (z) = ez /(1 + ez )               LD50

                                    LD50= −β0 /β1
CHAPTER 11. R                                                    52




> kalythos <- data.frame(x = c(20,35,45,55,70), n = rep(50,5),
                         y = c(6,17,26,37,44))

      glm()
  •                                         0/1
  •




> kalythos$Ymat <- cbind(kalythos$y, kalythos$n - kalythos$y)



> fmp <- glm(Ymat ~ x, family = binomial(link=probit), data = kalythos)
> fml <- glm(Ymat ~ x, family = binomial, data = kalythos)

                      logit


> summary(fmp)
> summary(fml)

                                LD50


> ld50 <- function(b) -b[1]/b[2]
> ldp <- ld50(coef(fmp)); ldl <- ld50(coef(fml)); c(ldp, ldl)

                              43.663   43.601



                         log
                     (surrogate Poisson log-linear models)




> fmod <- glm(y ~ A + B + x, family = poisson(link=sqrt),
              data = worm.counts)
CHAPTER 11. R                                                                 53

                  (Quasi-likelihood models)

                                                           V ar[y] = µ




          θ1 z1
    y=   z2 −θ2   +e

               1
    y=             +e
         β1 x1 +β2 x2
            x1 = z2 /z1 ,x2 = −1/x1 ,β1 = 1/θ1   β2 = θ2 /θ1


> nlfit <- glm(y ~ x1 + x2 - 1,
               family = quasi(link=inverse, variance=constant),
               data = biochem)




11.7
                                            (Generalized Linear Models)(glm())

     R                        nlm()              S-Plus        ms() nlmin()
                          lack-of-fit                                 nlm()

                           nlm()


11.7.1


                  Bates & Watts (1988), page51.

> x <- c(0.02, 0.02, 0.06, 0.06, 0.11, 0.11, 0.22, 0.22, 0.56, 0.56,
         1.10, 1.10)
> y <- c(76, 47, 97, 107, 123, 139, 159, 152, 191, 201, 207, 200)



> fn <- function(p) sum((y - (p[1] * x)/(p[2] + x))^2)




>   plot(x, y)
>   xfit <- seq(.02, 1.1, .05)
>   yfit <- 200 * xfit/(0.1 + xfit)
>   lines(spline(xfit, yfit))
CHAPTER 11. R                                                          54

                                  200 .1
> out <- nlm(fn, p = c(200, 0.1), hessian = TRUE)
              out$minimum SSE out$estimates


> sqrt(diag(2*out$minimum/(length(y) - 2) * solve(out$hessian)))
          2                            +1 .96SE       95%


>   plot(x, y)
>   xfit <- seq(.02, 1.1, .05)
>   yfit <- 212.68384222 * xfit/(0.06412146 + xfit)
>   lines(spline(xfit, yfit))

              nls
                    Michaelis-Menten
> df <- data.frame(x=x, y=y)
> fit <- nls(y ~ SSmicmen(x, Vm, K), df)
> fit
Nonlinear regression model
  model: y ~ SSmicmen(x, Vm, K)
   data: df
          Vm            K
212.68370749   0.06412123
 residual sum-of-squares: 1195.449
> summary(fit)

Formula: y ~ SSmicmen(x, Vm, K)

Parameters:
    Estimate Std. Error t value Pr(>|t|)
Vm 2.127e+02 6.947e+00 30.615 3.24e-11 ***
K 6.412e-02 8.281e-03     7.743 1.57e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 10.93 on 10 degrees of freedom

Correlation of Parameter Estimates:
      Vm
K 0.7651

11.7.2


    Dobson (1990),pp.108-111                      -         logistic
                   glm()
CHAPTER 11. R                                                       55

> x <- c(1.6907,   1.7242,   1.7552, 1.7842, 1.8113,
         1.8369,   1.8610,   1.8839)
> y <- c( 6, 13,   18, 28,   52, 53, 61, 60)
> n <- c(59, 60,   62, 56,   63, 59, 62, 60)



> fn <- function(p)
    sum( - (y*(p[1]+p[2]*x) - n*log(1+exp(p[1]+p[2]*x))
            + log(choose(n, y)) ))


    > out <- nlm(fn, p = c(-50,20), hessian = TRUE)
              out$minimum             out$estimates

    > sqrt(diag(solve(out$hessian)))
              ±1.96SE 95%


11.8
                       R


•        ( Mixed models )                    nlme
                   lme()        nlme()


•            ( Local approximating regressions )          loess()


         loess                  projection pursuit regression )
                           modreg
•        ( Robust regression)                                       —
                                                lqs       lqs
                 state-of-art
                                                            MASS
       rlm
•Additive models

•Tree based models                                  tbm


                                                               tree()
                                     tbm               plot() text()
     Tree models                           rpart tree
Chapter 12



                R


                    R

                                           UNIX   X11()   Windows
        windows()    MacOS 8/9   macintosh()
                         R



    •       (High-level)


    •       (Low-level)


    •       (Interactive)


               R


    12.1




    12.1.1          plot()
        R                           plot()
                                        (class)

plot(x,y)
plot(xy)

                                    56
CHAPTER 12.                                                                                 57

                      x y         plot(x,y)               y x                                  x y



     plot(x)

                      x                                                            x
                                                                               x


     plot(f)

   plot(f,y)
               f                   y                                                    f
                                   y       f
    plot(df)
 plot( expr)
plot(y expr)
               df                y                   expr
                    ’+’(    a + b + c)
                                                                                    y       expr


       12.1.2
       R                                                             X

                    > pairs(X)
                                                     X

                                                         coplot           a     b
            c
                    > coplot(a ~ b | c)
                    c                    a b                     c
                       c               a b                     c
                                                     conditioning intervals          c
                            a b                                               coplot()
           given.values                  co.intervals()

                 >coplot(a ~ b | c+d)
               c d                             a b
                 coplot() pairs()                           panel=
                                          points()
               panel=
                                  panel.smooth()
CHAPTER 12.                                                              58

                   12.1.3


            qqnorm(x)
              qqline(x)
           qqplot(x,y)
                                                              x

                                                 x            y


                 hist(x)
     hist(x,nclass=n)
hist(x,breaks=b, . . . )
                                       x
                             nclass=                                           breaks=
                                           probability=TRUE


      dotchart(x, . . . )

                                 x               (dotchart)            y       x            x



     image(x,y,z, . . . )
   contour(x,y,z, . . . )

     persp(x,y,z, . . . )
                                                     image
                            z          contour                             z        persp
                            3D

                   12.1.4


          add=TRUE


         axes=FALSE                               —           axis()
                                           axes=TRUE
               log=”x”
               log=”y”
CHAPTER 12.                                                                        59

            log=”xy”           x,y


                type=            type=
                  type=”p”
                  type=”l”
                  type=”b”                               both
                  type=”o”
                  type=”h”                                      high-density
                  type=”s”
                  type=”s”

                  type=”n”


         xlab=string
         ylab=string
                           x         y


        main=string


          sub=string
                                            x


                  12.2



          points(x,y)
            lines(x,y)
                                                              plot()       type=
                                          "p"       points() "l"     lines()
text(x,y,labels, . . . )
                                       x,y                               labels
                                 labels[i]              (x[i],y[i])               1:length(x)
                           Note                                                > plot(x, y, type="n"); text(x, y, names)
                                         type=”n”                                               text()
                                           names
CHAPTER 12.                                                              60

              abline(a, b)
             abline(h=y)
              abline(v=x)
           abline(lm.obj)
                                                         b        a           h=y
                                                                   v=x x
                                            lm.obj           coefficients
                                  2
       polygon(x, y, . . . )
                                                         (x,y)


legend(x, y, legend, . . . )


                                      legend                                          v


               legend( , fill=v)


               legend( , col=v)


               legend( , lty=v)


              legend( , lwd=v)


              legend( , pch=v)
                                            (        )

          title(main,sub)
                                                                      main
                                      sub
           axis(side, . . . )
                                                                                              1 4
                                                                                        —
                                                                                      axes=FALSE
                                plot()
                                                                            x,y
                                                     (user coordinates)

                                x,y                                                       x,y
                                                                          locator()
CHAPTER 12.                                                               61

          12.2.1
                                                              R           text, mtext,axis title


                  > text(x, y, expression(paste(bgroup("(", atop(n, x), ")"),
                                                p^x, q^{n-x})))

                                                               R
                      > help(plotmath)
                      > example(plotmath)

          12.2.2         Hershey
                         text contour               Hershey                       Hershey


              •                           Hershey


              • Hershey


              • Hershey
                      Hershey                         R

                  >   help(Hershey)
                  >   example(Hershey)
                  >   help(Japanese)
                  >   example(Japanese)


          12.3
          R
                           locator()

locator(n,type)
                                                                    n(     512)
                                                    Unix,Windows)   Mac
                                                      type

                        locator()                                           x,y


                      locator()


                      > text(locator(1), "Outlier", adj=0)
                      locator()
                       x,y
CHAPTER 12.                                                               62

identify(x, y, labels)
                                     x,y                                   labels
                                           labels
                                           Unix,Windows                   Mac



                                                                                    x,y
                                  (x, y)             identify()
                             > plot(x,y)      > identify(x,y)
                             identify()
                                                                                  x/y
                                       identify() labels
                                             plot=FALSE
                             identify()                                    x y



                12.4
                                                                    R

                         R
                                                                  ’col’




                12.4.1                       (Permanent changes): par()
                     par()

                par()
 par(c(”col”,”lty”))




  par(col=4, lty=2)




                             par()



                                     par()                                par()
                                                          —
CHAPTER 12.                                                                      63

                                                                          R
                             par()
                   > oldpar <- par(col=4, lty=2)        . . . plotting commands . . .    > par(oldpar)

       12.4.2

           par()

                   > plot(x, y, pch="+")




       12.5
                                                              par() R



name=value
                                  name                            par()
                                value

       12.5.1
       R


  pch=”+”
                   ’◦’          ”.”


     pch=4                   0 18

               > legend(locator(1), as.character(0:18), pch=0:18)
      lty=2
                                                1                2


     lwd=2                                  ”       ”                         line()


     col=2



    font=2                                                                        1
                         2            3         4
   font.axis
CHAPTER 12.                                                                              64

     font.lab
   font.main
     font.sub                                                 x,y


    adj=-0.1                                         0                  1            0.5
                                                                                      -0.1
                                   10%
     cex=1.5


        12.5.2
                R                                                               axis()
                                                         axis line                   lty
          tick mark                                           tick label


lab=c(5,7,12)
                                         x y



       las=1                                 0                              1                2


mgp=c(3,1,0)


                          (text lines)
                                                 0
    tck=0.01                                                                       tck
                    0.5             x y                                     tck=1
                                                                             tck=0.01 mgp=c(1,-1.5,0)

   xaxs=”s”
   yaxs=”d”               x y                            s(standard) e(extended)

                          (extended)
                                         i(internal) r(             )
                            r
                                         d
CHAPTER 12.                                                                                    65

             12.5.3           (Figure margins)
                 R                          figure                                      (plot region)




                                            −−−−−−−−−−−−−−−−−−
                                            −−−−−−−−−−−−−−−−−−
                                            −−−−−−−−−−−−−−−−−−
                                            −−−−−−−−−−−−−−−−−−               mar[3]
                                            −−−−−−−−−−−−−−−−−−
                                            −−−−−−−−−−−−−−−−−−
                                     3.0

                                                   Plot region
                                     1.5
                                     0.0
                                 y




                               mai[2]
                                     −1.5
                                     −3.0




                                            −3.0       −1.5      0.0   1.5       3.0

                                                     mai[1]      x
                                                                               Margin




mai=c(1,0.5,0.5,0)


   mar=c(4,2,2,1)
                      mai                                     (text lines)

                                                                                                  mai mar


                                                                                               postscript()
                   height=4                        mar mai
                 50%


             12.5.4
             R                    n×m
CHAPTER 12.                                                                 66

                                       −−−−−−−−−−−−−−−
                                       −−−−−−−−−−−−−−−
                                       −−−−−−−−−−−−−−−        oma[3]
                                       −−−−−−−−−−−−−−−
                                       −−−−−−−−−−−−−−−




                                                                    omi[4]
                                                   mfg=c(3,2,3,2)




                                                 omi[1]
                                mfrow=c(3,2)




     mfcol=c(3, 2)
    mfrow=c(2, 4)


                              mfcol                  mfrow
                        mfrow=c(3,2)
  mfg=c(2, 2, 3, 2)



                                               (true value)


fig=c(4, 9, 1, 4)/10




 oma=c(2, 0, 3, 0)
 omi=(0, 0, 0.8, 0)
                                    mar mai


                                                                             outer=TRUE
                mtext()
                oma omi
                     split.screen() layout()
CHAPTER 12.                                                               67

       12.6
       R
                                     R
                                           R            (       ”     ”)


                   help(Devices)
                  > postscript()
                                          PostScript


      X11()        X11
postscript()     PostScript                        PostScript
   pictex()              L TEX
                         A




                  > dev.off()




       12.6.1                      PostScript
                 postscript()             file                      PostScript
                                         horizontal=FALSE
               width height
                  > postscript("file.ps", horizontal=FALSE, height=5, pointsize=10)
                                         PostScript

                              R

       12.6.2
           R




      X11() [Unix]
 windows() [Windows]
Macintosh() [MacOS 8/9]
postscript()
   pictex()
        ...
R语言简介

Weitere ähnliche Inhalte

Was ist angesagt?

Code Conventions
Code ConventionsCode Conventions
Code Conventions51 lecture
 
Noodles Vending Machine - Bachelor Thesis
Noodles Vending Machine - Bachelor ThesisNoodles Vending Machine - Bachelor Thesis
Noodles Vending Machine - Bachelor ThesisAhmed Sabek
 
விளக்கங்கள்
விளக்கங்கள்விளக்கங்கள்
விளக்கங்கள்Mohamed Bilal Ali
 
Winning noble
Winning nobleWinning noble
Winning noblestryfe10
 
Jfreereport and Charts an essential Report generation tool for Java Developers
Jfreereport and Charts an essential Report generation tool for Java DevelopersJfreereport and Charts an essential Report generation tool for Java Developers
Jfreereport and Charts an essential Report generation tool for Java DevelopersSanjeev Kulkarni
 
Openbravo er diagram_1_0
Openbravo er diagram_1_0Openbravo er diagram_1_0
Openbravo er diagram_1_0khayer
 
WebHost Manager Online Help 1.0
WebHost Manager Online Help 1.0WebHost Manager Online Help 1.0
WebHost Manager Online Help 1.0webhostingguy
 
Dell Data Migration A Technical White Paper
Dell Data Migration  A Technical White PaperDell Data Migration  A Technical White Paper
Dell Data Migration A Technical White Papernomanc
 
Windows_Server_2016_Virtualization White Paper By Veeam
Windows_Server_2016_Virtualization White Paper By VeeamWindows_Server_2016_Virtualization White Paper By Veeam
Windows_Server_2016_Virtualization White Paper By VeeamKesavan Munuswamy
 
estee lauder 2007Proxy
estee lauder 2007Proxyestee lauder 2007Proxy
estee lauder 2007Proxyfinance32
 

Was ist angesagt? (12)

Code Conventions
Code ConventionsCode Conventions
Code Conventions
 
Noodles Vending Machine - Bachelor Thesis
Noodles Vending Machine - Bachelor ThesisNoodles Vending Machine - Bachelor Thesis
Noodles Vending Machine - Bachelor Thesis
 
விளக்கங்கள்
விளக்கங்கள்விளக்கங்கள்
விளக்கங்கள்
 
Winning noble
Winning nobleWinning noble
Winning noble
 
Sdd 2
Sdd 2Sdd 2
Sdd 2
 
Jfreereport and Charts an essential Report generation tool for Java Developers
Jfreereport and Charts an essential Report generation tool for Java DevelopersJfreereport and Charts an essential Report generation tool for Java Developers
Jfreereport and Charts an essential Report generation tool for Java Developers
 
Openbravo er diagram_1_0
Openbravo er diagram_1_0Openbravo er diagram_1_0
Openbravo er diagram_1_0
 
Avi
AviAvi
Avi
 
WebHost Manager Online Help 1.0
WebHost Manager Online Help 1.0WebHost Manager Online Help 1.0
WebHost Manager Online Help 1.0
 
Dell Data Migration A Technical White Paper
Dell Data Migration  A Technical White PaperDell Data Migration  A Technical White Paper
Dell Data Migration A Technical White Paper
 
Windows_Server_2016_Virtualization White Paper By Veeam
Windows_Server_2016_Virtualization White Paper By VeeamWindows_Server_2016_Virtualization White Paper By Veeam
Windows_Server_2016_Virtualization White Paper By Veeam
 
estee lauder 2007Proxy
estee lauder 2007Proxyestee lauder 2007Proxy
estee lauder 2007Proxy
 

Ähnlich wie R语言简介

Climate Change Road Map
Climate Change Road MapClimate Change Road Map
Climate Change Road MapShane Mitchell
 
Vehicle dynamics De Tradus
Vehicle dynamics De TradusVehicle dynamics De Tradus
Vehicle dynamics De TradusEdward Radulescu
 
Building a humane CMS for Plone: updated tutorial
Building a humane CMS for Plone: updated tutorialBuilding a humane CMS for Plone: updated tutorial
Building a humane CMS for Plone: updated tutorialVincenzo Barone
 
แผนการสอน เรื่อง เซลล์
แผนการสอน เรื่อง เซลล์แผนการสอน เรื่อง เซลล์
แผนการสอน เรื่อง เซลล์Nutt Yang
 
แผนการสอน เรื่อง เซลล์
แผนการสอน เรื่อง เซลล์แผนการสอน เรื่อง เซลล์
แผนการสอน เรื่อง เซลล์Nutt Yang
 
Roland Berger PPT Sample
Roland Berger PPT SampleRoland Berger PPT Sample
Roland Berger PPT Samplehaigou
 
Analogi сталей и св материалов
Analogi сталей и св материаловAnalogi сталей и св материалов
Analogi сталей и св материаловdivyarai27
 

Ähnlich wie R语言简介 (20)

Climate Change Road Map
Climate Change Road MapClimate Change Road Map
Climate Change Road Map
 
Math
MathMath
Math
 
1.1
1.11.1
1.1
 
Networkx 0.99
Networkx 0.99Networkx 0.99
Networkx 0.99
 
Pylons
PylonsPylons
Pylons
 
Pylons
PylonsPylons
Pylons
 
Vehicle dynamics De Tradus
Vehicle dynamics De TradusVehicle dynamics De Tradus
Vehicle dynamics De Tradus
 
Compost1
Compost1Compost1
Compost1
 
Building a humane CMS for Plone: updated tutorial
Building a humane CMS for Plone: updated tutorialBuilding a humane CMS for Plone: updated tutorial
Building a humane CMS for Plone: updated tutorial
 
แผนการสอน เรื่อง เซลล์
แผนการสอน เรื่อง เซลล์แผนการสอน เรื่อง เซลล์
แผนการสอน เรื่อง เซลล์
 
แผนการสอน เรื่อง เซลล์
แผนการสอน เรื่อง เซลล์แผนการสอน เรื่อง เซลล์
แผนการสอน เรื่อง เซลล์
 
Roland Berger PPT Sample
Roland Berger PPT SampleRoland Berger PPT Sample
Roland Berger PPT Sample
 
Analogi сталей и св материалов
Analogi сталей и св материаловAnalogi сталей и св материалов
Analogi сталей и св материалов
 
Chez paul's-gourmet-market
Chez paul's-gourmet-marketChez paul's-gourmet-market
Chez paul's-gourmet-market
 
Gem report
Gem reportGem report
Gem report
 
Learn c++
Learn c++Learn c++
Learn c++
 
Tortoise hg
Tortoise hgTortoise hg
Tortoise hg
 
Tortoise hg
Tortoise hgTortoise hg
Tortoise hg
 
test5
test5test5
test5
 
test6
test6test6
test6
 

Kürzlich hochgeladen

Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...
Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...
Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...nirzagarg
 
Q4-W4-SCIENCE-5 power point presentation
Q4-W4-SCIENCE-5 power point presentationQ4-W4-SCIENCE-5 power point presentation
Q4-W4-SCIENCE-5 power point presentationZenSeloveres
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja Nehwal
 
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...BarusRa
 
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...Call Girls in Nagpur High Profile
 
Hire 💕 8617697112 Meerut Call Girls Service Call Girls Agency
Hire 💕 8617697112 Meerut Call Girls Service Call Girls AgencyHire 💕 8617697112 Meerut Call Girls Service Call Girls Agency
Hire 💕 8617697112 Meerut Call Girls Service Call Girls AgencyNitya salvi
 
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard ...
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard  ...Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard  ...
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard ...nirzagarg
 
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...amitlee9823
 
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Call Girls in Nagpur High Profile
 
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...amitlee9823
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...kumaririma588
 
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)amitlee9823
 
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...Delhi Call girls
 
Jordan_Amanda_DMBS202404_PB1_2024-04.pdf
Jordan_Amanda_DMBS202404_PB1_2024-04.pdfJordan_Amanda_DMBS202404_PB1_2024-04.pdf
Jordan_Amanda_DMBS202404_PB1_2024-04.pdfamanda2495
 
DESIGN THINKING in architecture- Introduction
DESIGN THINKING in architecture- IntroductionDESIGN THINKING in architecture- Introduction
DESIGN THINKING in architecture- Introductionsivagami49
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxjanettecruzeiro1
 
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...Delhi Call girls
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfParomita Roy
 

Kürzlich hochgeladen (20)

Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...
Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...
Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...
 
Q4-W4-SCIENCE-5 power point presentation
Q4-W4-SCIENCE-5 power point presentationQ4-W4-SCIENCE-5 power point presentation
Q4-W4-SCIENCE-5 power point presentation
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
 
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...
 
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...
 
Hire 💕 8617697112 Meerut Call Girls Service Call Girls Agency
Hire 💕 8617697112 Meerut Call Girls Service Call Girls AgencyHire 💕 8617697112 Meerut Call Girls Service Call Girls Agency
Hire 💕 8617697112 Meerut Call Girls Service Call Girls Agency
 
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard ...
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard  ...Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard  ...
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard ...
 
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
 
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
 
Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman MuscatAbortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
 
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
 
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
B. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdfB. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdf
 
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
 
Jordan_Amanda_DMBS202404_PB1_2024-04.pdf
Jordan_Amanda_DMBS202404_PB1_2024-04.pdfJordan_Amanda_DMBS202404_PB1_2024-04.pdf
Jordan_Amanda_DMBS202404_PB1_2024-04.pdf
 
DESIGN THINKING in architecture- Introduction
DESIGN THINKING in architecture- IntroductionDESIGN THINKING in architecture- Introduction
DESIGN THINKING in architecture- Introduction
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptx
 
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
 

R语言简介

  • 1. R R : 1.7 R Development Core Team June 10, 2006
  • 2. Contents 1 1 1.1 R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.3 R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.4 R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.5 R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.7 . . . . . . . . . . . . . . . . . . . . . 3 1.8 R . . . . . . . . . . . . . . . . . . 3 1.9 . . . . . . . . . . . . . . . . . . . . . 4 1.10 . . . . . . . . . . . . . . . . 4 1.11 . . . . . . . . . . . . . . . . . . . . . . 4 2 5 2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.7 (index vector); . . . . . . . . . 8 2.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 3 10 3.1 . . . . . . . . . . . . . . . . . . . . . . . . 10 3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 4 12 4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 4.2 tapply() ragged . . . . . . . . . . . . . . . . . . . . . . 12 4.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 5 14 5.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 5.2 . . . . . . . . . . . . . . . . . . . . . . 14 5.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 i
  • 3. CONTENTS ii 5.4 array() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 5.4.1 . . . . . . . . . . . 16 5.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 5.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 5.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 5.7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 5.7.2 . . . . . . . . . . . . . . . . . . . . . 18 5.7.3 . . . . . . . . . . . . . . . . . . . . . . 19 5.8 . . . . . . . . . . . . . . . . . . . . . . . . . 19 5.9 QR . . . . . . . . . . . . . . . . . . . . . . . 19 5.10 cbind() rbind() . . . . . . . . . . . . . . . . . . 19 5.11 c() . . . . . . . . . . . . . . . . . . . . 19 5.12 . . . . . . . . . . . . . . . . . . . . . . . . . . 20 6 21 6.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 6.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 6.2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 6.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 6.3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . 22 6.3.2 attach() detach() . . . . . . . . . . . . . . . . . . . . . . 23 6.3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . 23 6.3.4 . . . . . . . . . . . . . . . . . . . . . . . . . 24 6.3.5 . . . . . . . . . . . . . . . . . . . . . . . . . 24 7 25 7.1 read.table() . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 7.2 scan() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 7.3 . . . . . . . . . . . . . . . . . . . . . . . . . . 26 7.3.1 R . . . . . . . . . . . . . . . . . 27 7.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 8 28 8.1 R— . . . . . . . . . . . . . . . . . . . . . . 28 8.2 . . . . . . . . . . . . . . . . . . . . . . . . . 29 8.3 . . . . . . . . . . . . . . . . . . . . . . . . . 32 9 35 9.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 9.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 9.2.1 if . . . . . . . . . . . . . . . . . . . . . . . 35 9.2.2 for repeat while . . . . . . . . . . . . 35 10 37 10.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 10.2 . . . . . . . . . . . . . . . . . . . . . . . . . 38 10.3 . . . . . . . . . . . . . . . . . . . . . . . . . 38 10.4 ’...’ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 10.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 10.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
  • 4. CONTENTS iii 10.6.1 (Efficiency factors) . . . . . . . . . . 39 10.6.2 . . . . . . . . . . . . . . . . . 40 10.6.3 . . . . . . . . . . . . . . . . . . . . . . . 41 10.7 (scope) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 10.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 10.9 . . . . . . . . . . . . . . . . . . . . . 44 11 R 45 11.1 . . . . . . . . . . . . . . . . . . . . . . . . . 45 11.1.1 (contrasts) . . . . . . . . . . . . . . . . . . . . . . . . 48 11.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 11.3 . . . . . . . . . . . . . . . . . . . . 48 11.4 . . . . . . . . . . . . . . . . . . . . . . . . . 49 11.4.1 (ANOVA tables) . . . . . . . . . . . . . . . . . 49 11.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 11.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 11.6.1 (families) . . . . . . . . . . . . . . . . . . . . . . . . . . 51 11.6.2 glm() . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 11.7 . . . . . . . . . . . . . . . . . . . 53 11.7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 11.7.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 11.8 . . . . . . . . . . . . . . . . . . . . . . . . . . 55 12 56 12.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 12.1.1 plot() . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 12.1.2 . . . . . . . . . . . . . . . . . . . . . . . . . 57 12.1.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 12.1.4 . . . . . . . . . . . . . . . . . . . . . 58 12.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 12.2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 12.2.2 Hershey . . . . . . . . . . . . . . . . . . . . . . 61 12.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 12.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 12.4.1 (Permanent changes): par() . . . . . . . . 62 12.4.2 . . . . . . . . . . . . . . . . 63 12.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 12.5.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 12.5.2 . . . . . . . . . . . . . . . . . . . . . . . . . 64 12.5.3 (Figure margins) . . . . . . . . . . . . . . . . . . . 65 12.5.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 12.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 12.6.1 PostScript . . . . . . . . . . . . . . . . . . 67 12.6.2 . . . . . . . . . . . . . . . . . . . . . . . . . 67 12.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
  • 5. Chapter 1 1.1 R R • • • • • S ” ” environment R R R R R 1.2 R S Rick Becker,John Chambers AllanWilks Bell S-Plus S John Chambers R , The New S Language: A Programming Environment for Data Analysis and Graphics Richard A. Becker, John M. Chambers and Allan R. Wilks 1991 S (S version 3) Statistical Models in S edited by John M. Chambers and Trevor J. Hastie S-Plus R R S 1
  • 6. CHAPTER 1. 2 1.3 R R R R 8 R CRAN http://cran.r-project.org R S R S SAS SPSS R R 1.4 R R R R X window UNIX Windows R 1.5 R R >, UNIX shell R UNIX shell $ UNIX R 1. , work. . $ mkdir work $ cd work 2. R $ R 3. R 4. R > q() R yes,no cancel( ) R R R 1. work R $ cd work $ R
  • 7. CHAPTER 1. 3 2. R q() Windows R R ” ” R 1.6 R 1.7 UNIX man R solve >help(solve) >?solve if,for function > help("[[") ”It’s im- portant” HTML > help.start() Web (UNIX netscape ) UNIX HTML ’Search Engine and Keywords’ R help.search ?help.search > example(topic) Windows R > ?help 1.8 R R UNIX R A a R ( locale ) ( ) . ***note 1*** (;) ( { }) (#)
  • 8. CHAPTER 1. 4 R + R 1.9 UNIX Windows R ( <DEL> ) UNIX readline Emacs ( ESS Emacs Speaks Statistics) R R Emacs 1.10 work commands.R R > source("commands.R") Windows Source File sink > sink("record.lis") record.lis > sink() 1.11 R R ( ) > objects() ( ls() ) R workspace rm > rm(x, y, z, ink, junk, temp, foo, bar) R R R .RData ***Note3*** R R workspace R x y
  • 9. Chapter 2 2.1 R R x 5 (10.4, 5.6, 3.1, 6.4 21.7) > x <- c(10.4, 5.6, 3.1, 6.4, 21.7) c() c() 1 ( ¡- ) = = ¡ ( ) - ( ) ¡ - ***note 5*** assign() > assign("x", c(10.4, 5.6, 3.1, 6.4, 21.7)) ¡- assign() > c(10.4, 5.6, 3.1, 6.4, 21.7) -> x > 1/x ( x ) > y <- c(x, 0, x) y 11 x 0 2.2 , ( ) > v <- 2*x + y + 1 5
  • 10. CHAPTER 2. 6 11 v 2*x 2.2 y 1 1 11 + - * / ^ log, exp, sin, cos, tan,sqrt max min range 2 c(min(x),max(x)) length(x) x x sum(x) x prod(x) x mean(x) var(x) sum(x)/length(x) sum((x-mean(x))^2)/(length(x)-1) var() n*p p*p p sort(x) x ( order() sort.list()) max min pmax pmin ( ) sqrt(-17) NaN( ) sqrt(-17+0i) 2.3 R 1:30 c(1,2,. . . ,29,30) (:) ( ) 2*1:15 c(2,4, ...,28,30) n<-10 1:n-1 1:(n-1) 30:1 seq() 5 seq(2,10) 2:10 seq() R from=value to=value seq(1,30), seq(from=1, to=30) seq(to=30, from=1) 1:30 seq() by=value length=value by=1 > seq(-5, 5, by=.2) -> s3 s3 c(-5.0, -4.8, -4.6, ..., 4.6, 4.8, 5.0) > s4 <- seq(length=51, from=-5, by=.2) s4 along=vector rep() > s5 <- rep(x, times=5) x s5 5
  • 11. CHAPTER 2. 7 2.4 R TRUE, FALSE, NA (not available ) T F T F TRUE FALSE TRUE FALSE > temp <- x > 13 temp x TRUE FALSE <, <=, >, >=, == != c1 c2 c1 & c2 (”and”) c1 | c2 (”or”) !c1 c1 FALSE 0 TRUE 1 2.5 ” ”(”not available”) ” ”(”missing value”) NA NA NA is.na(x) x NA TRUE FALSE > z <- c(1:3,NA); ind <- is.na(z) x==NA is.na(x) NA x==NA x NA ” ” Not a Number NaN > 0/0 > Inf - Inf NaN is.na(xx) NA NaN TRUE is.nan(xx) NaN TRUE 2.6 R e.g. "x-values" "New iteration results" (’) (”) ( ) R C backslash , ” ". n, , t, tab b, c() paste() sep=string > labs <- paste(c("X","Y"), 1:10, sep="")
  • 12. CHAPTER 2. 8 labs c("X1", "Y2", "X3", "Y4", "X5", "Y6", "X7", "Y8", "X9", "Y10") |c("X","Y")| 5 1:10 2.7 (index vector); 1. TRUE FALSE > y <- x[!is.na(x)] y x x y x > (x+1)[(!is.na(x)) & x>0] -> z z x+1 x 2. {1, 2, ...,length(x)} x[6] x > x[1:10] x 10 ( x 10) > c("x","y")[rep(c(1,2,2,1), times=4)] 16 "x", "y", "y", "x" 4 3. > y <- x[-(1:5)] y 5 4. names > fruit <- c(5, 10, 1, 20) > names(fruit) <- c("orange", "banana", "apple", "peach") > lunch <- fruit[c("apple","orange")]|
  • 13. CHAPTER 2. 9 (names) vector[index_vector] vector > x[is.na(x)] <- 0 replaces any missing values in x by zeros and > y[y < 0] <- -y[y < 0] has the same effect as > y <- abs(y) 2.8 R • (matrices) • (factors) • (lists) • (data frames) ” ” ( ) : • (functions) R workspace R
  • 14. Chapter 3 3.1 R ( ) (numeric) (complex) (logical) (character) NA character(0) numeric(0) R (list) ” ” (function) (expression) R mode(object) length(object) attributes(object) ” ” z 100 mode(z) ”complex” length(z) 100 ( ) R > z <- 0:9 > digits <- as.character(z) z c("0", "1", "2", ..., "9") digits (coercion) digits : > d <- as.integer(digits) d z R as.something() 10
  • 15. CHAPTER 3. 11 3.2 ” ” > e <- numeric() character() > e[3] <- 17 e 3 NA scan() alpha 10 > alpha <- alpha[2 * 1:5] 5 3.3 attributes(object) attr(object,name) R R attr() > attr(z,"dim") <- c(10,10) R z 10 × 10 3.4 R "data.frame" plot() summary() unclass() winter "data.frame" > winter > unclass(winter) sec10.9
  • 16. Chapter 4 1 R 4.1 30 > state <- c("tas", "sa", "qld", "nsw", "nsw", "nt", "wa", "wa", "qld", "vic", "nsw", "vic", "qld", "qld", "sa", "tas", "sa", "nt", "wa", "vic", "qld", "nsw", "nsw", "wa", "sa", "act", "nsw", "vic", "vic", "act") factor() > statef <- factor(state) print() > statef [1] tas sa qld nsw nsw nt wa wa qld vic nsw vic qld qld sa [16] tas sa nt wa vic qld nsw nsw wa sa act nsw vic vic act Levels: act nsw nt qld sa tas vic wa levels() 4.2 tapply() ragged > incomes <- c(60, 49, 40, 61, 64, 60, 59, 54, 62, 69, 70, 42, 56, 61, 61, 61, 58, 51, 48, 65, 49, 49, 41, 48, 52, 46, 59, 46, 58, 43) 1 12
  • 17. CHAPTER 4. 13 tapply() > incmeans <- tapply(incomes, statef, mean) act nsw nt qld sa tas vic wa 44.500 57.333 55.500 53.600 55.000 60.500 56.000 52.250 tapply() incomes mean() incomes tapply() statef incomes statef tapply() R R var() > stderr <- function(x) sqrt(var(x)/length(x)) > incster <- tapply(incomes, statef, stderr) > incster act nsw nt qld sa tas vic wa 1.5 4.3102 4.5 4.1061 2.7386 0.5 5.244 2.6575 95% tapply() length() qt() t tapply() ragged array 4.3 ordered()
  • 18. Chapter 5 5.1 R 2 k 1 R dim z 1500 > dim(z) <- c(3,5,100) dim 3 × 5 × 100 matrix() array() FORTRAN —” ” a c(3,4,2) 24 a[1,1,1], a[2,1,1],..., a[2,4,2], a[3,4,2] 5.2 a[2,,] 4×2 c(4,2) c(a[2,1,1], a[2,2,1], a[2,3,1], a[2,4,1], a[2,1,2], a[2,2,2], a[2,3,2], a[2,4,2]) a[,,] a Z dim() 14
  • 19. CHAPTER 5. 15 5.3 4×5 X • X[1,3], X[2,2] X[3,1] • 0 3×2 > x <- array(1:20,dim=c(4,5)) # Generate a 4 by 5 array. > x [,1] [,2] [,3] [,4] [,5] [1,] 1 5 9 13 17 [2,] 2 6 10 14 18 [3,] 3 7 11 15 19 [4,] 4 8 12 16 20 > i <- array(c(1:3,3:1),dim=c(3,2)) > i [,1] [,2] [1,] 1 3 [2,] 2 2 [3,] 3 1 > x[i] [1] 9 6 3 > x[i] <- 0 # Replace those elements by zeros. > x [,1] [,2] [,3] [,4] [,5] [1,] 1 5 0 13 17 [2,] 2 0 10 14 18 [3,] 0 7 11 15 19 [4,] 4 8 12 16 20 > blocks(b levels) varieties (v levels) n plot > Xb <- matrix(0, n, b) > Xv <- matrix(0, n, v)
  • 20. CHAPTER 5. 16 > ib <- cbind(1:n, blocks) > iv <- cbind(1:n, varieties) > Xb[ib] <- 1 > Xv[iv] <- 1 > X <- cbind(Xb, Xv) N > N <- crossprod(Xb, Xv) table() > N <- table(blocks, varieties) 5.4 array() dim array > Z <- array(data_vector, dim_vector) h 24 > Z <- array(h, dim=c(3,4,2)) h Z 3×4×2 h 24 > dim(Z) <- c(3,4,2) h 24 24 > Z <- array(0, c(3,4,2)) E dim(Z) c(3,4,2) Z[1:24] Z[] Z dim A,B,C > D <- 2*A*B + C + 1 D 5.4.1 • • • dim (As long as short vectors and arrays only are encountered, the arrays must all have the same dim attribute or an error results.)
  • 21. CHAPTER 5. 17 • • dim 5.5 a,b a b %o%: > ab <- a %o% b > ab <- outer(a, b, "*") f (x; y) = cos(y)/(1 + x2) x, y R x,y > f <- function(x, y) cos(y)/(1 + x^2) > z <- outer(x, y, f) 1 chapter10 R 2×2 2×2 [a, b; c, d] 0∼9 ad − bc outer() > d <- outer(0:9, 0:9) > fr <- table(outer(d, d, "-")) > plot(as.numeric(names(fr)), fr, type="h", xlab="Determinant", ylab="Frequency") names for for for chapter9 5.6 aperm(a, perm) perm 1, . . . , k k a a perm[j]
  • 22. CHAPTER 5. 18 A > B <- aperm(A, c(2,1)) B A t() B <- t(A) 5.7 R t(X) nrow(A) ncol(A) A 5.7.1 %*% n×1 1×n n A,B > A * B > A %*% B X > x %*% A %*% x crossprod() cross product crossprod(X, y) t(X) %*% y crossprod() crossprod(X) crossprod(X, X) diag() v diag(v) v M diag(M) M diag() Matlab k diag(k) k×k 5.7.2 > b <- A %*% x A b x R > solve(A,b) x solve(A) A A−1 x = A−1 b solve(A,b) x <- solve(A) %*% b x A−1 x x %*% solve(A,x) A
  • 23. CHAPTER 5. 19 5.7.3 eigen(Sm) values vectors > ev <- eigen(Sm) ev ev$val ev$vec Sm > evals <- eigen(Sm)$values evals > eigen(Sm) > eigen(Sm) 5.8 1 5.9 QR lsfit() > ans <- lsfit(X, y) y X ls.diag() ls.diag() X X qr() > Xplus <- qr(X) > b <- qr.coef(Xplus, y) > fit <- qr.fitted(Xplus, y) > res <- qr.resid(Xplus, y) y x 5.10 cbind() rbind() cbind() rbind() cbind() rbind() > X <- cbind(arg 1, arg 2, arg 3, ...) cbind() 5.11 c() cbind() rbind() dim c() dim,dimnames 1 http://mathworld.wolfram.com/SingularValueDecomposition.html
  • 24. CHAPTER 5. 20 as.vector() c() 5.12 table() k k statef statefr level incomef cut() ”income class”
  • 25. Chapter 6 6.1 R > Lst <- list(name="Fred", wife="Mary", no.children=3, child.ages=c(4,7,9))| Lst Lst[[1]], Lst[[2]], Lst[[3]] Lst[[4]] Lst[[4]] Lst[[4]][1] Lst length(Lst) ¿ name$component name Lst[[1]] Lst$name "Fred" Lst[[2]] Lst$wife "Mary" Lst[[4]][1] Lst$child.ages[1] 4 Lst[["name"]] Lst$name > x <- "name"; Lst[[x]] Lst[[1]] Lst[1] ’[[...]]’ ’[...]’ Lst Lst Lst$coefficients Lst$coe Lst$covariance Lst$cov 21
  • 26. CHAPTER 6. 22 names 6.2 list() > Lst <- list(name_1=object_1, . . ., name_m=object_m) m > Lst[5] <- list(matrix=Mat) 6.2.1 c() > list.ABC <- c(list.A, list.B, list.C) dim 6.3 ”data.frame” • • • • 6.3.1 data.frame > accountants <- data.frame(home=statef, loot=income, shot=incomef) as.data.frame() read.table() chapter 7
  • 27. CHAPTER 6. 23 6.3.2 attach() detach() $ account$statef attach() directory name lentils lentils$u,lentils$v,lentils$w > attach(lentils) 2 1 u,v,w > u <- v+w u 1 u $ > lentils$u <- v+w u detach attach > detach() 2 u,v,w lentils$u 2 detach detach(lentils) detach("lentils") NOTE R 20 NOTE R 2 6.3.3 • • 2 1 • $ detach() • x,y,z
  • 28. CHAPTER 6. 24 6.3.4 attach() "list" > attach(any.old.list) detach 6.3.5 search > search() [1] ".GlobalEnv" "Autoloads" "package:base" 1 .GlobalEnv workspace lentils > search() [1] ".GlobalEnv" "lentils" "Autoloads" "package:base" > ls(2) [1] "u" "v" "w" ls objects > detach("lentils") > search() [1] ".GlobalEnv" "Autoloads" "package:base" 1 autoload
  • 29. Chapter 7 R R R 1 Perl R read.table() scan() R R data Import/Export 7.1 read.table() • • Price Floor Area Rooms Age Cent.heat 01 52.00 111.0 830 5 6.2 no 02 54.75 128.0 710 5 7.5 no 03 57.50 101.0 1000 5 4.2 no 04 57.50 131.0 690 6 8.8 no 05 59.75 93.0 900 5 1.9 yes ... Cent.heat read.table() 1 Unix Sed Awk 25
  • 30. CHAPTER 7. 26 > HousePrice <- read.table("houses.data") Price Floor Area Rooms Age Cent.heat 52.00 111.0 830 5 6.2 no 54.75 128.0 710 5 7.5 no 57.50 101.0 1000 5 4.2 no 57.50 131.0 690 6 8.8 no 59.75 93.0 900 5 1.9 yes ... > HousePrice <- read.table("houses.data", header=TRUE) header=TRUE 7.2 scan() 3 ’input.dat’ scan() > inp <- scan("input.dat", list("",0,0)) inp > label <- inp[[1]]; x <- inp[[2]]; y <- inp[[3]] > inp <- scan("input.dat", list(id="", x=0, y=0)) > label <- in$id; x <- in$x; y <- in$y 2 chapter 6 > X <- matrix(scan("light.dat", 0), ncol=5, byrow=TRUE) R 7.3 R 50 S-Plus data data() data(infert) infert
  • 31. CHAPTER 7. 27 R 7.3.1 R package data(package="nls") data(Puromycin, package="nls") library() library(nls) data() data(Puromycin) base nls Puromycin 7.4 edit > xnew <- edit(xold) xold xnew > xnew <- edit(data.frame())
  • 32. Chapter 8 8.1 R— R P (X ≤ x) q P (X ≤ x) > q x Distribution R name additional arguments beta beta shape1, shape2, ncp binomial binom size, prob Cauchy cauchy location, scale chi-squared chisq df, ncp exponential exp rate F f df1, df1, ncp gamma gamma shape, scale geometric geom prob hypergeometric hyper m, n, k log-normal lnorm meanlog, sdlog logistic logis location, scale negative binomial nbinom size, prob normal norm mean, sd Poisson pois lambda Student s t t df, ncp uniform unif min, max Weibull weibull shape, scale Wilcoxon wilcox m, n ’d’ (density) ’p’ CDF ’q’ ’r’ dxxx x pxxx q qxxx p rxxx n rhyper rwilcox nn (non- centrality parameter) CDF pxxx qxxx lower.tail log.p dxxx log - pxxx(t, ..., lower.tail = FALSE, log.p = TRUE) cumulative/integrated hazard function H(t) = 28
  • 33. CHAPTER 8. 29 − log(1 − F (t)) dxxx(..., log = TRUE) ptukey qtukey > ## 2-tailed p-value for t distribution > 2*pt(-2.43, df = 13) > ## upper 1% point for an F(2, 7) distribution > qf(0.99, 2, 7) 8.2 summary fivenum stem ”stem and leaf” plot > data(faithful) > attach(faithful) > summary(eruptions) Min. 1st Qu. Median Mean 3rd Qu. Max. 1.600 2.163 4.000 3.488 4.454 5.100 > fivenum(eruptions) [1] 1.6000 2.1585 4.0000 4.4585 5.1000 > stem(eruptions) The decimal point is 1 digit(s) to the left of the | 16 | 070355555588 18 | 000022233333335577777777888822335777888 20 | 00002223378800035778 22 | 0002335578023578 24 | 00228 26 | 23 28 | 080 30 | 7 32 | 2337 34 | 250077 36 | 0000823577 38 | 2333335582225577 40 | 0000003357788888002233555577778 42 | 03335555778800233333555577778 44 | 02222335557780000000023333357778888 46 | 0000233357700000023578 48 | 00000022335800333 50 | 0370 R hist > hist(eruptions) ## make the bins smaller, make a plot of density > hist(eruptions, seq(1.6, 5.2, 0.2), prob=TRUE) > lines(density(eruptions, bw=0.1)) > rug(eruptions) # show the actual data points
  • 34. CHAPTER 8. 30 density density 1 bw (trial-and-error) MASS KernSmooth Histogram of eruptions 0.7 0.6 0.5 0.4 Density 0.3 0.2 0.1 0.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 eruptions stepfun ecdf > library(stepfun) > plot(ecdf(eruptions), do.points=FALSE, verticals=TRUE) eruption 3 CDF > long <- eruptions[eruptions > 3] > plot(ecdf(long), do.points=FALSE, verticals=TRUE) > x <- seq(3, 5.4, 0.01) > lines(x, pnorm(x, mean=mean(long), sd=sqrt(var(long))), lty=3) ecdf(long) 1.0 0.8 0.6 Fn(x) 0.4 0.2 0.0 3.0 3.5 4.0 4.5 5.0 x Quantile-quantile (Q-Q) 1 it usually does for interesting densities
  • 35. CHAPTER 8. 31 par(pty="s") qqnorm(long); qqline(long) t Normal Q−Q Plot 5.0 4.5 Sample Quantiles 4.0 3.5 3.0 −2 −1 0 1 2 Theoretical Quantiles x <- rt(250, df = 5) qqnorm(x); qqline(x) qqplot(qt(ppoints(250), df=5), x, xlab="Q-Q plot for t dsn") qqline(x) Q-Q ctest Shapiro-Wilk > library(ctest) > shapiro.test(long) Shapiro-Wilk normality test data: long W = 0.9793, p-value = 0.01052 Kolmogorov-Smirnov > ks.test(long, "pnorm", mean=mean(long), sd=sqrt(var(long))) One-sample Kolmogorov-Smirnov test data: long D = 0.0661, p-value = 0.4284 alternative hypothesis: two.sided
  • 36. CHAPTER 8. 32 8.3 R ctest library(ctest) (cal/gm) Rice(1995,P.490) Method A: 79.98 80.04 80.02 80.04 80.03 80.03 80.04 79.97 80.05 80.03 80.02 80.00 80.02 Method B: 80.02 79.94 79.98 79.97 79.97 80.03 79.95 79.97 box-plot A <- scan() 79.98 80.04 80.02 80.04 80.03 80.03 80.04 79.97 80.05 80.03 80.02 80.00 80.02 B <- scan() 80.02 79.94 79.98 79.97 79.97 80.03 79.95 79.97 boxplot(A, B) 80.04 80.02 80.00 79.98 79.96 79.94 1 2 t- > t.test(A, B) Welch Two Sample t-test data: A and B t = 3.2499, df = 12.027, p-value = 0.00694 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval:
  • 37. CHAPTER 8. 33 0.01385526 0.07018320 sample estimates: mean of x mean of y 80.02077 79.97875 R S-Plus t.test F > var.test(A, B) F test to compare two variances data: A and B F = 0.5837, num df = 12, denom df = 7, p-value = 0.3938 alternative hypothesis: true ratio of variances is not equal to 1 95 percent confidence interval: 0.1251097 2.1052687 sample estimates: ratio of variances 0.5837405 t- > t.test(A, B, var.equal=TRUE) Two Sample t-test data: A and B t = 3.4722, df = 19, p-value = 0.002551 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: 0.01669058 0.06734788 sample estimates: mean of x mean of y 80.02077 79.97875 Wilcoxon Mann- Whitney (null hypothesis) > wilcox.test(A, B) Wilcoxon rank sum test with continuity correction data: A and B W = 89, p-value = 0.007497 alternative hypothesis: true mu is not equal to 0
  • 38. CHAPTER 8. 34 Warning message: Cannot compute exact p-value with ties in: wilcox.test.default(A, B) > library(stepfun) > plot(ecdf(A), do.points=FALSE, verticals=TRUE, xlim=range(A, B)) > plot(ecdf(B), do.points=FALSE, verticals=TRUE, add=TRUE) CDF qqplot Q-Q Kolmogorov-Smirnov ecdf > ks.test(A, B) Two-sample Kolmogorov-Smirnov test data: A and B D = 0.5962, p-value = 0.05919 alternative hypothesis: two.sided Warning message: cannot compute correct p-values with ties in: ks.test(A, B)
  • 39. Chapter 9 9.1 R expr 1; . . . ; expr m 9.2 9.2.1 if R > if (expr_1) expr_2 else expr_3 expr 1 && || if & | && || & | && || if/else ifelse ifelse (condition,a,b) condition[i] a[i] b[i] a,b 9.2.2 for repeat while loop > for (name in expr_1) expr_2 name expr 1 1:20 expr 2 dummy name expr 2 name expr 1 35
  • 40. CHAPTER 9. 36 ind x y x coplot() > xc <- split(x, ind) > yc <- split(y, ind) > for (i in 1:length(yc)) { plot(xc[[i]], yc[[i]]); abline(lsfit(xc[[i]], yc[[i]])) } split() help for() R R ’ ’ whole object > repeat (expr ) > while (condition) expr break repeat next chapter 10
  • 41. Chapter 10 R function R R R R mean(), var(),postscript() R > name <- function(arg_1, arg_2, ...) expression expression R arg i name(expr 1,expr 2,...) 10.1 t > twosam <- function(y1, y2) { n1 <- length(y1); n2 <- length(y2) yb1 <- mean(y1); yb2 <- mean(y2) s1 <- var(y1); s2 <- var(y2) s <- ((n1-1)*s1 + (n2-1)*s2)/(n1+n2-2) tst <- (yb1 - yb2)/sqrt(s2*(1/n1 + 1/n2)) tst } t Z > tstat <- twosam(data$male, data$female); tstat Matlab backslash y X 37
  • 42. CHAPTER 10. 38 qr() n y n×p X Xy (X X)−1 X y −1 (X X) (X X) > bslash <- function(X, y) { X <- qr(X) qr.coef(X, y) } > regcoeff <- bslash(Xmat, yvar) 1 R lsfit() qr() qr.coef() 10.2 bslash() %anything% ! > "%!%" <- function(X, y) { ... } x%!%y (backslash) %*% %o% 10.3 section 2.3 "name = obj" fun1 > fun1 <- function(data, data.frame, graph, limit) { [function body omitted] } > ans <- fun1(d, df, TRUE, 20) > ans <- fun1(d, df, graph=TRUE, limit=20) > ans <- fun1(data=d, limit=20, graph=TRUE, data.frame=df) 1 chapter11
  • 43. CHAPTER 10. 39 fun1 > fun1 <- function(data, data.frame, graph=TRUE, limit=20) { ... } > ans <- fun1(d, df) > ans <- fun1(d, df, limit=10) 10.4 ’...’ par() plot() par() "..." fun1 <- function(data, data.frame, graph=TRUE, limit=20, ...) { [omitted statements] if (graph) par(pch="*", ...) [more omissions] } 10.5 (local) (temporary) X<-qr(x) R (global) (permanent) , <<- assign() S-PLUS <<- R 10.7 10.6 10.6.1 (Efficiency factors) sect blocks(b levels) varieties(v levels) R K v×v (replications matrix) b × b (block size matrix) N b×b (incidence matrix)
  • 44. CHAPTER 10. 40 E = Iv R1/2 N K −1 N R1/2 = Iv A A A = K 1/2 N R−1/2 > bdeff <- function(blocks, varieties) { blocks <- as.factor(blocks) # minor safety move b <- length(levels(blocks)) varieties <- as.factor(varieties) # minor safety move v <- length(levels(varieties)) K <- as.vector(table(blocks)) # remove dim attr R <- as.vector(table(varieties)) # remove dim attr N <- table(blocks, varieties) A <- 1/sqrt(K) * N * rep(1/sqrt(R), rep(b, v)) sv <- svd(A) list(eff=1 - sv$d^2, blockcv=sv$u, varietycv=sv$v) } (block and variety canonical contrasts) 10.6.2 dimnames dimnames X > temp <- X > dimnames(temp) <- list(rep("", nrow(X)), rep("", ncol(X)) > temp; rm(temp) no.dimnames() ”wrap around” no.dimnames <- function(a) { ## Remove all dimension names from an array for compact printing. d <- list() l <- 0 for(i in dim(a)) { d[[l <- l + 1]] <- rep("", i) } dimnames(a) <- d a } > no.dimnames(X)
  • 45. CHAPTER 10. 41 10.6.3 — one panel trapizum rule tow panel trapizum panel R area <- function(f, a, b, eps = 1.0e-06, lim = 10) { fun1 <- function(f, a, b, fa, fb, a0, eps, lim, fun) { ## function fun1 is only visible inside area d <- (a + b)/2 h <- (b - a)/4 fd <- f(d) a1 <- h * (fa + fd) a2 <- h * (fd + fb) if(abs(a0 - a1 - a2) < eps || lim == 0) return(a1 + a2) else { return(fun(f, a, d, fa, fd, a1, eps, lim - 1, fun) + fun(f, d, b, fd, fb, a2, eps, lim - 1, fun)) } } fa <- f(a) fb <- f(b) a0 <- ((fa + fb) * (b - a))/2 fun1(f, a, b, fa, fb, a0, eps, lim, fun1) } 10.7 (scope) S-Plus R (symbol) 3 binding f <- function(x) { y <- 2*x print(x) print(y) print(z) }
  • 46. CHAPTER 10. 42 x y z R (lexical scope) cube cube <- function(n) { sq <- function() n*n n*sq() } sq n (static scope) S- Plus n (R) sq n cube n sq R S-Plus S-Plus n R cube n ## first evaluation in S S> cube(2) Error in sq(): Object "n" not found Dumped S> n <- 3 S> cube(2) [1] 18 ## then the same function evaluated in R R> cube(2) [1] 8 (mutable state) R account account total total total <<- total total 2 total R <<- <<- open.account <- function(total) { list( deposit = function(amount) { if(amount <= 0) stop("Deposits must be positive!n") 2 S-Plus S-Plus
  • 47. CHAPTER 10. 43 total <<- total + amount cat(amount, "deposited. Your balance is", total, "nn") }, withdraw = function(amount) { if(amount > total) stop("You don t have that much money!n") total <<- total - amount cat(amount, "withdrawn. Your balance is", total, "nn") }, balance = function() { cat("Your balance is", total, "nn") } ) } ross <- open.account(100) robert <- open.account(200) ross$withdraw(30) ross$balance() robert$balance() ross$deposit(50) ross$balance() ross$withdraw(500) 10.8 R (site initialization file) .First .Last R_PROFILE R ’etc’ R ’.Rprofile’3 R ’.Rprofile’ R ’.Rprofile’ ’.RData’ .First() R ’Rprofile.site’, ’.Rprofile’,’.RData’ .First() > .First <- function() { options(prompt="$ ", continue="+t") # $ is the prompt options(digits=5, length=999) # custom numbers and printout x11() # for graphics par(pch = "+") # plotting character source(file.path(Sys.getenv("HOME"), "R", "mystuff.R")) 3 UNIX
  • 48. CHAPTER 10. 44 # my personal package library(stepfun) # attach the step function tools } .Last() > .Last <- function() { graphics.off() # a small safety measure. cat(paste(date(),"nAdiosn")) # Is it time for lunch? } 10.9 (class) (generic function) plot() summary(), anova() ’’data.frame’’ [ [[<- any as.matrix [<- model plot summary method() > methods(class="data.frame") ’’data.frame’’ ,’’density’’,’’factor’’— plot() method() > methods(plot)
  • 49. Chapter 11 R R 11.1 p yi = j=0 βj xij + ei , ei ∼ N ID(0, σ 2 ), i = 1, . . . , n y = Xβ + e y X x0 , x1 , . . . , xp x0 1 Examples y,x,x0,x1,x2,... X A,B,C,... y~x y~1+x y x y~0+x y~-1+x y~x-1 y x 45
  • 50. CHAPTER 11. R 46 log(y)~x1+x2 log(y) x1 x2 y~poly(x,2) y~1+x+I(x^2) y x y~X+poly(x,2) y X x y~A y A y~A+x y A x y~A*B y~A+B+A:B y~B%in%A y~A/B y A B (non-additive) (crossed classification) (nested classification) y~(A+B+C)^2 y~A*B*C-A:B:C y~A*x y~A/x y~A/(1+x)-1 y x A A y~A*B+Error(C) A B C (error strata) C R ~ response ˜ op 1 term 1 op 2 term 2 op 3 term 3 . . .
  • 51. CHAPTER 11. R 47 response op i + − term i • 1 • • 1 Wilkinson Rogers Glim Genstat R ’.’ ’:’ C&H 1992,p29 Y˜M Y M M 1+M 2 M1 M2 M 1-M 2 M1 M2 M 1:M 2 M1 M2 (subclasses) M 1 %in% M 2 M1 M2 M1*M2 M 1+M 2+M 1:M 2 M 1/M 2 M 1 + M 2 %in% M 1 Mˆn M n I(M) M M I()
  • 52. CHAPTER 11. R 48 11.1.1 (contrasts) 1 k A 2,. . .,k k−1 k−1 1,. . .,k k contrasts (options) R options(contrasts = c("contr.treatment", "contr.poly")) R S S Helmert Contrasts R S-Plus options(contrasts = c("contr.helmert", "contr.poly")) treatment contrast R contrasts C R 11.2 lm() > fitted.model <- lm(formula, data = data.frame) > fm2 <- lm(y ~ x1 + x2, data = production) y x1 x2 data = production production production 11.3 lm() "lm" "lm" add1 coef effects kappa predict residuals alias deviance family labels print step anova drop1 formula plot proj summary
  • 53. CHAPTER 11. R 49 anova(object 1, object 2) coefficients(object) coef(object) deviance(object) formula(object) plot(object) predict(object, newdata=data.frame) data.frame print(object) residuals(object) resid(object) step(object) (hierarchies) AIC summary(object) 11.4 aov(formula, data=data.frame) lm() Sect11.3 aov() (multiple error strata) (split plot experiments) (balanced incomplete block designs with recovery of inter-block information) response mean.formula + Error(strata.formula) strata.formula strata.formula > fm <- aov(yield ~ v + n*p*k + Error(farms/blocks), data=farm.data) v + n*p*k ” ” ” ” ” ” (”between farms”, ”within farms, between blocks” and ”within blocks”) 11.4.1 (ANOVA tables)
  • 54. CHAPTER 11. R 50 11.5 update() > new.model <- update(old.model, new.formula) new.formula ’.’ ” ” > fm05 <- lm(y ~ x1 + x2 + x3 + x4 + x5, data = production) > fm6 <- update(fm05, . ~ . + x6) > smf6 <- update(fm6, sqrt(.) ~ .) 5 production data= update() ’.’ > fmfull <- lm(y ~ . , data = production) y production add1(),drop1() step() 11.6 • y x1 , x2 , . . . , • y (linear predictor) η = β1 x1 + β2 x2 + · · · + βp xp , βi = 0 xi y • y fY (y; µ, ϕ) = exp[ A yλ(µ) − γ(λ(µ)) + τ (y, ϕ)] ϕ ϕ A µ y y • µ µ = m(η) η = m−1 (µ) = l(µ) l() (link function)
  • 55. CHAPTER 11. R 51 11.6.1 (families) R (quasi-likelihood ) 11.6.2 glm() (stimulus variables) R glm() > fitted.model <- glm(formula, family=family.generator, data=data.frame) family.generator (family generators) Sect 11.6 ”Family Name” xxxxxxxxx > fm <- glm(y ~ x1 + x2, family = gaussian, data = sales) > fm <- lm(y ~ x1+x2, data=sales) quasi Silvey Kalythos Age 20 35 45 55 70 No.tested 50 50 50 50 50 No.blind 6 17 26 37 44 logistic probit LD50 50 y x n y ∼ B(n, F (β0 + β1 x)) probit F (z) = Φ(z) logit F (z) = ez /(1 + ez ) LD50 LD50= −β0 /β1
  • 56. CHAPTER 11. R 52 > kalythos <- data.frame(x = c(20,35,45,55,70), n = rep(50,5), y = c(6,17,26,37,44)) glm() • 0/1 • > kalythos$Ymat <- cbind(kalythos$y, kalythos$n - kalythos$y) > fmp <- glm(Ymat ~ x, family = binomial(link=probit), data = kalythos) > fml <- glm(Ymat ~ x, family = binomial, data = kalythos) logit > summary(fmp) > summary(fml) LD50 > ld50 <- function(b) -b[1]/b[2] > ldp <- ld50(coef(fmp)); ldl <- ld50(coef(fml)); c(ldp, ldl) 43.663 43.601 log (surrogate Poisson log-linear models) > fmod <- glm(y ~ A + B + x, family = poisson(link=sqrt), data = worm.counts)
  • 57. CHAPTER 11. R 53 (Quasi-likelihood models) V ar[y] = µ θ1 z1 y= z2 −θ2 +e 1 y= +e β1 x1 +β2 x2 x1 = z2 /z1 ,x2 = −1/x1 ,β1 = 1/θ1 β2 = θ2 /θ1 > nlfit <- glm(y ~ x1 + x2 - 1, family = quasi(link=inverse, variance=constant), data = biochem) 11.7 (Generalized Linear Models)(glm()) R nlm() S-Plus ms() nlmin() lack-of-fit nlm() nlm() 11.7.1 Bates & Watts (1988), page51. > x <- c(0.02, 0.02, 0.06, 0.06, 0.11, 0.11, 0.22, 0.22, 0.56, 0.56, 1.10, 1.10) > y <- c(76, 47, 97, 107, 123, 139, 159, 152, 191, 201, 207, 200) > fn <- function(p) sum((y - (p[1] * x)/(p[2] + x))^2) > plot(x, y) > xfit <- seq(.02, 1.1, .05) > yfit <- 200 * xfit/(0.1 + xfit) > lines(spline(xfit, yfit))
  • 58. CHAPTER 11. R 54 200 .1 > out <- nlm(fn, p = c(200, 0.1), hessian = TRUE) out$minimum SSE out$estimates > sqrt(diag(2*out$minimum/(length(y) - 2) * solve(out$hessian))) 2 +1 .96SE 95% > plot(x, y) > xfit <- seq(.02, 1.1, .05) > yfit <- 212.68384222 * xfit/(0.06412146 + xfit) > lines(spline(xfit, yfit)) nls Michaelis-Menten > df <- data.frame(x=x, y=y) > fit <- nls(y ~ SSmicmen(x, Vm, K), df) > fit Nonlinear regression model model: y ~ SSmicmen(x, Vm, K) data: df Vm K 212.68370749 0.06412123 residual sum-of-squares: 1195.449 > summary(fit) Formula: y ~ SSmicmen(x, Vm, K) Parameters: Estimate Std. Error t value Pr(>|t|) Vm 2.127e+02 6.947e+00 30.615 3.24e-11 *** K 6.412e-02 8.281e-03 7.743 1.57e-05 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 10.93 on 10 degrees of freedom Correlation of Parameter Estimates: Vm K 0.7651 11.7.2 Dobson (1990),pp.108-111 - logistic glm()
  • 59. CHAPTER 11. R 55 > x <- c(1.6907, 1.7242, 1.7552, 1.7842, 1.8113, 1.8369, 1.8610, 1.8839) > y <- c( 6, 13, 18, 28, 52, 53, 61, 60) > n <- c(59, 60, 62, 56, 63, 59, 62, 60) > fn <- function(p) sum( - (y*(p[1]+p[2]*x) - n*log(1+exp(p[1]+p[2]*x)) + log(choose(n, y)) )) > out <- nlm(fn, p = c(-50,20), hessian = TRUE) out$minimum out$estimates > sqrt(diag(solve(out$hessian))) ±1.96SE 95% 11.8 R • ( Mixed models ) nlme lme() nlme() • ( Local approximating regressions ) loess() loess projection pursuit regression ) modreg • ( Robust regression) — lqs lqs state-of-art MASS rlm •Additive models •Tree based models tbm tree() tbm plot() text() Tree models rpart tree
  • 60. Chapter 12 R R UNIX X11() Windows windows() MacOS 8/9 macintosh() R • (High-level) • (Low-level) • (Interactive) R 12.1 12.1.1 plot() R plot() (class) plot(x,y) plot(xy) 56
  • 61. CHAPTER 12. 57 x y plot(x,y) y x x y plot(x) x x x plot(f) plot(f,y) f y f y f plot(df) plot( expr) plot(y expr) df y expr ’+’( a + b + c) y expr 12.1.2 R X > pairs(X) X coplot a b c > coplot(a ~ b | c) c a b c c a b c conditioning intervals c a b coplot() given.values co.intervals() >coplot(a ~ b | c+d) c d a b coplot() pairs() panel= points() panel= panel.smooth()
  • 62. CHAPTER 12. 58 12.1.3 qqnorm(x) qqline(x) qqplot(x,y) x x y hist(x) hist(x,nclass=n) hist(x,breaks=b, . . . ) x nclass= breaks= probability=TRUE dotchart(x, . . . ) x (dotchart) y x x image(x,y,z, . . . ) contour(x,y,z, . . . ) persp(x,y,z, . . . ) image z contour z persp 3D 12.1.4 add=TRUE axes=FALSE — axis() axes=TRUE log=”x” log=”y”
  • 63. CHAPTER 12. 59 log=”xy” x,y type= type= type=”p” type=”l” type=”b” both type=”o” type=”h” high-density type=”s” type=”s” type=”n” xlab=string ylab=string x y main=string sub=string x 12.2 points(x,y) lines(x,y) plot() type= "p" points() "l" lines() text(x,y,labels, . . . ) x,y labels labels[i] (x[i],y[i]) 1:length(x) Note > plot(x, y, type="n"); text(x, y, names) type=”n” text() names
  • 64. CHAPTER 12. 60 abline(a, b) abline(h=y) abline(v=x) abline(lm.obj) b a h=y v=x x lm.obj coefficients 2 polygon(x, y, . . . ) (x,y) legend(x, y, legend, . . . ) legend v legend( , fill=v) legend( , col=v) legend( , lty=v) legend( , lwd=v) legend( , pch=v) ( ) title(main,sub) main sub axis(side, . . . ) 1 4 — axes=FALSE plot() x,y (user coordinates) x,y x,y locator()
  • 65. CHAPTER 12. 61 12.2.1 R text, mtext,axis title > text(x, y, expression(paste(bgroup("(", atop(n, x), ")"), p^x, q^{n-x}))) R > help(plotmath) > example(plotmath) 12.2.2 Hershey text contour Hershey Hershey • Hershey • Hershey • Hershey Hershey R > help(Hershey) > example(Hershey) > help(Japanese) > example(Japanese) 12.3 R locator() locator(n,type) n( 512) Unix,Windows) Mac type locator() x,y locator() > text(locator(1), "Outlier", adj=0) locator() x,y
  • 66. CHAPTER 12. 62 identify(x, y, labels) x,y labels labels Unix,Windows Mac x,y (x, y) identify() > plot(x,y) > identify(x,y) identify() x/y identify() labels plot=FALSE identify() x y 12.4 R R ’col’ 12.4.1 (Permanent changes): par() par() par() par(c(”col”,”lty”)) par(col=4, lty=2) par() par() par() —
  • 67. CHAPTER 12. 63 R par() > oldpar <- par(col=4, lty=2) . . . plotting commands . . . > par(oldpar) 12.4.2 par() > plot(x, y, pch="+") 12.5 par() R name=value name par() value 12.5.1 R pch=”+” ’◦’ ”.” pch=4 0 18 > legend(locator(1), as.character(0:18), pch=0:18) lty=2 1 2 lwd=2 ” ” line() col=2 font=2 1 2 3 4 font.axis
  • 68. CHAPTER 12. 64 font.lab font.main font.sub x,y adj=-0.1 0 1 0.5 -0.1 10% cex=1.5 12.5.2 R axis() axis line lty tick mark tick label lab=c(5,7,12) x y las=1 0 1 2 mgp=c(3,1,0) (text lines) 0 tck=0.01 tck 0.5 x y tck=1 tck=0.01 mgp=c(1,-1.5,0) xaxs=”s” yaxs=”d” x y s(standard) e(extended) (extended) i(internal) r( ) r d
  • 69. CHAPTER 12. 65 12.5.3 (Figure margins) R figure (plot region) −−−−−−−−−−−−−−−−−− −−−−−−−−−−−−−−−−−− −−−−−−−−−−−−−−−−−− −−−−−−−−−−−−−−−−−− mar[3] −−−−−−−−−−−−−−−−−− −−−−−−−−−−−−−−−−−− 3.0 Plot region 1.5 0.0 y mai[2] −1.5 −3.0 −3.0 −1.5 0.0 1.5 3.0 mai[1] x Margin mai=c(1,0.5,0.5,0) mar=c(4,2,2,1) mai (text lines) mai mar postscript() height=4 mar mai 50% 12.5.4 R n×m
  • 70. CHAPTER 12. 66 −−−−−−−−−−−−−−− −−−−−−−−−−−−−−− −−−−−−−−−−−−−−− oma[3] −−−−−−−−−−−−−−− −−−−−−−−−−−−−−− omi[4] mfg=c(3,2,3,2) omi[1] mfrow=c(3,2) mfcol=c(3, 2) mfrow=c(2, 4) mfcol mfrow mfrow=c(3,2) mfg=c(2, 2, 3, 2) (true value) fig=c(4, 9, 1, 4)/10 oma=c(2, 0, 3, 0) omi=(0, 0, 0.8, 0) mar mai outer=TRUE mtext() oma omi split.screen() layout()
  • 71. CHAPTER 12. 67 12.6 R R R ( ” ”) help(Devices) > postscript() PostScript X11() X11 postscript() PostScript PostScript pictex() L TEX A > dev.off() 12.6.1 PostScript postscript() file PostScript horizontal=FALSE width height > postscript("file.ps", horizontal=FALSE, height=5, pointsize=10) PostScript R 12.6.2 R X11() [Unix] windows() [Windows] Macintosh() [MacOS 8/9] postscript() pictex() ...