REVISED: Saturday, March 2, 2013
R "Random Number Simulation".
I. GENERATING RANDOM NUMBERS
Probability distribution functions have four functions associated with them. The functions are prefixed with a:
A. d DENSITY
1. dbeta( )
2. dbinom( )
3. dcauchy( )
4. dchisq( )
5. dexp( )
6. df( )
7. dgamma( )
8. dgeom( )
9. dhyper( )
10. dlogis( )
11. dlnorm( )
12. dnbinom( )
13. dnorm( )
A. d DENSITY
1. dbeta( )
2. dbinom( )
3. dcauchy( )
4. dchisq( )
5. dexp( )
6. df( )
7. dgamma( )
8. dgeom( )
9. dhyper( )
10. dlogis( )
11. dlnorm( )
12. dnbinom( )
13. dnorm( )
dnorm(x, mean = 0, sd = 1, log = FALSE)
The dnorm( ) function evaluates the Normal probability density with a given mean or standard deviation at a point or vector of points.
14. dpois( )
15. dt( )
16. dunif( )
17. dweibull( )
15. dt( )
16. dunif( )
17. dweibull( )
B. p for cumulative distribution.
pnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
The pnorm( ) function evaluates the cumulative distribution function for a Normal distribution.
C. q for quantile function.
qnorm(n, mean = 0, sd = 1)
A quantile is each of any set of values of a variate which divide a frequency distribution into equal groups, each containing the same fraction of the total population.
1. rbeta( )
2. rbinon( )
3. rcauchy( )
4. rchisq( )
5. rexp( )
6. rf( )
7. rgamma( )
8. rgeom( )
9. rhyper( )
10. rlogis( )
11. rlnorm( )
12. rnbinom( )
13. rnorm( )
rnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
14. rpois( )
The rpois( ) function generates random Poisson variants with a given rate.
16. runif( )
The runif( ) uniform function.
II. User Defined Random Numbers
myFunction <- function(n) { # Input will be n.
for(i in 1:n) {
randomNumber <- runif(1, 0, 1) # For 1 number between 0 and 1.
print(randomNumber)
}
}
> myFunction(10) # 10 is input value for n.
[1] 0.7833938
[1] 0.1486565
[1] 0.6070959
[1] 0.3871606
[1] 0.8954091
[1] 0.1125646
[1] 0.5442749
[1] 0.7796356
[1] 0.8579459
[1] 0.9691205
>
III. RANDOM SAMPLING
The sample( ) function selects randomly from a set of scalar objects allowing you to sample from arbitrary distributions.
The set.seed( ) function sets the seed when conducting a simulation. This allows you to reproduce a random number selection.
The following are examples of R random sampling:
>set.seed(1)
>sample(1:10, 4) # Sampling without replacement.
[1] 3 4 5 7
>sample(1:10, 4) # Sampling without replacement.
[1] 3 9 8 5
>sample(letters, 5) # Sampling without replacement.
[1] "q" "b" "e" "x" "p"
>sample(1:10) # Permutation.
[1] 4 7 10 6 9 2 8 3 1 5
>sample(1:10) # Permutation.
[1] 2 3 4 1 9 5 10 8 6 7
>sample(1:10, replace = TRUE) # Sampling with replacement.
[1] 2 9 7 8 2 8 5 9 7 8
myFunction <- function(n) { # Input will be n.
for(i in 1:n) {
randomNumber <- runif(1, 0, 1) # For 1 number between 0 and 1.
print(randomNumber)
}
}
> myFunction(10) # 10 is input value for n.
[1] 0.7833938
[1] 0.1486565
[1] 0.6070959
[1] 0.3871606
[1] 0.8954091
[1] 0.1125646
[1] 0.5442749
[1] 0.7796356
[1] 0.8579459
[1] 0.9691205
>
III. RANDOM SAMPLING
The sample( ) function selects randomly from a set of scalar objects allowing you to sample from arbitrary distributions.
The set.seed( ) function sets the seed when conducting a simulation. This allows you to reproduce a random number selection.
The following are examples of R random sampling:
>set.seed(1)
>sample(1:10, 4) # Sampling without replacement.
[1] 3 4 5 7
>sample(1:10, 4) # Sampling without replacement.
[1] 3 9 8 5
>sample(letters, 5) # Sampling without replacement.
[1] "q" "b" "e" "x" "p"
>sample(1:10) # Permutation.
[1] 4 7 10 6 9 2 8 3 1 5
>sample(1:10) # Permutation.
[1] 2 3 4 1 9 5 10 8 6 7
>sample(1:10, replace = TRUE) # Sampling with replacement.
[1] 2 9 7 8 2 8 5 9 7 8
Enjoy R "Random Number Simulation".
Elcric Otto Circle
-->
-->
-->
How to Link to My Home Page
It will appear on your website as:"Link to: ELCRIC OTTO CIRCLE's Home Page"
No comments:
Post a Comment