0

I understand geom_smooth() allows you to incorporate model (such as a linear regression) directly onto plot.

But how do we go about doing this with more advanced models (kNN regression, elastic net, etc.)

Do I incorporate tidy() with the fit and then somehow add it as an extra layer on the scatterplot via geom_line() ?

At the moment, I can see how to do EITHER the scatter plot or the model fit (as far as very simple techniques), but not both at the same time.

As a long workaround, I can generate predicted y's, and put them in the data set, and THEN plot the model line onto the scatter plot through an aes(x = x, y = y_predicted) in the geom_line().

Is there a more elegant way of doing this?

DDD
  • 31
  • 1
  • 1
    https://stackoverflow.com/questions/15633714/adding-a-regression-line-on-a-ggplot This might help you. – itsDV7 Jan 16 '21 at 17:33
  • Thanks - unfortunately it seems that creating new values within the dataframe or data table is the only way. This is ok, but it would have been great if some other shortcut existed – DDD Jan 17 '21 at 19:50

0 Answers0