Categorical mediator and total effect estimation Let's say you have a linear variable ($X$) that has a causal effect on a final linear variable ($Y$) via a mediator which in turn is a categorical variable ($Z$).
$$ X \rightarrow Z \rightarrow Y $$
All these variables are observed.
These can be modeled like:
$$ \textbf p = \text{softmax}(\textbf a  + \textbf b X)  $$
$$ Z \sim \text{Categorical}(\textbf p) $$
The interpretation of $\textbf b$ is the effect of $X$ on $Z$ being a certain category.
The second part of the model is simpler, just a linear regression on a categorical variable:
$$ y = c_{Z} $$
$$ Y \sim \mathcal N (y, \sigma) $$
where $\textbf c$ is a vector with as many components as the categories of $Z$.
By regressing, we can get estimates on $\textbf b$ and $\textbf c$.
What is the total effect of $X$ on $Y$? Can it be expressed as a function of $\textbf b$ and $\textbf c$?

Your model is
$$E[Y \vert Z]=\Pr(Z=1 \vert X)\cdot \beta_1 + \cdots + \Pr(Z=k\vert X)\cdot \beta_k.$$
You want to know how that changes with $X$, so you can take the derivative:
$$\frac{\partial E[Y \vert Z]}{\partial X}=\frac{\Pr(Z=1 \vert X)}{\partial X}\cdot \beta_1 + \cdots + \frac{\Pr(Z=k\vert X)}{\partial X}\cdot \beta_k.$$
These derivatives are the marginal effects from the multinomial model.
You can calculate the overall effect by averaging this expression in your estimation sample.
