# Required libraries
library(ggplot2)
library(knitr)
library(DT)
# Create Uniform Random Variables between -5 & 5
unif.x <- runif(1000, -5, 5) # creates 1000 uniform random variables between -5 to 5
ggplot(data.frame(unif.x), aes(x=unif.x)) + geom_density() +
geom_vline(aes(xintercept=mean(unif.x)), color="blue", linetype="dashed", size=1) +
xlab("Uniform Random Variables") +
ggtitle("Uniform RV Density Plot")
# Create Normal Random Variables with mean zero and standard deviation 1
normal.x <- rnorm(1000) # creates 1000 standard normal random variables
ggplot(data.frame(normal.x), aes(x=normal.x)) + geom_density() +
geom_vline(aes(xintercept=mean(normal.x)), color="green", linetype="dashed", size=1) +
xlab("Normal Random Variables") +
ggtitle("Normal RV Density Plot")
# Create Exponential Random Variables with lambda = 1
exponential.x <- rexp(1000, 1) # creates 1000 exponential random variables
ggplot(data.frame(exponential.x), aes(x=exponential.x)) + geom_density() +
geom_vline(aes(xintercept=mean(exponential.x)), color="yellow", linetype="dashed", size=1) +
xlab("Exponential Random Variables") +
ggtitle("Exponential RV Density Plot")
# Create Poisson Random Variables with lambda = 1
poisson.x <- rpois(1000,2) # creates 1000 poisson random variables with lambda = 1
ggplot(data.frame(poisson.x), aes(x=poisson.x)) + geom_density() +
geom_vline(aes(xintercept=mean(poisson.x)), color="red", linetype="dashed", size=1) +
xlab("Poisson Random Variables") +
ggtitle("Poisson RV Density Plot")
d.rv<- round(data.frame(unif.x, normal.x, exponential.x, poisson.x), digits=3)
colnames(d.rv) <- c("Uniform RV", "Normal RV", "Exponential RV", "Poisson RV")
# Output Table
DT::datatable(d.rv, rownames=TRUE, options = list(autowidth=TRUE,sClass="alignRight", className = 'dt-center', pageLength=10, digit=3))
# Calculate Descriptive Statistics
unif.stat <- summary(unif.x)
normal.stat <- summary(normal.x)
exponential.stat <- summary(exponential.x)
poisson.stat <- summary(poisson.x)
# Combined statistics by RV into matrix
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")
# Output Table
#knitr::kable(descriptive.stats,pad=0, digits=3, format="markdown", title="Descriptive Statistics", #caption="Table1: Descriptive Statistics")
DT::datatable(descriptive.stats, rownames=c("Uniform RV", "Normal RV", "Exponential RV", "Poisson RV"), options = list(autowidth=TRUE,sClass="alignRight", className = 'dt-center', dom = 'tip'))
variable.values <- round(cbind(c(unif.x, normal.x, exponential.x, poisson.x)),3) # lists values in one column with dimensions [4000,1]
variable.names <- c( rep("Uniform", 1000), rep("Normal", 1000), rep("Exponential", 1000), rep("Poisson", 1000)) #creates vector for variable names
mydata <- data.frame(variable.values, variable.names) #creates dataframe
ggplot(mydata, aes(x=variable.names, y=variable.values) ) +
geom_boxplot(fill=c("yellow","green","red","blue")) +
xlab("Distribution") +
ylab("Random Variable Values") +
ggtitle("BoxPlot Comparisons")
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)
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.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)
x <- normal.x
density.x <- curve(dnorm(x,0,1), col="purple", type="l", lwd=2, add=TRUE)
Normal Density Function: \[f(x, \mu, \sigma) = \frac{1}{\sigma\sqrt{2\pi}}e^{-\frac{(x-\mu)^2}{2\sigma^2}}\]