Dear Stata experts,

please help me to find a mistake in my code. It worked fine just 3 days ago but then stopped for no obvious for me reason (have probably gone through my code for at least 10 times in search for a failure... now you are my last hope to find the mistake).
And sorry for putting out here such a complex code, but it could be relevant to see the whole code as I am not sure what exactly is causing the issue...

FYI: I have updated Stata and installed asdocx package around the same time the issue has started to appear. Not sure though that this could be somehow connected...


About my dataset: 20 years, ca. 7 000 observations, ca. 800 companies.

I am using loops to run and compare different models to check for stable results + to do plots to visualize the moderating effects. Depending on the model option, the effect is between related_share_w (my key IV) and (a) category variable, (b) continuous variable, (c) 2 category variables, (d) a category and continuous variable.

Would be great if you can advise me on the following:
1. how to fix the code to get the plots for (a) and (b) work again?
2. how to write properly the code to plot 3-way effects of (c) and (d)?



Current code:
Code:
xtset gvkey fyear

local DVs "tobinq3_ln" // could be switched to several - to simplify for now just 1
local controls "c.emp_ln c.roa_w c.total_divers_w"
local EXTRAs "c.mint_w" // could be switched to several - to simplify for now just 1

local Model3 "i.firstmover_related_sic4 i.related_serv_entry c.related_share_w##c.related_share_w##c.related_invest_firm_w c.related_share_w##c.related_share_w##c.serv_share_w c.related_share_w##c.related_share_w##i.firstmover_related_years c.related_share_w##c.related_share_w##i.related_serv_entry_years c.related_share_w##c.related_share_w##c.related_speed_w c.related_share_w##c.related_share_w##c.rslack2_w c.related_share_w##c.related_share_w##c.advint_w c.related_share_w##c.related_share_w##c.mgrowth_w c.related_share_w##c.related_share_w##c.mdynamism1_w c.related_share_w##c.related_share_w##c.mmunificence_w"

local Model4 "i.firstmover_related_sic4 c.related_share_w#c.related_speed_w#c.related_invest_firm_w c.rslack2_w c.advint_w c.mgrowth_w c.mdynamism1_w c.mmunificence_w c.related_share_w##c.related_share_w##c.related_invest_firm_w c.related_share_w##c.related_share_w##c.serv_share_w c.related_share_w##c.related_share_w##i.firstmover_related_years c.related_share_w##c.related_share_w##i.related_serv_entry_years c.related_share_w##c.related_share_w##c.related_speed_w"

// here further models from Model1 till Model 13 are stated, Models 3 and 4 are provided as one of them as an example

local ModelN = 13

local cMEs "c.related_invest_firm_w c.serv_share_w c.related_speed_w c.rslack2_w c.advint_w c.mgrowth_w c.mdynamism1_w c.mmunificence_w"
local iMEs "firstmover_related_years related_serv_entry_years"

foreach DV of local DVs {
    foreach EXTRA of local EXTRAs {
    local Mymodel "FEr-yd-6"
    forv i = 1/`ModelN' {
 
    xtreg `DV' `Model`i'' `controls' `EXTRA' i.fyear, fe vce (robust) // thats were the regression is defined
    outreg2 using "Regression_results", append excel dec(3) label(insert) ctitle(`DV', `EXTRA', `Mymodel', Model `i') drop(i.fyear) sideway noparen addtext(Company FE, YES, Year FE, YES) e(all) // up till here everything works well...
    
***** Creating margin plots for the continious MEs (above point (b)), e.g. for c.related_share_w##c.related_share_w##c.related_invest_firm_w - this worked perfectly just 3 days ago and suddenly started to produce empty graphs instead (the axes, legend are there - just the graphs themselves are missing!)
        summarize related_share_w
        local MyMin = max((r(mean) - r(sd)),r(min))
        local MyMax = min((r(mean) + r(sd)),r(max))
        local Grid = (`MyMax'-`MyMin')/10
    
        foreach cME of local cMEs {
        quietly summarize `cME'
        local MyMin2 = max((r(mean) - r(sd)),r(min))
        local MyMax2 = min((r(mean) + r(sd)),r(max))
        local Grid2 = (`MyMax2'-`MyMin2')/2
        
        // creating first margin plot - Modearting effect
        capture quietly margins, at(c.related_share_w=(`MyMin' (`Grid') `MyMax') `cME'=(`MyMin2' (`Grid2') `MyMax2'))
        capture marginsplot, recast(line) noci title("Moderating effect of `cME'") xtitle("Service relatedness") ytitle("Firm performance: `DV'") xlabel(, angle(45) format(%5.2f)) plot1opts(lcolor(black)) plot2opts(lcolor(gs6) lpattern("--")) legend(on order(1 "low level `cME'" 2 "medium level `cME'" 3 "high level `cME'")) name(main)
        
        // creating second margin plot - Marginal effect
        capture quietly margins, dydx(`cME') at(c.related_share_w=(`MyMin' (`Grid') `MyMax'))
        capture marginsplot, recast(line) plot1opts(lcolor(gs8)) ciopt(color(black%20)) recastci(rarea) title("Average marginal effects of `cME'") xtitle("Service relatedness") ytitle("Firm performance: `DV'") xlabel(, angle(45) format(%5.2f)) name(diff)
        
        // combining the plots together
        capture graph combine main diff, xsize(6.5) ysize(2.7) iscale(.8) name(comb)
        capture graph export "`DV', `Mymodel', Model_`i' - Effect of `cME' on Service relatedness.png", width(6000) replace
        capture graph close comb
        graph drop _all        
        }
        
***** Creating margin plots for 3-way effects with one being category variable (above point (d)), e.g. for c.related_share_w##c.related_invest_firm_w##i.firstmover_related_years - this does NOT work within the loop - only if used separately outside any loops
        
        summarize related_invest_firm_w
        local MyMin4 = max((r(mean) - r(sd)),r(min))
        local MyMax4 = min((r(mean) + r(sd)),r(max))
        local Grid4 = (`MyMax4'-`MyMin4')/2
        local mid4 = (`MyMax4'-`MyMin4')/2 + `MyMin4'
        
        capture marginscontplot2 related_share_w related_invest_firm_w, var1(10) var2_spec(`MyMin4' `mid4' `MyMax4') at(i.`iME') areaopts(bla (format(%5.2f)) yla("TobinQ", format(%5.2f)) xla("Service relatedness", format(%5.2f))) // I liked marginscontplot2 for this more, but happy to here if somebody has a nicer solution here. Also, appreciate if you could give me a hint how to manipulate the legend lables and format here, the normal graphs functions seem not to work with it.

capture graph export "`DV', `Mymodel', Model_`i' - Three-way effect with related invest and `iME'.png", replace
    graph close
    graph drop _all
        
  ***** Creating margin plots for category MEs (above point (a)), e.g. for c.related_share_w##c.related_share_w##i.firstmover_related_years:
   // creating first margin plot - Moderating effect
        foreach iME of local iMEs {
        capture quietly margins i.`iME', at(c.related_share_w=(`MyMin' (`Grid') `MyMax'))
        capture marginsplot, recast(line) noci title("Moderating effect of `iME'") xtitle("Service relatedness") ytitle("Firm performance: `DV'") xlabel(, angle(45) format(%5.2f)) plot1opts(lcolor(black)) plot2opts(lcolor(gs6) lpattern("--")) name (main)
        
        // creating second margin plot - Marginal effect
        capture quietly margins, dydx(`iME') at(c.related_share_w=(`MyMin' (`Grid') `MyMax'))
        capture marginsplot, recast(line) plot1opts(lcolor(gs8)) ciopt(color(black%20)) recastci(rarea) title("Average marginal effects of `iME'") xtitle("Service relatedness") ytitle("Firm performance: `DV'") xlabel(, angle(45) format(%5.2f)) name(diff)
                
        // combining the plots together
        capture graph combine main diff, xsize(6.5) ysize(2.7) iscale(.8) name(comb)
        capture graph export "`DV', `Mymodel', Model_`i' - Effect of `iME' on Service relatedness.png", width(6000) replace
        capture graph close comb
        graph drop _all
        }
    }
}
}
And for the 3-way effect (c) - when 3 category variables interact (e.g. c.related_share_w#c.related_speed_w#c.related_inve st_firm_w) - I came up with following solution of creating separate graphs for specific values of one of the variables... but am not super happy with the visuals as I experience challenge of manipulating marginscontplot2 function to format the legend / axes nicely. Do you have a better solution that ideally also works within loops?

Code:
summarize related_share_w
        local MyMin = max((r(mean) - r(sd)),r(min))
        local MyMax = min((r(mean) + r(sd)),r(max))
        local Grid = (`MyMax'-`MyMin')/10
    
    summarize related_invest_firm_w
        local MyMin4 = max((r(mean) - r(sd)),r(min))
        local MyMax4 = min((r(mean) + r(sd)),r(max))
        local Grid4 = (`MyMax4'-`MyMin4')/2
        local riv (`MyMin4' `Grid4' `MyMax4')
        
        summarize related_speed_w
        local MyMin5 = max((r(mean) - r(sd)),r(min))
        local MyMax5 = min((r(mean) + r(sd)),r(max))  
        local Grid5 = (`MyMax5'-`MyMin5')/2
    
marginscontplot2 related_share_w related_speed_w, var1(10) var2(3) at(related_invest_firm_w = `MyMin4') areaopts(bla (format(%5.2f)) yla("TobinQ", format(%5.2f)) xla("Service relatedness", format(%5.2f))) lineopts(legend(1 "slow related serv speed" 2 "medium related serv speed" 3 "fast related serv speed")) // not clear how to manipulate the legend lables and format
graph export "Three-way effect - related invest = low.png", replace
    graph close
    graph drop _all
    
marginscontplot2 related_share_w related_speed_w, var1(10) var2(3) at(related_invest_firm_w = `MyMin4') ci combopts(ycommon xcommon) areaopts(xla(, format(%5.2f)) ytitle("TobinQ") xtitle("Service relatedness"))
graph export "Three-way effect with ci - related invest = low.png", replace
    graph close
    graph drop _all

marginscontplot2 related_share_w related_speed_w, var1(10) var2(3) at(related_invest_firm_w = `Grid4') areaopts(bla (format(%5.2f)) yla("TobinQ", format(%5.2f)) xla("Service relatedness", format(%5.2f)))
graph export "Three-way effect - related invest = mean.png", replace
    graph close
    graph drop _all
    
marginscontplot2 related_share_w related_speed_w, var1(10) var2(3) at(related_invest_firm_w = `Grid4') ci combopts(ycommon xcommon) areaopts(xla(, format(%5.2f)) ytitle("TobinQ") xtitle("Service relatedness"))
graph export "Three-way effect with ci - related invest = mean.png", replace
    graph close
    graph drop _all

    marginscontplot2 related_share_w related_speed_w, var1(10) var2(3) at(related_invest_firm_w = `MyMax4') areaopts(bla (format(%5.2f)) yla("TobinQ", format(%5.2f)) xla("Service relatedness", format(%5.2f)))
graph export "Three-way effect - related invest = high.png", replace
    graph close
    graph drop _all
    
marginscontplot2 related_share_w related_speed_w, var1(10) var2(3) at(related_invest_firm_w = `MyMax4') ci combopts(ycommon xcommon) areaopts(xla(, format(%5.2f)) ytitle("TobinQ") xtitle("Service relatedness"))
graph export "Three-way effect with ci - related invest = high.png", replace
    graph close
    graph drop _all
Data example:
Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input float(related_share_w related_invest_firm_w serv_share_w) byte(related_serv_entry_years firstmover_related_years) float(related_speed_w rslack2_w advint_w mgrowth_w mdynamism1_w mmunificence_w emp_ln roa_w total_divers_w mint_w tobinq3_ln)
.5 .030393185        .5 2 0  .3228407 .50553256   .09835758  .4078782 . . 1.0807877    .247941        0  .10162693 1.631337
 0          0         0 0 0         0   .401124   .10396002  .0985591 . . 1.1693814 .025326524        0  .10314787        .
.5  .02390977        .5 2 2 .01662697 .17948684  .003808496 -.1843253 . . 1.9315214  .11211377        0          .  .690114
.5 .014010232        .5 2 0 -.3077015  .5669612   .10396002  .0985591 . . .03633193  -.3270054        0 .011902187 .3908798
 0          . .26832914 2 0 -.4882525  .4053705 .0039266297  .5676779 . .  .3653373 .074105754 .5815899  .29764232 .3269372
end
Appreciate a lot any feedback and hints on how the code can be fixed to make the plots show up!

This forum was my source of inspiration multiple times (dear Statalist community, you are fantastic! thanks a lot!). May this discussion and my code help somebody as well who is dealing with similar issues Hope we can find a solution together here.