A simple simulation of linear, binomial and Poisson models.

Noah Mutai

This is a very basic simulation of a linear, binomial and Poisson models using R.

Linear model

set.seed(123)
n <- 1000
x <- rnorm(n)
y <- rnorm(n,0,2)
e <- rnorm(n, 0, 2)
y <- 0.5 + 2 * x + e   
summary(y)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## -8.6228 -1.3096  0.4178  0.4920  2.2844  9.5968
hist(y)

plot(x,y)

Binomial model

set.seed(100)
x <- rbinom(n, 1, 0.5)
e <- rnorm(n, 0, 3)
y <- 0.5 + 2 * x + e
hist(y)

plot(x, y)

Poisson model

set.seed(123)
x <- rnorm(n)
log.mu <- 0.6 + 0.4 * x
y <- rpois(n, exp(log.mu))
summary(y)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   1.000   2.000   1.986   3.000  12.000
plot(x,y)

hist(y)