Simulating data

Functions that simulate data from a given distribution invariably follow a specific naming convention. For example, rpois can be used to simulate from a Poisson distribution, rbinom can be used to simulate from a binomial distribution, and rnorm can be used to simulate from a Normal distribution:

sim <- rnorm(10, 0, 1)

To sample from a Multivariate Normal distribution, we can use the mvrnorm function from the MASS package:

library(MASS)

mu <- c(7, 10)
sig <- matrix(c(2, 1, 1, 4), nrow = 2, ncol = 2)

set.seed(123)
multisim <- mvrnorm(100, mu, sig)

Plotting data

Scatter plot

Create a scatter plot of the simulated data:

plot(multisim, col = "blue", pch = 19, main = "Scatter plot of simulated data")

To produce a graph in 2x2 array format, use the par function: par(mfrow = c(2, 2))

Histogram

Create a histogram of the first column of the simulated data:

hist(multisim[, 1], main = "Histogram of first column")

hist(multisim[, 2], freq = FALSE, breaks = 10)
curve(dnorm(x, mean = 10, sd = 2), add = TRUE)
curve(dnorm(x, mean = mean(multisim[, 2]), sd = sd(multisim[, 2])), add = TRUE, lty = 2, col = 2)

Boxplot

Create a boxplot of the simulated data:

boxplot(as.data.frame(multisim), notch = TRUE)