Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • coefplot using z-score back-transformed and natural log back-transformed scale

    Hi, I have run a mixed model using data which was first natural log-transformed then Z-score standardised. I now want to plot the coefplot using the original scale. I have been able to back-transform the natural log (when running the model using data that has not been standardised; example code 1) but am having issues back-standardising for the Z-score standardised data (example code 2). I would be really grateful for some help!

    EXAMPLE DATASET

    Code:
    clear
    input id    timepoint    indep    indep_z    dep    dep_ln    dep_ln_z
    1    1    44.65982    -.6603368    275.3574    5.61807    -1.654174
    1    3    46.57632    -.4926517    424.2176    6.050247    -1.195439
    2    1    71.85764    1.719354    696.8698    6.546598    -.6685855
    2    3    73.61533    1.873145    1967.342    7.584439    .4330315
    3    1    63.32101    .9724367    2065.605    7.633179    .4847665
    3    3    65.4976    1.162879    4146.802    8.330092    1.224506
    4    1    69.39904    1.504238    4224.268    8.348601    1.244153
    4    2    69.49487    1.512622    3810.498    8.245515    1.134732
    4    3    71.20329    1.662101    4845.657    8.485838    1.389823
    5    1    56.25462    .3541578    1286.231    7.159472    -.0180506
    5    3    58.34086    .5366949    1138.097    7.037113    -.147928
    6    1    44.10404    -.7089653    3294.132    8.099897    .9801657
    6    3    46.11636    -.5328959    2985.959    8.001677    .8759091
    7    1    42.33265    -.8639541    1991.22    7.596503    .4458368
    7    3    44.22998    -.6979459    2559.941    7.847739    .7125121
    8    1    42.34634    -.8627566    1901.532    7.550415    .3969169
    8    3    44.11499    -.708007    1918.238    7.559162    .4062015
    9    1    53.21834    .0884967    391.9459    5.971124    -1.279424
    9    3    54.97878    .2425276    475.6463    6.164675    -1.073979
    10    1    69.25394    1.491542    3519.451    8.16606    1.050395
    10    3    71.0527    1.648926    .    .    .
    11    1    50.59548    -.1409922    275.9146    5.620091    -1.652028
    11    2    50.69678    -.1321289    401.0999    5.99421    -1.254919
    11    3    52.55305    .0302863    298.9796    5.700375    -1.56681
    12    1    46.77618    -.4751645    1588.578    7.370595    .2060467
    12    3    48.53936    -.3208942    1835.51    7.515078    .3594085
    13    1    56.99932    .4193156    2119.239    7.658813    .5119756
    13    3    58.85284    .5814908    2159.262    7.677522    .531835
    14    1    64.08214    1.039032    4523.702    8.417086    1.316846
    14    3    65.91102    1.199051    11463.95    9.346963    2.303865
    15    1    50.93224    -.1115273    2617.079    7.869814    .7359433
    16    1    44.33402    -.688843    1888.278    7.543421    .3894929
    16    3    46.26968    -.5194811    1798.193    7.494537    .3376055
    17    1    54.4011    .1919826    2395.07    7.781168    .6418495
    17    3    56.50924    .3764359    2736.567    7.91446    .7833326
    18    1    53.06776    .0753215    840.1093    6.733532    -.4701647
    18    3    54.97878    .2425276    712.717    6.569084    -.644718
    19    1    53.60986    .1227524    1810.514    7.501366    .3448539
    19    3    55.53456    .2911561    2003.817    7.602809    .452531
    20    1    47.12389    -.4447417    1371.873    7.223932    .0503709
    20    3    48.93361    -.2863988    1852.236    7.524149    .3690367
    21    1    32.48186    -1.725855    1107.922    7.010242    -.176451
    21    3    34.48049    -1.550984    1137.389    7.03649    -.1485895
    22    1    29.06776    -2.024574    168.108    5.124607    -2.17796
    22    3    31.34839    -1.825029    286.0926    5.656315    -1.613578
    23    1    55.44422    .2832511    3953.621    8.282387    1.173869
    23    3    57.21834    .4384793    4854.037    8.487566    1.391657
    24    1    58.90486    .5860425    1897.731    7.548414    .3947932
    24    2    59.01711    .5958639    1522.961    7.328412    .1612711
    24    3    60.65435    .7391148    1941.195    7.571059    .4188297
    25    1    54.38741    .1907847    1599.642    7.377535    .2134136
    25    3    56.1807    .34769    2159.759    7.677752    .5320789
    26    1    48.98837    -.2816079    229.0388    5.433891    -1.84967
    26    3    50.81998    -.1213492    294.4248    5.685024    -1.583105
    27    1    40.65435    -1.010798    2167.241    7.68121    .5357494
    27    3    42.75975    -.8265843    2378.167    7.774086    .6343322
    28    1    54.9733    .2420483    4379.471    8.384684    1.282452
    28    2    55.10746    .2537863    2878.377    7.964982    .8369596
    28    3    56.73101    .3958396    4021.941    8.29952    1.192055
    29    1    45.78234    -.5621211    2514.484    7.829823    .6934949
    29    2    45.86174    -.5551741    4789.418    8.474164    1.377432
    29    3    47.54826    -.4076112    4415.059    8.392776    1.291043
    30    1    38.32717    -1.214416    1252.621    7.132994    -.0461555
    30    2    38.42574    -1.205792    1336.739    7.197988    .0228328
    30    3    40.32307    -1.039784    1364.646    7.21865    .0447649
    31    1    61.01848    .7709751    3693.907    8.21444    1.101748
    31    2    61.09788    .7779219    2403.918    7.784855    .6457639
    31    3    62.88022    .9338689    2713.854    7.906125    .7744858
    32    1    41.01574    -.9791777    961.9236    6.868935    -.3264408
    32    3    42.93224    -.8114926    970.1036    6.877403    -.3174528
    33    1    45.69747    -.5695471    2891.598    7.969564    .8418236
    34    1    58.38741    .5407673    320.4724    5.769796    -1.493123
    35    1    60.10404    .6909652    1856.001    7.526179    .3711919
    35    3    62.04791    .8610458    2768.73    7.926144    .7957351
    36    1    53.71389    .1318553    474.4825    6.162225    -1.076579
    36    3    55.82478    .3165483    .    .    .
    37    1    51.8193    -.0339133    1838.434    7.516669    .3610975
    37    2    51.8987    -.0269662    1921.465    7.560843    .4079862
    37    3    53.68652    .1294599    1746.475    7.465354    .3066292
    38    1    47.34565    -.4253381    3781.011    8.237747    1.126487
    38    3    49.19644    -.2634021    5217.824    8.559835    1.468368
    39    1    55.23888    .2652847    679.6987    6.521649    -.6950678
    39    3    57.09788    .4279392    1089.775    6.993726    -.1939811
    40    1    35.40041    -1.470495    896.2548    6.798225    -.4014963
    40    2    35.54278    -1.458038    586.449    6.374085    -.8516997
    40    3    37.33607    -1.301133    1021.051    6.928588    -.2631223
    41    1    39.55921    -1.106618    368.7322    5.910071    -1.344229
    41    3    41.32786    -.951869    535.7909    6.283744    -.9475929
    42    1    60.6078    .7350425    1789.249    7.489551    .3323128
    42    3    62.51335    .9017693    2892.144    7.969753    .842024
    43    1    59.24162    .6155069    1746.482    7.465359    .3066338
    44    1    30.62286    -1.88851    637.092    6.456914    -.7637812
    44    3    32.42437    -1.730886    1029.267    6.936603    -.2546151
    45    1    71.34566    1.674558    1317.176    7.183245    .007184
    45    3    73.24572    1.840805    1756.601    7.471136    .3127657
    46    1    61.20192    .7870247    4502.428    8.412373    1.311843
    46    3    63.38398    .9779462    .    .    .
    47    1    54.55989    .2058764    985.6336    6.893285    -.3005948
    47    3    56.38877    .3658958    1167.249    7.062405    -.1210824
    48    1    37.35524    -1.299456    836.8149    6.729603    -.4743353
    48    2    37.50856    -1.286041    700.9619    6.552454    -.6623706
    48    3    39.18686    -1.139197    1076.176    6.981169    -.2073098
    49    1    61.37988    .8025954    2238.811    7.7137    .5702363
    49    3    63.16222    .9585425    2805.524    7.939345    .8097475
    50    1    29.91102    -1.950793    208.3546    5.339242    -1.950136
    51    1    74.62012    1.96106    642.2728    6.465013    -.7551845
    51    3    76.53936    2.128984    827.4002    6.718288    -.486345
    52    1    40.91171    -.9882806    3684.903    8.212    1.099157
    52    2    41.01574    -.9791777    2799.202    7.937089    .807353
    52    3    42.7269    -.8294591    2748.359    7.918759    .7878965
    53    1    49.06503    -.2749005    .    .    .
    53    2    49.20465    -.2626835    2511.94    7.828811    .6924204
    53    3    51.01711    -.1041013    2816.587    7.943281    .8139252
    54    1    61.82615    .8416421    2151.537    7.673938    .5280303
    54    3    63.86311    1.019867    4004.288    8.295121    1.187386
    55    1    53.44011    .1079004    1482.352    7.301385    .1325836
    55    3    55.34018    .2741483    1221.591    7.10791    -.072781
    56    1    48.19712    -.350838    296.0346    5.690476    -1.577317
    56    3    50.10267    -.1841112    249.9506    5.521263    -1.756929
    57    1    52.52019    .0274115    312.5521    5.744771    -1.519686
    57    3    54.39562    .1915033    312.1594    5.743514    -1.521021
    58    1    37.36619    -1.298498    149.9367    5.010213    -2.299383
    58    3    39.58385    -1.104462    227.4243    5.426817    -1.857179
    59    1    58.4668    .5477144    1986.069    7.593913    .4430875
    59    3    60.24093    .7029428    2215.075    7.703042    .5589225
    60    1    58.60369    .5596917    3455.811    8.147813    1.031026
    60    2    58.75428    .5728669    4436.277    8.397571    1.296131
    60    3    60.37508    .7146806    5968.703    8.694284    1.611079
    61    1    57.81793    .4909408    4408.998    8.391402    1.289584
    61    3    59.76455    .6612611    3624.288    8.195413    1.08155
    62    1    33.76318    -1.613746    434.4165    6.074004    -1.170222
    62    3    35.75359    -1.439593    189.8028    5.245986    -2.049123
    63    1    32.93634    -1.68609    1895.494    7.547235    .393541
    63    3    34.90212    -1.514093    2302.848    7.741902    .6001709
    64    1    71.40314    1.679588    2129.721    7.663746    .5172126
    64    3    73.27036    1.842961    3397.844    8.130897    1.01307
    65    1    59.86858    .670364    3434.66    8.141673    1.024509
    65    2    59.94524    .6770714    2960.603    7.993148    .8668568
    65    3    61.64819    .8260714    3520.905    8.166473    1.050833
    66    1    67.41958    1.331043    1623.378    7.392264    .2290477
    66    3    69.34976    1.499926    1408.739    7.250451    .0785193
    67    1    56.45448    .371645    459.3684    6.129853    -1.110941
    67    3    58.37645    .539809    832.9525    6.724977    -.4792458
    68    1    54.00684    .1574872    574.4127    6.353348    -.8737112
    68    2    54.09172    .1649132    539.5509    6.290737    -.9401698
    68    3    55.75907    .3107992    724.6711    6.585718    -.6270624
    69    1    55.14579    .25714    252.2744    5.530517    -1.747106
    69    3    57.04038    .4229086    326.9581    5.789832    -1.471856
    70    1    52.4846    .0242975    1324.438    7.188744    .0130203
    71    1    34.77071    -1.525591    490.0402    6.194488    -1.042334
    71    3    36.67625    -1.358864    848.3346    6.743275    -.4598227
    72    1    66.90486    1.286008    2639.639    7.878397    .7450543
    72    3    69.41547    1.505675    6615.416    8.797158    1.720274
    73    1    33.54689    -1.63267    259.8163    5.559975    -1.715839
    73    3    35.36756    -1.473369    206.8057    5.331779    -1.958057
    74    1    59.96167    .6785086    1505.062    7.31659    .1487229
    74    2    60.07392    .6883301    1182.27    7.075192    -.1075092
    74    3    61.89185    .8473912    1906.976    7.553274    .3999512
    75    1    43.16496    -.791131    188.5062    5.239131    -2.056399
    75    3    45.10336    -.6215294    293.6805    5.682492    -1.585792
    76    1    51.52088    -.0600241    723.2149    6.583706    -.6291972
    76    3    53.3306    .0983185    781.8101    6.661612    -.5465042
    77    1    70.04791    1.561011    3665.586    8.206743    1.093577
    77    3    71.83573    1.717437    2136.27    7.666817    .5204716
    78    1    33.69747    -1.619495    953.954    6.860615    -.335272
    78    3    35.47433    -1.464027    1103.682    7.006407    -.1805208
    79    1    25.90828    -2.301015    170.4778    5.138605    -2.163102
    79    3    27.67146    -2.146745    749.5142    6.619425    -.5912834
    80    1    52.64887    .0386706    655.5605    6.485491    -.7334483
    80    3    54.41478    .1931801    822.2954    6.7121    -.4929141
    end
    Transformed and Z-score standardised variables were generated as follows:

    Code:
    gen dep_ln = ln(dep)
    egen dep_ln_z = std(dep_ln)
    egen indep_z = std(indep)
    EXAMPLE CODE 1

    Code:
    sum indep
    local min = round(r(min))
    local max = round(r(max))
    
    mixed dep_ln c.indep || id:
    local ca = _b[_cons]
    local cb = _b[indep]
    
    margins, at(indep=(`min'(0.1)`max')) post
    est store dep_ln_model
    
    coefplot ///
    (dep_ln_model, ///
    transform(*=exp(@)) ///
    recast(line) lc("154 149 148") ///
    title("Control") noci) ///
    , ///
    at ///
    xsc(range(20 80) titleg(3pt)) ///
    ysc(log range("100 10000") titleg(3pt)) ///
    xlab(20(10)80) ///
    ylab("100 1000 10000") ///
    xtitle("Independent") ///                        
    ytitle("Dependent") ///
    legend(off) ///
    plotregion(lstyle(none)) ///
    aspectratio(1) ///
    addplot( ///
    (function y = exp(`ca' + `cb'*x), ///
    range(20 80) ///
    lpattern(dash) lcolor("154 149 148")) ///
    (scatter dep indep if dep!=0, mcolor("154 149 148")) ///
    )
    EXAMPLE CODE 2

    Code:
    sum indep_z
    local min = round(r(min))
    local max = round(r(max))
    
    mixed dep_ln_z c.indep_z || id:
    local ca = _b[_cons]
    local cb = _b[indep_z]
    
    margins, at(indep_z=(`min'(0.1)`max')) post
    est store dep_ln_z_model
    
    sum dep
    local dep_mean = r(mean)
    local dep_sd = r(sd)
    
    coefplot ///
    (dep_ln_z_model, ///
    transform(*=exp((@*``nfltr'_sd')+``nfltr'_mean')) ///
    recast(line) lc("154 149 148") ///
    title("Control") noci) ///
    , ///
    at ///
    xsc(range(20 80) titleg(3pt)) ///
    ysc(log range("100 10000") titleg(3pt)) ///
    xlab(20(10)80) ///
    ylab("100 1000 10000") ///
    xtitle("Independent") ///                        
    ytitle("Dependent") ///
    legend(off) ///
    plotregion(lstyle(none)) ///
    aspectratio(1) ///
    addplot( ///
    (function y = exp(`ca' + `cb'*x), ///
    range(20 80) ///
    lpattern(dash) lcolor("154 149 148")) ///
    (scatter dep indep if dep!=0, mcolor("154 149 148")) ///
    )
    Thanks so much in advance!

  • #2
    Before we go into coding, can you illustrate what results you expect for 2 or 3 cases from the enclosed data. That is, show the standardized value and the expected unstandardized value. I assume that you want to apply the formula:


    $$X = Z \cdot s + \bar{X}$$

    where \(X\) is the original (unstandardized) value, \(Z\) is the Z-score, \(s\) is the sample standard deviation and \(\bar{X}\) is sample mean.

    Comment


    • #3
      I note that the back-transform you use in your example 1 is biased; for more on this see -predlog- (use search to find and download) and the write-up, with cites, in STB-29 (available at the Stata website)

      Comment

      Working...
      X