> For the complete documentation index, see [llms.txt](https://ondrej-kvasnovsky-2.gitbook.io/handbook-of-hidden-data-scientist/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ondrej-kvasnovsky-2.gitbook.io/handbook-of-hidden-data-scientist/r/simulate_linear_model.md).

# Simulate Linear Model

Here is an example how to simplate data with linear relationship.

```r
set.seed(10)

x <- rnorm(100)
e <- rnorm(100, 0, 2)
y <- 0.5 + 2 * x +e
summary(y)
plot(x, y)
```

What if `x` is binary?

```r
set.seed(10)

x <- rbinom(100, 1, 0.5)
e <- rnorm(100, 0, 2)
y <- 0.5 + 2 * x +e
summary(y)
plot(x, y)
```

## Possion Distribution

```r
set.seed(10)

x <- rnorm(100)
log.mu <- 0.5 + 0.3 * x
y <- rpois(100, exp(log.mu))
summary(y)
plot(x, y)
```

> More about [Poisson distribution](http://en.wikipedia.org/wiki/Poisson_distribution).
