Rによるやさしい統計学

第1部

第1章

In [1]:
3 + 5
8
In [2]:
10 - 3
7
In [3]:
2 * 3
6
In [4]:
# コメント
20 / 4
5
In [5]:
sqrt(16)
4
In [6]:
2^2
4
In [7]:
summary(c(173,178,180,183,182,174,179,179,174,192))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  173.0   175.0   179.0   179.4   181.5   192.0 
In [8]:
height <- c(173,178,180,183,182,174,179,179,174,192)
summary(height)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  173.0   175.0   179.0   179.4   181.5   192.0 
In [9]:
sort(height)
  1. 173
  2. 174
  3. 174
  4. 178
  5. 179
  6. 179
  7. 180
  8. 182
  9. 183
  10. 192
In [10]:
library(pipeR)
  • 中央値
In [11]:
sort(height)[5:6] %>>% mean
179
  • 第一四分位,第三四分位
    • p.27 の方法は正しくない
In [12]:
sort(height)[2:3] %>>% mean
174
In [13]:
sort(height)[7:8] %>>% mean
181
In [14]:
my_quantile <- function(x, q){
  x <- sort(x)
  n <- length(x)
  # 1 - q + qn = 1 + (n - 1)q
  j <- 1 + (n - 1) * q / 100
  if (j == as.integer(j)) {
    ret <- x[j]
  } else {
    i <- floor(j)
    k <- ceiling(j)
    # (k - j)x[i] + (j - i)x[k] = (k + i - i -j)x[i] + (j - i)x[k] = {(k - i)-(j - i)}x[i] + (j - i)x[k]
    # k - i = 1 なので
    # x[i] + (j - i)(x[k] - x[i])
    ret <- x[i] + (j - i)*(x[k] - x[i])
  }
  return(ret)
}
In [15]:
my_quantile(height, 0)
173
In [16]:
my_quantile(height, 25)
175
In [17]:
my_quantile(height, 50)
179
In [18]:
my_quantile(height, 75)
181.5
In [19]:
my_quantile(height, 100)
192

1.6

In [20]:
hawks <- matrix(c(173,178,180,183,182,174,179,179,174,192, 17000,14000,9000,50000,30000,12000,900,2100,1000,25000),10,2)
hawks
17317000
17814000
1809000
18350000
18230000
17412000
179900
1792100
1741000
19225000
In [21]:
library(readr)
In [22]:
hawks2 <- read_csv("hawks.csv")
In [23]:
str(hawks2)
Classes 'tbl_df', 'tbl' and 'data.frame':	10 obs. of  2 variables:
 $ height: int  173 178 180 183 182 174 179 179 174 192
 $ salaly: int  17000 14000 9000 50000 30000 12000 900 2100 1000 25000
In [24]:
hawks2
heightsalaly
117317000
217814000
31809000
418350000
518230000
617412000
7179900
81792100
91741000
1019225000
In [25]:
varp <- function(x){
    return(var(x) * (length(x) - 1) / length(x))
}
In [26]:
x <- c(10, 13, 8, 15, 8)
var(x)
varp(x)
9.7
7.76
In [27]:
source("varp.r")
In [28]:
devtools::session_info()
Session info -------------------------------------------------------------------
Packages -----------------------------------------------------------------------
Session info -------------------------------------------------------------------
Packages -----------------------------------------------------------------------
 setting  value                       
 version  R version 3.2.3 (2015-12-10)
 system   x86_64, mingw32             
 ui       RTerm                       
 language en                          
 collate  Japanese_Japan.932          
 tz       Asia/Tokyo                  
 date     2016-05-18                  

 package   * version date       source                            
 base64enc   0.1-3   2015-07-28 CRAN (R 3.2.2)                    
 devtools    1.10.0  2016-01-23 CRAN (R 3.2.3)                    
 digest      0.6.9   2016-01-08 CRAN (R 3.2.3)                    
 evaluate    0.8     2015-09-18 CRAN (R 3.2.2)                    
 IRdisplay   0.3     2015-04-27 local                             
 IRkernel    0.6     2016-02-08 Github (IRkernel/IRkernel@40dc791)
 jsonlite    0.9.19  2015-11-28 CRAN (R 3.2.2)                    
 magrittr    1.5     2014-11-22 CRAN (R 3.2.1)                    
 memoise     1.0.0   2016-01-29 CRAN (R 3.2.3)                    
 pbdZMQ      0.2-1   2016-01-21 CRAN (R 3.2.3)                    
 pipeR     * 0.6.0.6 2015-07-08 CRAN (R 3.2.1)                    
 R6          2.1.2   2016-01-26 CRAN (R 3.2.3)                    
 Rcpp        0.12.3  2016-01-10 CRAN (R 3.2.3)                    
 readr     * 0.2.2   2015-10-22 CRAN (R 3.2.2)                    
 repr        0.4     2015-11-16 local                             
 stringi     1.0-1   2015-10-22 CRAN (R 3.2.3)                    
 stringr     1.0.0   2015-04-30 CRAN (R 3.2.3)                    
 uuid        0.1-2   2015-07-28 CRAN (R 3.2.2)