Announcement

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

  • ANCOVA plot of observed and predicted values

    Hi,
    When I try to get the graph of observed and predicted values ​​after an ANCOVA model I get this graph that I show below. I'm expecting two straight lines for predicted values. Could someone tell me how to fix it?. Thank you for your help Graph1gph.gph

  • #2
    What commands did you use for your model fitting and graphics?

    Comment


    • #3
      Thank you for the answer. I have variable Group with values 1 = B41 and 0 = B42, a dependent variable with baseline value AngFaseB and a final value AngFase4.The commands are:

      regress AngFase4 Group Hospital1 c.AngFaseB Group#c.AngFaseB

      predict Predict1 if Group==1, xb
      predict Predict2 if Group==0, xb

      twoway (scatter B41 AngFaseB if Group==1, sort mcolor(blue) msymbol(circle)) (scatter B42 AngFaseB if Group==0, sort mcolor(red) msymbol(circle)) (line Predict1 AngFaseB , sort lcolor(blue)) (line Predict2 AngFaseB , sort lcolor(red)), graphregion(fcolor(white) ifcolor(white)) plotregion(fcolor(white) ifcolor(white))

      Comment


      • #4
        Plot the predictions separately for each hospital, one a solid line and the other a dashed line.

        Assuming Hospital1 is coded 0/1, do something like the following.
        Code:
        regress AngFase4 i.Group#c.AngFaseB i.Hospital1
        predict double xb, xb
        
        graph twoway ///
            line xb AngFaseB if Group & !Hospital1, sort lcolor(blue) ///
                line xb AngFaseB if Group & Hospital, sort lcolor(blue) lpattern(dash) ///
            line xb AngFaseB if !Group & !Hospital1, sort lcolor(red) ///
                line xb AngFaseB if !Group & Hospital1, sort lcolor(red) lpattern(dash) ///
            scatter B41 AngFaseB if Group, mcolor(blue) msymbol(circle) ///
            scatter B42 AngFaseB if !Group, mcolor(red) msymbol(circle), ///
            graphregion(fcolor(white) ifcolor(white) plotregion(fcolor(white) ifcolor(white)

        Comment


        • #5
          By the way, what are B41 and B42, again? Are they the values of the outcome variable separately for the two groups?

          If so, then you can just plot the outcome variable, itself, separately by group.
          Code:
          regress AngFase4 i.Group#c.AngFaseB i.Hospital1
          predict double xb, xb
          
          graph twoway ///
              line xb AngFaseB if Group & !Hospital1, sort lcolor(blue) ///
                  line xb AngFaseB if Group & Hospital, sort lcolor(blue) lpattern(dash) ///
              line xb AngFaseB if !Group & !Hospital1, sort lcolor(red) ///
                  line xb AngFaseB if !Group & Hospital1, sort lcolor(red) lpattern(dash) ///
              scatter AngFase4 AngFaseB if Group, mcolor(blue) msymbol(circle) ///
              scatter AngFase4 AngFaseB if !Group, mcolor(red) msymbol(circle), ///
              graphregion(fcolor(white) ifcolor(white) plotregion(fcolor(white) ifcolor(white)

          Comment


          • #6
            Hi Joseph,
            Thank ypou very much for the answer. It is not necessary to include Hospital because there is no significance. In the outpud an error appears:

            first appears parentheses do not balance. I corrected putting parenthesis in graphregion and plotregion

            second appears invalid 'sort'




            Comment


            • #7
              Joseph Coveney's helpful code needs || separators between commands, I think.

              Comment


              • #8
                Originally posted by Nick Cox View Post
                Joseph Coveney's helpful code needs || separators between commands, I think.
                Yes, sorry about that. It's difficult to debug code without data to test it against.

                Comment


                • #9
                  Originally posted by Jorge Ruiz Moreno View Post
                  It is not necessary to include Hospital because there is no significance.
                  You need to plot separately by hospital because its regression coefficient is not zero and therefore predictions will differ between hospitals. That's why you see zigzag prediction lines when you plot predictions from both hospitals together..

                  Comment


                  • #10
                    Thank you to both. I put the separators. I'm including graphGraph2.gph

                    Comment


                    • #11
                      The result is the same using:


                      graph twoway line xb AngFaseB if Group ==1, sort lcolor(blue) || line xb AngFaseB if Group == 0, sort lcolor(blue) lpattern(dash) || scatter AngFase4 AngFaseB if Group == 1, mcolor(blue) msymbol(circle) || scatter AngFase4 AngFaseB if Group == 0, mcolor(red) msymbol(circle)

                      Comment


                      • #12
                        Originally posted by Jorge Ruiz Moreno View Post
                        The result is the same using:
                        You're not plotting the predictions separately by hospital. Within each group, plot the predictions separately by hospital, with one line solid and the other dashed. You should have four lines: two red lines, one solid and one dashed, and two blue lines, one solid and one dashed.
                        Code:
                        graph twoway ///
                            line xb AngFaseB if Group == 1 & Hospital1 == 0, sort lcolor(blue) || ///
                            line xb AngFaseB if Group == 1 & Hospital1 == 1, sort lcolor(blue) lpattern(dash) || ///
                            line xb AngFaseB if Group == 0 & Hospital1 == 0, sort lcolor(red) || ///
                            line xb AngFaseB if Group == 0 & Hospital1 == 1, sort lcolor(red) lpattern(dash) || ///
                            scatter AngFase4 AngFaseB if Group == 1, mcolor(blue) msymbol(circle) || ///
                            scatter AngFase4 AngFaseB if Group == 0, mcolor(red) msymbol(circle)

                        Comment


                        • #13
                          If you're still having difficulties or you are not interested in plotting the parallel lines, and you're convinced that hospital as a predictor can be ignored, then you can re-fit the ANCOVA model omitting hospital as a predictor. Predictions will then not have the zigzag problem.

                          Alternatively, there have been suggestions previously on the list to the effect of preserving the dataset and replacing the Hospital1 variable with 0.5 before issuing the predict command to generate the predictions for plotting.

                          Comment


                          • #14
                            Now it works. Thank you for your very valuable help!

                            Comment

                            Working...
                            X