Announcement

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

  • #46
    Originally posted by Clyde Schechter View Post
    Yes, that's what the code in #43 does. If time == 1, then 1.time is 1, 2.time is 0, and 3.time is 0. This is how factor variable notation in Stata works. See -help fvvarlist-.
    Thank you Clyde. I have also amended some of the code previously following your recommendations. Would this be correct syntax?

    return scalar p = `M'[r(p)]
    return scalar p_= (`M'[`r(p)'] < `alpha')

    Comment


    • #47
      No. I'm going to guess that what you want to return as r(p) is the overall p-value from the -mixed- command, and what you want to return as r(p_) is a dichotomous variable indicating whether or not that p-value is less than `alpha'. The syntax would then be:

      Code:
      return scalar p = r(p)
      return scalar p_ = (r(p) < `alpha')
      Matrix `M' is not relevant to this.

      Now, if what you want to return as r(p) is not the overall -mixed- p-value but the p-value associated with some specific coefficient, and r(p_) is to be an indicator of whether that is less than alpha, then that's a different matter:

      Code:
      return scalar p = `M'[4, #]
      return scalar p_ = (`M'[4, #] < `alpha')
      where you replace # by the column number in `M' that corresponds to the particular coefficient you are interested in.

      Comment


      • #48
        How did you determine that "#" was 8 in your original post? And, what does the 4 correspond to?

        Comment


        • #49
          If you run any estimation command in a recent version of Stata, some results are left behind in the matrix r(table) which is referenced in the code. Each column corresponds to one of the regressors. The number to choose is the column that corresponds to the regressor whose statistics you want. For example:

          Code:
          . sysuse auto, clear
          (1978 Automobile Data)
          
          . regress price mpg headroom gear_ratio
          
                Source |       SS           df       MS      Number of obs   =        74
          -------------+----------------------------------   F(3, 70)        =      6.95
                 Model |   145700708         3  48566902.7   Prob > F        =    0.0004
              Residual |   489364688        70  6990924.12   R-squared       =    0.2294
          -------------+----------------------------------   Adj R-squared   =    0.1964
                 Total |   635065396        73  8699525.97   Root MSE        =      2644
          
          ------------------------------------------------------------------------------
                 price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
                   mpg |  -241.8674   70.10672    -3.45   0.001    -381.6909    -102.044
              headroom |  -365.5239   407.8446    -0.90   0.373    -1178.944    447.8966
            gear_ratio |  -393.9483   874.0387    -0.45   0.654    -2137.164    1349.267
                 _cons |   13598.18    2910.74     4.67   0.000     7792.894    19403.47
          ------------------------------------------------------------------------------
          
          . matrix list r(table)
          
          r(table)[9,4]
                         mpg    headroom  gear_ratio       _cons
               b  -241.86743   -365.5239   -393.9483   13598.182
              se   70.106715   407.84463   874.03872     2910.74
               t  -3.4499895  -.89623322   -.4507218   4.6717269
          pvalue   .00095382   .37320004   .65358311   .00001403
              ll  -381.69086  -1178.9444  -2137.1636   7792.8943
              ul    -102.044   447.89656   1349.2669    19403.47
              df          70          70          70          70
            crit   1.9944371   1.9944371   1.9944371   1.9944371
           eform           0           0           0           0
          So if you need statistics referring to mpg, the column number is 1. If you need them for the constant term, it's 4. And so on. I suggest you run one example regression from your simulation and then -matrix list r(table)- so you can see which column(s) you are interested in.

          Similarly, each row corresponds to a particular kind of statistic. If you want the p-value, it's row 4. If you want the standard error of the coefficient, it's row 2. The coefficient itself is in row 1.

          Comment


          • #50
            Thank you Clyde.

            When I try to run the code, everything seems to work fine. However, I do get a couple of error message: matrix M not found and (num_clus not found, intrvcoeff not found, etc.)

            tempfile power
            capture postutil clear
            postfile step int num_clus intrvcoeff p p_ bias using `power' /*Maybe add ICC*/
            *Loop over number of clusters, ICC
            foreach c of local num_clus{
            display as text "Number of clusters" as result "`c'"
            [INDENT=2]forvalue i = 1/`nrep'{[/INDENT][INDENT=3]display as text "Iterations" as result `nrep'
            quietly swcrt `c' `intrvcoeff' `sigma_u3' `sigma_u2' `sigma_error' /*Maybe add ICC*/[/INDENT][INDENT=3]post step (`c') /*(`ICC')*/ (`r(p)') (`r(p_)') (`r(bias)')[/INDENT]
            }
            }
            postclose step

            *Open results, calculate power
            use `power', clear
            levelsof num_clus, local(num_clus)
            /*levelsof ICC, local(ICC)*/ /*Maybe remove or add ICC*/
            matrix drop _all
            *Loop over combinations of clusters and ICC
            *Add power results to matrix
            foreach c of local num_clus {
            /*foreach i of local ICC { */
            [INDENT=2]quietly ci proportions sig if clus_num == `c' & /*float(ICC)*/ == float(`i')
            local power `r(proportion)'
            local power_lb `r(lb)'
            local power_ub `r(ub)'
            quietly ci mean bias if clus_num == `c' & /*float(ICC)*/ == float(`i')
            local bias `r(mean)'
            local bias_lb `r(lb)'
            local bias_ub `r(ub)'[/INDENT][INDENT=3]matrix M = nullmat(M) \ (`c', `i', `power', `power_lb', `power_ub', `r(mean)', `r(lb)', `r(ub)', `intrvcoeff') /*May need to remove extra comma and space; why isn't rmean, rlb, and rub bias, biaslb, and biasub*/[/INDENT][INDENT=2]}[/INDENT]
            }
            *Display the matrix
            matrix colnames M = c /*ICC*/ intrvcoeff power power_lb power_ub bias bias_lb bias_ub //*THIS IS WHERE I GET THE ERROR MESSAGE*//
            matrix list M, noheader format(%3.2f)
            Last edited by CEdward; 03 Nov 2019, 18:58.

            Comment


            • #51
              Your code is long and would require considerable effort to read even if it were properly formatted. Given that it is not properly formatted, it is essentially unreadable. Please repost this code properly within code delimiters.

              Comment


              • #52
                Code:
                *Postfile to store results
                tempfile power
                capture postutil clear
                postfile step int num_clus intrvcoeff p p_ bias using `power' /*Maybe add ICC*/
                *Loop over number of clusters, ICC
                foreach c of local num_clus{
                    display as text "Number of clusters" as result "`c'"
                        forvalue i = 1/`nrep'{
                            display as text "Iterations" as result `nrep'    
                            quietly swcrt `c' `intrvcoeff' `sigma_u3' `sigma_u2' `sigma_error' /*Maybe add ICC*/
                            post step (`c') /*(`ICC')*/ (`r(p)') (`r(p_)') (`r(bias)')
                        }
                    }
                postclose step
                
                *Open results, calculate power
                use `power', clear
                levelsof num_clus, local(num_clus)
                /*levelsof ICC, local(ICC)*/ /*Maybe remove or add ICC*/
                matrix drop _all
                *Loop over combinations of clusters and ICC
                *Add power results to matrix
                foreach c of local num_clus {
                    /*foreach i of local ICC { */    
                        quietly ci proportions sig if clus_num == `c' & /*float(ICC)*/ == float(`i')
                        local power `r(proportion)'
                        local power_lb `r(lb)'
                        local power_ub `r(ub)'
                        quietly ci mean bias if clus_num == `c' & /*float(ICC)*/ == float(`i')
                        local bias `r(mean)' //*I have added the next 3 lines to the original post...would they be correct?*//
                        local bias_lb `r(lb)'
                        local bias_ub `r(ub)'
                        matrix M = nullmat(M) \ (`c', `i', `power', `power_lb', `power_ub', `r(mean)', `r(lb)', `r(ub)', `intrvcoeff') /*May need to remove extra comma and space; why isn't rmean, rlb, and rub bias, biaslb, and biasub*/
                    }
                }
                
                *Display the matrix
                matrix colnames M = c /*ICC*/ intrvcoeff power power_lb power_ub bias bias_lb bias_ub //*This is where I get the error*//
                matrix list M, noheader format(%3.2f)
                My apologies, Clyde. I have reposted the code.
                Last edited by CEdward; 04 Nov 2019, 16:09.

                Comment


                • #53
                  Jack, I am generally a patient person, but I am losing patience with this. The code you show in #52 is not producing the results you describe in #50. It cannot even get to the point where you claim to get your error.

                  Code:
                  /*foreach i of local ICC { */ quietly ci proportions sig if clus_num == `c' & /*float(ICC)*/ == float(`i')
                  is as far as this code will take you.* That's because, with the commented out material, there is no local macro i, and the -ci- command reduces to:
                  Code:
                  quietly ci proportions sig if clus_num == `c' & == float()
                  which is a double syntax error. I'm not sure whether you will get caught because of the absence of an argument to float() or because there is no expression between the & and ==, but the parser will definitely complain about at least one of those.

                  You are wasting both your time and mine by asking people to debug code that is not actually producing the behavior you describe.

                  Please read the Forum FAQ again. It is crucial that when asking for help with debugging code you post exactly the code you ran and exactly the results you got. Don't make "little" changes to it before posting, because in coding there is no such thing as a "little" change.

                  *In fact, you might not even get to here, because in the code segment you show, you use a local macro nrep which is not defined there, but I'm willing to believe that it is, in fact, defined somewhere earlier in the code, before the parts you are showing here. If it isn't, then your code dies at -forvalue i = 1/`nrep' { -

                  Comment


                  • #54
                    Dr. Clyde, I do want to sincerely apologize. In my haste to get my code running, I carelessly posted. I would like to emphasize that your time is very much appreciated and I have learned a significant amount from your and others posts. I have posted the full code below:

                    Code:
                    capture program drop swcrt
                    program define swcrt, rclass
                            version 15.1 
                            preserve
                            clear
                            *Stepped-wedge specifications
                            local num_clus 3 6 9 18 36
                            /*local ICC_lvl3 
                            local ICC_lvl2*/
                            local clussize 25
                            *Model specifications
                            local intercept 17.87
                            local timecoeff1 -5.42
                            local timecoeff2 -5.72
                            local timecoeff3 -7.03
                            local timecoeff4 -6.13
                            local timecoeff5 -9.13
                            local intrvcoeff 5.00
                            local sigma_u3 25.77
                            local sigma_u2 120.62
                            local sigma_error 38.35
                    
                            local nrep 100
                            local alpha 0.05
                            
                            args num_clus ICC_lvl3 ICC_lvl2 clussize intercept timecoeff1 timecoeff2 timecoeff3 timecoeff4 timecoeff5 intrvcoeff sigma_u3 sigma_u2 sigma_error
                            assert `num_clus' > 0 & `ICC_lvl3' > 0 & `ICC_lvl2' > 0 & `clussize' > 0 & `intercept' > 0 & `timecoeff1' > 0 & `timecoeff2' > 0 & `timecoeff3' > 0 & `timecoeff4' > 0 & `timecoeff5' > 0 & `intrvcoeff' > 0 & `sigma_u3' > 0 & `sigma_u2' > 0 & `sigma_error' > 0
                        /*Generate simulated multi—level data*/
                            qui
                                    set seed 12345
                                    clear
                                    set obs `num_clus'   
                                    qui gen cluster = _n
                                    //Generate cluster-level errors //
                                    qui gen u_3 = rnormal(0,`sigma_u3')   
                                    expand `clussize'
                                    bysort cluster: gen individual = _n
                                    //Generate patient-level errors //
                                    qui gen u_2 = rnormal(0,`sigma_u2') 
                                    expand 6
                                    //Set up time//
                                    bysort cluster individual: gen time = _n-1 
                                    //Set up intervention variable//
                                    gen intrv = (time>=cluster) 
                                    //Generate residual errors 
                                    qui gen error = rnormal(0,`sigma_error') 
                                    //Generate outcome y
                                    qui gen y = `intercept' + `timecoeff1'*1.time + `timecoeff2'*2.time + `timecoeff3'*3.time + `timecoeff4'*4.time + `timecoeff5'*5.time + `intrvcoeff'*intrv + u_3 + u_2 + error if time ==0
                        
                        /*Fit multi-level model to simulated dataset*/ 
                        mixed y intrv i.time ||cluster: ||individual:, covariance(unstructured) reml dfmethod(kroger)
                        /*Return estimated effect size, bias, p-value, and significance dichotomy*/
                        tempname M
                        matrix `M' = r(table)
                        return scalar bias = _b[intrv] - `intrvcoeff'
                        return scalar p = `M'[1,4]
                        return scalar p_= (`M'[1,4] < `alpha') 
                        exit
                    end swcrt
                     
                    *Postfile to store results
                    tempfile powerresults
                    capture postutil clear
                    postfile step int num_clus intrvcoeff p p_ bias using `powerresults' /*Maybe add ICC*/
                    *Loop over number of clusters, ICC
                    foreach c of local num_clus{
                        display as text "Number of clusters" as result "`c'"
                        /*foreach icc of local ICC{ 
                            display as text "Intra-class correlation" as result `i'*/
                            forvalue i = 1/`nrep'{
                                display as text "Iterations" as result `nrep'    
                                quietly swcrt `c' `intrvcoeff' `sigma_u3' `sigma_u2' `sigma_error' /*Maybe add ICC*/
                                post step (`c') /*(`icc')*/ (`r(p)') (`r(p_)') (`r(bias)')
                            }
                        /*}*/
                    }
                    postclose step
                    
                    *Open results, calculate power
                    use `powerresults', clear
                    levelsof num_clus, local(num_clus)
                    /*levelsof ICC, local(ICC)*/ /*Maybe remove or add ICC*/
                    matrix drop _all
                    *Loop over combinations of clusters and ICC
                    *Add power results to matrix
                    foreach c of local num_clus {
                        /*foreach i of local ICC { */    
                            quietly ci proportions sig if clus_num == `c' /*& float(ICC) == float(`i')*/ 
                            local power `r(proportion)'
                            local power_lb `r(lb)'
                            local power_ub `r(ub)'
                            quietly ci mean bias if clus_num == `c' /*& float(ICC) == float(`i')*/
                            local bias `r(mean)'
                            local bias_lb `r(lb)'
                            local bias_ub `r(ub)'
                            matrix M = nullmat(M) \ (`c', `i', `power', `power_lb', `power_ub', `r(mean)', `r(lb)', `r(ub)', `intrvcoeff') /*May need to remove extra comma and space; why isn't rmean, rlb, and rub bias, biaslb, and biasub*/
                        /*}*/
                    }
                    
                    *Display the matrix
                    matrix colnames M = c /*ICC*/ intrvcoeff power power_lb power_ub bias bias_lb bias_ub /*THIS IS WHERE THE ERROR HAPPENS*/
                    matrix list M, noheader format(%3.2f)
                    The error happens when I try to display the matrix:

                    matrix M not found
                    r(111);

                    It has also been indicated to me that the variables (e.g. c, intrvcoeff) are not found either.

                    Again, my sincerest apologies.

                    Comment


                    • #55
                      OK, this is workable. You are tripping over how local macros work. You define local macros num_clus and nrep inside program swcrt. But the meaning of local in local macro is that those same macros do not exist outside program swcrt--or if they do exist, it is because you create them again there, and the values you give them there may or may not have any relationship to the values you gave them inside program swcrt. Anyway, when you get to the driver code (i.e. what begins right after -*Postfile to store results-) these local macros do not exist. Since local num_clus does not exist, it is taken to be an empty string, so the -foreach c of local num_clus- loop never iterates because there are no values in num_clus for c to take on. So that entire loop is skipped. So your file powerresults emerges empty from that.

                      Consequently after -*Open results, calculate power-, you read `powerresults' into memory: and that is an empty file, so none of the variables you expect to see are there. You once again try to iterate after -*Add power results to matrix- with -foreach c of local num_clus-, but num_clus is still an empty string, so zero iterations actually occur. Consequently, matrix M is never created. Hence when you try to assign column names to matrix M, Stata informs you that there is no such matrix.

                      That's the gist of it. All of those locals that you define at the to of program swcrt need to be defined outside of any program. And then, you need to explicitly pass them as arguments to the program (or any other program that needs to use those values) because, just as local macros defined inside a program don't exist outside it, local macros defined outside a program don't exist inside it.

                      Finally, without going into the details, it seems that when you actually call program swcrt, the argument list does not correspond to what program swcrt's -args- command expects. Had the loop executed Stata would have stopped there and complained about that. (The emptiness of local macro num_clus masked this error.)

                      Comment


                      • #56
                        That makes total sense. However, even though I have moved the local macros outside of the program (and passed it into the program via the -args- command) like so:

                        Code:
                          *Stepped-wedge specifications
                                local num_clus 3 6 9 18 36
                                /*local ICC_lvl3
                                local ICC_lvl2*/
                                local clussize 25
                                *Model specifications
                                local intercept 17.87
                                local timecoeff1 -5.42
                                local timecoeff2 -5.72
                                local timecoeff3 -7.03
                                local timecoeff4 -6.13
                                local timecoeff5 -9.13
                                local intrvcoeff 5.00
                                local sigma_u3 25.77
                                local sigma_u2 120.62
                                local sigma_error 38.35
                        
                                local nrep 100
                                local alpha 0.05
                        
                        capture program drop swcrt
                        program define swcrt, rclass
                                version 15.1
                                preserve
                                clear
                                args num_clus ICC_lvl3 ICC_lvl2 clussize intercept timecoeff1 timecoeff2 timecoeff3 timecoeff4 timecoeff5 intrvcoeff sigma_u3 sigma_u2 sigma_error
                                assert `num_clus' > 0 & `ICC_lvl3' > 0 & `ICC_lvl2' > 0 & `clussize' > 0 & `intercept' > 0 & `timecoeff1' > 0 & `timecoeff2' > 0 & `timecoeff3' > 0 & `timecoeff4' > 0 & `timecoeff5' > 0 & `intrvcoeff' > 0 & `sigma_u3' > 0 & `sigma_u2' > 0 & `sigma_error' > 0
                        
                        //*Remainder of program omitted*//
                        
                        end swcrt
                        
                        //*Remainder of the code omitted*//
                        However, I still end up with the same error at the same step.
                        Last edited by CEdward; 06 Nov 2019, 09:44.

                        Comment


                        • #57
                          I do not understand how you can end up with the same error at the same place because the code never gets to that place due to the syntax errors referred to above.

                          Code:
                          
                          .   *Stepped-wedge specifications
                          .         local num_clus 3 6 9 18 36
                          
                          .         /*local ICC_lvl3
                          >         local ICC_lvl2*/
                          .         local clussize 25
                          
                          .         *Model specifications
                          .         local intercept 17.87
                          
                          .         local timecoeff1 -5.42
                          
                          .         local timecoeff2 -5.72
                          
                          .         local timecoeff3 -7.03
                          
                          .         local timecoeff4 -6.13
                          
                          .         local timecoeff5 -9.13
                          
                          .         local intrvcoeff 5.00
                          
                          .         local sigma_u3 25.77
                          
                          .         local sigma_u2 120.62
                          
                          .         local sigma_error 38.35
                          
                          .
                          .         local nrep 100
                          
                          .         local alpha 0.05
                          
                          .
                          . capture program drop swcrt
                          
                          . program define swcrt, rclass
                            1.         version 15.1
                            2.         preserve
                            3.         clear
                            4.         args num_clus ICC_lvl3 ICC_lvl2 clussize intercept timecoeff1 timecoeff2 timecoeff3 timecoeff4 timecoeff5 intrvcoeff sigma_u3 sigma_u2 sigma_error
                            5.         assert `num_clus' > 0 & `ICC_lvl3' > 0 & `ICC_lvl2' > 0 & `clussize' > 0 & `intercept' > 0 & `timecoeff1' > 0 & `timecoeff2' > 0 & `timecoeff3' > 0 & `timecoeff4' > 0 & `timecoeff5' > 0 & `int
                          > rvcoeff' > 0 & `sigma_u3' > 0 & `sigma_u2' > 0 & `sigma_error' > 0
                            6.
                          .     /*Generate simulated multi—level data*/
                          .         qui
                            7.                 set seed 12345
                            8.                 clear
                            9.                 set obs `num_clus'  
                           10.                 qui gen cluster = _n
                           11.                 //Generate cluster-level errors //
                          .                 qui gen u_3 = rnormal(0,`sigma_u3')  
                           12.                 expand `clussize'
                           13.                 bysort cluster: gen individual = _n
                           14.                 //Generate patient-level errors //
                          .                 qui gen u_2 = rnormal(0,`sigma_u2')
                           15.                 expand 6
                           16.                 //Set up time//
                          .                 bysort cluster individual: gen time = _n-1
                           17.                 //Set up intervention variable//
                          .                 gen intrv = (time>=cluster)
                           18.                 //Generate residual errors
                          .                 qui gen error = rnormal(0,`sigma_error')
                           19.                 //Generate outcome y
                          .                 qui gen y = `intercept' + `timecoeff1'*1.time + `timecoeff2'*2.time + `timecoeff3'*3.time + `timecoeff4'*4.time + `timecoeff5'*5.time + `intrvcoeff'*intrv + u_3 + u_2 + error if time ==0
                           20.    
                          .     /*Fit multi-level model to simulated dataset*/
                          .     mixed y intrv i.time ||cluster: ||individual:, covariance(unstructured) reml dfmethod(kroger)
                           21.     /*Return estimated effect size, bias, p-value, and significance dichotomy*/
                          .     tempname M
                           22.     matrix `M' = r(table)
                           23.     return scalar bias = _b[intrv] - `intrvcoeff'
                           24.     return scalar p = `M'[1,4]
                           25.     return scalar p_= (`M'[1,4] < `alpha')
                           26.     exit
                           27. end swcrt
                          
                          . 
                          . *Postfile to store results
                          . tempfile powerresults
                          
                          . capture postutil clear
                          
                          . postfile step int num_clus intrvcoeff p p_ bias using `powerresults' /*Maybe add ICC*/
                          
                          . *Loop over number of clusters, ICC
                          . foreach c of local num_clus{
                            2.     display as text "Number of clusters" as result "`c'"
                            3.     /*foreach icc of local ICC{
                          >         display as text "Intra-class correlation" as result `i'*/
                          .         forvalue i = 1/`nrep'{
                            4.             display as text "Iterations" as result `nrep'   
                            5.             quietly swcrt `c' `intrvcoeff' `sigma_u3' `sigma_u2' `sigma_error' /*Maybe add ICC*/
                            6.             post step (`c') /*(`icc')*/ (`r(p)') (`r(p_)') (`r(bias)')
                            7.         }
                            8.     /*}*/
                          . }
                          Number of clusters3
                          Iterations100
                          >0 invalid name
                          r(198);
                          
                          end of do-file
                          The explanation of this error is simple.
                          As I pointed out yesterday, the command you use to call program swcrt does not match the -args- command in there. So, all of the arguments in swcrt from timecoeff1 through sigma_error are undefined. When the immediately following -assert- command processes it, it translates to: -
                          assert 3 > 0 & 5.00 > 0 & 25.77 > 0 & 120.62 > 0 & 38.35 > 0 & > 0 & > 0 & > 0 & > 0 & > 0 & > 0 & > 0 & > 0 & > 0-, which is a long series of syntax errors. Your program breaks there.

                          Comment


                          • #58
                            So I have edited the call line to reflect the list of macros in the -args- command:

                            Code:
                            quietly swcrt `c' `intrvcoeff' `clussize' `intercept' `timecoeff1' `timecoeff2' `timecoeff3' `timecoeff4' `timecoeff5' `intrvcoeff' `sigma_u3' `sigma_u2' `sigma_error'
                            But, indeed - I get the error:

                            matrix M not found
                            r(111);

                            I tried to debug the issue by enclosing the name of the matrix, M, in single quotes - but that did not resolve the problem either:

                            Code:
                            matrix colnames `M' = c /*ICC*/ intrvcoeff power power_lb power_ub bias bias_lb bias_ub
                            matrix list `M', noheader format(%3.2f)

                            Comment


                            • #59
                              Once again, I do not see how that is possible, as when I try to run your code it still breaks in the same place I pointed out in #57 and does not get to the place you claim to get this error message.

                              Your change in the code still does not match the -args- command in the function: you are still missing one argument. Based on the names, it seems that the program requires two arguments ICC_lvl3 and ICC_lvl2, but your command calling the program has in their place only one, called `intrvcoeff'. So you still need to fix that. It isn't clear to me what the fix for that is. Your code never defines ICC_lvl3 and ICC_lvl2-- in fact most of the references to ICC are commented out. And it seems quite peculiar for the calling command to have intrvcoeff in their place, and also have the exact same thing appear later in the calling command (just before sigma_u3. So you need to work out what's going on here.

                              Let me add that in addition to that mismatch in the number of arguments, it looks like you are destined to crash again on the -assert- that follows the -args- command in swcrt once you fix the mismatch. That's because the assert requires all the timecoeff's to be > 0, but your definitions of them are all negative. So you will need to fix that inconsistency as well.

                              After that you will find out that you have still more problems. The final -return scalar p_ = ...' command refers to an `alpha' that is undefined. Yes, you do have a definition of alpha in the list of macro definitions at the top of the program, but you never pass alpha to program swcrt, so within that program it is undefined.

                              And when you fix that you will find that your -post- command only mentions four expressions, whereas the -postfile- command requires five.

                              After you fix all those problems, then I think you will actually get your tempfile `powerresults' created (assuming you don't run into convergence problems with your -mixed- estimations).

                              But there will still be troubled waters ahead at that point. Both of your -quietly ci- commands will fail. The first because of sig and the second because of mean: neither of these variables exist in the dataset `powerresults'. Based on your -postfile- command, the only variables you will have at that point are num_clus intrvcoeff p p_ and bias. So you need to change the workings of the code that creates -powerresults- to include those variables and calculate them in your loop.

                              Whether that will finally solve all the problems, I cannot say. I have not attempted to chase it any farther than that.

                              I tried to debug the issue by enclosing the name of the matrix, M, in single quotes - but that did not resolve the problem either

                              Well, no, there is no reason to think that would help matters. All that does is replace a reference to a matrix M that ought to exist but, at least so far, you have not been able to create, with a matrix `M' that has no reason to exist at all.


                              Last edited by Clyde Schechter; 06 Nov 2019, 22:49.

                              Comment


                              • #60
                                Originally posted by Jack Chau View Post
                                So I have edited the call line to reflect the list of macros in the -args- command:

                                . . . But, indeed - I get the error:

                                . . . I tried to debug the issue . . . - but that did not resolve the problem either: . . .
                                You've been banging your head against the wall for what seems like ages on this. Sometimes when I hit an impasse in a coding problem, I find that it's better to just throw the unredeemable attempt out, take a break in order to forget it (to avoid going down the same path), and start over again from a clean slate.

                                Here. Maybe try something like this. (I've attached the do-file so that you can pick it up from there, and I have a few notes at the end.)

                                .ÿ
                                .ÿversionÿ15.1

                                .ÿ
                                .ÿclearÿ*

                                .ÿ
                                .ÿlogÿcloseÿ_all

                                .ÿlogÿusingÿSWCRT.smcl,ÿreplaceÿnomsgÿname(lo)

                                .ÿ
                                .ÿsetÿseedÿ`=strreverse("1523658")'

                                .ÿ
                                .ÿprogramÿdefineÿsimem
                                ÿÿ1.ÿÿÿÿÿÿÿÿÿversionÿ15.1
                                ÿÿ2.ÿÿÿÿÿÿÿÿÿsyntaxÿ,ÿ[Clinics(integerÿ1)ÿDelta(realÿ5)]
                                ÿÿ3.ÿ
                                .ÿÿÿÿÿÿÿÿÿdropÿ_all
                                ÿÿ4.ÿ
                                .ÿÿÿÿÿÿÿÿÿ//ÿRandomizationÿgroupsÿ(fiveÿtimeÿpointsÿafterÿinitial)
                                .ÿÿÿÿÿÿÿÿÿsetÿobsÿ5
                                ÿÿ5.ÿÿÿÿÿÿÿÿÿgenerateÿbyteÿrdgÿ=ÿ_n
                                ÿÿ6.ÿ
                                .ÿÿÿÿÿÿÿÿÿ//ÿClinicsÿperÿrandomizationÿgroup
                                .ÿÿÿÿÿÿÿÿÿexpandÿ`=max(`clinics',ÿ1)'
                                ÿÿ7.ÿÿÿÿÿÿÿÿÿgenerateÿintÿcidÿ=ÿ_n
                                ÿÿ8.ÿÿÿÿÿÿÿÿÿgenerateÿdoubleÿcid_uÿ=ÿrnormal(0,ÿ25.77)
                                ÿÿ9.ÿ
                                .ÿÿÿÿÿÿÿÿÿ//ÿPatientsÿwithinÿclinicsÿ(appearsÿtoÿbeÿfixedÿatÿ25)
                                .ÿÿÿÿÿÿÿÿÿexpandÿ25
                                ÿ10.ÿÿÿÿÿÿÿÿÿgenerateÿlongÿpidÿ=ÿ_n
                                ÿ11.ÿÿÿÿÿÿÿÿÿgenerateÿdoubleÿpid_uÿ=ÿrnormal(0,ÿ120.62)
                                ÿ12.ÿ
                                .ÿÿÿÿÿÿÿÿÿ//ÿPatientÿvisitsÿtoÿclinics
                                .ÿÿÿÿÿÿÿÿÿquietlyÿexpandÿ6
                                ÿ13.ÿÿÿÿÿÿÿÿÿbysortÿpid:ÿgenerateÿbyteÿvisÿ=ÿ_nÿ-ÿ1
                                ÿ14.ÿ
                                .ÿÿÿÿÿÿÿÿÿ//ÿSwitch-overs
                                .ÿÿÿÿÿÿÿÿÿgenerateÿbyteÿtrtÿ=ÿvisÿ>=ÿrdg
                                ÿ15.ÿ
                                .ÿÿÿÿÿÿÿÿÿ//ÿOutcome
                                .ÿÿÿÿÿÿÿÿÿgenerateÿdoubleÿrspÿ=ÿ`delta'ÿ*ÿ1.trtÿ+ÿ///ÿresearchÿquestionÿfixed
                                >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ17.87ÿ-ÿ5.42ÿ*ÿ1.visÿ-ÿ5.72ÿ*ÿ2.visÿ-ÿ7.03ÿ*ÿ3.visÿ-ÿ6.13ÿ*ÿ4.visÿ-ÿ9.13ÿ*ÿ5.visÿ+ÿ///ÿtimeÿfixed
                                >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿcid_uÿ+ÿpid_uÿ+ÿ///ÿrandom
                                >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿrnormal(0,ÿ38.35)
                                ÿ16.ÿ
                                .ÿÿÿÿÿÿÿÿÿifÿ`clinics'ÿ>ÿ1ÿmixedÿrspÿi.rdgÿi.visÿi.trtÿ||ÿcid:ÿ||ÿpid:ÿ,ÿremlÿdfmethod(kroger)ÿiterate(25)ÿnogroupÿnolrtest
                                ÿ17.ÿÿÿÿÿÿÿÿÿelseÿmixedÿrspÿi.visÿi.trtÿ||ÿcid:ÿ||ÿpid:ÿ,ÿremlÿdfmethod(kroger)ÿiterate(25)ÿnogroupÿnolrtest
                                ÿ18.ÿ
                                .ÿÿÿÿÿÿÿÿÿifÿe(converged)ÿtestÿ1.trt,ÿsmall
                                ÿ19.ÿÿÿÿÿÿÿÿÿelseÿreturnNULL
                                ÿ20.ÿend

                                .ÿ
                                .ÿprogramÿdefineÿreturnNULL,ÿrclass
                                ÿÿ1.ÿÿÿÿÿÿÿÿÿversionÿ15.1
                                ÿÿ2.ÿÿÿÿÿÿÿÿÿsyntax
                                ÿÿ3.ÿ
                                .ÿÿÿÿÿÿÿÿÿreturnÿscalarÿpÿ=ÿ.
                                ÿÿ4.ÿend

                                .ÿ
                                .ÿprogramÿdefineÿshowem
                                ÿÿ1.ÿÿÿÿÿÿÿÿÿversionÿ15.1
                                ÿÿ2.ÿÿÿÿÿÿÿÿÿsyntaxÿ,ÿc(integer)ÿd(real)ÿ[Reps(integerÿ40)]
                                ÿÿ3.ÿ
                                .ÿÿÿÿÿÿÿÿÿquietlyÿsimulateÿpÿ=ÿr(p),ÿreps(`reps'):ÿsimemÿ,ÿc(`c')ÿd(`d')
                                ÿÿ4.ÿÿÿÿÿÿÿÿÿgenerateÿbyteÿposÿ=ÿpÿ<ÿ0.05ÿifÿ!missing(p)
                                ÿÿ5.ÿÿÿÿÿÿÿÿÿsummarizeÿpos,ÿmeanonly
                                ÿÿ6.ÿ
                                .ÿÿÿÿÿÿÿÿÿdisplayÿinÿsmclÿasÿtextÿ_newline(1)
                                ÿÿ7.ÿ
                                .ÿÿÿÿÿÿÿÿÿifÿr(N)ÿ<ÿ`reps'ÿdisplayÿinÿsmclÿasÿerrorÿ`reps'ÿ-ÿr(N)ÿ"ÿfailed-convergenceÿreplicates"
                                ÿÿ8.ÿ
                                .ÿÿÿÿÿÿÿÿÿdisplayÿinÿsmclÿasÿtextÿ"Clinics"ÿ_column(10)ÿ"Delta"ÿ_column(17)ÿ"RejectionÿRate"
                                ÿÿ9.ÿÿÿÿÿÿÿÿÿdisplayÿinÿsmclÿasÿtextÿ%4.0fÿ`c'ÿ*ÿ5ÿ_column(9)ÿ%3.1gÿ`d'ÿ_column(20)ÿasÿresultÿ%04.2fÿr(mean)
                                ÿ10.ÿend

                                .ÿ
                                .ÿtimerÿclearÿ1

                                .ÿtimerÿonÿ1

                                .ÿ
                                .ÿ//ÿTestÿsize
                                .ÿshowemÿ,ÿc(10)ÿd(0)ÿreps(300)


                                ClinicsÿÿDeltaÿÿRejectionÿRate
                                ÿÿ50ÿÿÿÿÿÿÿ0ÿÿÿÿÿÿÿ0.04

                                .ÿ
                                .ÿtimerÿoffÿ1

                                .ÿ
                                .ÿtimerÿclearÿ2

                                .ÿtimerÿonÿ2

                                .ÿ
                                .ÿ//ÿPowerÿ(atÿdeltaÿ=ÿ5)
                                .ÿforvaluesÿclinic_tallyÿ=ÿ6(2)10ÿ{
                                ÿÿ2.ÿÿÿÿÿÿÿÿÿshowemÿ,ÿc(`clinic_tally')ÿd(5)ÿreps(100)
                                ÿÿ3.ÿ}


                                ClinicsÿÿDeltaÿÿRejectionÿRate
                                ÿÿ30ÿÿÿÿÿÿÿ5ÿÿÿÿÿÿÿ0.66


                                ClinicsÿÿDeltaÿÿRejectionÿRate
                                ÿÿ40ÿÿÿÿÿÿÿ5ÿÿÿÿÿÿÿ0.84


                                ClinicsÿÿDeltaÿÿRejectionÿRate
                                ÿÿ50ÿÿÿÿÿÿÿ5ÿÿÿÿÿÿÿ0.89

                                .ÿ
                                .ÿtimerÿoffÿ2

                                .ÿ
                                .ÿtimerÿlistÿ1
                                ÿÿÿ1:ÿÿÿ3696.21ÿ/ÿÿÿÿÿÿÿÿ1ÿ=ÿÿÿÿ3696.2130

                                .ÿtimerÿlistÿ2
                                ÿÿÿ2:ÿÿÿ2439.49ÿ/ÿÿÿÿÿÿÿÿ1ÿ=ÿÿÿÿ2439.4920

                                .ÿ
                                .ÿlogÿcloseÿlo

                                .ÿ
                                .ÿexit

                                endÿofÿdo-file


                                .


                                Notes:
                                1. In the example, I hardcoded all of your magic numbers, including in the calling program (where the five randomization groups are hardcoded in in order to compute total count of clincs). If you want to play around with assumptions for sensitivity analysis, either make manual changes in the hardcoded values or add them as named options—if the latter, then I recommend avoiding Stata's -args- syntax for passing arguments to commands.
                                2. I see that you’re using Release 15.1 and have indicated that throughout; if you’ve upgraded recently, just change the -version- specifications.
                                3. I have included randomization group (cohort of clinics that is randomly crossed over to treatment at each visit) as a categorical predictor in the model when it and clinic are not collinear, that is, when there is more than one clinic to a cohort. Also, I have only a passing familiarity with the stepped-wedge design, and so don’t know whether treatment × visit, treatment × randomization group or other interaction terms are typically included in statistical models of them, but, following your example, I have not included them here.
                                Attached Files

                                Comment

                                Working...
                                X