******************************************************************************** * xtmispanel — Example / Test Do-File * Author: Dr. Merwan Roudane (merwanroudane920@gmail.com) * Date: 03 March 2026 * Version: 1.0.0 * * This do-file demonstrates all features of the xtmispanel command * using simulated panel data with intentionally introduced missing values. ******************************************************************************** clear all set more off set seed 12345 * ────────────────────────────────────────────────────────────────────────────── * STEP 1: Create simulated balanced panel data * 10 panels × 30 time periods = 300 observations * 3 variables: GDP, Investment, Trade * ────────────────────────────────────────────────────────────────────────────── di _n "{hline 78}" di "{bf:STEP 1: Creating simulated panel data}" di "{hline 78}" local N = 10 local T = 30 local nobs = `N' * `T' set obs `nobs' * Panel and time identifiers gen country = ceil(_n / `T') gen year = 1990 + mod(_n - 1, `T') label var country "Country ID" label var year "Year" * Generate base variables with panel-specific intercepts gen double GDP = 5 + 0.5 * country + 0.1 * (year - 1990) + rnormal(0, 1) gen double Investment = 2 + 0.3 * country + 0.05 * (year - 1990) + 0.6 * GDP + rnormal(0, 0.8) gen double Trade = 1 + 0.2 * country + 0.08 * (year - 1990) + 0.4 * GDP + rnormal(0, 0.7) label var GDP "Log GDP per capita" label var Investment "Investment (% of GDP)" label var Trade "Trade Openness" * Set panel structure xtset country year di _n "Panel data created: `N' countries × `T' years = `nobs' observations" * ────────────────────────────────────────────────────────────────────────────── * STEP 2: Introduce missing values of different types * - MCAR: random 10% in GDP * - MAR: missing in Investment when GDP is low (bottom quartile) * - Block: entire periods missing for some panels in Trade * ────────────────────────────────────────────────────────────────────────────── di _n "{hline 78}" di "{bf:STEP 2: Introducing missing values (MCAR, MAR, Block patterns)}" di "{hline 78}" * MCAR in GDP (random 10%) replace GDP = . if runiform() < 0.10 * MAR in Investment (missing when GDP was low — bottom quartile) qui su GDP, detail replace Investment = . if GDP < r(p25) & GDP != . * Additional random MAR in Investment replace Investment = . if runiform() < 0.05 * Block missingness in Trade (entire years missing for countries 3, 7) replace Trade = . if inlist(country, 3, 7) & inrange(year, 2005, 2010) * Random scattered missing in Trade replace Trade = . if runiform() < 0.08 di "Missing values introduced:" foreach v in GDP Investment Trade { qui count if missing(`v') di " `v': " r(N) " missing" } * ────────────────────────────────────────────────────────────────────────────── * STEP 3: MODULE 1 — Detection & Summary Tables * ────────────────────────────────────────────────────────────────────────────── di _n(3) "{hline 78}" di "{bf:STEP 3: Running xtmispanel detection module}" di "{hline 78}" _n xtmispanel GDP Investment Trade, detect * ────────────────────────────────────────────────────────────────────────────── * STEP 4: MODULE 2 — Mechanism Tests * ────────────────────────────────────────────────────────────────────────────── di _n(3) "{hline 78}" di "{bf:STEP 4: Running xtmispanel mechanism tests}" di "{hline 78}" _n xtmispanel GDP Investment Trade, test * Display stored results di _n "Stored results from test:" di " Mechanism: " r(mechanism) di " MCAR chi2: " r(mcar_chi2) di " MCAR p-val: " r(mcar_pval) * ────────────────────────────────────────────────────────────────────────────── * STEP 5: MODULE 3 — Single Imputation (various methods) * ────────────────────────────────────────────────────────────────────────────── di _n(3) "{hline 78}" di "{bf:STEP 5: Testing individual imputation methods}" di "{hline 78}" _n * 5a. Linear interpolation for GDP di "{bf:5a. Linear interpolation for GDP}" _n xtmispanel GDP, impute(linear) replace * 5b. MICE for Investment di _n "{bf:5b. MICE imputation for Investment}" _n xtmispanel Investment, impute(mice) generate(Investment_mice) mice(10) * 5c. KNN for Trade di _n "{bf:5c. KNN imputation for Trade}" _n xtmispanel Trade, impute(knn) generate(Trade_knn) knn(7) * 5d. EM algorithm for Trade di _n "{bf:5d. EM imputation for Trade}" _n xtmispanel Trade, impute(em) generate(Trade_em) * 5e. Random Forest-style for Trade di _n "{bf:5e. Random Forest-style imputation for Trade}" _n xtmispanel Trade, impute(rf) generate(Trade_rf) * 5f. PMM for Trade di _n "{bf:5f. Predictive Mean Matching for Trade}" _n xtmispanel Trade, impute(pmm) generate(Trade_pmm) * ────────────────────────────────────────────────────────────────────────────── * STEP 6: MODULE 4 — Sensitivity Analysis * ────────────────────────────────────────────────────────────────────────────── di _n(3) "{hline 78}" di "{bf:STEP 6: Running sensitivity analysis for Trade}" di "{hline 78}" _n xtmispanel Trade, sensitivity * Display recommendation di _n "Recommended method: " r(best_method) di "Mean change: " r(best_dmean_pct) "%" * ────────────────────────────────────────────────────────────────────────────── * STEP 7: MODULE 5 — Visualization * ────────────────────────────────────────────────────────────────────────────── di _n(3) "{hline 78}" di "{bf:STEP 7: Generating diagnostic visualizations}" di "{hline 78}" _n * Basic graphs (without density overlay) xtmispanel GDP Investment Trade, graph * Display the combined dashboard graph display xtmis_combined * With density overlay (compare original Trade vs imputed) xtmispanel GDP Investment Trade, graph impvar(Trade_knn) * Display individual graphs graph display xtmis_heatmap graph display xtmis_density * ────────────────────────────────────────────────────────────────────────────── * STEP 8: Export graphs * ────────────────────────────────────────────────────────────────────────────── di _n(3) "{hline 78}" di "{bf:STEP 8: Exporting graphs}" di "{hline 78}" _n capture { graph export "xtmis_heatmap.png", name(xtmis_heatmap) replace width(1200) graph export "xtmis_barvar.png", name(xtmis_barvar) replace width(1200) graph export "xtmis_barpanel.png", name(xtmis_barpanel) replace width(1200) graph export "xtmis_bartime.png", name(xtmis_bartime) replace width(1200) graph export "xtmis_pattern.png", name(xtmis_pattern) replace width(1200) graph export "xtmis_timeline.png", name(xtmis_timeline) replace width(1200) graph export "xtmis_combined.png", name(xtmis_combined) replace width(1200) graph export "xtmis_density.png", name(xtmis_density) replace width(1200) } di _n "Graphs exported to PNG files." * ────────────────────────────────────────────────────────────────────────────── * STEP 9: Full workflow — best practice * ────────────────────────────────────────────────────────────────────────────── di _n(3) "{hline 78}" di "{bf:STEP 9: Full best-practice workflow}" di "{hline 78}" _n di "{bf:Step 9.1: Detect and test}" _n xtmispanel Trade, detect test di _n "{bf:Step 9.2: Sensitivity analysis}" _n xtmispanel Trade, sensitivity di _n "{bf:Step 9.3: Impute with best method}" _n * Based on sensitivity results, impute xtmispanel Trade, impute(linear) generate(Trade_best) replace di _n "{bf:Step 9.4: Verify with visualization}" _n xtmispanel Trade, graph impvar(Trade_best) graph display xtmis_density * ────────────────────────────────────────────────────────────────────────────── * DONE * ────────────────────────────────────────────────────────────────────────────── di _n(2) "{hline 78}" di "{bf:ALL TESTS COMPLETED SUCCESSFULLY}" di "{hline 78}" di "xtmispanel v1.0.0 — All modules functional" di " Module 1: Detection tables ✓" di " Module 2: Mechanism tests ✓" di " Module 3: Imputation (13) ✓" di " Module 4: Sensitivity ✓" di " Module 5: Visualization (8) ✓" di "{hline 78}"