Hello,
is there a work around for using factor variables for interactions with margins after using xtmixed?
I am estimating a hierarchical model with xtmixed and group-mean center and demean all time-varying variables. Than I include both terms jointly in the model, so I can differentiate the 'between' from 'within' effects. Finally, I am estimating a couple of interactions, using the 'between' terms but also the 'within' terms.
For example, I have two time-varying continuous variables var1 and var2. Separating the within and the between terms leads to four variables and two interaction terms. The problem is that I cannot use the factor variables for the interactions since they miscalculate the interaction terms:
xtmixed y c.var1_mean##c.var2_mean c.var1_within##c.var2_within || country:
Here, I create two new interaction terms and use only the 'within' terms or the 'between' terms for the generation of the interaction effects. Unfortunately, this is not the correct way of doing it. I have to create the interaction variable before creating the group-mean centered and demeaned variables, and then group-mean center and demean that interaction term itself and include both terms into the model.
Two things would help me:
1.) Is there a way of specifying interactions in margins other than by relying on factor variables? I know there is a work around for transformed variables, say age and age1.5. Is there something similar for interactions?
2.) How could I plot the marginal effects of continuous variables after xtmixed without relying on margins? Some code would be appreciated.
Following is an example where I only estimate the "within" terms with xtmixed and contrast it with xtreg , fe since they both should provide almost equivalent results (all differences are due to the ML-estimators of xtmixed).
is there a work around for using factor variables for interactions with margins after using xtmixed?
I am estimating a hierarchical model with xtmixed and group-mean center and demean all time-varying variables. Than I include both terms jointly in the model, so I can differentiate the 'between' from 'within' effects. Finally, I am estimating a couple of interactions, using the 'between' terms but also the 'within' terms.
For example, I have two time-varying continuous variables var1 and var2. Separating the within and the between terms leads to four variables and two interaction terms. The problem is that I cannot use the factor variables for the interactions since they miscalculate the interaction terms:
xtmixed y c.var1_mean##c.var2_mean c.var1_within##c.var2_within || country:
Here, I create two new interaction terms and use only the 'within' terms or the 'between' terms for the generation of the interaction effects. Unfortunately, this is not the correct way of doing it. I have to create the interaction variable before creating the group-mean centered and demeaned variables, and then group-mean center and demean that interaction term itself and include both terms into the model.
Two things would help me:
1.) Is there a way of specifying interactions in margins other than by relying on factor variables? I know there is a work around for transformed variables, say age and age1.5. Is there something similar for interactions?
2.) How could I plot the marginal effects of continuous variables after xtmixed without relying on margins? Some code would be appreciated.
Following is an example where I only estimate the "within" terms with xtmixed and contrast it with xtreg , fe since they both should provide almost equivalent results (all differences are due to the ML-estimators of xtmixed).
HTML Code:
. xtreg DV c.var1##c.var2, fe // Xtreg gets the interaction right Fixed-effects (within) regression Number of obs = 820 Group variable: ccode Number of groups = 59 R-sq: within = 0.1696 Obs per group: min = 4 between = 0.4895 avg = 13.9 overall = 0.4106 max = 23 F(3,758) = 51.60 corr(u_i, Xb) = 0.3716 Prob > F = 0.0000 ------------------------------------------------------------------------------- DV | Coef. Std. Err. t P>|t| [95% Conf. Interval] --------------+---------------------------------------------------------------- var1 | -.6884199 .3839652 -1.79 0.073 -1.442181 .0653416 var2 | -.2343191 .194814 -1.20 0.229 -.6167581 .1481199 | c.var1#c.var2 | .0133886 .0039325 3.40 0.001 .0056686 .0211085 | _cons | 44.74163 19.18391 2.33 0.020 7.081734 82.40153 --------------+---------------------------------------------------------------- sigma_u | 14.244203 sigma_e | 9.0203402 rho | .71376406 (fraction of variance due to u_i) ------------------------------------------------------------------------------- F test that all u_i=0: F(58, 758) = 22.62 Prob > F = 0.0000 . . gen interaction = var1 * var2 // generate the interaction term (104 missing values generated) . egen interaction_mean = mean(interaction), by(country) // generate the group-mean centered 'between' term of the interaction (32 missing values generated) . gen interaction_w = interaction - interaction_mean // generate the de-meaned 'within' term of the interaction (104 missing values generated) . egen var1_mean = mean(var1), by(country) // generate the group-mean centered 'between' term of var1 (32 missing values generated) . gen var1_w = var1 - var1_mean // generate the de-meaned 'within' term of var2 (104 missing values generated) . egen var2_mean = mean(var2), by(country) // generate the group-mean centered 'between' term of var1 . gen var2_w = var2 - var2_mean // generate the de-meaned 'within' term of var2 . . . xtmixed DV var1_w var2_w interaction_w || country: // Calculating and entering the 'within' term seperatly provides similar results to xtreg (all differences are due to the ML estim > ation) Performing EM optimization: Performing gradient-based optimization: Iteration 0: log likelihood = -3081.1327 Iteration 1: log likelihood = -3081.1327 Computing standard errors: Mixed-effects ML regression Number of obs = 820 Group variable: country Number of groups = 59 Obs per group: min = 4 avg = 13.9 max = 23 Wald chi2(3) = 155.04 Log likelihood = -3081.1327 Prob > chi2 = 0.0000 ------------------------------------------------------------------------------- DV | Coef. Std. Err. z P>|z| [95% Conf. Interval] --------------+---------------------------------------------------------------- var1_w | -.6414611 .3821777 -1.68 0.093 -1.390516 .1075934 var2_w | -.2061704 .1936854 -1.06 0.287 -.5857867 .173446 interaction_w | .0128301 .0039103 3.28 0.001 .0051661 .0204941 _cons | 50.24374 2.338986 21.48 0.000 45.65941 54.82807 ------------------------------------------------------------------------------- ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ country: Identity | sd(_cons) | 17.7541 1.679949 14.74875 21.37185 -----------------------------+------------------------------------------------ sd(Residual) | 9.004228 .2308852 8.562884 9.468319 ------------------------------------------------------------------------------ LR test vs. linear regression: chibar2(01) = 988.33 Prob >= chibar2 = 0.0000 . . xtmixed DV c.var1_w##c.var2_w || country: // Using factor variable notation instead creates an incorrect Interaction term. Performing EM optimization: Performing gradient-based optimization: Iteration 0: log likelihood = -3086.2154 Iteration 1: log likelihood = -3086.2154 Computing standard errors: Mixed-effects ML regression Number of obs = 820 Group variable: country Number of groups = 59 Obs per group: min = 4 avg = 13.9 max = 23 Wald chi2(3) = 142.71 Log likelihood = -3086.2154 Prob > chi2 = 0.0000 ----------------------------------------------------------------------------------- DV | Coef. Std. Err. z P>|z| [95% Conf. Interval] ------------------+---------------------------------------------------------------- var1_w | .4555315 .1936883 2.35 0.019 .0759093 .8351537 var2_w | .4163586 .036279 11.48 0.000 .3452532 .4874641 | c.var1_w#c.var2_w | .0101805 .0143113 0.71 0.477 -.0178691 .0382301 | _cons | 50.00698 2.320443 21.55 0.000 45.45899 54.55496 ----------------------------------------------------------------------------------- ------------------------------------------------------------------------------ Random-effects Parameters | Estimate Std. Err. [95% Conf. Interval] -----------------------------+------------------------------------------------ country: Identity | sd(_cons) | 17.61508 1.668055 14.63122 21.20748 -----------------------------+------------------------------------------------ sd(Residual) | 9.069851 .2325692 8.625289 9.537327 ------------------------------------------------------------------------------ LR test vs. linear regression: chibar2(01) = 985.06 Prob >= chibar2 = 0.0000
Comment