R: Simulation & Sampling
Code Only
|
Code with Rweb Output
|
### To replicate your random results we can use the set.seed function
set.seed(101) ### Just choose a random number ####################################################### ######## RANDOM SIMULATIONS - NORMAL DISTRIBUTION ###### ####################################################### ### Create 3 random numbers that are standard normal: X~N(0,1) rnorm(3) ### Create 3 random numbers that normal with a mean of 20 ### and a standard deviation of 7. In other words => X~N(20,7) rnorm(3, 20, 7) # rnorm(number of random numbers, mean, sd) ####################################################### ######## RANDOM SIMULATIONS - BINOMIAL DISTRIBUTION ###### ####################################################### ### rbinom(# of experiments, trials per experiment, probability of success) rbinom(10, 1, 0.5) ### Conduct 10 experiments of flipping coin once rbinom(1, 10, 0.5) ### Conduct 1 experiment of flipping coin 10 times rbinom(3, 100, .5) ### Conduct 3 experiments of flipping a coin 100 times ####################################################### ################## RANDOM SAMPLING #################### ####################################################### ### Roll a die 5 times sample(1:6, 5, replace=TRUE) ### Simulate flipping a coin 5 times sample(c("H","T"), 5, replace=TRUE) ### pick 5 numbers from a hat filled with 30 numbers sample(1:30, 5) # no replacement ### From TeachingDemos package library(TeachingDemos) ### must use in order to use dice function # 10 rolls of 4 fair dice dice(10,4) # or plot(dice(10,4)) library(mosaic) deal(cards, 13) deal(cards, 5) shuffle(cards) ### One must load the UsingR package to use the simple.sim function library(UsingR) ### Central Limit Theorem Demonstration f<-function() mean(runif(100, 10, 20)) sim <- simple.sim(1000,f) # create 100 random normal numbers length(sim) hist(sim) |
> ### To replicate your random results we can use the set.seed function
> set.seed(101) ### Just choose a random number > > ####################################################### > ######## RANDOM SIMULATIONS - NORMAL DISTRIBUTION ###### > ####################################################### > > ### Create 3 random numbers that are standard normal: X~N(0,1) > rnorm(3) [1] -0.3260365 0.5524619 -0.6749438 > > ### Create 3 random numbers that normal with a mean of 20 > ### and a standard deviation of 7. In other words => X~N(20,7) > rnorm(3, 20, 7) # rnorm(number of random numbers, mean, sd) [1] 21.50052 22.17538 28.21776 > > ####################################################### > ######## RANDOM SIMULATIONS - BINOMIAL DISTRIBUTION ###### > ####################################################### > > # rbinom(# of experiments, trials per experiment, probability of success) > rbinom(10, 1, 0.5) ### Conduct 10 experiments of flipping coin once [1] 1 1 0 1 1 0 0 0 1 1 > > rbinom(1, 10, 0.5) ### Conduct 1 experiment of flipping coin 10 times [1] 4 > > rbinom(3, 100, .5) # Conduct 3 experiments of flipping a coin 100 times [1] 56 54 54 > > ####################################################### > ################## RANDOM SAMPLING #################### > ####################################################### > > ### Roll a die 5 times > sample(1:6, 5, replace=TRUE) [1] 1 4 6 2 5 > ### Simulate flipping a coin 5 times > sample(c("H","T"), 5, replace=TRUE) [1] "H" "T" "H" "H" "T" > > ### pick 5 numbers from a hat filled with 30 numbers > sample(1:30, 5) # no replacement [1] 10 12 7 2 24 > > ### From TeachingDemos package > library(TeachingDemos) ### must use in order to use dice function > # 10 rolls of 4 fair dice > dice(10,4) |
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.