Announcement

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

  • 95% CI and PI after linear regression

    Hi, Only one of the most important three parameters was shown after multiple-linear regression, the betas.

    I want to get the 95% CI of population mean(i), and 95% PI of the interested variable, but Stata does not say how.

    95% CI:



    and 95% PI


    Tom
    Last edited by Tom Hsiung; 19 Nov 2017, 07:22.

  • #2
    Hello Tom. I think you will find this old thread useful. Pay attention to the distinction between stdp (standard error of the linear prediction) and stdf (standard error of the forecast). Cheers,
    Bruce
    --
    Bruce Weaver
    Email: [email protected]
    Version: Stata/MP 18.5 (Windows)

    Comment


    • #3
      I don't quite understand the methods in that thread. Seems like there is no a single syntax to compute the 95% CI of mean and 95%PI.

      Comment


      • #4
        Perhaps this will help. The following code pulls together bits and pieces from a few posts in that thread.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input int(x y)
         80 399
         30 121
         50 221
         90 376
         70 361
         60 224
        120 546
         80 352
        100 353
         50 157
         40 160
         70 252
         90 389
         20 113
        110 435
        100 420
         30 212
         50 268
         90 377
        110 421
         30 273
         90 468
         40 244
         80 342
         70 323
        end
        
        regress y x
        matrix table = r(table)
        scalar tcrit = table[8,1]
        predict yhat       // fitted value of Y
        predict stdp, stdp // SE of linear prediction
        predict stdf, stdf // SE of the forecast
        * Compute CI for conditional mean
        generate lowerp = yhat - tcrit*stdp
        generate upperp = yhat + tcrit*stdp
        * Compute prediction interval
        generate lowerf = yhat - tcrit*stdf
        generate upperf = yhat + tcrit*stdf
        list
        
        twoway lfitci y x,  stdf ciplot(rline) color(gray)   ///
            || lfitci y x, lpattern(solid)                 ///
            || scatter y x, ytitle(Y) xtitle(X) legend(off)
        Click image for larger version

Name:	scatterplot_CI_PI.png
Views:	1
Size:	36.0 KB
ID:	1418829
        --
        Bruce Weaver
        Email: [email protected]
        Version: Stata/MP 18.5 (Windows)

        Comment


        • #5
          Have to generate the predict value? What does the command of margin do, 95% CI of conditional means?

          Comment


          • #6
            I need to compare 95% CI for conditional means and 95% PI for response variable between two levels (0 vs 1) of qualitative independent variable. Bruce's method generates 95% CI and 95% PI for every single observation.

            With the command 'margins' after regression, I get a 95% confidence interval. Is this the 95% CI of conditional means of qualitative independent variable at two levels (0 vs 1), respectively? marginsplot generates a graph.

            PS: In my paper, I plant to do separate t-test between two levels of each qualitative independent variables first, along with the 95%CI of population means of both group, respectively (0 vs 1). Then I do a multiple-linear regression, along with the 95% CI of conditional means of both group, respectively. The 95% CI of population means by t-test certainly are different from these by multiply-linear regression, I think.

            Tom

            Code:
            margins chf
            
            Predictive margins                              Number of obs     =        175
            Model VCE    : OLS
            
            Expression   : Linear prediction, predict()
            
            ------------------------------------------------------------------------------
                         |            Delta-method
                         |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
            -------------+----------------------------------------------------------------
                     chf |
                      0  |   2.725683   .0574121    47.48   0.000      2.61231    2.839055
                      1  |   2.450355   .1338941    18.30   0.000     2.185953    2.714758
            ------------------------------------------------------------------------------
            Code:
            marginsplot
            Click image for larger version

Name:	1.png
Views:	1
Size:	94.0 KB
ID:	1419246

            Attached Files
            Last edited by Tom Hsiung; 22 Nov 2017, 07:09.

            Comment


            • #7
              In #6, Tom wrote:

              With the command 'margins' after regression, I get a 95% confidence interval. Is this the 95% CI of conditional means of qualitative independent variable at two levels (0 vs 1), respectively? marginsplot generates a graph.
              Yes, the CI you get from margins & marginsplot is the CI for the mean response. Here is an elaboration of the example I posted in #4 that adds margins & marginsplot.

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input int(x y)
               80 399
               30 121
               50 221
               90 376
               70 361
               60 224
              120 546
               80 352
              100 353
               50 157
               40 160
               70 252
               90 389
               20 113
              110 435
              100 420
               30 212
               50 268
               90 377
              110 421
               30 273
               90 468
               40 244
               80 342
               70 323
              end
              
              regress y x
              matrix table = r(table)
              scalar tcrit = table[8,1]
              predict yhat       // fitted value of Y
              predict stdp, stdp // SE of linear prediction
              predict stdf, stdf // SE of the forecast
              * Compute CI for conditional mean
              generate lowerp = yhat - tcrit*stdp
              generate upperp = yhat + tcrit*stdp
              * Compute prediction interval
              generate lowerf = yhat - tcrit*stdf
              generate upperf = yhat + tcrit*stdf
              list
              
              twoway lfitci y x,  stdf ciplot(rline) color(gray)   ///
                  || lfitci y x, lpattern(solid)                 ///
                  || scatter y x, ytitle(Y) xtitle(X) legend(off) ylabel(0(100)600)
              graph export "C:\Temp\graph1.png", as(png) replace
              
              * Now use -margins- & -marginsplot
              quietly margins, at(x=(20(1)120))    
              marginsplot, xlabel(20(20)120) ///
              recast(line) recastci(rarea) ciopts(color(*.2))
              graph export "C:\Temp\graph2.png", as(png) replace
              
              * The CI produced via -margins- and -marginsplot- matches
              * the CI computed using the stdp option for -predict-.
              * I.e., it is the CI for the mean response.
              Here is the first graph (using predict and twoway):

              Click image for larger version

Name:	graph1.png
Views:	1
Size:	46.2 KB
ID:	1419343


              And here is the second graph (using margins & marginsplot):

              Click image for larger version

Name:	graph2.png
Views:	1
Size:	43.6 KB
ID:	1419344
              --
              Bruce Weaver
              Email: [email protected]
              Version: Stata/MP 18.5 (Windows)

              Comment


              • #8
                Thanks, Bruce

                OK, 95% CI of conditional means is what I want to get. I do not need 95% PI as my intention is to test whether dummy independent variables have significant impact on response variable, rather than to predict the individual response variable value.

                Tom

                Comment

                Working...
                X