Announcement

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

  • Saving loop output from metaregression models

    Hi. Please note I have also cross-posted this on Cross-validated (https://stackoverflow.com/questions/...02194_73607763) but I figured that most people using STATA would be on here rather than CV. I have outlined my issue below


    Recap of issue.
    I'm a beginner in Stata programming. I am using Stata 17. I am trying to examine how effects on different structures differ by age (and extract a p for trend). I have managed to obtain regression results in STATA within a loop. However, I am having massive difficulty creating an output I can export to excel (The outcome name, b, p value for the difference in each outcome, by age group). I am trying to replicate someone else's code from STATA list. They create a matrix to store the p-value. However, they have several predictors which they store in a macro. I want to store one predictor (but this predictor has levels). Hence, I want a row for each level of predictor.
    I want to store the different levels within a single variable (Outcome) into a local macro as I want to use this further on to make the rownames and also to establish the number of rows of the matrix to (obtain a p-value column). Someone responded on there saying that the use of assigning the levels of a string variable to a local macro using local macro y=levelsof Outcome was bizarre, however STATA has not shown an error but when I read the local macro y, the levels are not separated by any spaces - I've pasted the output below. Any advice is greatly appreciated!

    ```

    [CODE]
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str47 Outcome double(TE seTE Age) byte(E F G)
    "Accumbens" .2779 .0675765306122449 10.94 . . .
    "Amygdala" .1187 .05571428571428571 10.94 . . .
    "Banks of the superior temporal sulcus thickness" -.0769 .05375 10.94 . . .
    "Banks of the superior temporal sulcus area" .0099 .05209183673469388 10.94 . . .
    "Brainstem" .1256 .05505102040816327 10.94 . . .
    "Caudal anterior cingulate area" .0033 .05630102040816327 10.94 . . .
    "Caudal anterior cingulate thickness" -.0504 .06614795918367347 10.94 . . .
    "Caudal middle frontal area" -.0013 .04487244897959184 10.94 . . .
    "Caudal middle frontal thickness" -.0241 .04512755102040816 10.94 . . .
    "Caudate" .0993 .06142857142857144 10.94 . . .
    "Cuneus area" -.0294 .04938775510204082 10.94 . . .
    "Cuneus thickness" .081 .07831632653061224 10.94 . . .
    "Entorhinal area" .1013 .059260204081632656 10.94 . . .
    "Entorhinal thickness" -.0982 .06102040816326531 10.94 . . .
    "Estimated total intracranial volume" .3988 .061887755102040816 10.94 . . .
    "Frontal pole area" -.1164 .09293367346938775 10.94 . . .
    "Frontal pole thickness" -.0074 .06344387755102042 10.94 . . .
    "Fusiform area" .0226 .04178571428571429 10.94 . . .
    "Fusiform thickness" -.0547 .04341836734693878 10.94 . . .
    "Hippocampus" .1336 .06061224489795919 10.94 . . .
    "Inferior parietal area" .0425 .06591836734693876 10.94 . . .
    "Inferior parietal thickness" .0373 .03627551020408163 10.94 . . .
    "Inferior temporal area" .0051 .040841836734693875 10.94 . . .
    "Inferior temporal thickness" -.0769 .09548469387755103 10.94 . . .
    "Insula area" .0336 .05165816326530612 10.94 . . .
    "Insula thickness" -.0284 .05984693877551021 10.94 . . .
    "Isthmus cingulate area" -.0276 .049795918367346946 10.94 . . .
    "Isthmus cingulate thickness" .023 .06497448979591838 10.94 . . .
    "Lateral occipital area" -.0146 .04147959183673469 10.94 . . .
    "Lateral occipital thickness" .0467 .08099489795918367 10.94 . . .
    "Lateral orbitofrontal area" .0932 .04262755102040816 10.94 . . .
    "Lateral orbitofrontal thickness" -.0721 .053469387755102044 10.94 . . .
    "Lateral ventricles" .0113 .11719387755102041 10.94 . . .
    "Lingual area" -.0297 .06576530612244898 10.94 . . .
    "Lingual thickness" .0793 .060892857142857144 10.94 . . .
    "Mean thickness" .11 .06948979591836735 10.94 . . .
    "Medial orbitofrontal area" .0695 .041020408163265305 10.94 . . .
    "Medial orbitofrontal thickness" .02 .06354591836734694 10.94 . . .
    "Middle temporal area" .0017 .03744897959183673 10.94 . . .
    "Middle temporal thickness" -.0948 .042831632653061225 10.94 . . .
    "Pallidum" .0999 .05772959183673469 10.94 . . .
    "Paracentral area" .0595 .05155612244897959 10.94 . . .
    "Paracentral thickness" -.0203 .04982142857142857 10.94 . . .
    "Parahippocampal area" -.0404 .05681122448979592 10.94 . . .
    "Parahippocampal thickness" .0901 .06056122448979592 10.94 . . .
    "Pars opercularis area" .0468 .08443877551020407 10.94 . . .
    "Pars opercularis thickness" -.0341 .044540816326530605 10.94 . . .
    "Pars orbitalis area" .0547 .044566326530612245 10.94 . . .
    "Pars orbitalis thickness" -.0619 .05448979591836734 10.94 . . .
    "Pars triangularis area" .0346 .05239795918367347 10.94 . . .
    "Pars triangularis thickness" -.094 .045892857142857145 10.94 . . .
    "Pericalcarine area" -.0455 .057117346938775515 10.94 . . .
    "Pericalcarine thickness" -.0275 .06418367346938775 10.94 . . .
    "Postcentral area" -.0749 .04109693877551021 10.94 . . .
    "Postcentral thickness" .1381 .05089285714285715 10.94 . . .
    "Posterior cingulate area" -.0085 .04839285714285715 10.94 . . .
    "Posterior cingulate thickness" .0062 .06030612244897959 10.94 . . .
    "Precentral area" .0081 .039540816326530615 10.94 . . .
    "Precuneus area" .0014 .041198979591836736 10.94 . . .
    "Precuneus thickness" .0077 .042755102040816326 10.94 . . .
    "Precentral thickness" -.0044 .04155612244897959 10.94 . . .
    "Putamen" .0285 .05732142857142857 10.94 . . .
    "Rostral anterior cingulate area" .0582 .04653061224489796 10.94 . . .
    "Rostral anterior cingulate thickness" -.0659 .06183673469387755 10.94 . . .
    "Rostral middle frontal area" -.0445 .03487244897959183 10.94 . . .
    "Rostral middle frontal thickness" -.0289 .040612244897959185 10.94 . . .
    "Superior frontal area" .0247 .030790816326530614 10.94 . . .
    "Superior frontal thickness" -.0799 .04196428571428572 10.94 . . .
    "Superior parietal area" -.0774 .041836734693877546 10.94 . . .
    "Superior parietal thickness" .0445 .03795918367346939 10.94 . . .
    "Superior temporal area" .026 .03571428571428572 10.94 . . .
    "Superior temporal thickness" -.0376 .04239795918367347 10.94 . . .
    "Supramarginal area" -.0214 .0411734693877551 10.94 . . .
    "Supramarginal thickness" .0303 .07234693877551021 10.94 . . .
    "Temporal pole area" .0408 .06752551020408164 10.94 . . .
    "Temporal pole thickness" -.0127 .04982142857142857 10.94 . . .
    "Thalamus" .056 .06928571428571428 10.94 . . .
    "Total surface area" .4217 .061275510204081625 10.94 . . .
    "Transverse temporal area" .004 .11076530612244899 10.94 . . .
    "Transverse temporal thickness" -.1021 .058418367346938775 10.94 . . .
    "Total white matter volume" .0938 .04221938775510204 10.94 . . .
    "Accumbens" .2915 .15183673469387754 19.59 . . .
    "Amygdala" -.0031 .11943877551020408 19.59 . . .
    "Banks of the superior temporal sulcus area" .1311 .12556122448979593 19.59 . . .
    "Banks of the superior temporal sulcus thickness" .0352 .11683673469387755 19.59 . . .
    "Brainstem" .1553 .12025510204081634 19.59 . . .
    "Caudal anterior cingulate area" -.0446 .14403061224489797 19.59 . . .
    "Caudal anterior cingulate thickness" .2181 .12910714285714286 19.59 . . .
    "Caudal middle frontal area" .0455 .10589285714285715 19.59 . . .
    "Caudal middle frontal thickness" -.0086 .19048469387755104 19.59 . . .
    "Caudate" .1465 .11326530612244898 19.59 . . .
    "Cuneus area" -.1295 .1523469387755102 19.59 . . .
    "Cuneus thickness" -.0212 .12280612244897961 19.59 . . .
    "Entorhinal area" -.073 .1556122448979592 19.59 . . .
    "Entorhinal thickness" .1737 .13561224489795917 19.59 . . .
    "Estimated total intracranial volume" .142 .13081632653061223 19.59 . . .
    "Frontal pole area" .1182 .2192091836734694 19.59 . . .
    "Frontal pole thickness" .0141 .12589285714285714 19.59 . . .
    "Fusiform area" -.0196 .08298469387755103 19.59 . . .
    "Fusiform thickness" .0867 .16400510204081634 19.59 . . .
    end


    meta set TE seTE *Define parameters

    replace Outcome = subinstr(Outcome," ", "_", .) *Outcome has spaces originally, replace with underscore

    *Rename outcomes which present issues due to length, pressumably
    replace Outcome="Banks_area" if Outcome=="Banks_of_the_superior_temporal_sulcus_ar ea"
    replace Outcome="Banks_thickness" if Outcome=="Banks_of_the_superior_temporal_sulcus_th ickness"
    replace Outcome="Caudalanteriorcingthick" if Outcome=="Caudal_anterior_cingulate_thickness"
    replace Outcome="Caudalanteriorcingarea" if Outcome=="Caudal_anterior_cingulate_area"
    replace Outcome="eTIV" if Outcome=="Estimated_total_intracranial_volume"
    replace Outcome="Rostanteriorcingthick" if Outcome=="Rostral_anterior_cingulate_thickness"
    replace Outcome="Rostanteriorcingarea" if Outcome=="Rostral_anterior_cingulate_area"


    local y= levelsof Outcome
    local l= word count `y'
    matrix p=J(`l',1,.)
    matrix rownames p = "`y'"
    matrix colnames p = "p"
    matlist p

    egen group=group(Outcome)
    su group, meanonly
    forvalues i in 1/`r(max)' {
    meta regress Age if group==`i'
    matrix p[`i++',1]=2*ttail(e(df_r), abs(_b[Age]/_se[Age]))
    }
    matlist p
    ```
    The error IS:

    . matrix rownames p = "`y'"
    conformability error
    r(503);

    end of do-file

    r(503);


    -------
    Output when I try and read local macro containing levels of variable Outcome

    di `y'
    AccumbensAmygdalaBanks_areaBanks_thicknessBrainste mCaudal_middle_frontal_areaCaudal_middle_frontal_t hicknessCaudalanteriorcingareaCaudalanteriorcing
    > thickCaudateCuneus_areaCuneus_thicknessEntorhinal_ areaEntorhinal_thicknessFrontal_pole_areaFrontal_p ole_thicknessFusiform_areaFusiform_thicknessHi
    > ppocampusInferior_parietal_areaInferior_parietal_t hicknessInferior_temporal_areaInferior_temporal_th icknessInsula_areaInsula_thicknessIsthmus_cing
    > ulate_areaIsthmus_cingulate_thicknessLateral_occip ital_areaLateral_occipital_thicknessLateral_orbito frontal_areaLateral_orbitofrontal_thicknessLat
    > eral_ventriclesLingual_areaLingual_thicknessMean_t hicknessMedial_orbitofrontal_areaMedial_orbitofron tal_thicknessMiddle_temporal_areaMiddle_tempor
    > al_thicknessPallidumParacentral_areaParacentral_th icknessParahippocampal_areaParahippocampal_thickne ssPars_opercularis_areaPars_opercularis_thickn
    > essPars_orbitalis_areaPars_orbitalis_thicknessPars _triangularis_areaPars_triangularis_thicknessPeric alcarine_areaPericalcarine_thicknessPostcentra
    > l_areaPostcentral_thicknessPosterior_cingulate_are aPosterior_cingulate_thicknessPrecentral_areaPrece ntral_thicknessPrecuneus_areaPrecuneus_thickne
    > ssPutamenRostanteriorcingareaRostanteriorcingthick Rostral_middle_frontal_areaRostral_middle_frontal_ thicknessSuperior_frontal_areaSuperior_frontal
    > _thicknessSuperior_parietal_areaSuperior_parietal_ thicknessSuperior_temporal_areaSuperior_temporal_t hicknessSupramarginal_areaSupramarginal_thickn
    > essTemporal_pole_areaTemporal_pole_thicknessThalam usTotal_surface_areaTotal_white_matter_volumeTrans verse_temporal_areaTransverse_temporal_thickne
    > sseTIV

  • #2
    The code below corrects errors in your original code - I'm reasonably sure I highlighted all the changes in red.

    The conformability error arose because you used quotation marks in
    Code:
    matrix rownames p = "`y'"
    which caused your list of names to be treated as a single item. In the same way that you did not use quotation marks when counting the number of items in the list, you should not use quotation marks when using the list to assign rownames.

    The display command deceived you; it just displayed all the items in the local y with no intervening spaces. (The display command sometimes tries too hard to be helpful.) The macro list command correctly displays the separate items in a local macro list.
    Code:
    . local y 1 2
    
    . display `y'
    12
    
    . macro list _y
    _y:             1 2
    
    .
    For more details see the following documentation.
    Code:
    help comment
    help levelsof
    help macro 
    help forvalues
    Code:
    meta set TE seTE // Define parameters
    
    replace Outcome = subinstr(Outcome," ", "_", .) // Outcome has spaces originally, replace with underscore
    
    // Rename outcomes which present issues due to length, pressumably
    replace Outcome="Banks_area" if Outcome=="Banks_of_the_superior_temporal_sulcus_area"
    replace Outcome="Banks_thickness" if Outcome=="Banks_of_the_superior_temporal_sulcus_thickness"
    replace Outcome="Caudalanteriorcingthick" if Outcome=="Caudal_anterior_cingulate_thickness"
    replace Outcome="Caudalanteriorcingarea" if Outcome=="Caudal_anterior_cingulate_area"
    replace Outcome="eTIV" if Outcome=="Estimated_total_intracranial_volume"
    replace Outcome="Rostanteriorcingthick" if Outcome=="Rostral_anterior_cingulate_thickness"
    replace Outcome="Rostanteriorcingarea" if Outcome=="Rostral_anterior_cingulate_area"
    
    
    levelsof Outcome, local(y)
    local l : word count `y'
    matrix p=J(`l',1,.)
    matrix rownames p = `y'
    matrix colnames p = "p"
    matlist p
    
    egen group=group(Outcome)
    su group, meanonly
    forvalues i = 1/`r(max)' {
    meta regress Age if group==`i'
    matrix p[`i++',1]=2*ttail(e(df_r), abs(_b[Age]/_se[Age]))
    }
    matlist p

    Comment


    • #3
      Thanks William for pointing this out, works now

      Comment


      • #4
        Has something changed in STATA. The code which worked doesn't work anymore?

        Comment


        • #5
          I noticed the e(df_r) is not the matrix when I enter ereturn list and my matrix p is empty.

          Comment


          • #6
            Are you running just parts of the do-file? You need to ensure that the local macro that creates the matrix is defined each time you run the do-file. Local macros are local, as the following explains: https://journals.sagepub.com/doi/10....36867X20931028

            Comment

            Working...
            X