|
368 | 368 | "$$"
|
369 | 369 | ]
|
370 | 370 | },
|
| 371 | + { |
| 372 | + "cell_type": "markdown", |
| 373 | + "metadata": {}, |
| 374 | + "source": [ |
| 375 | + ":::{info}\n", |
| 376 | + "We can also express Model 1 in Wilkinson notation as `y ~ 1 + x` which is equivalent to `y ~ x` as the intercept is included by default.\n", |
| 377 | + "\n", |
| 378 | + "* The `1` term corresponds to the intercept term $\\beta_0$.\n", |
| 379 | + "* The `x` term corresponds to the slope term $\\beta_1$.\n", |
| 380 | + ":::" |
| 381 | + ] |
| 382 | + }, |
371 | 383 | {
|
372 | 384 | "cell_type": "markdown",
|
373 | 385 | "metadata": {},
|
|
918 | 930 | "Where $g_i$ is the group index for observation $i$. So the parameters $\\beta_0$ and $\\beta_1$ are now length $g$ vectors, not scalars. And the $[g_i]$ acts as an index to look up the group for the $i^\\text{th}$ observation."
|
919 | 931 | ]
|
920 | 932 | },
|
| 933 | + { |
| 934 | + "cell_type": "markdown", |
| 935 | + "metadata": {}, |
| 936 | + "source": [ |
| 937 | + ":::{info}\n", |
| 938 | + "We can also express this Model 2 in Wilkinson notation as `y ~ g + x:g`.\n", |
| 939 | + "\n", |
| 940 | + "* The `g` term captures the group specific intercept $\\beta_0[g_i]$ parameters.\n", |
| 941 | + "* The `x:g` term captures group specific slope $\\beta_1[g_i]$ parameters.\n", |
| 942 | + ":::" |
| 943 | + ] |
| 944 | + }, |
921 | 945 | {
|
922 | 946 | "cell_type": "markdown",
|
923 | 947 | "metadata": {},
|
|
1423 | 1447 | "This model could also be called a partial pooling model. "
|
1424 | 1448 | ]
|
1425 | 1449 | },
|
| 1450 | + { |
| 1451 | + "cell_type": "markdown", |
| 1452 | + "metadata": {}, |
| 1453 | + "source": [ |
| 1454 | + ":::{info}\n", |
| 1455 | + "We can also express this Model 3 in Wilkinson notation as `1 + x + (1 + x | g)`.\n", |
| 1456 | + "\n", |
| 1457 | + "* The `1` captures the global intercept, $\\mathrm{Normal}(p_{0\\mu}, p_{0\\sigma})$.\n", |
| 1458 | + "* The `x` captures the global slope, $\\mathrm{Normal}(p_{1\\mu}, p_{1\\sigma})$.\n", |
| 1459 | + "* The `(1 + x | g)` term captures group specific random effects for the intercept and slope.\n", |
| 1460 | + " * `1 | g` captures the group specific intercept $\\vec{\\beta_0}[g_i]$ parameters.\n", |
| 1461 | + " * `x | g` captures the group specific slope $\\vec{\\beta_1}[g_i]$ parameters.\n", |
| 1462 | + ":::" |
| 1463 | + ] |
| 1464 | + }, |
1426 | 1465 | {
|
1427 | 1466 | "cell_type": "markdown",
|
1428 | 1467 | "metadata": {},
|
|
1478 | 1517 | " pm.Normal(\"y\", mu=μ, sigma=sigma, observed=data.y, dims=\"obs_id\")"
|
1479 | 1518 | ]
|
1480 | 1519 | },
|
| 1520 | + { |
| 1521 | + "cell_type": "code", |
| 1522 | + "execution_count": null, |
| 1523 | + "metadata": {}, |
| 1524 | + "outputs": [], |
| 1525 | + "source": [ |
| 1526 | + "with pm.Model(coords=coords) as model3:\n", |
| 1527 | + " # Define priors\n", |
| 1528 | + " intercept_mu = pm.Normal(\"intercept_mu\", 0, 1)\n", |
| 1529 | + " slope_mu = pm.Normal(\"slope_mu\", 0, 1)\n", |
| 1530 | + " intercept_sigma = pm.Gamma(\"intercept_sigma\", 2, 2)\n", |
| 1531 | + " slope_sigma = pm.Gamma(\"slope_sigma\", 2, 2)\n", |
| 1532 | + " sigma = pm.Gamma(\"sigma\", 2, 2)\n", |
| 1533 | + " β0 = pm.Normal(\"β0\", intercept_mu, intercept_sigma, dims=\"group\")\n", |
| 1534 | + " β1 = pm.Normal(\"β1\", slope_mu, slope_sigma, dims=\"group\")\n", |
| 1535 | + " # Data\n", |
| 1536 | + " x = pm.Data(\"x\", data.x, dims=\"obs_id\")\n", |
| 1537 | + " g = pm.Data(\"g\", data.group_idx, dims=\"obs_id\")\n", |
| 1538 | + " # Linear model\n", |
| 1539 | + " μ = pm.Deterministic(\"μ\", β0[g] + β1[g] * x, dims=\"obs_id\")\n", |
| 1540 | + " # Define likelihood\n", |
| 1541 | + " pm.Normal(\"y\", mu=μ, sigma=sigma, observed=data.y, dims=\"obs_id\")" |
| 1542 | + ] |
| 1543 | + }, |
1481 | 1544 | {
|
1482 | 1545 | "cell_type": "markdown",
|
1483 | 1546 | "metadata": {},
|
|
0 commit comments