Announcement

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

  • Question on linear regression with multiple conditions

    I am supposed to generate a linear regression with multiple conditions.

    Data is as shown below:

    Code:
    input byte id float time int conc str8 diet float(elim pred lnalcohol pred_rep)
    1    3  75 "noGuar"   .31319055 4.3103285  4.317488 4.4182615
    1  3.5  64 "noGuar"   .31319055 4.1537333  4.158883 4.2178164
    1    4  53 "noGuar"   .31319055  3.997138  3.970292 4.0173717
    1  4.5  47 "noGuar"   .31319055  3.840543 3.8501475  3.816927
    1    5  40 "noGuar"   .31319055 3.6839476 3.6888795  3.616482
    1  3.5  65 "withGuar" .28823623   4.18296 4.1743875 4.2178164
    1    4  57 "withGuar" .28823623 4.0388417 4.0430512 4.0173717
    1  4.5  50 "withGuar" .28823623  3.894724  3.912023  3.816927
    1    5  42 "withGuar" .28823623 3.7506056   3.73767  3.616482
    2    3  82 "noGuar"    .4008896 4.4182615  4.406719 4.4182615
    2  3.5  68 "noGuar"    .4008896 4.2178164 4.2195077 4.2178164
    2    4  57 "noGuar"    .4008896 4.0173717 4.0430512 4.0173717
    2  4.5  45 "noGuar"    .4008896  3.816927 3.8066626  3.816927
    2    5  37 "noGuar"    .4008896  3.616482  3.610918  3.616482
    2 1.33 122 "withGuar" .22233166  4.846237  4.804021  5.087747
    2 1.67 124 "withGuar" .22233166  4.770644  4.820282  4.951445
    2    2 108 "withGuar" .22233166  4.697274 4.6821313  4.819151
    2  2.5 103 "withGuar" .22233166 4.5861087  4.634729  4.618706
    2    3  92 "withGuar" .22233166 4.4749427 4.5217886 4.4182615
    2  3.5  65 "withGuar" .22233166 4.3637767 4.1743875 4.2178164
    2    4  75 "withGuar" .22233166  4.252611  4.317488 4.0173717
    2  4.5  68 "withGuar" .22233166  4.141445 4.2195077  3.816927
    2    5  54 "withGuar" .22233166  4.030279  3.988984  3.616482
    3    4  17 "noGuar"   1.2237754    2.8603  2.833213 4.0173717
    3  4.5  10 "noGuar"   1.2237754 2.2484121 2.3025851  3.816927
    3    5   5 "noGuar"   1.2237754 1.6365244  1.609438  3.616482
    3    4  26 "withGuar"  .9555115 3.2947736 3.2580965 4.0173717
    3  4.5  18 "withGuar"  .9555115  2.817018  2.890372  3.816927
    3    5  10 "withGuar"  .9555115  2.339262 2.3025851  3.616482
    4  3.5  37 "noGuar"   .57818496  3.668399  3.610918 4.2178164
    4    4  30 "noGuar"   .57818496 3.3793066 3.4011974 4.0173717
    4  4.5  25 "noGuar"   .57818496  3.090214  3.218876  3.816927
    4    5  15 "noGuar"   .57818496  2.801122   2.70805  3.616482
    4  2.5  60 "withGuar"  .3793953 4.0677114 4.0943446  4.618706
    4    3  46 "withGuar"  .3793953  3.878014 3.8286414 4.4182615
    4  3.5  40 "withGuar"  .3793953  3.688316 3.6888795 4.2178164
    4    4  35 "withGuar"  .3793953 3.4986186  3.555348 4.0173717
    4  4.5  26 "withGuar"  .3793953  3.308921 3.2580965  3.816927
    4    5  23 "withGuar"  .3793953  3.119223  3.135494  3.616482
    5    4  20 "noGuar"    1.609438  3.032923  2.995732 4.0173717
    5  4.5  10 "noGuar"    1.609438  2.228204 2.3025851  3.816927
    5    5   4 "noGuar"    1.609438  1.423485 1.3862944  3.616482
    5    4  22 "withGuar"  1.299283  3.105544 3.0910425 4.0173717
    5  4.5  12 "withGuar"  1.299283  2.455903  2.484907  3.816927
    5    5   6 "withGuar"  1.299283 1.8062614 1.7917595  3.616482
    6    4  25 "noGuar"    .9162908 3.2620895  3.218876 4.0173717
    6  4.5  18 "noGuar"    .9162908  2.803944  2.890372  3.816927
    6    5  10 "noGuar"    .9162908  2.345799 2.3025851  3.616482
    6 1.67  50 "withGuar"  .3639005  3.892754  3.912023  4.951445
    6    2  42 "withGuar"  .3639005  3.772667   3.73767  4.819151
    6  2.5  32 "withGuar"  .3639005  3.590717  3.465736  4.618706
    6    3  35 "withGuar"  .3639005 3.4087665  3.555348 4.4182615
    6  3.5  26 "withGuar"  .3639005  3.226816 3.2580965 4.2178164
    6    4  23 "withGuar"  .3639005  3.044866  3.135494 4.0173717
    6  4.5  15 "withGuar"  .3639005  2.862916   2.70805  3.816927
    6    5  15 "withGuar"  .3639005 2.6809654   2.70805  3.616482
    7    4  40 "noGuar"    .3566749  3.703815 3.6888795 4.0173717
    7  4.5  35 "noGuar"    .3566749 3.5254774  3.555348  3.816927
    7    5  28 "noGuar"    .3566749   3.34714 3.3322046  3.616482
    7    3  61 "withGuar"  .1965659  4.101937 4.1108737 4.4182615
    7  3.5  55 "withGuar"  .1965659  4.003654 4.0073333 4.2178164
    7    4  48 "withGuar"  .1965659  3.905371  3.871201 4.0173717
    7  4.5  46 "withGuar"  .1965659  3.807088 3.8286414  3.816927
    end
    My current code:

    Code:
    regress lnalcohol time if diet == "withGuar" & id == 1 
    regress lnalcohol time if diet == "noGuar" & id == 1
    regress lnalcohol time if diet == "withGuar" & id == 2
    regress lnalcohol time if diet == "noGuar" & id == 2
    Code:
    predict pred_rep, xb
    I am supposed to verify pred, with the new variable, pred_rep which is similar to pred. To do this, I need to create a regression line. The current problem is that I only get the correct pred_rep values for the last line of code. In this case,
    Code:
    regress lnalcohol time if diet == "noGuar" & id == 2
    gives me the correct output for pred_rep but not for the other diet/id combinations.

    How do I combine the regression codes above to form one output to resolve this problem?

  • #2
    Originally posted by Goh jedrek View Post
    I am supposed to verify pred, with the new variable . . .
    How do I combine the regression codes above to form one output to resolve this problem?
    Something like the following should work.

    .ÿ
    .ÿversionÿ17.0

    .ÿ
    .ÿclearÿ*

    .ÿ
    .ÿquietlyÿinputÿbyteÿidÿfloatÿtimeÿintÿconcÿstr8ÿdietÿ///
    >ÿÿÿÿÿÿÿÿÿfloat(elimÿpredÿlnalcoholÿpred_rep)

    .ÿ
    .ÿlabelÿdefineÿGuarStatusÿ0ÿnoGuarÿ1ÿwithGuar

    .ÿencodeÿdiet,ÿgenerate(dit)ÿlabel(GuarStatus)ÿnoextend

    .ÿ
    .ÿregressÿlnalcoholÿc.time##i.dit##i.idÿifÿinlist(id,ÿ1,ÿ2)

    ÿÿÿÿÿÿSourceÿ|ÿÿÿÿÿÿÿSSÿÿÿÿÿÿÿÿÿÿÿdfÿÿÿÿÿÿÿMSÿÿÿÿÿÿNumberÿofÿobsÿÿÿ=ÿÿÿÿÿÿÿÿ23
    -------------+----------------------------------ÿÿÿF(7,ÿ15)ÿÿÿÿÿÿÿÿ=ÿÿÿÿÿ94.19
    ÿÿÿÿÿÿÿModelÿ|ÿÿ2.60723948ÿÿÿÿÿÿÿÿÿ7ÿÿ.372462783ÿÿÿProbÿ>ÿFÿÿÿÿÿÿÿÿ=ÿÿÿÿ0.0000
    ÿÿÿÿResidualÿ|ÿÿÿ.05931505ÿÿÿÿÿÿÿÿ15ÿÿ.003954337ÿÿÿR-squaredÿÿÿÿÿÿÿ=ÿÿÿÿ0.9778
    -------------+----------------------------------ÿÿÿAdjÿR-squaredÿÿÿ=ÿÿÿÿ0.9674
    ÿÿÿÿÿÿÿTotalÿ|ÿÿ2.66655453ÿÿÿÿÿÿÿÿ22ÿÿ.121207024ÿÿÿRootÿMSEÿÿÿÿÿÿÿÿ=ÿÿÿÿ.06288

    -------------------------------------------------------------------------------
    ÿÿÿÿlnalcoholÿ|ÿCoefficientÿÿStd.ÿerr.ÿÿÿÿÿÿtÿÿÿÿP>|t|ÿÿÿÿÿ[95%ÿconf.ÿinterval]
    --------------+----------------------------------------------------------------
    ÿÿÿÿÿÿÿÿÿtimeÿ|ÿÿ-.3131906ÿÿÿÿ.039771ÿÿÿÿ-7.87ÿÿÿ0.000ÿÿÿÿ-.3979605ÿÿÿ-.2284207
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
    ÿÿÿÿÿÿÿÿÿÿditÿ|
    ÿÿÿÿwithGuarÿÿ|ÿÿÿ-.058114ÿÿÿ.2902195ÿÿÿÿ-0.20ÿÿÿ0.844ÿÿÿÿ-.6767022ÿÿÿÿ.5604743
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
    ÿÿÿdit#c.timeÿ|
    ÿÿÿÿwithGuarÿÿ|ÿÿÿ.0249545ÿÿÿ.0688854ÿÿÿÿÿ0.36ÿÿÿ0.722ÿÿÿÿ-.1218714ÿÿÿÿ.1717803
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
    ÿÿÿÿÿÿÿÿÿ2.idÿ|ÿÿÿ.3710292ÿÿÿ.2284672ÿÿÿÿÿ1.62ÿÿÿ0.125ÿÿÿÿÿ-.115937ÿÿÿÿ.8579955
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
    ÿÿÿÿid#c.timeÿ|
    ÿÿÿÿÿÿÿÿÿÿÿ2ÿÿ|ÿÿ-.0876989ÿÿÿ.0562447ÿÿÿÿ-1.56ÿÿÿ0.140ÿÿÿÿ-.2075817ÿÿÿÿ.0321839
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
    ÿÿÿÿÿÿÿdit#idÿ|
    ÿÿwithGuar#2ÿÿ|ÿÿÿ-.420878ÿÿÿÿ.336965ÿÿÿÿ-1.25ÿÿÿ0.231ÿÿÿÿ-1.139102ÿÿÿÿÿ.297346
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
    dit#id#c.timeÿ|
    ÿÿwithGuar#2ÿÿ|ÿÿÿ.1536034ÿÿÿ.0813923ÿÿÿÿÿ1.89ÿÿÿ0.079ÿÿÿÿ-.0198802ÿÿÿÿ.3270869
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
    ÿÿÿÿÿÿÿÿ_consÿ|ÿÿÿÿÿ5.2499ÿÿÿ.1615507ÿÿÿÿ32.50ÿÿÿ0.000ÿÿÿÿÿ4.905563ÿÿÿÿ5.594238
    -------------------------------------------------------------------------------

    .ÿpredictÿdoubleÿxb,ÿxb

    .ÿ
    .ÿassertÿround(pred,ÿ0.00001)ÿ==ÿround(xb,ÿ0.00001)ÿifÿinlist(id,ÿ1,ÿ2)

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .

    Comment


    • #3
      Originally posted by Joseph Coveney View Post
      Something like the following should work.

      .ÿ
      .ÿversionÿ17.0

      .ÿ
      .ÿclearÿ*

      .ÿ
      .ÿquietlyÿinputÿbyteÿidÿfloatÿtimeÿintÿconcÿstr8ÿdietÿ///
      >ÿÿÿÿÿÿÿÿÿfloat(elimÿpredÿlnalcoholÿpred_rep)

      .ÿ
      .ÿlabelÿdefineÿGuarStatusÿ0ÿnoGuarÿ1ÿwithGuar

      .ÿencodeÿdiet,ÿgenerate(dit)ÿlabel(GuarStatus)ÿnoextend

      .ÿ
      .ÿregressÿlnalcoholÿc.time##i.dit##i.idÿifÿinlist(id,ÿ1,ÿ2)

      ÿÿÿÿÿÿSourceÿ|ÿÿÿÿÿÿÿSSÿÿÿÿÿÿÿÿÿÿÿdfÿÿÿÿÿÿÿMSÿÿÿÿÿÿNumberÿofÿobsÿÿÿ=ÿÿÿÿÿÿÿÿ23
      -------------+----------------------------------ÿÿÿF(7,ÿ15)ÿÿÿÿÿÿÿÿ=ÿÿÿÿÿ94.19
      ÿÿÿÿÿÿÿModelÿ|ÿÿ2.60723948ÿÿÿÿÿÿÿÿÿ7ÿÿ.372462783ÿÿÿProbÿ>ÿFÿÿÿÿÿÿÿÿ=ÿÿÿÿ0.0000
      ÿÿÿÿResidualÿ|ÿÿÿ.05931505ÿÿÿÿÿÿÿÿ15ÿÿ.003954337ÿÿÿR-squaredÿÿÿÿÿÿÿ=ÿÿÿÿ0.9778
      -------------+----------------------------------ÿÿÿAdjÿR-squaredÿÿÿ=ÿÿÿÿ0.9674
      ÿÿÿÿÿÿÿTotalÿ|ÿÿ2.66655453ÿÿÿÿÿÿÿÿ22ÿÿ.121207024ÿÿÿRootÿMSEÿÿÿÿÿÿÿÿ=ÿÿÿÿ.06288

      -------------------------------------------------------------------------------
      ÿÿÿÿlnalcoholÿ|ÿCoefficientÿÿStd.ÿerr.ÿÿÿÿÿÿtÿÿÿÿP>|t|ÿÿÿÿÿ[95%ÿconf.ÿinterval]
      --------------+----------------------------------------------------------------
      ÿÿÿÿÿÿÿÿÿtimeÿ|ÿÿ-.3131906ÿÿÿÿ.039771ÿÿÿÿ-7.87ÿÿÿ0.000ÿÿÿÿ-.3979605ÿÿÿ-.2284207
      ÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
      ÿÿÿÿÿÿÿÿÿÿditÿ|
      ÿÿÿÿwithGuarÿÿ|ÿÿÿ-.058114ÿÿÿ.2902195ÿÿÿÿ-0.20ÿÿÿ0.844ÿÿÿÿ-.6767022ÿÿÿÿ.5604743
      ÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
      ÿÿÿdit#c.timeÿ|
      ÿÿÿÿwithGuarÿÿ|ÿÿÿ.0249545ÿÿÿ.0688854ÿÿÿÿÿ0.36ÿÿÿ0.722ÿÿÿÿ-.1218714ÿÿÿÿ.1717803
      ÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
      ÿÿÿÿÿÿÿÿÿ2.idÿ|ÿÿÿ.3710292ÿÿÿ.2284672ÿÿÿÿÿ1.62ÿÿÿ0.125ÿÿÿÿÿ-.115937ÿÿÿÿ.8579955
      ÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
      ÿÿÿÿid#c.timeÿ|
      ÿÿÿÿÿÿÿÿÿÿÿ2ÿÿ|ÿÿ-.0876989ÿÿÿ.0562447ÿÿÿÿ-1.56ÿÿÿ0.140ÿÿÿÿ-.2075817ÿÿÿÿ.0321839
      ÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
      ÿÿÿÿÿÿÿdit#idÿ|
      ÿÿwithGuar#2ÿÿ|ÿÿÿ-.420878ÿÿÿÿ.336965ÿÿÿÿ-1.25ÿÿÿ0.231ÿÿÿÿ-1.139102ÿÿÿÿÿ.297346
      ÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
      dit#id#c.timeÿ|
      ÿÿwithGuar#2ÿÿ|ÿÿÿ.1536034ÿÿÿ.0813923ÿÿÿÿÿ1.89ÿÿÿ0.079ÿÿÿÿ-.0198802ÿÿÿÿ.3270869
      ÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
      ÿÿÿÿÿÿÿÿ_consÿ|ÿÿÿÿÿ5.2499ÿÿÿ.1615507ÿÿÿÿ32.50ÿÿÿ0.000ÿÿÿÿÿ4.905563ÿÿÿÿ5.594238
      -------------------------------------------------------------------------------

      .ÿpredictÿdoubleÿxb,ÿxb

      .ÿ
      .ÿassertÿround(pred,ÿ0.00001)ÿ==ÿround(xb,ÿ0.00001)ÿifÿinlist(id,ÿ1,ÿ2)

      .ÿ
      .ÿexit

      endÿofÿdo-file


      .
      I have tried this and it doesn't work.

      My issue is with grouping the variables together for the predict command to give the correct output.

      I have tried doing this and it still doesn't work

      Code:
      gen participant_dietgroup = .
      replace participant_dietgroup = 1 if diet == "withGuar" & id == 1 
      replace participant_dietgroup = 2 if diet == "noGuar" & id == 1
      replace participant_dietgroup = 3 if diet == "withGuar" & id == 2
      replace participant_dietgroup = 4 if diet == "noGuar" & id == 2
      replace participant_dietgroup = 5 if diet == "withGuar" & id == 3 
      replace participant_dietgroup = 6 if diet == "noGuar" & id == 3
      replace participant_dietgroup = 7 if diet == "withGuar" & id == 4
      replace participant_dietgroup = 8 if diet == "noGuar" & id == 4
      Code:
      regress lnalcohol time if participant_dietgroup == 1 & participant_dietgroup == 2
      I get the error 2000 and if I regress for just the first group, then I only get accurate output for the first group, while the other groups are inaccurate.

      Comment


      • #4
        Originally posted by Goh jedrek View Post

        I have tried this and it doesn't work.
        Um, what I showed is output, ergo, I just showed you that it does work.

        My issue is with grouping the variables together for the predict command to give the correct output.

        I have tried doing this and it still doesn't work[code redacted]
        You don't need to do that. In fact, as you discovered, if you try to do that, then it will thwart your intention.

        Instead, form the proper interaction terms and fit a single model, just as I showed above. Then there need be only a single -predict- postestimation command, and it will match your earlier predict variable, just as I showed it does in your example.

        Comment

        Working...
        X