Write R code to generate data for a simple linear regression model in which the true slope is 0.5 and the true intercept is 2.
Click here for the answer
library(data.table)
library(ggplot2)
x <- rnorm(100, 0, 1)
y <- 2 + 0.5 * x + rnorm(100, 0, 1)
d <- data.table(x, y)
fm <- lm(y ~ x, data = d)
ggplot(d, aes(x = x, y = y)) +
geom_point() +
geom_smooth(method = "lm", se = T) +
labs(title = "Simple Linear Regression Model",
x = "x",
y = "y") +
theme_minimal()