12.yakir
12.yakir

Reputation: 323

How to generate random Y at specific X from a linear model in R?

Say we have a linear model f1 that was fit to some x and y data points:

f1 <- lm(y ~ x,data=d)

How can I generate new y values at new x values (that are different from the old x values but are within the range of the old x values) using this f1 fit in R?

Upvotes: 4

Views: 7173

Answers (3)

jan-glx
jan-glx

Reputation: 9536

stats:::simulate.lm allows you to sample from a linear model fitted with lm. (In contrast to the approach of @Bulat this uses unbiased estimates of the residual variance). To simulate at different values of the independent variable, you could hack around like this:

# simulate example data
x <- runif(20, 0, 100)
y <- 5*x + rnorm(20, 0, 10)
df <- data.frame(x, y)

# fit linear model
mod <- lm(y ~ x, data = df)

# new values of the independent variable
x_new <- 1:100

# replaces fitted values of the model object with predictions for new data,
mod$fitted.values <- predict(mod, data.frame(x=x_new)) # "hack" 

# simulate samples appropriate noise and adds it the models `fitted.values`
y_new <- simulate(mod)[, 1] # simulate can return multiple samples (as columns), we only need one

# visualize original data ...
plot(df)
# ... alongside simulated data at new values of the independent variable (x)
points(x_new, y_new, col="red")

enter image description here

(original data in black, simulated in red)

Upvotes: 5

Bulat
Bulat

Reputation: 6979

I am looking at the same problem.

In simple terms it can be done by using sample from residuals:

mod <- lm(y ~ x, data = df)

x_new <- c(5) # value that you need to simulate for.
pred <- predict(mod, newdata=data.frame(x = x_new))
err <- sample(mod$residuals, 1)
y <- pred + err

There is a simulate(fit, nsim = 10, XX = x_new) function, that is supposed to do it for you.

Upvotes: 1

EDi
EDi

Reputation: 13310

You can use predict for this:

x <- runif(20, 0, 100)
y <- 5*x + rnorm(20, 0, 10)
df <- data.frame(x, y)
df
plot(df)

mod <- lm(y ~ x, data = df)

x_new <- 1:100
pred <- predict(mod, newdata=data.frame(x = x_new))
plot(df)
points(x_new, pred)

Upvotes: 0

Related Questions