rnorm(10, 170, 10) #Kogus, keskväärtus, standardhälve 
##  [1] 183.6060 167.3414 165.6200 171.1106 183.9981 179.2919 166.1464
##  [8] 186.7021 174.6433 179.6424
 round(rnorm(10, 170, 10))
##  [1] 161 170 172 162 163 172 165 158 187 162
 sort(round(rnorm(10, 170, 10)))
##  [1] 156 156 158 164 166 167 171 173 174 183
 rev(sort(round(rnorm(10, 170, 10))))
##  [1] 184 181 178 175 175 174 172 170 155 147
 set.seed(258) #Juhuarvugeneraatori algväärtus
 round(rnorm(10, 170, 10))
##  [1] 179 169 179 181 170 173 172 183 180 178
 round(rnorm(10, 170, 10))
##  [1] 162 172 171 148 166 170 158 152 170 179
 eesnimed=c("Juku" ,"Kati", "Mati", "Madis", "Mari", "Martin")
 sample(eesnimed, 3)
## [1] "Martin" "Mari"   "Juku"
 sample(eesnimed, 3)
## [1] "Mari" "Mati" "Kati"
 head(mtcars) #näitandmed 70ndate autodega
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
 mtcars[1:3, ] #Kolm esimest rida
##                mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4     21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710    22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
 mtcars[sample(1:nrow(mtcars), 5),] #5 juhuslikku autot, nrows- ridade arv
##                    mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Merc 450SLC       15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## Ford Pantera L    15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
## Toyota Corolla    33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## Chrysler Imperial 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
 #Harjutus - arvutage kümne juhusliku auto keskmine mpg (miles per gallon)
 mean(mtcars[sample(1:nrow(mtcars), 10),"mpg"])
## [1] 20.64
 #mtcars$mpg
 1:nrow(mtcars)
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30 31 32
 sample(5:32, 10) #esimest nelja ei vali
##  [1]  9  7 24 16 30  6 12 10 29 28
 #Viiel korral käivitatakse käsklust, kus valitakse kümne juhusliku auto keskmine
 sapply(1:5, function(i){ mean(mtcars[sample(1:nrow(mtcars), 10),"mpg"])})
## [1] 21.03 19.85 19.84 21.45 18.54
 hist(sapply(1:100, function(i){ mean(mtcars[sample(1:nrow(mtcars), 10),"mpg"])}))

 summary(sapply(1:100, function(i){ mean(mtcars[sample(1:nrow(mtcars), 5),"mpg"])}))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   14.68   18.86   20.06   19.99   21.24   26.06
 sample(eesnimed, 10, replace=TRUE) #Kordused lubatud
##  [1] "Mati"   "Mati"   "Madis"  "Juku"   "Madis"  "Juku"   "Juku"  
##  [8] "Martin" "Kati"   "Mari"
 #bussiooteaeg ühtlase jaotuse järgi 0-7 min
 runif(1, 0, 7) #üks väärtus random uniform
## [1] 5.42535
 runif(20, 0, 7)
##  [1] 5.77247319 2.58999152 3.32226904 1.96473961 1.40787691 4.17666601
##  [7] 0.06701862 5.62912596 2.80423478 6.32425679 5.70144005 4.04969707
## [13] 1.86213824 1.03944634 2.05853889 1.02489841 1.69111271 5.80053457
## [19] 6.91361361 4.49201933
 #Püüdke modelleerida bussiga kohale jõudmise aega:
 #ooteaeg 0-7 minutit ühtlase jaotuse järgi, 
 #Sõiduaeg normaaljaotusega keskväärtusega 20 minutit, standardhälve 3 minutit
 #Tekitage käsuga üks tulemus, korrake käsku, jälgige tulemusi
 runif(1, 0, 7)+rnorm(1, 20, 3)
## [1] 17.41337
 #Leidke, mitmel juhul sajast oli kohale jõudmise aeg üle 25 minuti
 sapply(1:100, function(i){runif(1, 0, 7)+rnorm(1, 20, 3)})
##   [1] 29.11322 21.71840 23.58097 22.28106 21.55456 29.75523 30.11284
##   [8] 25.97437 17.09062 21.58720 27.89258 21.43209 25.41527 23.74427
##  [15] 23.77225 21.69429 26.31299 17.92246 23.95913 16.93469 27.83307
##  [22] 27.20376 20.66867 25.91598 24.23704 19.07705 26.09275 26.32807
##  [29] 24.99013 18.36085 30.50155 25.70791 22.94316 28.39643 17.02537
##  [36] 23.19889 31.10508 22.74684 23.16333 23.34466 29.59527 26.95016
##  [43] 23.59573 25.47840 20.42723 26.41380 28.75433 27.31718 27.33992
##  [50] 18.74272 24.60766 25.18463 21.65846 19.98717 23.19538 25.04167
##  [57] 22.07836 23.99556 26.93769 16.51895 22.90385 26.42548 23.47269
##  [64] 13.44981 28.14308 23.05273 20.14640 20.24029 27.01863 23.32243
##  [71] 15.86142 24.20807 24.67220 20.39500 23.07053 24.87616 21.16198
##  [78] 25.37083 23.35999 24.35513 28.08235 25.32993 17.42529 21.65088
##  [85] 27.35891 27.30793 27.40201 24.25352 27.82874 24.35449 25.80345
##  [92] 24.13197 25.70148 21.69222 23.83253 22.42461 24.18690 29.16936
##  [99] 23.85800 26.43971
 sapply(1:100, function(i){runif(1, 0, 7)+rnorm(1, 20, 3)})>25
##   [1]  TRUE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE  TRUE  TRUE FALSE
##  [12] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE
##  [23] FALSE  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE
##  [34]  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE
##  [45]  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE
##  [56] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE
##  [67] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
##  [78]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
##  [89] FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE
## [100]  TRUE
 ajad=sapply(1:100, function(i){runif(1, 0, 7)+rnorm(1, 20, 3)})
 length(ajad[ajad>25])
## [1] 37
 #ajad[ajad>25]
 ajad=sapply(1:100, function(i){runif(1, 0, 7)+rnorm(1, 20, 3)})
 length(ajad[ajad>30]) #veidi pikem minekuaeg
## [1] 2
 #http://www.tlu.ee/~jaagup/andmed/muu/ounad/ounad3_100.txt
 ounad3_100=read.table("http://www.tlu.ee/~jaagup/andmed/muu/ounad/ounad3_100.txt", header=TRUE, sep=",")
 ounad2=edit(ounad3_100)
 antoonovkad=ounad3_100[ounad3_100$ounasort=="Antoonovka", "diameeter"]
 antoonovkad
##  [1]  7.57  6.06 10.63  5.72  7.47 10.16  7.73  7.75  4.76  6.90  8.48
## [12]  7.28  3.87  7.46  7.78  8.01  6.05  6.92  8.13  7.71  2.26  7.91
## [23]  6.29  7.21  8.21  5.35  6.72  9.51  5.16  6.14  3.61  3.87
 (antoonovkad=ounad3_100[ounad3_100$ounasort=="Antoonovka", "diameeter"])
##  [1]  7.57  6.06 10.63  5.72  7.47 10.16  7.73  7.75  4.76  6.90  8.48
## [12]  7.28  3.87  7.46  7.78  8.01  6.05  6.92  8.13  7.71  2.26  7.91
## [23]  6.29  7.21  8.21  5.35  6.72  9.51  5.16  6.14  3.61  3.87
 t.test(antoonovkad)
## 
##  One Sample t-test
## 
## data:  antoonovkad
## t = 20.722, df = 31, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  6.16115 7.50635
## sample estimates:
## mean of x 
##   6.83375
 t.test(antoonovkad, conf.level=0.9)
## 
##  One Sample t-test
## 
## data:  antoonovkad
## t = 20.722, df = 31, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 90 percent confidence interval:
##  6.274594 7.392906
## sample estimates:
## mean of x 
##   6.83375
 #Leidke Kuldrenettide keskmise diameetri usaldusvahemik 99% täpsusega
 t.test(antoonovkad, mu=7.5, alternative="less")
## 
##  One Sample t-test
## 
## data:  antoonovkad
## t = -2.0203, df = 31, p-value = 0.02603
## alternative hypothesis: true mean is less than 7.5
## 95 percent confidence interval:
##      -Inf 7.392906
## sample estimates:
## mean of x 
##   6.83375
 mean(antoonovkad)
## [1] 6.83375
 sd(antoonovkad)
## [1] 1.865543
 x=seq(3, 10, 0.1)
 y=dnorm(x, mean(antoonovkad), sd(antoonovkad))
 plot(x, y, type="l") # l nagu line
 qnorm(0.95, mean(antoonovkad), sd(antoonovkad))
## [1] 9.902295
 abline(v=qnorm(0.95, mean(antoonovkad), sd(antoonovkad)))

   #95% antoonovkate valimi jaotusega üldkogumist võiksid olla
   #väiksemad kui 9,9
 
 #Näidake diameeter, millest nende parameetritega normaaljaotuse puhul
 #on õuntest väiksemad 25%, tõmmake vastav joon
 kuldrenetid=ounad3_100[ounad3_100$ounasort=="Kuldrenett", "diameeter"]
 kuldrenetid
##  [1] 4.47 5.73 2.52 0.36 3.10 0.83 2.96 5.86 5.02 1.93 6.48 3.10 1.76 4.58
## [15] 1.96 3.43 6.66 8.75 2.55 2.73 5.80 4.96 5.74 2.18 2.09 2.41 5.10 0.87
## [29] 6.31 5.35 2.25 3.13 3.51 3.51 0.90 5.45
 t.test(antoonovkad, kuldrenetid)
## 
##  Welch Two Sample t-test
## 
## data:  antoonovkad and kuldrenetid
## t = 6.6421, df = 65.779, p-value = 7.069e-09
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  2.169562 4.034605
## sample estimates:
## mean of x mean of y 
##  6.833750  3.731667
 #95 percent confidence interval:
#   2.169562 4.034605
 attributes(t.test(antoonovkad, kuldrenetid))
## $names
## [1] "statistic"   "parameter"   "p.value"     "conf.int"    "estimate"   
## [6] "null.value"  "alternative" "method"      "data.name"  
## 
## $class
## [1] "htest"
 t.test(antoonovkad, kuldrenetid)[["conf.int"]]
## [1] 2.169562 4.034605
## attr(,"conf.level")
## [1] 0.95
 t.test(antoonovkad, kuldrenetid)[["conf.int"]][1]
## [1] 2.169562
 piirid=round(t.test(antoonovkad, kuldrenetid)[["conf.int"]], 2)
 paste('Antoonovkad on 95% tõenäosusega suuremad ', piirid[1],
         'kuni', piirid[2], "cm")
## [1] "Antoonovkad on 95% tõenäosusega suuremad  2.17 kuni 4.03 cm"
 #Kuvage Antoonovkate ja Liivi sibulõunte keskmise diameetri erinevus
 #välja 90% tõenäosusega