# ex1.R # Demonstrate simple statistics in R # to be used for copy-paste into browser at https://rdrr.io/snippets/ # 2019-01-06 17:12 (Gunnar Taraldsen) # 2019-01-13 16:16 (GT) ######## Coin tossing ########################### nP=0.5 # Coin tossing probability iN=10 # Number of trials iM=1 # Number of tosses in each trial aX = rbinom(iN, iM, nP) # experiment! aX summary(aX) # Show results hist(aX) ####### Dice library(purrr) iN=10 # Number of trials iM=6 # Number of faces aX = rdunif(iN, iM) # experiment! aX summary(aX) # Show results hist(aX) ######## Uniform ########################### iN=10 # Number of trials aX = runif(iN) # experiment! Use rnorm(iN, 0, 1) in Ex1 :-) aX summary(aX) # Show results hist(aX) print(sd(aX)) print(var(aX)) print(mean(aX)) print(mean((aX > 2/3))) print(quantile(aX, 0.9)) aSortX = sort(aX) iInd = round(iN*0.9) print(aSortX[iInd]) ######## Uniform and random variable ########################### iN=10000 # Number of trials aX = runif(iN) # experiment! Use rnorm(iN, 0, 1) in Ex1 :-) summary(aX) # Show results hist(aX) g = function(x) x^2 + exp(x) aY = g(aX) summary(aY) # Show results hist(aY) # EOF ex1.R