How to plot a subset of mulitple parameters of a variable

I have the follwowing multiple regression, where y is of dimension Nx1 and X is of dimension NxK, where K=26.

with pm.Model() as model:

    K_1 = pm.Normal(name="K_1", mu=0.0, sigma=1.0, shape=X.shape[1])

    # Model error
    eps_1 = pm.Gamma(name="eps_1", alpha=9.0, beta=4.0, shape=())

    # Model mean
    y_hat = pm.Deterministic(name="y_hat", var=pm.math.dot(X_1, K_1) )

    # Likelihood
    y_like = pm.Normal(name="y_like", mu=y_hat, sigma=eps_1, observed=y)

with model:
    trace = pm.sample()

Obviously, I obtain the parameter K_1 which is of dimension 26x1, i.e. K_1[0], K_1[1], ..., K_1[25] .

Now, I would like to plot only the traces of the 7th to 10th parameter, i.e. K_1[7], K_1[8],K_1[9], K_1[10] .

I’ve tried,
az.plot_posterior(trace, var_names=[“K_1[7]”, “K_1[8]”, “K_1[9]”, “K_1[10]”])

However, this does not work.

You can do something along these lines:

az.plot_trace(trace, coords={"K_1_dim_0": [7, 8, 9, 10]})

But I would check out Oriol’s blog post about xarray coords and dims for a more robust approach to handling arrays of parameters.

1 Like

Oh, and you can still toss in var_names if you want to narrow the number of parameters plotted:

az.plot_trace(trace,
              var_names = ['K_1'],
              coords={"K_1_dim_0": [7, 8, 9, 10]})
1 Like