As per ex18q1 in "R for Data Science" I am trying to find the best model for the data:
sim1a <- tibble(
x = rep(1:10, each = 3),
y = x * 1.5 + 6 + rt(length(x), df = 2)
)
I've applied linear model and am trying to plot the results on a graph using ggplot:
sim1a_mod <- lm(x ~ y, data = sim1a)
ggplot(sim1a, aes(x, y)) +
geom_point(size = 2, colour= "gray") +
geom_abline(intercept = coef(sim1a_mod)[[1]], slope = coef(sim1a_mod)[[2]], colour = "red")
coef(sim1a_mod)[[1]]
prints -1.14403
coef(sim1a_mod)[[2]]
prints 0.4384473
I create the plot with the data points, but the model is not showing. What am I doing wrong?
+ geom_smooth(method = "lm")
instead ofgeom_abline
– Philsim1a_mod <- lm(y ~ x, data = sim1a)
– Phil