I would like to thank Professor Choonjoo Lee , who has continually amazed me with his Stata programming over the past few days. He used coordinate transformations to fix overlapping bars in 2D, and he use the exact same approach to brute-force a 3D projection onto Stata's strictly 2D canvas. I don't fully understand his codes. But, Cool!


Code:
clear
set obs 160
generate double x = 2*runiform()-1
generate double y = 2*runiform()-1
generate double outcome = 10 + 2*x - 1.5*y + 3*x*y - 1.2*x^2 + .6*y^2 + 2*rnormal()
regress outcome c.x##c.y c.x#c.x c.y#c.y
estimates save fitted_model, replace
scalar critical = invttail(e(df_r), .025)
save training_data, replace
clear
set obs 441
generate int ix = mod(_n-1,21)
generate int iy = floor((_n-1)/21)
generate double x = (ix-10)/10
generate double y = (iy-10)/10
predict double mu, xb
predict double se_mean, stdp
generate double lower = mu - critical*se_mean
generate double upper = mu + critical*se_mean
generate byte selected = inlist(ix,5,10,15) & inlist(iy,5,10,15)
assert lower < mu & mu < upper
isid x y
save prediction_grid, replace
* Orthographic projection prototype. It draws with official twoway primitives.
* Not part of pseudobars; no hidden-surface removal or interactive Stata camera.
* Normalized Z = (mean-10)/5; the same transformation applies to every endpoint.
capture program drop surface_view
program define surface_view
syntax, AZimuth(real) ELevation(real) NAME(name) TITLE(string) ///
[ASPECT(real 1) ZStretch(real 1)]
preserve
local a = `azimuth'*_pi/180
local e = `elevation'*_pi/180
generate double zn = `zstretch'*(mu-10)/5
generate double u = cos(`a')*x - sin(`a')*y
generate double v = -sin(`e')*(sin(`a')*x+cos(`a')*y) + cos(`e')*zn
generate double depth = cos(`e')*(sin(`a')*x+cos(`a')*y) + sin(`e')*zn
generate double vl = -sin(`e')*(sin(`a')*x+cos(`a')*y) + cos(`e')*`zstretch'*(lower-10)/5
generate double vh = -sin(`e')*(sin(`a')*x+cos(`a')*y) + cos(`e')*`zstretch'*(upper-10)/5
* A rotation preserves 3D length in the normalized coordinate system.
assert abs(u^2+v^2+depth^2 - (x^2+y^2+zn^2)) < 1e-10
* The projected interval midpoint stays on its projected mean.
assert abs((vl+vh)/2-v) < 1e-12
* Inverse rotation recovers the normalized data coordinates.
generate double q = -sin(`e')*v + cos(`e')*depth
assert abs(cos(`a')*u + sin(`a')*q - x) < 1e-12
assert abs(-sin(`a')*u + cos(`a')*q - y) < 1e-12
assert abs(cos(`e')*v + sin(`e')*depth - zn) < 1e-12
if `elevation' == 90 & `azimuth' == 0 {
assert abs(u-x)<1e-12
assert abs(v+y)<1e-12
}
export delimited x y mu lower upper u v depth vl vh using layout_`name'.csv, replace
local mesh
forvalues j=0(2)20 {
local mesh `"`mesh' (line v u if iy==`j', sort(ix) lcolor("70 115 145") lwidth(vthin))"'
local mesh `"`mesh' (line v u if ix==`j', sort(iy) lcolor("70 115 145") lwidth(vthin))"'
}
* Axis endpoints and ticks are projected by the identical transform.
local axes
local texts
local ox -1
local oy -1
local oz = `zstretch'*(2-10)/5
local ou = cos(`a')*`ox'-sin(`a')*`oy'
local ov = -sin(`e')*(sin(`a')*`ox'+cos(`a')*`oy')+cos(`e')*`oz'
foreach axis in x y z {
local ex = cond("`axis'"=="x", 1, -1)
local ey = cond("`axis'"=="y", 1, -1)
local ez = cond("`axis'"=="z", `zstretch'*(16-10)/5, `oz')
local eu = cos(`a')*`ex'-sin(`a')*`ey'
local ev = -sin(`e')*(sin(`a')*`ex'+cos(`a')*`ey')+cos(`e')*`ez'
local axes `"`axes' (pci `ov' `ou' `ev' `eu', lcolor(gs6) lwidth(thin))"'
local label = upper("`axis'")
if "`axis'"=="z" local label "Mean"
local texts `"`texts' text(`ev' `eu' "`label'", place(ne) size(small))"'
local ticks 0 1
if "`axis'"=="z" local ticks 2 6 10 14 16
foreach t of local ticks {
local tx = cond("`axis'"=="x", `t', -1)
local ty = cond("`axis'"=="y", `t', -1)
local tz = cond("`axis'"=="z", `zstretch'*(`t'-10)/5, `oz')
local tu = cos(`a')*`tx'-sin(`a')*`ty'
local tv = -sin(`e')*(sin(`a')*`tx'+cos(`a')*`ty')+cos(`e')*`tz'
local texts `"`texts' text(`tv' `tu' "`t'", place(sw) size(vsmall))"'
}
}
twoway `mesh' `axes' ///
(pcspike vl u vh u if selected, lcolor(maroon) lwidth(medthick)) ///
(scatter v u if selected, mcolor(maroon) msize(vsmall)), ///
`texts' title("`title'") ///
subtitle("Azimuth `azimuth'; elevation `elevation'; Z stretch `zstretch'", size(small)) ///
note("Red: nine means and pointwise 95% CIs. Wireframe: all depths visible.", size(vsmall)) ///
xscale(off range(-2.2 2.2)) yscale(off range(-2.2 2.2)) ///
xlabel(-2.2 2.2, nogrid) ylabel(-2.2 2.2, nogrid) ///
xtitle("") ytitle("") legend(off) aspectratio(`aspect') ///
scheme(s2color) graphregion(color(white)) name(`name', replace)
restore
end
surface_view, azimuth(35) elevation(25) name(view_a) title("coordinate projection")
Code:
capture ssc install surface surface x y mu


Comment