I've been using the "adjustrcspline" and "mfxrcspline" packages to model non-linear associations (activity and telomeres). The packages are helpful, but I am finding some aspects a bit clunky. The main thing I am struggling with is removing the empty space in the final plots for adjustrcspline.
It doesn't seem to be possible to limit x and y axis ranges despite using xscale and yscale and other tools usually used for plotting. I want to focus on trim and focus certain portions of the spline plot, but I don't want to have to drop data in order to do that or the spline is calculated differently. Even if I remove the scatter plot, I am still left with empty space.
I've attached the output of the below code for context so you can see what I mean about the extra space. I would ideally like to be able to plot this spline and focus on the areas of interest (i.e. as currently plotted and trimmed for spline) with useful restricted axis. Even if I remove the scatter and set the ranges for y-axis (e.g. yscale(r(-1 1)) xscale(r(0 50)) ), I still have extra space (i.e. see 2nd and 3rd images).
Perhaps there are even better ways to model these regression splines now with flexibility (i.e. to be able to pull out the data from the plot and plot using a twoway)?
Thanks in advance!
Code:
clear all use "$data\analysis.dta", clear // MODELS set more off global cov1 global cov2 age sex global cov3 $cov2 education employment smoking global cov4 $cov3 bmi global xvars enmo_accel_ad intensity //activity volume and intensity global yvars telomere local model = "M" foreach x of global xvars{ foreach y of global yvars{ forvalues i = 1/4{ //foreach model of global covars{ su `x', d return list egen p2_5 = pctile(`x'), p(2.5) egen p97_5 = pctile(`x'), p(97.5) //Plots mkspline2 rc = `x', cubic nknots(4) displayknots regress `y' rc* ${cov`i'} adjustrcspline if (`x' >p2_5 & `x' <p97_5) , name(a) addplot(scatter `y' `x' , below msymbol(oh) msize(small) mcolor(%40)) scheme(plottigblind) /// //Predicted outcome against spline variable title(Adjusted predictions) subtitle("n=`e(N)'; `x'; model=$cov`model'`i'", size(small) ring(0) pos(11)) /*CAN'T SEEM TO LIMIT THE X and Y AXIS IN PLOT - EVEN IF REMOVE addplot(scatter) - e.g. yscale(r(-1 1)) xscale(r(0 50)) */ mfxrcspline , yline(0) name(b) title(Marginal effects) scheme(plottigblind) /// //Change in predicted outcome for a unit change in exposure graph combine a b, ysize(3) graph export "$results/Prelim_Spline_plots/`x'_`y'_$cov`model'`i'.png", replace drop rc* p2_5 p97_5 graph drop _all } } }
Array
Array
0 Response to adjustrcspline and plotting axis/limits
Post a Comment