tag:blogger.com,1999:blog-3534970434503513102.post3958947167446880721..comments2023-09-21T04:52:34.034-04:00Comments on Fight Entropy: Plotting restricted cubic splines in Stata [with controls]solhttp://www.blogger.com/profile/00936469103707728475noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-3534970434503513102.post-4934228992852171082013-05-07T15:59:27.440-04:002013-05-07T15:59:27.440-04:00Alright, a few other folks emailed about plots tha...Alright, a few other folks emailed about plots that dropped the constant term in a bivariate model, so I've updated the code to have an option "keepcons". This option will keep the constant term when computing the spline, which is appropriate only in models where there are no control variables. So if you are plotting the relationship between X and Y, and you want it look similar to what you would get if you typed "lowess Y X" then you should type<br /><br />plot_rcspline Y X, keepcons<br /><br />and you should recover a curve that looks more familiar. If you include controls Z but type<br /><br />plot_rcspline Y X Z, keepcons<br /><br />then you will keep the constant but drop the average effect of Z when you plot the spline. This can be interpreted as the prediction of the model when all covariates are set to zero (note that this doesn't make too much sense in many models, so I would stick to using the "keepcons" option only in bivariate models).solhttps://www.blogger.com/profile/00936469103707728475noreply@blogger.comtag:blogger.com,1999:blog-3534970434503513102.post-18976035611650416972013-03-18T14:37:48.220-04:002013-03-18T14:37:48.220-04:00Thanks for this. When there are no control variabl...Thanks for this. When there are no control variables, it's possible just to use -predict- directly to get predictions in terms of the response. I'd guess other users than myself might expect that to be the default with no control variables, but as you imply that clashes with your broader purpose in getting marginal effects. That in turn raises the question of getting Stata's -marginsplot- to do your work for you. Nick Coxhttps://www.blogger.com/profile/00192879854970720964noreply@blogger.comtag:blogger.com,1999:blog-3534970434503513102.post-81784285128317983162013-03-18T11:52:58.130-04:002013-03-18T11:52:58.130-04:00Nick: thanks for pointing out rcspline (and writin...Nick: thanks for pointing out rcspline (and writing it!). It looks really nice.<br /><br />One day, I will learn how to write proper help files for Stata... but until then, I apologize that getting the details on my functions is a little unwieldy.<br /><br />In your test, the predictions for mpg are all negative because plot_rcspline does not account for the constant term. The reason is that when there are multiple additive constants, I was only interested in the marginal effect of the variable of interest, all other non-weight variables are not included in the prediction. If you center the variables first (so the constant is zero), you see that the scatter is identical to the spline:<br /><br />sysuse auto, clear <br />center mpg weight<br />sc c_mpg c_weight <br />plot_rcspline c_mpg c_weight <br /><br />In the next version, I'll make including the constant an option. Thanks for pointing this out.solhttps://www.blogger.com/profile/00936469103707728475noreply@blogger.comtag:blogger.com,1999:blog-3534970434503513102.post-67564963988119220152013-03-17T12:48:21.868-04:002013-03-17T12:48:21.868-04:00Solomon: I agree with your advocacy of restricted ...Solomon: I agree with your advocacy of restricted cubic splines as a good scatter plot smoother. <br /><br />You are correct that there is no official command to do this in Stata, if that's what you mean by "standard". As far as user-written commands are concerned, -rcspline- has been downloadable from SSC since 2007. It doesn't have quite the same objectives as your program: yours goes further in supporting what you call control variables). -rcspline- always shows the original data for the variables plotted. <br /><br />In terms of your program, Stata users would typically expect a help file that would explain the syntax in a standard way, not documentation embedded in the code. Your implementation makes use of permanent variable names in a way that poses a small but not zero risk of messing up existing datasets and in a future revision could usefully support -in- as well as -if-. <br /><br />In a quick test of your program I went <br /><br />sysuse auto, clear <br />plot_rcspline mpg weight <br /><br />and I see predictions shown for -mpg- that are all negative. What did I do wrong? Nick Coxhttps://www.blogger.com/profile/00192879854970720964noreply@blogger.com