Replicate geom_smooth in plotly using gam. Can't get gam to match up

I am hoping to replicate a gam in geom_smooth but in plotly (not ggplotly).

I can't get the model to match up though. The gam using mgcv doesn't match up though. They seem to be standardised or something. What am I doing wrong? Is there an argument missing anywhere? I had a look at the documentation, but couldn't work it out. Can you help?

library(gapminder)
library(tidyverse)
library(plotly)

df2 <- gapminder %>% 
  filter(country == "Afghanistan")

ggplot(df2, aes(year, gdpPercap)) +
  geom_point() +
  geom_smooth(method = "gam", method.args = list(family = "quasipoisson"))

But the mgcv version seems to be standardised or something.

# test gam
gam2 <- mgcv::gam(gdpPercap ~ s(year), data = df2, family = "quasipoisson")
summary(gam2)
plot(gam2)

image

And then obviously the plotly version is wrong.

# test plotly
plot_ly(df2, x = ~year, y = ~gdpPercap, type = 'scatter', name = 'points') %>% 
  add_trace(y = predict(gam2), type = 'scatter', mode = 'lines')

I am hoping to use plotly rather than ggplotly (where it is fine) as I want to add a couple of other things later that can only be done in plotly.

When geom_smooth does its thing it writes notes to the console, in your case

`geom_smooth()` using formula 'y ~ s(x, bs = "cs")'

compare this to your formula for mgvc::gam, you use s() but the default bs, which is not 'cs'
therefore add that bs="cs" param to your s() and it should align.

in the console you can also do

?smooth.terms

to read about the different smoothing terms that bs choice represents

Thanks. I have tried using 'y ~ s(x, bs = "cs")' as suggested, but the gam values are still centred around 0, rather than around 800.

# test gam
gam2 <- mgcv::gam(gdpPercap ~ s(year, bs = "cs"), data = df2, family = "quasipoisson")
summary(gam2)
plot(gam2)

ah, the last part of the puzzle, is when you come to predict from the gam, to ask for the type="response"

add_trace(y = predict(gam2,type="response"), type

Great. Thanks for your help!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.