How to generate random Y at specific X from a linear model in R? -
say have linear model f1
fit x
, y
data points:
f1 <- lm(y ~ x,data=d)
how can generate new y
values @ new x
values (that different old x
values within range of old x
values) using f1
fit in r?
you can use predict
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)
Comments
Post a Comment