class: front .pull-left-wide[ # Modelos multinivel] .pull-right-narrow[![:scale 85%](img/logo.png)] ## Unidades en contexto ---- .pull-left[ ## Juan Carlos Castillo ## Sociología FACSO - UChile ## 2do Sem 2023 ## [.yellow[multinivel-facso.netlify.com]](https://multinivel-facso.netlify.com) ] .pull-right-narrow[ .center[ .content-block-gray[ ## Sesión 8: ## **.yellow[Interacciones entre niveles]**] ] ] --- layout: true class: animated, fadeIn --- class: roja right # Contenidos ## .yellow[1- Resumen sesión anterior] ## 2- Interacción entre niveles --- ## Ej.Estimación de intercepto aleatorio (medias posteriores) .small[ .pull-left[ ```r library(lme4) mlm = read_dta("http://www.stata-press.com/data/mlmus3/hsb.dta") results_0 <-lmer(mathach ~ 1 + (1 | schoolid), data=mlm) ``` ] .pull-right[ ```r sjPlot::tab_model(results_0) ``` <table style="border-collapse:collapse; border:none;"> <tr> <th style="border-top: double; text-align:center; font-style:normal; font-weight:bold; padding:0.2cm; text-align:left; "> </th> <th colspan="3" style="border-top: double; text-align:center; font-style:normal; font-weight:bold; padding:0.2cm; ">mathach</th> </tr> <tr> <td style=" text-align:center; border-bottom:1px solid; font-style:italic; font-weight:normal; text-align:left; ">Predictors</td> <td style=" text-align:center; border-bottom:1px solid; font-style:italic; font-weight:normal; ">Estimates</td> <td style=" text-align:center; border-bottom:1px solid; font-style:italic; font-weight:normal; ">CI</td> <td style=" text-align:center; border-bottom:1px solid; font-style:italic; font-weight:normal; ">p</td> </tr> <tr> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; ">(Intercept)</td> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">12.64</td> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; ">12.16 – 13.12</td> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:center; "><strong><0.001</strong></td> </tr> <tr> <td colspan="4" style="font-weight:bold; text-align:left; padding-top:.8em;">Random Effects</td> </tr> <tr> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; padding-top:0.1cm; padding-bottom:0.1cm;">σ<sup>2</sup></td> <td style=" padding:0.2cm; text-align:left; vertical-align:top; padding-top:0.1cm; padding-bottom:0.1cm; text-align:left;" colspan="3">39.15</td> </tr> <tr> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; padding-top:0.1cm; padding-bottom:0.1cm;">τ<sub>00</sub> <sub>schoolid</sub></td> <td style=" padding:0.2cm; text-align:left; vertical-align:top; padding-top:0.1cm; padding-bottom:0.1cm; text-align:left;" colspan="3">8.61</td> <tr> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; padding-top:0.1cm; padding-bottom:0.1cm;">ICC</td> <td style=" padding:0.2cm; text-align:left; vertical-align:top; padding-top:0.1cm; padding-bottom:0.1cm; text-align:left;" colspan="3">0.18</td> <tr> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; padding-top:0.1cm; padding-bottom:0.1cm;">N <sub>schoolid</sub></td> <td style=" padding:0.2cm; text-align:left; vertical-align:top; padding-top:0.1cm; padding-bottom:0.1cm; text-align:left;" colspan="3">160</td> <tr> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; padding-top:0.1cm; padding-bottom:0.1cm; border-top:1px solid;">Observations</td> <td style=" padding:0.2cm; text-align:left; vertical-align:top; padding-top:0.1cm; padding-bottom:0.1cm; text-align:left; border-top:1px solid;" colspan="3">7185</td> </tr> <tr> <td style=" padding:0.2cm; text-align:left; vertical-align:top; text-align:left; padding-top:0.1cm; padding-bottom:0.1cm;">Marginal R<sup>2</sup> / Conditional R<sup>2</sup></td> <td style=" padding:0.2cm; text-align:left; vertical-align:top; padding-top:0.1cm; padding-bottom:0.1cm; text-align:left;" colspan="3">0.000 / 0.180</td> </tr> </table> ] ] --- .medium[ .pull-left[ ```r coef(results_0) ``` ``` ## $schoolid ## (Intercept) ## 1224 9.973039 ## 1288 13.376384 ## 1296 8.068508 ## 1308 15.585491 ## 1317 13.130920 ## 1358 11.394462 ## 1374 10.134624 ## 1433 18.905217 ## 1436 17.599082 ## 1461 16.333549 ## 1462 10.653692 ## 1477 14.119776 ## 1499 8.053397 ## 1637 7.832769 ## 1906 15.718897 ## 1909 14.173866 ## 1942 17.369276 ## 1946 12.880104 ## 2030 12.127460 ## 2208 15.209788 ## 2277 9.529151 ## 2305 11.232995 ## 2336 16.175537 ## 2458 13.886090 ## 2467 10.347606 ## 2526 16.726902 ## 2626 13.315460 ## 2629 14.740087 ## 2639 7.203427 ## 2651 11.250174 ## 2655 12.368626 ## 2658 13.326516 ## 2755 16.137977 ## 2768 11.156122 ## 2771 11.904624 ## 2818 13.752161 ## 2917 8.424206 ## 2990 17.945314 ## 2995 9.824023 ## 3013 12.612895 ## 3020 14.269518 ## 3039 16.194052 ## 3088 9.510212 ## 3152 13.163059 ## 3332 14.102843 ## 3351 11.587478 ## 3377 9.503202 ## 3427 19.114780 ## 3498 16.094014 ## 3499 13.208208 ## 3533 10.601741 ## 3610 15.174743 ## 3657 9.776114 ## 3688 14.463236 ## 3705 10.543152 ## 3716 10.595004 ## 3838 15.796873 ## 3881 12.017848 ## 3967 12.083454 ## 3992 14.486603 ## 3999 11.096263 ## 4042 14.204136 ## 4173 12.716450 ## 4223 14.440479 ## 4253 9.647137 ## 4292 12.849495 ## 4325 13.192375 ## 4350 11.950029 ## 4383 11.645854 ## 4410 13.389554 ## 4420 13.720300 ## 4458 6.401756 ## 4511 13.352934 ## 4523 8.729575 ## 4530 9.296663 ## 4642 14.462988 ## 4868 12.348708 ## 4931 13.706969 ## 5192 10.720556 ## 5404 15.209843 ## 5619 15.237193 ## 5640 13.121475 ## 5650 14.123412 ## 5667 13.699098 ## 5720 14.152359 ## 5761 11.258531 ## 5762 5.234155 ## 5783 13.080792 ## 5815 8.096726 ## 5819 12.180400 ## 5838 13.555023 ## 5937 16.215205 ## 6074 13.693358 ## 6089 15.214590 ## 6144 8.936208 ## 6170 13.906338 ## 6291 10.398038 ## 6366 15.436995 ## 6397 12.784896 ## 6415 11.920503 ## 6443 9.891454 ## 6464 7.842918 ## 6469 18.026039 ## 6484 12.880746 ## 6578 12.042264 ## 6600 11.773933 ## 6808 9.599819 ## 6816 14.393124 ## 6897 14.888779 ## 6990 6.502795 ## 7011 13.671150 ## 7101 11.959836 ## 7172 8.494673 ## 7232 12.550217 ## 7276 12.676046 ## 7332 14.463195 ## 7341 10.026777 ## 7342 11.273270 ## 7345 11.436018 ## 7364 14.028416 ## 7635 14.866823 ## 7688 17.973209 ## 7697 15.338153 ## 7734 10.915297 ## 7890 8.692590 ## 7919 14.607885 ## 8009 13.957075 ## 8150 14.644961 ## 8165 16.127482 ## 8175 11.811741 ## 8188 12.727285 ## 8193 15.888589 ## 8202 11.818683 ## 8357 14.130463 ## 8367 6.533962 ## 8477 12.534794 ## 8531 13.439703 ## 8627 11.022184 ## 8628 16.258556 ## 8707 12.862577 ## 8775 9.741179 ## 8800 7.995177 ## 8854 5.284060 ## 8857 15.120574 ## 8874 12.120259 ## 8946 10.539443 ## 8983 11.126593 ## 9021 14.542053 ## 9104 16.511918 ## 9158 8.868329 ## 9198 18.266919 ## 9225 14.439747 ## 9292 10.734658 ## 9340 11.376142 ## 9347 13.472163 ## 9359 15.062624 ## 9397 10.556629 ## 9508 13.466893 ## 9550 11.298842 ## 9586 14.704440 ## ## attr(,"class") ## [1] "coef.mer" ``` ] .pull-right[ ```r ranef(results_0) ``` ``` ## $schoolid ## (Intercept) ## 1224 -2.66393473 ## 1288 0.73940984 ## 1296 -4.56846563 ## 1308 2.94851706 ## 1317 0.49394609 ## 1358 -1.24251162 ## 1374 -2.50234961 ## 1433 6.26824325 ## 1436 4.96210829 ## 1461 3.69657495 ## 1462 -1.98328157 ## 1477 1.48280174 ## 1499 -4.58357634 ## 1637 -4.80420488 ## 1906 3.08192302 ## 1909 1.53689246 ## 1942 4.73230257 ## 1946 0.24312991 ## 2030 -0.50951423 ## 2208 2.57281413 ## 2277 -3.10782330 ## 2305 -1.40397850 ## 2336 3.53856279 ## 2458 1.24911599 ## 2467 -2.28936743 ## 2526 4.08992825 ## 2626 0.67848597 ## 2629 2.10311300 ## 2639 -5.43354661 ## 2651 -1.38679960 ## 2655 -0.26834758 ## 2658 0.68954234 ## 2755 3.50100331 ## 2768 -1.48085168 ## 2771 -0.73234978 ## 2818 1.11518762 ## 2917 -4.21276802 ## 2990 5.30834044 ## 2995 -2.81295064 ## 3013 -0.02407879 ## 3020 1.63254392 ## 3039 3.55707772 ## 3088 -3.12676207 ## 3152 0.52608563 ## 3332 1.46586922 ## 3351 -1.04949536 ## 3377 -3.13377137 ## 3427 6.47780570 ## 3498 3.45703989 ## 3499 0.57123415 ## 3533 -2.03523297 ## 3610 2.53776920 ## 3657 -2.86085994 ## 3688 1.82626231 ## 3705 -2.09382208 ## 3716 -2.04196953 ## 3838 3.15989933 ## 3881 -0.61912613 ## 3967 -0.55352008 ## 3992 1.84962915 ## 3999 -1.54071083 ## 4042 1.56716203 ## 4173 0.07947627 ## 4223 1.80350568 ## 4253 -2.98983646 ## 4292 0.21252084 ## 4325 0.55540098 ## 4350 -0.68694440 ## 4383 -0.99111943 ## 4410 0.75258055 ## 4420 1.08332584 ## 4458 -6.23521735 ## 4511 0.71596012 ## 4523 -3.90739865 ## 4530 -3.34031073 ## 4642 1.82601437 ## 4868 -0.28826542 ## 4931 1.06999469 ## 5192 -1.91641734 ## 5404 2.57286887 ## 5619 2.60021915 ## 5640 0.48450127 ## 5650 1.48643856 ## 5667 1.06212374 ## 5720 1.51538473 ## 5761 -1.37844243 ## 5762 -7.40281887 ## 5783 0.44381843 ## 5815 -4.54024780 ## 5819 -0.45657380 ## 5838 0.91804946 ## 5937 3.57823103 ## 6074 1.05638386 ## 6089 2.57761596 ## 6144 -3.70076593 ## 6170 1.26936411 ## 6291 -2.23893576 ## 6366 2.80002082 ## 6397 0.14792182 ## 6415 -0.71647091 ## 6443 -2.74551989 ## 6464 -4.79405536 ## 6469 5.38906505 ## 6484 0.24377254 ## 6578 -0.59470979 ## 6600 -0.86304044 ## 6808 -3.03715524 ## 6816 1.75614974 ## 6897 2.25180533 ## 6990 -6.13417914 ## 7011 1.03417644 ## 7101 -0.67713815 ## 7172 -4.14230123 ## 7232 -0.08675681 ## 7276 0.03907201 ## 7332 1.82622086 ## 7341 -2.61019712 ## 7342 -1.36370392 ## 7345 -1.20095588 ## 7364 1.39144186 ## 7635 2.22984902 ## 7688 5.33623560 ## 7697 2.70117923 ## 7734 -1.72167664 ## 7890 -3.94438331 ## 7919 1.97091148 ## 8009 1.32010086 ## 8150 2.00798675 ## 8165 3.49050832 ## 8175 -0.82523286 ## 8188 0.09031145 ## 8193 3.25161501 ## 8202 -0.81829092 ## 8357 1.49348947 ## 8367 -6.10301147 ## 8477 -0.10217975 ## 8531 0.80272900 ## 8627 -1.61478957 ## 8628 3.62158228 ## 8707 0.22560316 ## 8775 -2.89579513 ## 8800 -4.64179681 ## 8854 -7.35291359 ## 8857 2.48360013 ## 8874 -0.51671483 ## 8946 -2.09753088 ## 8983 -1.51038060 ## 9021 1.90507896 ## 9104 3.87494381 ## 9158 -3.76864489 ## 9198 5.62994490 ## 9225 1.80277352 ## 9292 -1.90231628 ## 9340 -1.26083166 ## 9347 0.83518918 ## 9359 2.42565069 ## 9397 -2.08034443 ## 9508 0.82991907 ## 9550 -1.33813133 ## 9586 2.06746598 ## ## with conditional variances for "schoolid" ``` ] ] --- # Ej: escuela 1224 - con `ranef` obtenemos su efecto aleatorio `\(\mu\)` = -2.66, que equivale a su desviación del gran intercepto `\(\gamma_{00}\)` - `\(\gamma_{00}\)` = 12.64 - el valor predicho para el intercepto de la escuela 1224 es `\(\gamma{00} + \mu_j\)` - 12.64 + (-2.66) = 9.98 , que es el valor que se obtiene para esta escuela con la función `coef` --- ## Coeficientes de regresión - predicción con efectos fijos .small[ ```r results_4 = lmer(mathach ~ 1 + ses + female + mnses + sector + (1 | schoolid), data=mlm) coef(results_4) # coef: comando que muestra coeficientes por grupo $id ``` ``` ## $schoolid ## (Intercept) ses female mnses sector ## 1224 12.707923 2.152109 -1.196833 3.067472 1.251059 ## 1288 13.133888 2.152109 -1.196833 3.067472 1.251059 ## 1296 11.184956 2.152109 -1.196833 3.067472 1.251059 ## 1308 12.475802 2.152109 -1.196833 3.067472 1.251059 ## 1317 11.694382 2.152109 -1.196833 3.067472 1.251059 ## 1358 12.108733 2.152109 -1.196833 3.067472 1.251059 ## 1374 11.257433 2.152109 -1.196833 3.067472 1.251059 ## 1433 14.091378 2.152109 -1.196833 3.067472 1.251059 ## 1436 13.885307 2.152109 -1.196833 3.067472 1.251059 ## 1461 13.567230 2.152109 -1.196833 3.067472 1.251059 ## 1462 12.739162 2.152109 -1.196833 3.067472 1.251059 ## 1477 12.697167 2.152109 -1.196833 3.067472 1.251059 ## 1499 11.302319 2.152109 -1.196833 3.067472 1.251059 ## 1637 11.929833 2.152109 -1.196833 3.067472 1.251059 ## 1906 12.688447 2.152109 -1.196833 3.067472 1.251059 ## 1909 13.273374 2.152109 -1.196833 3.067472 1.251059 ## 1942 14.294538 2.152109 -1.196833 3.067472 1.251059 ## 1946 13.269292 2.152109 -1.196833 3.067472 1.251059 ## 2030 11.372184 2.152109 -1.196833 3.067472 1.251059 ## 2208 12.664836 2.152109 -1.196833 3.067472 1.251059 ## 2277 13.115381 2.152109 -1.196833 3.067472 1.251059 ## 2305 14.031712 2.152109 -1.196833 3.067472 1.251059 ## 2336 14.230611 2.152109 -1.196833 3.067472 1.251059 ## 2458 12.742226 2.152109 -1.196833 3.067472 1.251059 ## 2467 12.534944 2.152109 -1.196833 3.067472 1.251059 ## 2526 14.703184 2.152109 -1.196833 3.067472 1.251059 ## 2626 13.817248 2.152109 -1.196833 3.067472 1.251059 ## 2629 13.997802 2.152109 -1.196833 3.067472 1.251059 ## 2639 12.451456 2.152109 -1.196833 3.067472 1.251059 ## 2651 11.741216 2.152109 -1.196833 3.067472 1.251059 ## 2655 15.735301 2.152109 -1.196833 3.067472 1.251059 ## 2658 11.171818 2.152109 -1.196833 3.067472 1.251059 ## 2755 12.434801 2.152109 -1.196833 3.067472 1.251059 ## 2768 12.147697 2.152109 -1.196833 3.067472 1.251059 ## 2771 13.872969 2.152109 -1.196833 3.067472 1.251059 ## 2818 13.587599 2.152109 -1.196833 3.067472 1.251059 ## 2917 12.832689 2.152109 -1.196833 3.067472 1.251059 ## 2990 13.506608 2.152109 -1.196833 3.067472 1.251059 ## 2995 11.977510 2.152109 -1.196833 3.067472 1.251059 ## 3013 13.141475 2.152109 -1.196833 3.067472 1.251059 ## 3020 13.034486 2.152109 -1.196833 3.067472 1.251059 ## 3039 13.378465 2.152109 -1.196833 3.067472 1.251059 ## 3088 12.404857 2.152109 -1.196833 3.067472 1.251059 ## 3152 13.438828 2.152109 -1.196833 3.067472 1.251059 ## 3332 12.121618 2.152109 -1.196833 3.067472 1.251059 ## 3351 11.012839 2.152109 -1.196833 3.067472 1.251059 ## 3377 12.745610 2.152109 -1.196833 3.067472 1.251059 ## 3427 16.390762 2.152109 -1.196833 3.067472 1.251059 ## 3498 12.361494 2.152109 -1.196833 3.067472 1.251059 ## 3499 11.171742 2.152109 -1.196833 3.067472 1.251059 ## 3533 11.150342 2.152109 -1.196833 3.067472 1.251059 ## 3610 13.749518 2.152109 -1.196833 3.067472 1.251059 ## 3657 13.376831 2.152109 -1.196833 3.067472 1.251059 ## 3688 12.560126 2.152109 -1.196833 3.067472 1.251059 ## 3705 9.645341 2.152109 -1.196833 3.067472 1.251059 ## 3716 13.069586 2.152109 -1.196833 3.067472 1.251059 ## 3838 14.158099 2.152109 -1.196833 3.067472 1.251059 ## 3881 12.083343 2.152109 -1.196833 3.067472 1.251059 ## 3967 13.457604 2.152109 -1.196833 3.067472 1.251059 ## 3992 12.151636 2.152109 -1.196833 3.067472 1.251059 ## 3999 12.251872 2.152109 -1.196833 3.067472 1.251059 ## 4042 11.768611 2.152109 -1.196833 3.067472 1.251059 ## 4173 12.295909 2.152109 -1.196833 3.067472 1.251059 ## 4223 13.938627 2.152109 -1.196833 3.067472 1.251059 ## 4253 10.711470 2.152109 -1.196833 3.067472 1.251059 ## 4292 13.856972 2.152109 -1.196833 3.067472 1.251059 ## 4325 13.772931 2.152109 -1.196833 3.067472 1.251059 ## 4350 11.944010 2.152109 -1.196833 3.067472 1.251059 ## 4383 12.041554 2.152109 -1.196833 3.067472 1.251059 ## 4410 13.211432 2.152109 -1.196833 3.067472 1.251059 ## 4420 14.607346 2.152109 -1.196833 3.067472 1.251059 ## 4458 12.185523 2.152109 -1.196833 3.067472 1.251059 ## 4511 13.646765 2.152109 -1.196833 3.067472 1.251059 ## 4523 9.689643 2.152109 -1.196833 3.067472 1.251059 ## 4530 12.250395 2.152109 -1.196833 3.067472 1.251059 ## 4642 14.288264 2.152109 -1.196833 3.067472 1.251059 ## 4868 10.626388 2.152109 -1.196833 3.067472 1.251059 ## 4931 11.705201 2.152109 -1.196833 3.067472 1.251059 ## 5192 11.122172 2.152109 -1.196833 3.067472 1.251059 ## 5404 11.444270 2.152109 -1.196833 3.067472 1.251059 ## 5619 12.561805 2.152109 -1.196833 3.067472 1.251059 ## 5640 14.159767 2.152109 -1.196833 3.067472 1.251059 ## 5650 13.476656 2.152109 -1.196833 3.067472 1.251059 ## 5667 11.203233 2.152109 -1.196833 3.067472 1.251059 ## 5720 13.241622 2.152109 -1.196833 3.067472 1.251059 ## 5761 12.762717 2.152109 -1.196833 3.067472 1.251059 ## 5762 11.712223 2.152109 -1.196833 3.067472 1.251059 ## 5783 12.871770 2.152109 -1.196833 3.067472 1.251059 ## 5815 11.999153 2.152109 -1.196833 3.067472 1.251059 ## 5819 12.032238 2.152109 -1.196833 3.067472 1.251059 ## 5838 13.273634 2.152109 -1.196833 3.067472 1.251059 ## 5937 13.531048 2.152109 -1.196833 3.067472 1.251059 ## 6074 14.600889 2.152109 -1.196833 3.067472 1.251059 ## 6089 14.761866 2.152109 -1.196833 3.067472 1.251059 ## 6144 11.791491 2.152109 -1.196833 3.067472 1.251059 ## 6170 14.779048 2.152109 -1.196833 3.067472 1.251059 ## 6291 13.352288 2.152109 -1.196833 3.067472 1.251059 ## 6366 13.303538 2.152109 -1.196833 3.067472 1.251059 ## 6397 14.051100 2.152109 -1.196833 3.067472 1.251059 ## 6415 13.285636 2.152109 -1.196833 3.067472 1.251059 ## 6443 12.343162 2.152109 -1.196833 3.067472 1.251059 ## 6464 11.849552 2.152109 -1.196833 3.067472 1.251059 ## 6469 13.153112 2.152109 -1.196833 3.067472 1.251059 ## 6484 13.964413 2.152109 -1.196833 3.067472 1.251059 ## 6578 14.192437 2.152109 -1.196833 3.067472 1.251059 ## 6600 12.635478 2.152109 -1.196833 3.067472 1.251059 ## 6808 11.104890 2.152109 -1.196833 3.067472 1.251059 ## 6816 11.964985 2.152109 -1.196833 3.067472 1.251059 ## 6897 13.660358 2.152109 -1.196833 3.067472 1.251059 ## 6990 10.106525 2.152109 -1.196833 3.067472 1.251059 ## 7011 13.137230 2.152109 -1.196833 3.067472 1.251059 ## 7101 12.457267 2.152109 -1.196833 3.067472 1.251059 ## 7172 9.995348 2.152109 -1.196833 3.067472 1.251059 ## 7232 13.465171 2.152109 -1.196833 3.067472 1.251059 ## 7276 12.897951 2.152109 -1.196833 3.067472 1.251059 ## 7332 12.642896 2.152109 -1.196833 3.067472 1.251059 ## 7341 11.720700 2.152109 -1.196833 3.067472 1.251059 ## 7342 12.364160 2.152109 -1.196833 3.067472 1.251059 ## 7345 12.008754 2.152109 -1.196833 3.067472 1.251059 ## 7364 14.068113 2.152109 -1.196833 3.067472 1.251059 ## 7635 13.087198 2.152109 -1.196833 3.067472 1.251059 ## 7688 15.368184 2.152109 -1.196833 3.067472 1.251059 ## 7697 14.072866 2.152109 -1.196833 3.067472 1.251059 ## 7734 13.289875 2.152109 -1.196833 3.067472 1.251059 ## 7890 11.911265 2.152109 -1.196833 3.067472 1.251059 ## 7919 12.903578 2.152109 -1.196833 3.067472 1.251059 ## 8009 10.836287 2.152109 -1.196833 3.067472 1.251059 ## 8150 12.759413 2.152109 -1.196833 3.067472 1.251059 ## 8165 13.368508 2.152109 -1.196833 3.067472 1.251059 ## 8175 13.026372 2.152109 -1.196833 3.067472 1.251059 ## 8188 12.837975 2.152109 -1.196833 3.067472 1.251059 ## 8193 15.002671 2.152109 -1.196833 3.067472 1.251059 ## 8202 12.209222 2.152109 -1.196833 3.067472 1.251059 ## 8357 14.494788 2.152109 -1.196833 3.067472 1.251059 ## 8367 9.266714 2.152109 -1.196833 3.067472 1.251059 ## 8477 13.822098 2.152109 -1.196833 3.067472 1.251059 ## 8531 12.267737 2.152109 -1.196833 3.067472 1.251059 ## 8627 11.333326 2.152109 -1.196833 3.067472 1.251059 ## 8628 15.827569 2.152109 -1.196833 3.067472 1.251059 ## 8707 12.727478 2.152109 -1.196833 3.067472 1.251059 ## 8775 12.075734 2.152109 -1.196833 3.067472 1.251059 ## 8800 11.596298 2.152109 -1.196833 3.067472 1.251059 ## 8854 10.190318 2.152109 -1.196833 3.067472 1.251059 ## 8857 12.245814 2.152109 -1.196833 3.067472 1.251059 ## 8874 13.954819 2.152109 -1.196833 3.067472 1.251059 ## 8946 12.824607 2.152109 -1.196833 3.067472 1.251059 ## 8983 12.966605 2.152109 -1.196833 3.067472 1.251059 ## 9021 11.234016 2.152109 -1.196833 3.067472 1.251059 ## 9104 12.528424 2.152109 -1.196833 3.067472 1.251059 ## 9158 11.719501 2.152109 -1.196833 3.067472 1.251059 ## 9198 14.372418 2.152109 -1.196833 3.067472 1.251059 ## 9225 13.645952 2.152109 -1.196833 3.067472 1.251059 ## 9292 13.343269 2.152109 -1.196833 3.067472 1.251059 ## 9340 13.529763 2.152109 -1.196833 3.067472 1.251059 ## 9347 12.463314 2.152109 -1.196833 3.067472 1.251059 ## 9359 12.310581 2.152109 -1.196833 3.067472 1.251059 ## 9397 10.983298 2.152109 -1.196833 3.067472 1.251059 ## 9508 12.944517 2.152109 -1.196833 3.067472 1.251059 ## 9550 12.021441 2.152109 -1.196833 3.067472 1.251059 ## 9586 11.831361 2.152109 -1.196833 3.067472 1.251059 ## ## attr(,"class") ## [1] "coef.mer" ``` ] --- ## Coeficientes regresión - predicción con pendiente aleatoria .small[ ```r results_5 = lmer(mathach ~ 1 + ses + female + mnses + sector + (1 + ses | schoolid), data=mlm) coef(results_5) # coef: comando que muestra coeficientes por grupo $id ``` ``` ## $schoolid ## (Intercept) ses female mnses sector ## 1224 12.718688 2.229928 -1.185924 3.07283 1.429747 ## 1288 13.118254 2.315323 -1.185924 3.07283 1.429747 ## 1296 11.049900 1.893727 -1.185924 3.07283 1.429747 ## 1308 12.338591 2.009420 -1.185924 3.07283 1.429747 ## 1317 11.563797 1.872895 -1.185924 3.07283 1.429747 ## 1358 12.147338 2.404293 -1.185924 3.07283 1.429747 ## 1374 11.261404 2.215734 -1.185924 3.07283 1.429747 ## 1433 13.862506 2.379662 -1.185924 3.07283 1.429747 ## 1436 13.677847 2.315760 -1.185924 3.07283 1.429747 ## 1461 13.372596 2.834185 -1.185924 3.07283 1.429747 ## 1462 12.237417 1.630519 -1.185924 3.07283 1.429747 ## 1477 12.529403 1.938104 -1.185924 3.07283 1.429747 ## 1499 11.454040 2.467499 -1.185924 3.07283 1.429747 ## 1637 12.006810 2.298976 -1.185924 3.07283 1.429747 ## 1906 12.533231 2.120530 -1.185924 3.07283 1.429747 ## 1909 13.240021 2.296086 -1.185924 3.07283 1.429747 ## 1942 14.193660 2.347177 -1.185924 3.07283 1.429747 ## 1946 13.279340 2.399089 -1.185924 3.07283 1.429747 ## 2030 11.377733 1.835520 -1.185924 3.07283 1.429747 ## 2208 12.493501 2.191759 -1.185924 3.07283 1.429747 ## 2277 12.235888 1.186028 -1.185924 3.07283 1.429747 ## 2305 13.404714 1.442942 -1.185924 3.07283 1.429747 ## 2336 14.155767 2.337910 -1.185924 3.07283 1.429747 ## 2458 12.572619 2.304372 -1.185924 3.07283 1.429747 ## 2467 12.553604 2.254845 -1.185924 3.07283 1.429747 ## 2526 14.521539 2.081324 -1.185924 3.07283 1.429747 ## 2626 13.852562 2.481026 -1.185924 3.07283 1.429747 ## 2629 13.734021 1.793893 -1.185924 3.07283 1.429747 ## 2639 12.054817 1.724141 -1.185924 3.07283 1.429747 ## 2651 11.764077 2.400970 -1.185924 3.07283 1.429747 ## 2655 16.035512 2.674553 -1.185924 3.07283 1.429747 ## 2658 11.059088 1.938731 -1.185924 3.07283 1.429747 ## 2755 12.347354 1.858877 -1.185924 3.07283 1.429747 ## 2768 12.174405 2.338538 -1.185924 3.07283 1.429747 ## 2771 13.980828 2.501683 -1.185924 3.07283 1.429747 ## 2818 13.570566 2.370264 -1.185924 3.07283 1.429747 ## 2917 12.573740 1.848210 -1.185924 3.07283 1.429747 ## 2990 13.342024 2.145252 -1.185924 3.07283 1.429747 ## 2995 11.838660 1.850906 -1.185924 3.07283 1.429747 ## 3013 13.160223 2.400036 -1.185924 3.07283 1.429747 ## 3020 12.865964 2.075182 -1.185924 3.07283 1.429747 ## 3039 13.245543 2.325450 -1.185924 3.07283 1.429747 ## 3088 12.332119 2.064017 -1.185924 3.07283 1.429747 ## 3152 13.438322 2.396298 -1.185924 3.07283 1.429747 ## 3332 12.123488 2.011861 -1.185924 3.07283 1.429747 ## 3351 11.006787 2.015229 -1.185924 3.07283 1.429747 ## 3377 12.371916 1.578023 -1.185924 3.07283 1.429747 ## 3427 16.189039 2.113855 -1.185924 3.07283 1.429747 ## 3498 12.338567 1.739255 -1.185924 3.07283 1.429747 ## 3499 11.078267 1.728836 -1.185924 3.07283 1.429747 ## 3533 10.892292 1.721764 -1.185924 3.07283 1.429747 ## 3610 13.588966 2.422726 -1.185924 3.07283 1.429747 ## 3657 13.581872 2.518576 -1.185924 3.07283 1.429747 ## 3688 12.410368 2.046851 -1.185924 3.07283 1.429747 ## 3705 9.515435 1.541705 -1.185924 3.07283 1.429747 ## 3716 13.442287 2.982233 -1.185924 3.07283 1.429747 ## 3838 13.973101 1.999673 -1.185924 3.07283 1.429747 ## 3881 12.057814 2.124468 -1.185924 3.07283 1.429747 ## 3967 13.500676 2.382283 -1.185924 3.07283 1.429747 ## 3992 12.031843 1.832326 -1.185924 3.07283 1.429747 ## 3999 12.322912 2.532469 -1.185924 3.07283 1.429747 ## 4042 11.656947 1.896690 -1.185924 3.07283 1.429747 ## 4173 12.163090 2.273549 -1.185924 3.07283 1.429747 ## 4223 13.798733 2.279179 -1.185924 3.07283 1.429747 ## 4253 10.273404 1.461998 -1.185924 3.07283 1.429747 ## 4292 13.420117 1.637567 -1.185924 3.07283 1.429747 ## 4325 13.792054 2.438777 -1.185924 3.07283 1.429747 ## 4350 11.946850 2.348998 -1.185924 3.07283 1.429747 ## 4383 12.062749 2.365468 -1.185924 3.07283 1.429747 ## 4410 13.191939 2.287018 -1.185924 3.07283 1.429747 ## 4420 14.621465 2.361761 -1.185924 3.07283 1.429747 ## 4458 12.005552 1.980520 -1.185924 3.07283 1.429747 ## 4511 13.402952 1.861434 -1.185924 3.07283 1.429747 ## 4523 9.513791 1.917411 -1.185924 3.07283 1.429747 ## 4530 12.023144 2.042287 -1.185924 3.07283 1.429747 ## 4642 14.268713 2.533765 -1.185924 3.07283 1.429747 ## 4868 10.512227 1.712426 -1.185924 3.07283 1.429747 ## 4931 11.601200 1.753519 -1.185924 3.07283 1.429747 ## 5192 10.930830 1.907021 -1.185924 3.07283 1.429747 ## 5404 11.464347 1.780138 -1.185924 3.07283 1.429747 ## 5619 12.303160 2.646535 -1.185924 3.07283 1.429747 ## 5640 14.231149 2.534929 -1.185924 3.07283 1.429747 ## 5650 13.273346 1.947294 -1.185924 3.07283 1.429747 ## 5667 11.053304 2.163370 -1.185924 3.07283 1.429747 ## 5720 13.088570 2.244200 -1.185924 3.07283 1.429747 ## 5761 12.686967 2.367094 -1.185924 3.07283 1.429747 ## 5762 11.490666 1.948480 -1.185924 3.07283 1.429747 ## 5783 12.848013 2.277046 -1.185924 3.07283 1.429747 ## 5815 12.018246 2.218960 -1.185924 3.07283 1.429747 ## 5819 12.007924 2.047928 -1.185924 3.07283 1.429747 ## 5838 13.232824 2.193121 -1.185924 3.07283 1.429747 ## 5937 13.467194 2.217537 -1.185924 3.07283 1.429747 ## 6074 14.411909 2.140286 -1.185924 3.07283 1.429747 ## 6089 14.726282 2.339448 -1.185924 3.07283 1.429747 ## 6144 11.821378 2.244576 -1.185924 3.07283 1.429747 ## 6170 14.921393 2.646232 -1.185924 3.07283 1.429747 ## 6291 13.439845 2.383177 -1.185924 3.07283 1.429747 ## 6366 13.139159 2.085864 -1.185924 3.07283 1.429747 ## 6397 14.096697 2.412982 -1.185924 3.07283 1.429747 ## 6415 13.358783 2.500028 -1.185924 3.07283 1.429747 ## 6443 12.111530 1.719363 -1.185924 3.07283 1.429747 ## 6464 11.739236 2.018432 -1.185924 3.07283 1.429747 ## 6469 12.987687 2.152797 -1.185924 3.07283 1.429747 ## 6484 13.838982 1.939921 -1.185924 3.07283 1.429747 ## 6578 14.056908 2.231729 -1.185924 3.07283 1.429747 ## 6600 12.719872 2.703482 -1.185924 3.07283 1.429747 ## 6808 11.072415 2.061460 -1.185924 3.07283 1.429747 ## 6816 11.875889 1.859838 -1.185924 3.07283 1.429747 ## 6897 13.583240 2.589229 -1.185924 3.07283 1.429747 ## 6990 9.886787 1.730689 -1.185924 3.07283 1.429747 ## 7011 13.074902 2.533965 -1.185924 3.07283 1.429747 ## 7101 12.390930 2.019152 -1.185924 3.07283 1.429747 ## 7172 9.723452 1.768357 -1.185924 3.07283 1.429747 ## 7232 13.547272 2.656759 -1.185924 3.07283 1.429747 ## 7276 12.911277 2.557707 -1.185924 3.07283 1.429747 ## 7332 12.481961 2.178251 -1.185924 3.07283 1.429747 ## 7341 11.645933 1.950030 -1.185924 3.07283 1.429747 ## 7342 12.109755 1.945832 -1.185924 3.07283 1.429747 ## 7345 12.058370 2.666148 -1.185924 3.07283 1.429747 ## 7364 13.870748 2.074413 -1.185924 3.07283 1.429747 ## 7635 12.921321 2.210800 -1.185924 3.07283 1.429747 ## 7688 15.182217 2.186222 -1.185924 3.07283 1.429747 ## 7697 14.036678 2.461218 -1.185924 3.07283 1.429747 ## 7734 13.579474 2.775843 -1.185924 3.07283 1.429747 ## 7890 11.620794 1.657940 -1.185924 3.07283 1.429747 ## 7919 12.846154 2.366528 -1.185924 3.07283 1.429747 ## 8009 10.782401 1.743695 -1.185924 3.07283 1.429747 ## 8150 12.609378 1.822869 -1.185924 3.07283 1.429747 ## 8165 13.203263 2.178620 -1.185924 3.07283 1.429747 ## 8175 12.963808 2.083656 -1.185924 3.07283 1.429747 ## 8188 12.842029 2.440730 -1.185924 3.07283 1.429747 ## 8193 14.885212 2.355826 -1.185924 3.07283 1.429747 ## 8202 12.206762 2.303005 -1.185924 3.07283 1.429747 ## 8357 14.518478 2.443492 -1.185924 3.07283 1.429747 ## 8367 9.107836 1.608686 -1.185924 3.07283 1.429747 ## 8477 13.929579 2.607668 -1.185924 3.07283 1.429747 ## 8531 12.233119 2.240760 -1.185924 3.07283 1.429747 ## 8627 11.306052 1.965358 -1.185924 3.07283 1.429747 ## 8628 15.640972 2.182031 -1.185924 3.07283 1.429747 ## 8707 12.716847 2.446274 -1.185924 3.07283 1.429747 ## 8775 11.959725 1.924073 -1.185924 3.07283 1.429747 ## 8800 11.521896 2.237809 -1.185924 3.07283 1.429747 ## 8854 10.184219 2.133731 -1.185924 3.07283 1.429747 ## 8857 12.154898 1.879477 -1.185924 3.07283 1.429747 ## 8874 14.087009 2.565375 -1.185924 3.07283 1.429747 ## 8946 12.734504 1.998656 -1.185924 3.07283 1.429747 ## 8983 12.882791 2.009176 -1.185924 3.07283 1.429747 ## 9021 11.164565 1.905033 -1.185924 3.07283 1.429747 ## 9104 12.411019 2.038344 -1.185924 3.07283 1.429747 ## 9158 11.883105 2.536046 -1.185924 3.07283 1.429747 ## 9198 14.173518 2.479962 -1.185924 3.07283 1.429747 ## 9225 13.609250 2.412433 -1.185924 3.07283 1.429747 ## 9292 13.249691 2.094634 -1.185924 3.07283 1.429747 ## 9340 13.549198 2.294651 -1.185924 3.07283 1.429747 ## 9347 12.299050 2.224249 -1.185924 3.07283 1.429747 ## 9359 12.207862 1.649750 -1.185924 3.07283 1.429747 ## 9397 10.960936 1.983996 -1.185924 3.07283 1.429747 ## 9508 12.858864 2.363500 -1.185924 3.07283 1.429747 ## 9550 12.042265 2.373925 -1.185924 3.07283 1.429747 ## 9586 11.750845 1.908395 -1.185924 3.07283 1.429747 ## ## attr(,"class") ## [1] "coef.mer" ``` ] --- .medium[ ```r ranef(results_5) ``` ``` ## $schoolid ## (Intercept) ses ## 1224 0.06576763 0.074088592 ## 1288 0.46533310 0.159482892 ## 1296 -1.60302074 -0.262113091 ## 1308 -0.31433001 -0.146419320 ## 1317 -1.08912322 -0.282944309 ## 1358 -0.50558231 0.248452744 ## 1374 -1.39151698 0.059894582 ## 1433 1.20958568 0.223822726 ## 1436 1.02492592 0.159919866 ## 1461 0.71967569 0.678345100 ## 1462 -0.41550369 -0.525321036 ## 1477 -0.12351763 -0.217735607 ## 1499 -1.19888035 0.311659290 ## 1637 -0.64611059 0.143136160 ## 1906 -0.11968944 -0.035309765 ## 1909 0.58710017 0.140246514 ## 1942 1.54073890 0.191337342 ## 1946 0.62641918 0.243249482 ## 2030 -1.27518764 -0.320319959 ## 2208 -0.15941936 0.035919573 ## 2277 -0.41703298 -0.969812210 ## 2305 0.75179352 -0.712897763 ## 2336 1.50284588 0.182070452 ## 2458 -0.08030143 0.148532113 ## 2467 -0.09931694 0.099005163 ## 2526 1.86861869 -0.074515336 ## 2626 1.19964173 0.325186106 ## 2629 1.08109997 -0.361946427 ## 2639 -0.59810360 -0.431698785 ## 2651 -0.88884329 0.245130060 ## 2655 3.38259130 0.518713718 ## 2658 -1.59383309 -0.217108857 ## 2755 -0.30556642 -0.296962280 ## 2768 -0.47851594 0.182697989 ## 2771 1.32790690 0.345843083 ## 2818 0.91764525 0.214424373 ## 2917 -0.07918081 -0.307629348 ## 2990 0.68910297 -0.010588174 ## 2995 -0.81426069 -0.304933534 ## 3013 0.50730214 0.244196377 ## 3020 0.21304293 -0.080657892 ## 3039 0.59262223 0.169610354 ## 3088 -0.32080184 -0.091823125 ## 3152 0.78540142 0.240458304 ## 3332 -0.52943264 -0.143979257 ## 3351 -1.64613405 -0.140611095 ## 3377 -0.28100441 -0.577816535 ## 3427 3.53611861 -0.041984927 ## 3498 -0.31435352 -0.416584856 ## 3499 -1.57465371 -0.427003335 ## 3533 -1.76062878 -0.434075795 ## 3610 0.93604570 0.266885812 ## 3657 0.92895112 0.362735989 ## 3688 -0.24255253 -0.108989055 ## 3705 -3.13748596 -0.614135247 ## 3716 0.78936660 0.826392997 ## 3838 1.32018001 -0.156166865 ## 3881 -0.59510633 -0.031371303 ## 3967 0.84775570 0.226442921 ## 3992 -0.62107769 -0.323513723 ## 3999 -0.33000876 0.376628762 ## 4042 -0.99597410 -0.259149589 ## 4173 -0.48983077 0.117708806 ## 4223 1.14581272 0.123339352 ## 4253 -2.37951670 -0.693841622 ## 4292 0.76719613 -0.518272727 ## 4325 1.13913344 0.282937113 ## 4350 -0.70607097 0.193157987 ## 4383 -0.59017156 0.209628683 ## 4410 0.53901839 0.131178328 ## 4420 1.96854404 0.205921386 ## 4458 -0.64736911 -0.175319475 ## 4511 0.75003139 -0.294405822 ## 4523 -3.13912986 -0.238429060 ## 4530 -0.62977631 -0.113552655 ## 4642 1.61579191 0.377924984 ## 4868 -2.14069391 -0.443413868 ## 4931 -1.05172025 -0.402320978 ## 5192 -1.72209080 -0.248818401 ## 5404 -1.18857350 -0.375701613 ## 5619 -0.34976048 0.490695174 ## 5640 1.57822791 0.379088785 ## 5650 0.62042543 -0.208546216 ## 5667 -1.59961623 0.007530266 ## 5720 0.43564909 0.088359945 ## 5761 0.03404600 0.211254062 ## 5762 -1.16225483 -0.207360082 ## 5783 0.19509257 0.121206242 ## 5815 -0.63467458 0.063120271 ## 5819 -0.64499669 -0.107911428 ## 5838 0.57990383 0.037281088 ## 5937 0.81427322 0.061696839 ## 6074 1.75898847 -0.015553398 ## 6089 2.07336170 0.183607842 ## 6144 -0.83154276 0.088736366 ## 6170 2.26847265 0.490391935 ## 6291 0.78692397 0.227337468 ## 6366 0.48623827 -0.069975984 ## 6397 1.44377600 0.257142478 ## 6415 0.70586266 0.344188022 ## 6443 -0.54139104 -0.436476692 ## 6464 -0.91368451 -0.137407658 ## 6469 0.33476684 -0.003043016 ## 6484 1.18606158 -0.215918617 ## 6578 1.40398709 0.075889615 ## 6600 0.06695105 0.547641917 ## 6808 -1.58050541 -0.094380075 ## 6816 -0.77703148 -0.296002163 ## 6897 0.93031902 0.433388830 ## 6990 -2.76613377 -0.425150679 ## 7011 0.42198138 0.378124904 ## 7101 -0.26199060 -0.136688061 ## 7172 -2.92946869 -0.387482885 ## 7232 0.89435138 0.500919071 ## 7276 0.25835596 0.401867147 ## 7332 -0.17095922 0.022410861 ## 7341 -1.00698714 -0.205809729 ## 7342 -0.54316535 -0.210008105 ## 7345 -0.59455097 0.510307944 ## 7364 1.21782785 -0.081427028 ## 7635 0.26840026 0.054959901 ## 7688 2.52929673 0.030381750 ## 7697 1.38375702 0.305377810 ## 7734 0.92655361 0.620003646 ## 7890 -1.03212655 -0.497899892 ## 7919 0.19323316 0.210688367 ## 8009 -1.87051983 -0.412144823 ## 8150 -0.04354300 -0.332971039 ## 8165 0.55034259 0.022780387 ## 8175 0.31088712 -0.072183964 ## 8188 0.18910831 0.284890372 ## 8193 2.23229153 0.199985849 ## 8202 -0.44615880 0.147165361 ## 8357 1.86555705 0.287651816 ## 8367 -3.54508505 -0.547153648 ## 8477 1.27665876 0.451828117 ## 8531 -0.41980136 0.084920429 ## 8627 -1.34686833 -0.190481570 ## 8628 2.98805172 0.026191000 ## 8707 0.06392637 0.290434028 ## 8775 -0.69319513 -0.231766623 ## 8800 -1.13102473 0.081969535 ## 8854 -2.46870162 -0.022108625 ## 8857 -0.49802276 -0.276362459 ## 8874 1.43408882 0.409535423 ## 8946 0.08158312 -0.157183639 ## 8983 0.22986995 -0.146663575 ## 9021 -1.48835604 -0.250807186 ## 9104 -0.24190126 -0.117495861 ## 9158 -0.76981531 0.380205875 ## 9198 1.52059694 0.324122338 ## 9225 0.95632896 0.256592821 ## 9292 0.59677065 -0.061206148 ## 9340 0.89627732 0.138811508 ## 9347 -0.35387089 0.068408853 ## 9359 -0.44505860 -0.506089679 ## 9397 -1.69198474 -0.171843624 ## 9508 0.20594353 0.207659960 ## 9550 -0.61065557 0.218085692 ## 9586 -0.90207583 -0.247445198 ## ## with conditional variances for "schoolid" ``` ] --- ## Plots: intercepto aleatorio ![:scale 60%](images/sjp_plot.jpeg) --- ## Plots: pendiente aleatoria ![:scale 60%](images/xy_plot.jpeg) --- ## Plots ![:scale 70%](images/ranef.jpeg) --- # Resumen predicción efectos aleatorios Usos - Pedagógico: para entender el sentido de la estimación con modelos mixtos (efectos fijos y aleatorios) - Diagnóstico: para analizar y visualizar la variación de unidades de nivel dos a nivel de intercepto y pendiente(s) - Informativo: para conocer los resultados de las unidades de nivel 2 y sus variaciones - Contraste de hipótesis de investigación --- class: roja right # Contenidos ## 1- Resumen sesión anterior ## .yellow[2- Interacción entre niveles] --- # Interacciones entre niveles: bases ¿Qué es una interacción en un modelo de regresión? .pull-left[ .content-box-red[ -> en términos de .red[especificación]: un término multiplicativo entre predictores (ej: edad x sexo) que se suma a la ecuación ] ] .pull-right[ .content-box-green[ -> en términos de .red[hipótesis]: test de una relación de .red[moderación]. ] ] --- # Ejemplo: - hipótesis 1: mujeres se identifican más con la izquierda que los hombres - hipótesis 2: a medida que aumenta la edad, aumenta la identificación con la derecha - hipótesis de interacción: el efecto de la edad en la identificación con la derecha es **moderado** por el sexo --- class: middle center ![:scale 100%](img/interaction.png) Si la interacción es significativa quiere decir que las **diferencias entre pendientes** son distintas de cero --- # Interacciones entre niveles en MLM .pull-left[ - Modelo multinivel con predictores individuales y contextuales .center[![:scale ](../images/mod4.png)] ] -- .pull-right[ - Modelo multinivel con interacción entre niveles .center[![:scale ](../images/mod5.png) .red[¿Existen cambios en la relación entre x e y en función de una variable Z?] ] ] --- # Interacciones entre niveles en MLM - la variable nivel 2 (Z) modera la relación entre x e y - se especifica multiplicando x*Z: .medium[ ```r reg_mlm <-lmer (y ~ 1 + x + Z # efectos principales + x*Z # interacción + (1 + x | cluster), # pendiente aleatoria data=datosmlm) ``` ] .center[ .red[Interpretación: por cada unidad de aumento en Z, la relación entre x e y se modifica en `\(\gamma_{11}\)` (coeficiente de interacción) ]] --- # Interacciones entre niveles y pendiente aleatoria - al .red[aleatorizar una pendiente] se puede evaluar mediante un test de devianza si la varianza de la pendiente es significativa o no -- - si hay evidencia de variación, .red[se procede con la interacción entre niveles] -- - si la .red[interacción es significativa], entonces parte de la varianza de la pendiente se asocia al predictor de nivel 2 -- - y por lo tanto, el efecto aleatorio asociado a la pendiente .red[disminuye significativamente] en el modelo con interacción --- class: inverse, middle, center # Interacciones entre niveles ## Ejemplo 1 HSB data ¿En qué medida el carácter público o privado de la escuela afectan la relación entre nivel socioeconómico y desempeño en matemáticas? --- ## Datos e inspección de variables ```r pacman::p_load(lme4,sjPlot,haven, texreg, summarytools) mlm = read_dta("http://www.stata-press.com/data/mlmus3/hsb.dta") ``` ### Variables principales: - y = mathach, puntaje en matemática - x = ses, nivel socioeconómico individual - Z = sector, tipo de escuela (pública - privada) --- - Selección de variables ```r mlm_int <- mlm %>% select(mathach, ses, sector, schoolid) %>% as.data.frame() ``` --- # Exploración inicial .small[ ```r sjmisc::descr(mlm_int,show = c("label","range", "mean", "sd", "NA.prc", "n")) %>% kable(.,digits =2,"markdown") ``` | |var |label | n| NA.prc| mean| sd|range | |:--|:--------|:--------|----:|------:|-------:|-------:|:-------------------| |1 |mathach |mathach | 7185| 0| 12.75| 6.88|27.83 (-2.83-24.99) | |4 |ses |ses | 7185| 0| 0.00| 0.78|6.45 (-3.76-2.69) | |3 |sector |sector | 7185| 0| 0.49| 0.50|1 (0-1) | |2 |schoolid |schoolid | 7185| 0| 5277.90| 2499.58|8362 (1224-9586) | ] --- # Descriptivos con gtsummary - vamos a probar la función .red[`tbl_summary`] de la librería .blue[`gtsummary`] para descriptivos .medium[ - ventajas: - output resumido - customizable - permite distinguir en una misma tabla entre variables categóricas y continuas - desventaja: - sintaxis algo críptica, requiere costumbre ... pero una vez que se establecen opciones básicas solo se reemplazan la(s) variable(s) ] --- # Descriptivos nivel 1 .medium[ .pull-left[ ```r pacman::p_load(gtsummary) mlm_int %>% select(mathach,ses) %>% tbl_summary(., statistic = list(all_continuous() ~ "{mean} ({sd}) [{min}, {max}]", all_categorical() ~ "{n} / {N} ({p}%)"), type = all_categorical() ~ "categorical", digits = all_continuous() ~ 1, ) %>% modify_header(label = "**Variable**") ``` ] ] .pull-right[ <br> <table style="NAborder-bottom: 0; font-size: 30px; margin-left: auto; margin-right: auto;" class="table"> <thead> <tr> <th style="text-align:left;"> Variable </th> <th style="text-align:center;"> N = 7,185 </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> mathach </td> <td style="text-align:center;"> 12.7 (6.9) [-2.8, 25.0] </td> </tr> <tr> <td style="text-align:left;"> ses </td> <td style="text-align:center;"> 0.0 (0.8) [-3.8, 2.7] </td> </tr> </tbody> <tfoot><tr><td style="padding: 0; " colspan="100%"> <sup>1</sup> Mean (SD) [Range]</td></tr></tfoot> </table> ] --- ## Descriptivos nivel 2 con tbl_summary (gtsummary) ```r mlm_int %>% group_by(schoolid) %>% summarise_all(funs(mean)) %>% select(sector) %>% sjlabelled::set_labels(sector,labels=c ("Publico"=0, "Privado"=1)) %>% sjlabelled::as_character(sector) %>% tbl_summary(., statistic = list(all_continuous() ~ "{mean} ({sd}) [{min}, {max}]", all_categorical() ~ "{n} ({p}%)"), type = all_categorical() ~ "categorical", digits = all_continuous() ~ 1, ) %>% modify_header(label = "**Variable**") ``` --- ## Descriptivos nivel 2 con tbl_summary (gtsummary) <br> <table style="NAborder-bottom: 0; font-size: 30px; margin-left: auto; margin-right: auto;" class="table"> <thead> <tr> <th style="text-align:left;"> Variable </th> <th style="text-align:center;"> N = 160 </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> sector </td> <td style="text-align:center;"> </td> </tr> <tr> <td style="text-align:left;padding-left: 2em;" indentlevel="1"> Privado </td> <td style="text-align:center;"> 70 (44%) </td> </tr> <tr> <td style="text-align:left;padding-left: 2em;" indentlevel="1"> Publico </td> <td style="text-align:center;"> 90 (56%) </td> </tr> </tbody> <tfoot><tr><td style="padding: 0; " colspan="100%"> <sup>1</sup> n (%)</td></tr></tfoot> </table> --- ## 1. Evaluar ajuste modelo pendiente aleatoria ```r # Modelo con pendiente fija reg_mlm_a = lmer(mathach ~ 1 + ses + sector + (1 | schoolid), data=mlm_int) # Modelo con pendiente aleatoria reg_mlm_b = lmer(mathach ~ 1 + ses + sector + (1 + ses | schoolid), data=mlm_int) ``` --- .small[ <table class="texreg" style="margin: 10px auto;border-collapse: collapse;border-spacing: 0px;caption-side: bottom;color: #000000;border-top: 2px solid #000000;"> <caption>Statistical models</caption> <thead> <tr> <th style="padding-left: 5px;padding-right: 5px;"> </th> <th style="padding-left: 5px;padding-right: 5px;">Model 1</th> <th style="padding-left: 5px;padding-right: 5px;">Model 2</th> </tr> </thead> <tbody> <tr style="border-top: 1px solid #000000;"> <td style="padding-left: 5px;padding-right: 5px;">(Intercept)</td> <td style="padding-left: 5px;padding-right: 5px;">11.72<sup>***</sup></td> <td style="padding-left: 5px;padding-right: 5px;">11.47<sup>***</sup></td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">(0.23)</td> <td style="padding-left: 5px;padding-right: 5px;">(0.23)</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">ses</td> <td style="padding-left: 5px;padding-right: 5px;">2.37<sup>***</sup></td> <td style="padding-left: 5px;padding-right: 5px;">2.39<sup>***</sup></td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">(0.11)</td> <td style="padding-left: 5px;padding-right: 5px;">(0.12)</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">sector</td> <td style="padding-left: 5px;padding-right: 5px;">2.10<sup>***</sup></td> <td style="padding-left: 5px;padding-right: 5px;">2.54<sup>***</sup></td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">(0.34)</td> <td style="padding-left: 5px;padding-right: 5px;">(0.34)</td> </tr> <tr style="border-top: 1px solid #000000;"> <td style="padding-left: 5px;padding-right: 5px;">AIC</td> <td style="padding-left: 5px;padding-right: 5px;">46621.15</td> <td style="padding-left: 5px;padding-right: 5px;">46615.85</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">BIC</td> <td style="padding-left: 5px;padding-right: 5px;">46655.55</td> <td style="padding-left: 5px;padding-right: 5px;">46664.01</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">Log Likelihood</td> <td style="padding-left: 5px;padding-right: 5px;">-23305.58</td> <td style="padding-left: 5px;padding-right: 5px;">-23300.93</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">Num. obs.</td> <td style="padding-left: 5px;padding-right: 5px;">7185</td> <td style="padding-left: 5px;padding-right: 5px;">7185</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">Num. groups: schoolid</td> <td style="padding-left: 5px;padding-right: 5px;">160</td> <td style="padding-left: 5px;padding-right: 5px;">160</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">Var: schoolid (Intercept)</td> <td style="padding-left: 5px;padding-right: 5px;">3.69</td> <td style="padding-left: 5px;padding-right: 5px;">3.96</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">Var: Residual</td> <td style="padding-left: 5px;padding-right: 5px;">37.04</td> <td style="padding-left: 5px;padding-right: 5px;">36.80</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">Var: schoolid ses</td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">0.43</td> </tr> <tr style="border-bottom: 2px solid #000000;"> <td style="padding-left: 5px;padding-right: 5px;">Cov: schoolid (Intercept) ses</td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">0.72</td> </tr> </tbody> <tfoot> <tr> <td style="font-size: 0.8em;" colspan="3"><sup>***</sup>p < 0.001; <sup>**</sup>p < 0.01; <sup>*</sup>p < 0.05</td> </tr> </tfoot> </table> ] --- ## Devianza .small[ ```r anova(reg_mlm_a,reg_mlm_b) ``` ``` ## Data: mlm_int ## Models: ## reg_mlm_a: mathach ~ 1 + ses + sector + (1 | schoolid) ## reg_mlm_b: mathach ~ 1 + ses + sector + (1 + ses | schoolid) ## npar AIC BIC logLik deviance Chisq Df Pr(>Chisq) ## reg_mlm_a 5 46616 46651 -23303 46606 ## reg_mlm_b 7 46611 46660 -23299 46597 9.0438 2 0.01087 * ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ``` ] -> Modelo con pendiente aleatoria es distinto al modelo sin pendiente aleatoria (p<0.05) --- # Estimación de modelo con interacción entre niveles ```r reg_mlm_c = lmer(mathach ~ 1 + ses + sector + ses*sector + (1 + ses | schoolid), data=mlm) ``` --- .pull-left-wide[ .small[ <table class="texreg" style="margin: 10px auto;border-collapse: collapse;border-spacing: 0px;caption-side: bottom;color: #000000;border-top: 2px solid #000000;"> <caption>Statistical models</caption> <thead> <tr> <th style="padding-left: 5px;padding-right: 5px;"> </th> <th style="padding-left: 5px;padding-right: 5px;">Model 1</th> <th style="padding-left: 5px;padding-right: 5px;">Model 2</th> <th style="padding-left: 5px;padding-right: 5px;">Model 3</th> </tr> </thead> <tbody> <tr style="border-top: 1px solid #000000;"> <td style="padding-left: 5px;padding-right: 5px;">(Intercept)</td> <td style="padding-left: 5px;padding-right: 5px;">11.72<sup>***</sup></td> <td style="padding-left: 5px;padding-right: 5px;">11.47<sup>***</sup></td> <td style="padding-left: 5px;padding-right: 5px;">11.75<sup>***</sup></td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">(0.23)</td> <td style="padding-left: 5px;padding-right: 5px;">(0.23)</td> <td style="padding-left: 5px;padding-right: 5px;">(0.23)</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">ses</td> <td style="padding-left: 5px;padding-right: 5px;">2.37<sup>***</sup></td> <td style="padding-left: 5px;padding-right: 5px;">2.39<sup>***</sup></td> <td style="padding-left: 5px;padding-right: 5px;">2.96<sup>***</sup></td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">(0.11)</td> <td style="padding-left: 5px;padding-right: 5px;">(0.12)</td> <td style="padding-left: 5px;padding-right: 5px;">(0.14)</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">sector</td> <td style="padding-left: 5px;padding-right: 5px;">2.10<sup>***</sup></td> <td style="padding-left: 5px;padding-right: 5px;">2.54<sup>***</sup></td> <td style="padding-left: 5px;padding-right: 5px;">2.13<sup>***</sup></td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">(0.34)</td> <td style="padding-left: 5px;padding-right: 5px;">(0.34)</td> <td style="padding-left: 5px;padding-right: 5px;">(0.35)</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">ses:sector</td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">-1.31<sup>***</sup></td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">(0.22)</td> </tr> <tr style="border-top: 1px solid #000000;"> <td style="padding-left: 5px;padding-right: 5px;">AIC</td> <td style="padding-left: 5px;padding-right: 5px;">46621.15</td> <td style="padding-left: 5px;padding-right: 5px;">46615.85</td> <td style="padding-left: 5px;padding-right: 5px;">46585.11</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">BIC</td> <td style="padding-left: 5px;padding-right: 5px;">46655.55</td> <td style="padding-left: 5px;padding-right: 5px;">46664.01</td> <td style="padding-left: 5px;padding-right: 5px;">46640.15</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">Log Likelihood</td> <td style="padding-left: 5px;padding-right: 5px;">-23305.58</td> <td style="padding-left: 5px;padding-right: 5px;">-23300.93</td> <td style="padding-left: 5px;padding-right: 5px;">-23284.56</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">Num. obs.</td> <td style="padding-left: 5px;padding-right: 5px;">7185</td> <td style="padding-left: 5px;padding-right: 5px;">7185</td> <td style="padding-left: 5px;padding-right: 5px;">7185</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">Num. groups: schoolid</td> <td style="padding-left: 5px;padding-right: 5px;">160</td> <td style="padding-left: 5px;padding-right: 5px;">160</td> <td style="padding-left: 5px;padding-right: 5px;">160</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">Var: schoolid (Intercept)</td> <td style="padding-left: 5px;padding-right: 5px;">3.69</td> <td style="padding-left: 5px;padding-right: 5px;">3.96</td> <td style="padding-left: 5px;padding-right: 5px;">3.82</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">Var: Residual</td> <td style="padding-left: 5px;padding-right: 5px;">37.04</td> <td style="padding-left: 5px;padding-right: 5px;">36.80</td> <td style="padding-left: 5px;padding-right: 5px;">36.79</td> </tr> <tr> <td style="padding-left: 5px;padding-right: 5px;">Var: schoolid ses</td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">0.43</td> <td style="padding-left: 5px;padding-right: 5px;">0.08</td> </tr> <tr style="border-bottom: 2px solid #000000;"> <td style="padding-left: 5px;padding-right: 5px;">Cov: schoolid (Intercept) ses</td> <td style="padding-left: 5px;padding-right: 5px;"> </td> <td style="padding-left: 5px;padding-right: 5px;">0.72</td> <td style="padding-left: 5px;padding-right: 5px;">0.54</td> </tr> </tbody> <tfoot> <tr> <td style="font-size: 0.8em;" colspan="4"><sup>***</sup>p < 0.001; <sup>**</sup>p < 0.01; <sup>*</sup>p < 0.05</td> </tr> </tfoot> </table> ] ] .pull-right-narrow[ <br><br><br> Por cada unidad de aumento en sector, el efecto de ses en mathach cambia en -1.31 Como sector=1 (dummy), entonces la pendiente de ses para sector=1 es 2.13-1.31=0.82 ] --- .pull-left-narrow[ ```r plot_model( reg_mlm_c, type = "int") ``` ] .pull-right-wide[ ![](08-interactions_files/figure-html/unnamed-chunk-23-1.png)<!-- --> ] --- class: inverse ## ¿Qué significa este resultado? - recordemos que la interacción es una diferencia de **pendientes** - la pendiente de ses en mathach es distinta (de cero) segun sector - recordemos que privado=1, y la interacción es negativa. Es decir, el sector correspondiente a 1 (privado) tiene una pendiente mas negativa (o menos positiva) que el sector 0 (público) --- class: inverse ## ¿Qué significa este resultado? Entonces ... - la relación entre el nivel socioeconómico de los estudiantes y rendimiento en matemáticas es menor en colegios privados (1) - efecto "ecualizador" de escuelas privadas? --- class: inverse ## Resumen - la interacción entre niveles en modelos multinivel es una extensión de interacciones en modelos de regresión OLS - en el caso mutinivel, la interacción entre niveles es la que especifica entre una variable de nivel 1 y una de nivel 2 - en interacción entre niveles, la variable de nivel 2 busca dar cuenta de la variabilidad de la pendiente de la variable de nivel 1 --- class: front .pull-left-wide[ # Modelos multinivel] .pull-right-narrow[![:scale 85%](img/logo.png)] ## Unidades en contexto ---- .pull-left[ ## Juan Carlos Castillo ## Sociología FACSO - UChile ## 2do Sem 2023 ## [.yellow[multinivel-facso.netlify.com]](https://multinivel-facso.netlify.com) ] .pull-right-narrow[ .center[ ] ]