Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plot media contribution over time on a out of sample dataset #828

Open
NMaidanenko opened this issue Jul 11, 2024 · 2 comments
Open

Plot media contribution over time on a out of sample dataset #828

NMaidanenko opened this issue Jul 11, 2024 · 2 comments
Labels

Comments

@NMaidanenko
Copy link

Hi all, is there any way to plot the media contribution plot over time on a out of sample dataset? Can anyone share their experience or suggest a solution ?

@wd60622
Copy link
Contributor

wd60622 commented Jul 11, 2024

Hi @NMaidanenko

Give this a try

import xarray as xr
import pandas as pd

from pymc_marketing.mmm import MMM


def new_data_media_contributions(X: pd.DataFrame, mmm: MMM) -> xr.DataArray:
    mmm._data_setter(X)
    with mmm.model:
        return pm.sample_posterior_predictive(
            mmm.idata,
            var_names=["channel_contributions"],
        ).posterior_predictive["channel_contributions"]

Be careful that the adstock effect from the last observations of the data set might want to be considered!

It is based off the sample_posterior_preditive method which does account the last observations of the fit data set:

if include_last_observations:
X_pred = pd.concat(
[self.X.iloc[-self.adstock.l_max :, :], X_pred], axis=0
).sort_values(by=self.date_column)
self._data_setter(X_pred)
with self.model: # sample with new input data
post_pred = pm.sample_posterior_predictive(
self.idata, **sample_posterior_predictive_kwargs
)
if extend_idata:
self.idata.extend(post_pred, join="right") # type: ignore

@wd60622 wd60622 added the MMM label Jul 11, 2024
@wd60622
Copy link
Contributor

wd60622 commented Jul 30, 2024

On second thought, I think this can be done with the current implementation

mmm: MMM | DelayedSaturatedMMM = ...
var_names = ["channel_contributions"]
_ = mmm.sample_posterior_predictive(X_pred=..., var_names=var_names, extend_idata=True)

mmm.idata.posterior_predictive[var_names]

Give that a try and let me know if that works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants