Using “Margins” Package to get the results from a “PLM” random effects model

good day everyone!

I am using PLM Package to run 6 mixed models (random effects). If you would compare, I am using the xtreg command from STATA.

Besides getting two way different R2 (STATA I get ~.28 and R I am getting ~.57), in Stata I can run the margins command and plot the results, which is very handy. I have installed the package "Margins" in R and I could not proceed with the analysis. Could I get any help please? Thank you!

Hi jasonford,
The simple answer is that the plm model output object is not supported by the margins::cplot() function. Check out the examples for both packages.

Also note that R is case-sensitive: PLM != plm, and Margins != margins.
HTH

Dear DavoWW, good morning, how are you?

Thank you so much for your answer and time.

Yeah, I saw that, but is there any way that I could replicate the margins within mixed methods random from STATA in R?

Thank you again.

No idea, but I suggest you contact the margins package authors for possible options - see the DESCRIPTION file for their email addresses.
HTH

Dear DavoWW, good morning, how are you?
This was the first thing that I did. He suggested me to come here and ask.

Hi Jason,
I'm afraid I dont have any STATA background, so it may be that things that seem obvious I just wont get. I'm just thinking that the xtreg command seems to support 6 different model types, and the PLM also supports many. Can we be specific about the models you need ? I just glanced at the margins package demo page, and they show examples of fitting all kinds of models (without PLM) and plotting the marginal info you seem to need, maybe there is a better analog the PLM something that is closer to one of their examples ?

Sorry if its not helpful, just a suggestion

Dear Nirgrahamuk, good morning, how are you?

Let me share my code from Stata and from R. The code that I am running on STATA, after running the mixed models random effect, I can get the margins and plot it. It is perfect. In R, the model runs but I cant get the margins.

STATA:
xtset IndividualCode Year
xtreg DEPEDENTVARIABLEY  IndependentvariableX1 IndependentvariableX2 c.IndependentvariableX1#c.IndependentvariableX1 c.IndependentvariableX2#c.IndependentvariableX2 (c.IndependentvariableX1 c.IndependentvariableX2)##i.Moderator1 (c.IndependentvariableX1#c.IndependentvariableX1 c.IndependentvariableX2#c.IndependentvariableX2)##i.Moderator1, vce(robust) re
margins Moderator1, at(IndependentvariableX1=(0(1)15))
marginsplot
margins r.Moderator1, at(IndependentvariableX1=(0(1)15))
marginsplot
margins Moderator1, at(IndependentvariableX2=(0(0.5)10))
marginsplot
margins r.Moderator1, at(IndependentvariableX2=(0(0.5)10))
marginsplot


R:
library(plm)
random6 <- plm(log(DEPEDENTVARIABLEY+1) ~ log(IndependentvariableX1+1) + 
I(log(IndependentvariableX1+1)^2) + log(IndependentvariableX2+1) + 
I(log(IndependentvariableX2+1)^2) + I((log(IndependentvariableX1+1))*Moderator1) + 
I((log(IndependentvariableX1+1)^2)*Moderator1) + 
I((log(IndependentvariableX2+1))*Moderator1) + 
I((log(IndependentvariableX2+1)^2)*Moderator1) + Moderator1, data=Panel2, 
index=c("IndividualCode", "Year"), model="random")

so from STATA using xtreg you were doing 'GLS random-effects (RE) model' right ?

I'm seeing this page teaching about such models recommends use of lmer()
https://rcompanion.org/handbook/G_03.html
I'm also seeing hints that margins has been updated to support output of lmer
(https://github.com/leeper/margins/issues/56#issuecomment-388339065)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.