R Lab 4 (I): 2023-05-08

How to simulate and plot a univariate and a multivariate normal distributions?

Simulate and plot a univariate normal distribution

dnorm() calculates the density function of a normal distribution.

# Create a sequence of values between -10 and 10 with a difference of 0.1
x <- seq(-10, 10, by=0.1)

y <- dnorm(x, mean(x), sd(x))
plot(x, y, type="l")
abline(v=mean(x), col="red")
plot of chunk unnamed-chunk-1

pnorm() calculates the cumulative distribution function.

x <- seq(-10, 10, by=0.1)
y <- pnorm(x, mean(x), sd(x))
plot(x, y, type="l")
plot of chunk unnamed-chunk-2

qnorm() is the inverse of pnorm(). It takes the probability value and gives output which corresponds to the probability value. It is useful in finding the percentiles of a normal distribution.

x <- seq(0, 1, by=0.02)
y <- qnorm(x, mean(x), sd(x))
plot(x, y, type="l")
plot of chunk unnamed-chunk-3

rnorm() is used to generate a vector of random numbers which are normally distributed.

# Create a vector of 1000 random numbers with mean=10 and sd=5
set.seed(1)
x <- rnorm(1000, mean=10, sd=5)

# Create a histogram with probability=TRUE in order to add a normal curve or a density line 
hist(x, probability=TRUE)
plot of chunk unnamed-chunk-4

Plot a histogram with a normal curve

# X-axis grid
x2 <- seq(min(x), max(x), length=50)

fun <- dnorm(x2, mean(x), sd(x))
hist(x, probability=TRUE, ylim = c(0, max(fun)), main="Histogram with normal curve")
lines(x2, fun, col="red", lwd=1.5)
plot of chunk unnamed-chunk-5

Plot a histogram with a density line

hist(x, probability=TRUE, ylim = c(0, max(fun)), main="Histogram with density curve")
lines(density(x), col="red", lwd=1.5)
plot of chunk unnamed-chunk-6

Simulate and plot a bivariate normal distribution

The easiest way to simulate a bivariate normal distribution in R is to use the mvrnorm() function from the MASS package.

library(MASS)

set.seed(1)
bidata <- as.data.frame(mvrnorm(n=100, mu=c(0, 0), Sigma=matrix(c(2, -1, -1, 1), ncol=2)))
head(bidata)
          V1         V2
1  1.0638091 -0.2067474
2 -0.2664476  0.1340747
3  1.4461205 -0.2319283
4 -2.2470623  1.2739463
5 -0.2408411  0.6244316
6  0.5550514 -1.6270500

Plot a bivariate normal distribution

The easiest way to plot a bivariate normal distribution in R is to use functions from the mnormt() package. To create a contour plot, which offers a 2-D visualization of a bivariate normal distribution, we use the contour() function.

library(mnormt)

set.seed(1)

# Create a bivariate normal distribution
x <- seq(-3, 3, 0.1)
y <- seq(-3, 3, 0.1)
mu <- c(0, 0)
sigma <- matrix(c(2, -1, -1, 2), nrow=2)
f <- function(x, y) dmnorm(cbind(x, y), mu, sigma)
z <- outer(x, y, f)

# Create the contour plot
contour(x, y, z)
plot of chunk unnamed-chunk-8

We also use the persp() function to create a surface plot, which offers a 3-D visualization of a bivariate normal distribution.

persp(x, y, z, theta=-30, phi=20, col="blue", expand=0.7, ticktype="detailed")
plot of chunk unnamed-chunk-9
# theta, phi: Defines the angles of the viewing direction
# expand: Controls the size of the z-axis
# ticktype: Controls the appearance of the ticks on the axes