Skip to content

GLMGam can't predict on new data #7830

Closed

Description

@AlejandroBaron

Trying to predict on new data using GLMGam, but it's basically impossible, not even with the training dataset

import statsmodels.api as sm

from statsmodels.gam.api import GLMGam, BSplines

# import data
from statsmodels.gam.tests.test_penalized import df_autos

# create spline basis for weight and hp
x_spline = df_autos[['weight', 'hp']]

bs = BSplines(x_spline, df=[12, 10], degree=[3, 3])

# penalization weight
alpha = np.array([21833888.8, 6460.38479])

gam_bs = GLMGam.from_formula('city_mpg ~ fuel + drive', data=df_autos,
                       smoother=bs, alpha=alpha)
 

res_bs = gam_bs.fit()

None of these will work (and at least one whould)

gam_bs.predict(df_autos)
gam_bs.predict(x_spline)
res_bs.predict(df_autos)
res_bs.predict(x_spline)
res_bs.predict(x_spline)

I finally tried with

res_bs.predict(bs.transform(x_spline.values))

And it takes AGES (i'm assuming it will finish, because 5 minutes in and it hasn't). It's just 200 samples

Activity

  1. josef-pkt commented on Oct 29, 2021

    @josef-pkt
    Member

    based on the docstring https://www.statsmodels.org/dev/generated/statsmodels.gam.generalized_additive_model.GLMGamResults.predict.html
    it should be
    res_bs.predict(df_autos[['fuel', 'drive']], exog_smooth=df_autos[['weight', 'hp']])
    or variations on it.
    I guess when using formulas, this should work also
    res_bs.predict(df_autos, exog_smooth=df_autos[['weight', 'hp']])

    Where df_autos can be replace with a dataframe with new, out of sample values

  2. modified the milestones: 0.13.2, 0.13.3 on Oct 30, 2022
  3. modified the milestones: 0.13.3, 0.14 on Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions