Announcement

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

  • Creating country specific linear and quadratic time trends

    Hi everyone,

    I was trying to include linear and quadratic id specific time trends in my model (separately in two different models). I used the following code. Can anyone confirm if I did the correct code? Any help would be highly appreciated:

    Linear time trend: i.Country##c.Year
    Quadratic time trend: ***Generating variable for quadratic time trend.
    egen Year1=group(Year)
    gen Year2=Year1^2
    i.Country1##c.Year2

    One additional question I was facing was, how will the results differ if I use one "#" instead of two "##"? Thank you!


  • #2
    No, country specific quadratic trends: i.Country##c.Year##c.Year
    Last edited by Maarten Buis; 15 Nov 2020, 02:16.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Originally posted by Maarten Buis View Post
      No, country specific quadratic trends: i.Country##c.Year##c.Year
      Hi, Thanks for your response! Why do we need to include "##"? this would give me an interaction of Country1#c.Year. However, I just need need the quadratic time trends. So should both the linear and quadratic time trends not contain only "#" insteadof "##" ?

      Comment


      • #4
        Originally posted by Himani Vardhan View Post
        Why do we need to include "##"?. . . I just need need the quadratic time trends. So should both the linear and quadratic time trends not contain only "#" insteadof "##" ?
        You know, you can try things out for yourself easily enough in Stata, creating compact toy examples. See an illustration below. (I tried to read your mind as to what you're suggesting as the alternative to what Maarten recommended. If it's not quite what you had in mind, then you can easily modify the code or create new code that does do what you're trying to suggest.)

        .ÿ
        .ÿversionÿ16.1

        .ÿ
        .ÿclearÿ*

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

        .ÿ
        .ÿquietlyÿsetÿobsÿ20

        .ÿgenerateÿbyteÿcidÿ=ÿmod(_n,ÿ2)

        .ÿ
        .ÿbysortÿcid:ÿgenerateÿbyteÿtimÿ=ÿ_n

        .ÿ
        .ÿgenerateÿdoubleÿoutÿ=ÿ///
        >ÿÿÿÿÿÿÿÿÿ(cidÿ-ÿ0.5)ÿ*ÿ2ÿ+ÿ///
        >ÿÿÿÿÿÿÿÿÿ(cidÿ-ÿ0.5)ÿ*ÿ(timÿ-ÿ5.5)ÿ+ÿ///
        >ÿÿÿÿÿÿÿÿÿ(cidÿ-ÿ0.5)ÿ*ÿ(timÿ-ÿ5.5)ÿ*ÿ(timÿ-ÿ5.5)ÿ+ÿ///
        >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿrnormal()

        .ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
        .ÿ*
        .ÿ*ÿBeginÿhere
        .ÿ*
        .ÿ//ÿMaarten'sÿrecommendation
        .ÿregressÿoutÿi.cid##c.tim##c.tim

        ÿÿÿÿÿÿSourceÿ|ÿÿÿÿÿÿÿSSÿÿÿÿÿÿÿÿÿÿÿdfÿÿÿÿÿÿÿMSÿÿÿÿÿÿNumberÿofÿobsÿÿÿ=ÿÿÿÿÿÿÿÿ20
        -------------+----------------------------------ÿÿÿF(5,ÿ14)ÿÿÿÿÿÿÿÿ=ÿÿÿÿ110.31
        ÿÿÿÿÿÿÿModelÿ|ÿÿ825.864045ÿÿÿÿÿÿÿÿÿ5ÿÿ165.172809ÿÿÿProbÿ>ÿFÿÿÿÿÿÿÿÿ=ÿÿÿÿ0.0000
        ÿÿÿÿResidualÿ|ÿÿ20.9622647ÿÿÿÿÿÿÿÿ14ÿÿ1.49730462ÿÿÿR-squaredÿÿÿÿÿÿÿ=ÿÿÿÿ0.9752
        -------------+----------------------------------ÿÿÿAdjÿR-squaredÿÿÿ=ÿÿÿÿ0.9664
        ÿÿÿÿÿÿÿTotalÿ|ÿÿ846.826309ÿÿÿÿÿÿÿÿ19ÿÿ44.5698058ÿÿÿRootÿMSEÿÿÿÿÿÿÿÿ=ÿÿÿÿ1.2236

        ---------------------------------------------------------------------------------
        ÿÿÿÿÿÿÿÿÿÿÿÿoutÿ|ÿÿÿÿÿÿCoef.ÿÿÿStd.ÿErr.ÿÿÿÿÿÿtÿÿÿÿP>|t|ÿÿÿÿÿ[95%ÿConf.ÿInterval]
        ----------------+----------------------------------------------------------------
        ÿÿÿÿÿÿÿÿÿÿ1.cidÿ|ÿÿÿ24.70432ÿÿÿ2.035324ÿÿÿÿ12.14ÿÿÿ0.000ÿÿÿÿÿ20.33898ÿÿÿÿ29.06965
        ÿÿÿÿÿÿÿÿÿÿÿÿtimÿ|ÿÿÿ4.269738ÿÿÿ.6010669ÿÿÿÿÿ7.10ÿÿÿ0.000ÿÿÿÿÿ2.980578ÿÿÿÿ5.558898
        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
        ÿÿÿÿÿÿcid#c.timÿ|
        ÿÿÿÿÿÿÿÿÿÿÿÿÿ1ÿÿ|ÿÿ-9.217564ÿÿÿÿ.850037ÿÿÿ-10.84ÿÿÿ0.000ÿÿÿÿ-11.04071ÿÿÿ-7.394416
        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
        ÿÿÿÿc.tim#c.timÿ|ÿÿ-.4414601ÿÿÿ.0532523ÿÿÿÿ-8.29ÿÿÿ0.000ÿÿÿÿ-.5556749ÿÿÿ-.3272454
        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
        cid#c.tim#c.timÿ|
        ÿÿÿÿÿÿÿÿÿÿÿÿÿ1ÿÿ|ÿÿÿ.9435872ÿÿÿ.0753101ÿÿÿÿ12.53ÿÿÿ0.000ÿÿÿÿÿ.7820631ÿÿÿÿ1.105111
        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
        ÿÿÿÿÿÿÿÿÿÿ_consÿ|ÿÿÿ-11.0878ÿÿÿ1.439191ÿÿÿÿ-7.70ÿÿÿ0.000ÿÿÿÿ-14.17456ÿÿÿÿ-8.00104
        ---------------------------------------------------------------------------------

        .ÿpredictÿdoubleÿxb,ÿxb

        .ÿ
        .ÿgraphÿtwowayÿ///
        >ÿÿÿÿÿÿÿÿÿscatterÿoutÿtimÿifÿ!cid,ÿmcolor(red)ÿmsize(small)ÿ||ÿ///
        >ÿÿÿÿÿÿÿÿÿlineÿxbÿtimÿifÿ!cid,ÿlcolor(red)ÿlpattern(dash)ÿ||ÿ///
        >ÿÿÿÿÿÿÿÿÿscatterÿoutÿtimÿifÿcid,ÿmcolor(blue)ÿmsize(small)ÿ||ÿ///
        >ÿÿÿÿÿÿÿÿÿlineÿxbÿtimÿifÿcid,ÿlcolor(red)ÿlpattern(dash)ÿ,ÿ///
        >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿylabel(ÿ,ÿangle(horizontal)ÿnogrid)ÿlegend(off)

        .ÿquietlyÿgraphÿexportÿCorrect.png,ÿreplace

        .ÿ
        .ÿ//ÿCompareÿyours
        .ÿdropÿxb

        .ÿregressÿoutÿi.cid#c.tim#c.tim

        ÿÿÿÿÿÿSourceÿ|ÿÿÿÿÿÿÿSSÿÿÿÿÿÿÿÿÿÿÿdfÿÿÿÿÿÿÿMSÿÿÿÿÿÿNumberÿofÿobsÿÿÿ=ÿÿÿÿÿÿÿÿ20
        -------------+----------------------------------ÿÿÿF(2,ÿ17)ÿÿÿÿÿÿÿÿ=ÿÿÿÿÿ21.15
        ÿÿÿÿÿÿÿModelÿ|ÿÿ604.022178ÿÿÿÿÿÿÿÿÿ2ÿÿ302.011089ÿÿÿProbÿ>ÿFÿÿÿÿÿÿÿÿ=ÿÿÿÿ0.0000
        ÿÿÿÿResidualÿ|ÿÿ242.804131ÿÿÿÿÿÿÿÿ17ÿÿ14.2825959ÿÿÿR-squaredÿÿÿÿÿÿÿ=ÿÿÿÿ0.7133
        -------------+----------------------------------ÿÿÿAdjÿR-squaredÿÿÿ=ÿÿÿÿ0.6795
        ÿÿÿÿÿÿÿTotalÿ|ÿÿ846.826309ÿÿÿÿÿÿÿÿ19ÿÿ44.5698058ÿÿÿRootÿMSEÿÿÿÿÿÿÿÿ=ÿÿÿÿ3.7792

        ---------------------------------------------------------------------------------
        ÿÿÿÿÿÿÿÿÿÿÿÿoutÿ|ÿÿÿÿÿÿCoef.ÿÿÿStd.ÿErr.ÿÿÿÿÿÿtÿÿÿÿP>|t|ÿÿÿÿÿ[95%ÿConf.ÿInterval]
        ----------------+----------------------------------------------------------------
        cid#c.tim#c.timÿ|
        ÿÿÿÿÿÿÿÿÿÿÿÿÿ0ÿÿ|ÿÿ-.1081245ÿÿÿ.0310055ÿÿÿÿ-3.49ÿÿÿ0.003ÿÿÿÿ-.1735404ÿÿÿ-.0427086
        ÿÿÿÿÿÿÿÿÿÿÿÿÿ1ÿÿ|ÿÿÿ.1102438ÿÿÿ.0310055ÿÿÿÿÿ3.56ÿÿÿ0.002ÿÿÿÿÿ.0448279ÿÿÿÿ.1756597
        ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
        ÿÿÿÿÿÿÿÿÿÿ_consÿ|ÿÿÿ.5266589ÿÿÿ1.311959ÿÿÿÿÿ0.40ÿÿÿ0.693ÿÿÿÿ-2.241332ÿÿÿÿÿ3.29465
        ---------------------------------------------------------------------------------

        .ÿpredictÿdoubleÿxb,ÿxb

        .ÿ
        .ÿgraphÿtwowayÿ///
        >ÿÿÿÿÿÿÿÿÿscatterÿoutÿtimÿifÿ!cid,ÿmcolor(red)ÿmsize(small)ÿ||ÿ///
        >ÿÿÿÿÿÿÿÿÿlineÿxbÿtimÿifÿ!cid,ÿlcolor(red)ÿlpattern(dash)ÿ||ÿ///
        >ÿÿÿÿÿÿÿÿÿscatterÿoutÿtimÿifÿcid,ÿmcolor(blue)ÿmsize(small)ÿ||ÿ///
        >ÿÿÿÿÿÿÿÿÿlineÿxbÿtimÿifÿcid,ÿlcolor(red)ÿlpattern(dash)ÿ,ÿ///
        >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿylabel(ÿ,ÿangle(horizontal)ÿnogrid)ÿlegend(off)

        .ÿquietlyÿgraphÿexportÿIncorrect.png,ÿreplace

        .ÿ
        .ÿexit

        endÿofÿdo-file


        .


        Exploring things on your own can give you insights that are more difficult to apprehend when they are spoon-fed by others.

        Click image for larger version

Name:	Correct.png
Views:	1
Size:	21.9 KB
ID:	1581809


        Click image for larger version

Name:	Incorrect.png
Views:	1
Size:	21.3 KB
ID:	1581810

        Comment

        Working...
        X