I am so grateful for the time you are spending on explaining stata. Your videos are more useful than any formal education I've received on this subject matter. Keep on good work!
Your videos are great, you explain in a very efficient way, at least from my perspective. I am fluent in Stata and keep learning things from your videos! Keep up!
Thank you! They are clearly different. I like the official Stat videos because they are short but cover a lot of ground. However, I don't like their menu driven approach since this is not how most of us actually program. But, it is a great way to learn about how commands and options work together.
Hi Alan, your video is GREAT! Thank you so much. I want to ask you if you know if I can use the margins estimations and plots using the command "xtscc" with fixed effects. When I try to use it, I got the error: "factor variables may not contain noninteger values". Thank you in advance.
These are great demonstration videos, thanks. I have one quick question. i would like to fit an xtreg model on a subset of observations and then predict the outcome for all the observations. I know this is achievable with -predict but is there a way to use with margins and/or marginsplot?
+Cathy Morgan Yes, this is possible. the margins command operates on all cases that are non-missing on the independent variables, allowing the calculation of out of sample marginal effects. The margins post-estimation command allows the optional use of the if and in command modifiers. So you could do the following, assuming you have cases: margins if !e(sample) Or if you wanted in-sample margins only you could: margins if e(sample) Best, Alan
Hi Nimra, I do not have a video on margins and marginsplot after estimating logistic or logit models. But, everything works pretty much the same way as after estimating a regression model except for two differences. Actually, one difference and one complication. The difference is that the margins produce predicted probabilities. The complication is if you prefer to produce odds ratios. Regardless, you can play around with the Stata code below to show you how you can begin producing visualizations of your limited dependent variable models. Best, Alan /* Load some data to use */ sysuse auto, clear /* Recode the rep79 variable into two outcome groups */ capture drop rep78_2cat recode rep78 (1/3=1) (4/5=2), generate(rep78_2cat) /* Estimate a model */ logistic foreign c.mpg##c.mpg i.rep78_2cat /* Margins showing predicted probabilities by various levels of mpg */ margins, at(mpg=(10(2)40)) marginsplot, recast(line) noci plotopts(lw(thick)) name(p1, replace) /* Add repair variable to the mix */ margins rep78_2cat, at(mpg=(10(2)40)) marginsplot, recast(line) noci plotopts(lw(thick)) name(p2, replace) /* Convert probabilities to odds ratios */ margins rep78_2cat, expression(exp(predict(xb))) marginsplot, yline(1, lw(thick) lp(dash) lc(cranberry)) /// plotopts(connect(none)) xlabel(.5 " " 1 2 2.5 " ") name(p3, replace) margins rep78_2cat, at(mpg=(10(5)40)) expression(exp(predict(xb))) marginsplot, yline(1, lw(thick) lp(dash) lc(cranberry)) /// plotopts(connect(none)) name(p4, replace)
Juciara, can you please replicate the problem with a Stata-supplied dataset? From the command window type "help dta_examples" followed by the "Enter" key. Maybe the auto.dta dataset ("sysuse auto, clear"). With data and analogous Stata code, I may be able to determine what is going on.
+Marvin Aliaga Yes, I would like to do that. The big difference in OLS and logistic regression margins is that the estimated values from logistic regression (and other limited dependent variable models) are by default expressed as probabilities.
+Alan Neustadtl for sure it's a good one! I am struggling to build a discrete choice model for 2011 elections in Tunisia, and your tutorials eased the pain ;)
luca, this is difficult to answer because unless your independent variables are standardized, it is difficult to plot thing s with very different metrics. But, it may be possible to 1) use standardized measures, and 2) use the undocumented "saving" option to margins. You can get more help by typing "help undocumented" in the command window (yes, there is documentation to some undocumented commands and options). Basically, you can 1) created standardized measures, 2) regress your DV on the IV's, 3) generate margins foreach variables and save them to a file, 5) merge the two sets of margins, and 6) plot them. Here is a quick example that might give you some clues about your particular situation: sysuse auto, clear egen zweight=std(weight) egen zlength=std(length) regress mpg zweight zlength margins, at(zweight=(-3(.25)3)) saving(margins1, replace) marginsplot, recast(line) noci name(g1, replace) margins, at(zlength=(-3(.25)3)) saving(margins2, replace) marginsplot, recast(line) noci name(g2, replace) use _at _margin using margins1, clear rename _margin weight merge 1:1 _n using margins2 rename _margin length keep _at weight length graph twoway (line weight _at) /// (line length _at)
Thank you so much, it was really helpful. I managed to combine two marginsplots, however I have a further question: how can I merge three graphs? Thank you again for your precious help
luca, there is a difference, I think, between "merging" and "combining" graphs. The code I showed you placed two plots in one graph. To add a third plot you would just extend the code I provided to accommodate a third variable. Here is an example of combining three graphs: sysuse auto, clear hist mpg, name(g1) tw(scatter mpg weight) (lfit mpg weight), name(g2) kdensity weight, name(g3) graph combine g1 g2 g3
Yes sorry, I meant merging before. However, since I am new to Stata could you please show me how to merge a possible third graph in the example above? Thank you so much I really appreciate your help
I am so grateful for the time you are spending on explaining stata. Your videos are more useful than any formal education I've received on this subject matter. Keep on good work!
Just seen this video, much better than any official Stata video. Please keep going!
Your videos are great, you explain in a very efficient way, at least from my perspective. I am fluent in Stata and keep learning things from your videos! Keep up!
+anidemolimacnauj Thank you! I am glad you have found these videos helpful.
Alan your videos are great. I'd dare to say, better than the official stata tutorials.
Thank you! They are clearly different. I like the official Stat videos because they are short but cover a lot of ground. However, I don't like their menu driven approach since this is not how most of us actually program. But, it is a great way to learn about how commands and options work together.
As a reminder, the official Stata videos can be found at ua-cam.com/users/statacorp
One of the best Stata tutors. Great stuff. Thanks.
Thank you , this video and patr 1 were super instructive and helpful ! Now I understand the powere of marginplot
I found this just when I needed to use the command. Thanks.
thanks, you are a great teacher.
Your videos are very very helpful! Thanks a lot
This is great
Thanks alot
thank you very much, this one and pt.1 are very helpful :)
Hi Alan, your video is GREAT! Thank you so much. I want to ask you if you know if I can use the margins estimations and plots using the command "xtscc" with fixed effects. When I try to use it, I got the error: "factor variables may not contain noninteger values".
Thank you in advance.
Thanks so much this is of great help!
Thank you very much. Where I get the link to downloap your do-file. I'm very interest. Thank in advance.
Hi Soro, I do not have Web space to store the do-file. But, they can easily be duplicated form the videos.
These are great demonstration videos, thanks. I have one quick question. i would like to fit an xtreg model on a subset of observations and then predict the outcome for all the observations. I know this is achievable with -predict but is there a way to use with margins and/or marginsplot?
+Cathy Morgan Yes, this is possible. the margins command operates on all cases that are non-missing on the independent variables, allowing the calculation of out of sample marginal effects. The margins post-estimation command allows the optional use of the if and in command modifiers. So you could do the following, assuming you have cases:
margins if !e(sample)
Or if you wanted in-sample margins only you could:
margins if e(sample)
Best,
Alan
You are awesome!
I'm glad you found this video useful.
Sir do you have any video on marginplot in logistic regression
Hi Nimra, I do not have a video on margins and marginsplot after estimating logistic or logit models. But, everything works pretty much the same way as after estimating a regression model except for two differences. Actually, one difference and one complication. The difference is that the margins produce predicted probabilities. The complication is if you prefer to produce odds ratios. Regardless, you can play around with the Stata code below to show you how you can begin producing visualizations of your limited dependent variable models.
Best,
Alan
/* Load some data to use */
sysuse auto, clear
/* Recode the rep79 variable into two outcome groups */
capture drop rep78_2cat
recode rep78 (1/3=1) (4/5=2), generate(rep78_2cat)
/* Estimate a model */
logistic foreign c.mpg##c.mpg i.rep78_2cat
/* Margins showing predicted probabilities by various levels of mpg */
margins, at(mpg=(10(2)40))
marginsplot, recast(line) noci plotopts(lw(thick)) name(p1, replace)
/* Add repair variable to the mix */
margins rep78_2cat, at(mpg=(10(2)40))
marginsplot, recast(line) noci plotopts(lw(thick)) name(p2, replace)
/* Convert probabilities to odds ratios */
margins rep78_2cat, expression(exp(predict(xb)))
marginsplot, yline(1, lw(thick) lp(dash) lc(cranberry)) ///
plotopts(connect(none)) xlabel(.5 " " 1 2 2.5 " ") name(p3, replace)
margins rep78_2cat, at(mpg=(10(5)40)) expression(exp(predict(xb)))
marginsplot, yline(1, lw(thick) lp(dash) lc(cranberry)) ///
plotopts(connect(none)) name(p4, replace)
How can I deal woth an error "not sorted" in estimating margins and marginsplot?
Juciara, can you please replicate the problem with a Stata-supplied dataset? From the command window type "help dta_examples" followed by the "Enter" key. Maybe the auto.dta dataset ("sysuse auto, clear"). With data and analogous Stata code, I may be able to determine what is going on.
Do you have any video for marginsplot in xtdpd moldels?
I do not.
When you create the marginsplot for educations and father education, the other variables (age and race) are held constant? At their mean?
+Marvin Aliaga Yes, the mean, but you can set the values any way you want.
+Alan Neustadtl Hi Alan, are you considering doing post estimations tutorials for logistic regression?
+Marvin Aliaga Yes, I would like to do that. The big difference in OLS and logistic regression margins is that the estimated values from logistic regression (and other limited dependent variable models) are by default expressed as probabilities.
Yes! And that the probabilities does not have a linear function (s shape). That is why it is more confusing for me.
Correct, the probabilities tend to be linear near the middle of the distribution but depart from normality at both the bottom and top ends.
wow!
+Valeria Resta Hopefully, a good wow. Best, Alan
+Alan Neustadtl for sure it's a good one! I am struggling to build a discrete choice model for 2011 elections in Tunisia, and your tutorials eased the pain ;)
how can I combine two marginsplots in the same graph?
luca, this is difficult to answer because unless your independent variables are standardized, it is difficult to plot thing s with very different metrics. But, it may be possible to 1) use standardized measures, and 2) use the undocumented "saving" option to margins. You can get more help by typing "help undocumented" in the command window (yes, there is documentation to some undocumented commands and options). Basically, you can 1) created standardized measures, 2) regress your DV on the IV's, 3) generate margins foreach variables and save them to a file, 5) merge the two sets of margins, and 6) plot them. Here is a quick example that might give you some clues about your particular situation:
sysuse auto, clear
egen zweight=std(weight)
egen zlength=std(length)
regress mpg zweight zlength
margins, at(zweight=(-3(.25)3)) saving(margins1, replace)
marginsplot, recast(line) noci name(g1, replace)
margins, at(zlength=(-3(.25)3)) saving(margins2, replace)
marginsplot, recast(line) noci name(g2, replace)
use _at _margin using margins1, clear
rename _margin weight
merge 1:1 _n using margins2
rename _margin length
keep _at weight length
graph twoway (line weight _at) ///
(line length _at)
Thank you so much, it was really helpful. I managed to combine two marginsplots, however I have a further question: how can I merge three graphs? Thank you again for your precious help
luca, there is a difference, I think, between "merging" and "combining" graphs. The code I showed you placed two plots in one graph. To add a third plot you would just extend the code I provided to accommodate a third variable. Here is an example of combining three graphs:
sysuse auto, clear
hist mpg, name(g1)
tw(scatter mpg weight) (lfit mpg weight), name(g2)
kdensity weight, name(g3)
graph combine g1 g2 g3
Yes sorry, I meant merging before. However, since I am new to Stata could you please show me how to merge a possible third graph in the example above?
Thank you so much I really appreciate your help
Here is an extension to a third variable in a 3-IV variable regression model:
sysuse auto, clear
egen zweight=std(weight)
egen zlength=std(length)
egen zdisplacement=std(displacement)
regress mpg zweight zlength zdisplacement
margins, at( zweight=(-3(.25)3)) saving(margins1, replace)
margins, at( zlength=(-3(.25)3)) saving(margins2, replace)
margins, at(zdisplacement=(-3(.25)3)) saving(margins3, replace)
use _at _margin using margins1, clear
rename _margin weight
merge 1:1 _n using margins2
rename _margin length
keep _at weight length
merge 1:1 _n using margins3
rename _margin displacement
keep _at weight length displacement
tw (line weight length displacement _at)