Running a model on multiple datasets error

@cluhmann Thanks! That’s spot on. When I break it out into individual containers, it works but only with predictors that are continuous. For instance this works

with pm.Model() as m_fake_multiple:   
      
    x_pred = pm.Data('x_pred', tmp_dfs[0]['x'])   
    y_obs = pm.Data('y_obs', tmp_dfs[0]['y'])   
    
    b = pm.Normal('b', mu = 0, sigma = 10, shape = 1)  
    
    ## linear predictor continuous var
    lp = b*x_pred 
    
    _ = pm.Normal('y', mu = lp, sigma = 1, observed = y_obs)

traces = []
for data_vals in tmp_dfs:
    with m_fake_multiple:
        # Switch out the observed dataset
        pm.set_data({"x_pred": data_vals['x']})
        pm.set_data({"y_obs": data_vals['y']})
        traces.append(pm.sample(return_inferencedata=False))

Having an index container doesn’t work

with pm.Model() as m_fake_multiple:   
      
    x_pred = pm.Data('x_pred', tmp_dfs[0]['x'])   
    y_obs = pm.Data('y_obs', tmp_dfs[0]['y'])   

    ### This is the categorical index with 4 levels 
    run_id = pm.Data('run_id', tmp_dfs[0]['id'])  
    
    a = pm.Normal('a', mu = 0, sigma = 10, shape = 4)  
    b = pm.Normal('b', mu = 0, sigma = 10, shape = 1)  
    
    ### linear predictor a function of both categorical index and continuous 
    lp = a[run_id] + b*x_pred  
    
    _ = pm.Normal('y', mu = lp, sigma = 1, observed = y_obs) 

Error.
index must be integers.

How do I convert the index to integer within pm.Data? I tried using astype(int) but that didn’t work either.

1 Like