第II部

第12章 二項検定

In [1]:
sapply(c("pipeR", "dplyr", "tidyr", "ggplot2", "readr", "readxl", "Cairo", "grid", "gridExtra"), 
       require, character.only = TRUE)
Loading required package: pipeR
Loading required package: dplyr
Warning message:
: package 'dplyr' was built under R version 3.3.1
Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

    filter, lag

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

Loading required package: tidyr
Loading required package: ggplot2
Loading required package: readr
Loading required package: readxl
Loading required package: Cairo
Loading required package: grid
Loading required package: gridExtra
Warning message:
: package 'gridExtra' was built under R version 3.3.1
Attaching package: 'gridExtra'

The following object is masked from 'package:dplyr':

    combine

pipeR
TRUE
dplyr
TRUE
tidyr
TRUE
ggplot2
TRUE
readr
TRUE
readxl
TRUE
Cairo
TRUE
grid
TRUE
gridExtra
TRUE
  • 日ハム : 楽天 = 17:3
  • 日ハムの方が強いか => 勝つ確率が0.5より大きいかどうか => 帰無仮説: 勝つ確率は0.5(片側検定)

勝つ確率が0.5の時に17勝する確率は

In [2]:
dbinom(17, 20, 0.5)
0.00108718872070313
In [3]:
options(repr.plot.width = 4, repr.plot.height = 4)
In [4]:
data.frame(x = c(0:20), y = dbinom(c(0:20), 20, 0.5)) %>>% 
    ggplot(aes(x = x, y = y)) + 
        geom_point()

In [5]:
pbinom(c(0:20), 20, 0.5) %>>% round(digit = 4)
  1. 0
  2. 0
  3. 2e-04
  4. 0.0013
  5. 0.0059
  6. 0.0207
  7. 0.0577
  8. 0.1316
  9. 0.2517
  10. 0.4119
  11. 0.5881
  12. 0.7483
  13. 0.8684
  14. 0.9423
  15. 0.9793
  16. 0.9941
  17. 0.9987
  18. 0.9998
  19. 1
  20. 1
  21. 1

棄却域は15勝以上

In [6]:
data.frame(win = c(0:20), prob = pbinom(c(0:20), 20, 0.5)) %>>% filter(prob >= 0.95)
winprob
114.0000000 0.9793053
215.000000 0.994091
316.0000000 0.9987116
417.0000000 0.9997988
518.00000 0.99998
619.000000 0.999999
720 1

$p$値は

In [7]:
pbinom(16, 20, 0.5, lower.tail = FALSE)
0.00128841400146485

12.3 じゃんけん

10回中7回的中は1/3より高いか

棄却域は7回以上

In [8]:
data.frame(x = c(0:10), prob = pbinom(c(0:10), 10, 1/3)) %>>% filter(prob >= 0.95)
xprob
16.00000000.9803384
27.0000000.996596
38.00000000.9996444
49.00000000.9999831
510 1
In [9]:
pbinom(6, 10, 1/3, lower.tail = FALSE)
0.0196616369455876
In [10]:
devtools::session_info()
Session info -------------------------------------------------------------------
Packages -----------------------------------------------------------------------
Session info -------------------------------------------------------------------
Packages -----------------------------------------------------------------------
 setting  value                       
 version  R version 3.3.0 (2016-05-03)
 system   x86_64, mingw32             
 ui       RTerm                       
 language (EN)                        
 collate  Japanese_Japan.932          
 tz       Asia/Tokyo                  
 date     2016-07-07                  

 package    * version    date       source                             
 assertthat   0.1        2013-12-06 CRAN (R 3.3.0)                     
 base64enc    0.1-3      2015-07-28 CRAN (R 3.3.0)                     
 Cairo      * 1.5-9      2015-09-26 CRAN (R 3.3.0)                     
 colorspace   1.2-6      2015-03-11 CRAN (R 3.3.0)                     
 DBI          0.4-1      2016-05-08 CRAN (R 3.3.0)                     
 devtools     1.11.1     2016-04-21 CRAN (R 3.3.0)                     
 digest       0.6.9      2016-01-08 CRAN (R 3.3.0)                     
 dplyr      * 0.5.0      2016-06-24 CRAN (R 3.3.1)                     
 evaluate     0.9        2016-04-29 CRAN (R 3.3.0)                     
 ggplot2    * 2.1.0      2016-03-01 CRAN (R 3.3.0)                     
 gridExtra  * 2.2.1      2016-02-29 CRAN (R 3.3.1)                     
 gtable       0.2.0      2016-02-26 CRAN (R 3.3.0)                     
 IRdisplay    0.3.0.9000 2016-06-20 Github (IRkernel/IRdisplay@69fd5d8)
 IRkernel     0.6        2016-06-20 Github (IRkernel/IRkernel@735d4ff) 
 jsonlite     0.9.22     2016-06-15 CRAN (R 3.3.0)                     
 labeling     0.3        2014-08-23 CRAN (R 3.3.0)                     
 lazyeval     0.2.0      2016-06-12 CRAN (R 3.3.0)                     
 magrittr     1.5        2014-11-22 CRAN (R 3.3.0)                     
 memoise      1.0.0      2016-01-29 CRAN (R 3.3.0)                     
 munsell      0.4.3      2016-02-13 CRAN (R 3.3.0)                     
 pbdZMQ       0.2-3      2016-05-20 CRAN (R 3.3.0)                     
 pipeR      * 0.6.1.3    2016-04-04 CRAN (R 3.3.0)                     
 plyr         1.8.4      2016-06-08 CRAN (R 3.3.0)                     
 R6           2.1.2      2016-01-26 CRAN (R 3.3.0)                     
 Rcpp         0.12.5     2016-05-14 CRAN (R 3.3.0)                     
 readr      * 0.2.2      2015-10-22 CRAN (R 3.3.0)                     
 readxl     * 0.1.1      2016-03-28 CRAN (R 3.3.0)                     
 repr         0.7        2016-05-13 CRAN (R 3.3.0)                     
 scales       0.4.0      2016-02-26 CRAN (R 3.3.0)                     
 stringi      1.1.1      2016-05-27 CRAN (R 3.3.0)                     
 stringr      1.0.0      2015-04-30 CRAN (R 3.3.0)                     
 tibble       1.0        2016-03-23 CRAN (R 3.3.0)                     
 tidyr      * 0.5.1      2016-06-14 CRAN (R 3.3.0)                     
 uuid         0.1-2      2015-07-28 CRAN (R 3.3.0)                     
 withr        1.0.1      2016-02-04 CRAN (R 3.3.0)