Search code examples
rsimulationnonlinear-functions

How do I simulate two variables in R which are non-linearly related?


I've been lurking on SO for a while, but finally have a question to which I couldn't find an answer on this site or anywhere else. So account created, and here goes. Apologies if this question has indeed been answered elsewhere. My searching skills need improving then!

In R, I would like to generate two variables which are non-linearly related.

I've figured out how to use the mvrnorm function in the library MASS to simulate a linear association between two variables as below, but would now like to simulate two variables which follow a non-linear association.

library ('MASS')

data = mvrnorm(n=100, mu=c(170, 80), Sigma=matrix(c(1, 0.85, 0.85, 1),   nrow=2), empirical=TRUE)
height = data[, 1]  # standard normal (mu=170, sd=1)
weight = data[, 2]  # standard normal (mu=80, sd=1)

I don't particularly mind what kind of non-linear association it is (e.g. exponential, logarithmic etc). I merely would like to generate some scatterplots that demonstrate the difference between a linear and a non-linear association.

I am no statistics nor an R expert, so seemingly overly simplistic answers would be greatly appreciated!

Many thanks for any help you can give.


Solution

  • a quick way is to create a non-linear variable, then add some noise to it:

    x = seq(-100, 100)    # just a sequence of numbers
    y = x^2 + rnorm(length(x), 0, 1000)      # generate non-linear association + noise
    plot(x, y)
    

    enter image description here

    If some function is not clear, please let me know.

    PS: No need to apologize. If nobody ever asked rookie questions, there would be no experts in the world.