Announcement

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

  • Conformability error, r(503)

    Hi. I've been getting the conformability error r(503) on running the code below. Obviously my matrix specifications are off, but I can't seem to figure out where. Any help would be much appreciated. Thank you.

    Code:
    local time "r30 r90 r365"
    local total "total"
    local base_char "ib3.stratumn i.dschrgyr"  
    local pati_char "ib1.racecat1n_mice c.age##c.age ib1.stkcat1n ib1.sscat2n_mice ib2.sexn ib0.hisstktian"
    local hosp_char "ib2.hrucacat2n ib2.medschcatn"
    local other_char "ib2.rucacat2n_mice ib0.dc_refneedn_mice ib0.dc_refhhn_mice ib0.pcpfln ib2.dualcatn ib0.medhisdpn ib0.medhishtn ib0.medhissmn ib0.anycvdn"
    
    
    *******************************
    * coefplots for total payments
    *******************************
    
    cap program drop multireg
    program multireg
    syntax varlist(numeric) [if]
    local timestamp=subinstr("`c(current_time)'", ":", "_", .)
    local indvar i.tx
    *four specifications
    local spec1 ib3.stratumn i.dschrgyr
    local spec2 ib3.stratumn i.dschrgyr ib1.racecat1n_mice c.age##c.age ib1.stkcat1n ib1.sscat2n_mice ib2.sexn ib0.hisstktian
    
    *placeholder for results
    mat m = J(9,2,0)
    loc b = 6   
    foreach i in `*' {
    loc n = 1
    while `n' <= 2 {
    display " "
    display "This is a regression of `i' on `indvar' with `spec`n''."
    
    
    **********************************************
    **     30-day model -   TPM (gamma)    **
    **********************************************
    if "`i'" == "r30_total_w"   {
    tpm `i' `indvar' `spec`n'', first(logit) second(glm, fam(gamma) link(log)) cluster(hospclus)
    margins, at(tx=(0 1))
    mat a = r(table)  
    sca a1 = a[1,1]  
    sca a2 = a[1,2]  
    mat m[`b', 1] = a1, a2
    loc b = `b' + 1
    margins, dydx(tx) post
    est store m`i'_`n'
    outreg2 `*' using "./table/`i'_`timestamp'.doc", ctitle("`i'_`n'") alpha(.01, .05, .1)
    }
    
    
    ******************************************************************
    ** 90-day model - TPM(inverse guassian) **
    ******************************************************************
    else if "`i'" == "r90_total_w" {
    tpm `i' `indvar' `spec`n'', first(logit) second(glm, fam(igaussian) link(log)) cluster(hospclus)
    margins, at(tx=(0 1))
    mat a = r(table)
    sca a1 = a[1,1]
    sca a2 = a[1,2]
    mat m[`b', 1] = a1, a2
    loc b = `b' + 1
    margins, dydx(tx) post
    est store m`i'_`n'
    outreg2 `*' using "./table/`i'_`timestamp'.doc", ctitle("`i'_`n'") alpha(.01, .05, .1)
    }
    
    
    **********************************************
    ** 365-day model - GLM (gamma)    **
    **********************************************
    else if  "`i'" == "r365_total_w" {
    glm `i' `indvar' `spec`n'', fam(gamma) link(log) cluster(hospclus)
    margins, at(tx=(0 1))
    mat a = r(table)
    sca a1 = a[1,1]
    sca a2 = a[1,2]
    mat m[`b', 1] = a1, a2
    loc b = `b' + 1
    margins, dydx(tx) post
    est store m`i'_`n'
    outreg2 `*' using "./table/`i'_`timestamp'.doc", ctitle("`i'_`n'") alpha(.01, .05, .1)
    loc n = `n' + 1
    }
    loc b = `b' + 1
    }
    putexcel set "$output/table/coefplot.xlsx", sheet(Total Payments) replace
    *rownames
    matrix rownames m = "0" "0" "30-day" "Base Covariates" "+Patient Covariates" "90-day" "Base Covariates" "+Patient Covariates" "365-day" "Base Covariates" "+Patient Covariates" 
    *main table
    putexcel A1 = mat(m), nformat(number) rownames
    mat list m
    
    
    **********************************************
    ** format table     **
    **********************************************
    *title
    putexcel A1 = "Post-acute total payments for COMPASS patients versus control patients by timewindows"
    *subtitles
    putexcel B2 = "Adjusted mean spending", bold
    putexcel A4 = "Spending", bold border(bottom)
    putexcel B3 = "Control", bold
    putexcel C3 = "Treatment", bold
    putexcel B4 = "N=201", bold border(bottom)
    putexcel C4 = "N=140", bold border(bottom)
    putexcel D4 = "Differences in Spending", bold border(bottom)
    putexcel (A1:I1), merge
    putexcel (B2:C2), merge hcenter
    putexcel (D4:I4), merge hcenter
    end
    
    multireg r30_total_w r90_total_w r365_total_w
    
    *generate coefplots
    coefplot ///
    (mr30_total_w_?, label("30-day")) (mr90_total_w_?, label("90-day")) (mr365_total_w_?, label("365-day")), ///
    keep(1.tx) aseq swapnames xline(0) ///
    mlabel(cond(@pval<.001, "$"+string(@b, "%9.0f")+"***", cond(@pval<.01, "$"+string(@b, "%9.0f")+"**", cond(@pval<.05, "$"+string(@b, "%9.0f")+"*", cond(@pval<.1, "$"+string(@b, "%9.0f")+"+", "$"+string(@b, "%9.0f")))))) mlabposition(1) mlabsize(medium) ///
    yscale(off) groups(mr30_total_w_? = "{bf:30-day}" mr90_total_w_? = "{bf:90-day}" mr365_total_w_? = "{bf:365-day}", nolab) ///
    coeflabels(*1 = "base" *2 = "patient" *3 = "hospital" *4 = "other") legend(off) grid(none) ///
    note("+ p < .1, * p < .05, ** p < .01, *** p < .001", size(vsmall))
    
    graph export "./graph/totpmt_adjusted.png", replace
    
    *put into excel (to combine with other statistics)
    putexcel set "$output/table/coefplot.xlsx", sheet(Total Payments) modify
    putexcel D5 = picture("$output/graph/totpmt_adjusted.png")
    
    
    log close

  • #2
    This post contains a lot of code that is specific to your project, which we cannot work with in Stata. As asked, you are unlikely to get a helpful response. By now, you should be familiar with the FAQ and how to ask questions here.

    Could you at least point which line is causing the conformability error? At a guess, you might be trying to access or assign to an index that does not exist in your -m- matrix.

    Comment


    • #3
      Leonardo Guizzetti You are right, that code is very specific to the project. If I could've broken it down and provided it here, I would've. Unfortunately, I am to adopt someone else's code (who has left) from a shared project to my part of the project, and a large part of the problem lies in there.

      Since the loop is so long and has multiple nested loops within, it's hard to identify what exactly is driving the problem here. The code breaks down after the following lines, so your hunch on the -m- matrix seems right. Now to figure how to resolve it.

      Code:
       margins, dydx(tx) post  
      est store m`i'_`n'
      Last edited by Karishma DSouza; 13 Oct 2021, 11:52.

      Comment


      • #4
        Your example is still unworkable so my generic advice then is to use

        Code:
        set trace on
        set tracedepth 1
        To try to watch your code run as macro variables are substituted. You can turn trace off again when finished. Pay attention to the dimensions of m and how you index it.

        Comment


        • #5
          Here is my guess.

          You loop over your three arguments, and for each pass through the loop you do
          Code:
          mat m[`b', 1] = a1, a2
          loc b = `b' + 1
          The local macro starts at 6, but at the end of the loop over your arguments, you have
          Code:
          loc b = `b' + 1
          }
          So the first time through the loop, b will be 6 for the matrix assignment. The second time through the loop, b will be 8. The third time through the loop, b will be 10. But your matrix x has only 9 rows.

          If this is the case, you could have easily found this by including
          Code:
          display "b = `b'"
          before each of the matrix m assignments.

          If you don't have enough information to solve a programming problem, as yourself, "how can I get more information to help me find the problem?" Rather than "how can I deduce an answer with the information I have?"

          Comment

          Working...
          X