Simulate Random Variables
# Create Uniform Random Variables between -5 & 5
unif.x <- runif(500, -5, 5) # creates 500 uniform random variables between -5 to 5
unif.x[1:20] # first 20 obervations
## [1] -3.8485915 -4.0878805 4.6864432 0.8230546 -1.3364967 2.6298313
## [7] -4.1253368 -4.8892864 3.7691680 -3.3684331 1.8058893 3.9732274
## [13] 4.7195032 4.4676963 -2.8430584 -1.5271761 3.6011320 1.0403397
## [19] -1.1955698 -4.0793886
# Create Normal Random Variables with mean zero and standard deviation 1
normal.x <- rnorm(500) # creates 500 standard normal random variables
normal.x[1:20] # first 20 obervations
## [1] -0.37008774 -1.34395499 -0.51926407 0.86700375 0.01882165
## [6] 0.49836381 0.34364823 1.34610195 -2.22414889 1.77235610
## [11] -0.05511051 -0.30531144 -1.14322571 0.08494618 0.77034027
## [16] 2.17717146 -0.66648568 -0.03090895 -1.48435688 0.47065628
# Create Exponential Random Variables with lambda = 1
exponential.x <- rexp(500, 1)
exponential.x[1:20] # first 20 obervations
## [1] 3.87367772 0.85531322 0.16389799 1.13415106 0.29007801 3.03823830
## [7] 2.56686094 0.19006729 0.05060175 0.34559948 1.55218133 0.69820813
## [13] 1.16888428 0.30764358 1.17386457 0.80644489 1.13929865 0.06289959
## [19] 0.56891532 0.64548060
# Create Poisson Random Variables with lambda = 1
poisson.x <- rpois(500,2) # creates 500 poisson random variables with lambda = 1
poisson.x[1:20] # first 20 obervations
## [1] 3 2 3 1 1 3 3 2 2 4 3 1 1 1 1 3 4 1 1 2
Descriptive Statistics and Summaries for each Random Variable
unif.stat <- summary(unif.x)
normal.stat <- summary(normal.x)
exponential.stat <- summary(exponential.x)
poisson.stat <- summary(poisson.x)
descriptive.stats <- matrix(c(unif.stat, normal.stat, exponential.stat, poisson.stat),nrow=4)
colnames(descriptive.stats) = c("Min", "Q1", "Median", "Mean", "Q3", "Max")
rownames(descriptive.stats) <- c("Uniform RV", "Normal RV", "Exponential RV", "Poisson RV")
# Further table customization via htmlTable (add captions, format decimal places)
library(htmlTable)
Summaries <- htmlTable(formatC(descriptive.stats,digits=2, format="f") , border=1, align = "center", pos.caption = "bottom", caption = "Table1: Descriptive Statistics", col.rgroup = c("none", "#F7F7F7"), )
Summaries
|
|
Min
|
Q1
|
Median
|
Mean
|
Q3
|
Max
|
|
Uniform RV
|
-4.99
|
2.49
|
-0.10
|
0.00
|
1.32
|
2.00
|
|
Normal RV
|
-2.81
|
4.99
|
-0.07
|
0.29
|
5.73
|
2.08
|
|
Exponential RV
|
0.10
|
-3.12
|
0.57
|
0.67
|
0.00
|
3.00
|
|
Poisson RV
|
-0.05
|
-0.66
|
2.30
|
0.96
|
1.00
|
8.00
|
|
Table1: Descriptive Statistics
|
Construct Histograms for each Random Variable
par(mfrow=c(2,2))
hist.unif <- hist(unif.x, main = "Uniform RV [-5, 5]", col="blue", xlim = c(-5,5), ylim = c(0,0.2), prob=TRUE, xaxt="n", yaxt="n")
axis(1, at = seq(-5, 5, by = 1), las=2)
axis(2, at = seq(0, 0.2, by = 0.05), las=2)
hist.normal <- hist(normal.x, main= "Standard Normal RV (" ~mu~"=0, " ~sigma~"=1)", col="green", xlim=c(-4,4), ylim = c(0, 0.5), prob=TRUE, xaxt="n", yaxt="n")
axis(1, at = seq(-4, 4, by = 1), las=2)
axis(2, at = seq(0, 0.5, by = 0.1), las=2)
hist.Exponential <- hist(exponential.x, main= "Exponential RV ("~lambda~"=1)" , col="yellow", xlim=c(0,6), ylim = c(0, 1), prob=TRUE, xaxt="n", yaxt="n")
axis(1, at = seq(0, 6, by = 1), las=2)
axis(2, at = seq(0, 1, by = 0.2), las=2)
hist.Poisson <- hist(poisson.x, main= "Poisson RV (" ~mu~ "=2)", col="red", xlim=c(0,6), ylim = c(0, 0.5), prob=TRUE, xaxt="n", yaxt="n")
axis(1, at = seq(0, 6, by = 1), las=2)
axis(2, at = seq(0, 0.5, by = 0.1), las=2)

Plot Normal Histogram overlayed with Density Curve
par(mfrow=c(1,1))
hist.normal <- hist(normal.x, main= "Histogram with Density Curve", col="green", xlim=c(-4,4), ylim = c(0, 0.4), prob=TRUE, xaxt="n", yaxt="n")
axis(1, at = seq(-4, 4, by = 1), las=2)
axis(2, at = seq(0, 0.4, by = 0.1), las=2)
x <- normal.x
density.x <- curve(dnorm(x,0,1), col="purple", type="l", lwd=2, add=TRUE)
