Announcement

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

  • overlay two mixed-model graphs

    I'm using Stata 14.0 in windows. I was curious if there is a way to overlay two mixed model graphs.
    I have data in 6 months on physical activity and temperature. I used mixed model to get the trend of physical activity during the 6 months.

    xtmixed time_exercise i.week ||study_id:,
    margins i.week
    marginsplot, noci

    I did the same to see the trend in temperature during the 6 months

    xtmixed wk_temperature i.week ||study_id:,
    margins i.week
    marginsplot, noci

    So I got two graphs : one is physical activity (y axis) vs. week (x axis), the other is temperature (y axis) vs. week (x axis). I'd like to know how can I overlay these two graphs together.
    I have seen some graphs in the literature combined these types of graphs (one plot, one x axis, one y axis. and using two y axis legend to explain). But haven't figured out how to do it in Stata.

    Thank you
    Xiaochen

  • #2
    You could use predict after fitting each model and plot the linear predictions for the fixed effects on one graph using graph twoway. Something like
    Code:
    xtmixed time_exercise i.week ||study_id:
    predict double exercise_hat, xb
    xtmixed wk_temperature i.week ||study_id:
    predict double temperature_hat, xb
    graph twoway line exercise_hat week || line temperature_hat week
    But, if the two variables are related in some manner, why not just model them together in a single model? Below, I've gone with your assertion that temperature is an outcome (you modeled it as one) and shown how you can get them both into a single marginsplot. It would seem, though, that if it's ambient temperature, then it would be a predictor and not a second outcome.

    .ÿversionÿ14.1

    .ÿ
    .ÿclearÿ*

    .ÿsetÿmoreÿoff

    .ÿsetÿseedÿ`=date("2015-12-05",ÿ"YMD")'

    .ÿ
    .ÿquietlyÿdrawnormÿu1ÿu2,ÿmeans(60ÿ20)ÿsd(1ÿ2)ÿcorr(1ÿ0.5ÿ\ÿ0.5ÿ1)ÿn(250)

    .ÿ
    .ÿgenerateÿintÿstudyÿ=ÿ_n

    .ÿ
    .ÿforvaluesÿweekÿ=ÿ1/26ÿ{
    ÿÿ2.ÿÿÿÿÿÿÿÿÿgenerateÿdoubleÿexercise`week'ÿ=ÿu1ÿ+ÿrnormal()
    ÿÿ3.ÿÿÿÿÿÿÿÿÿgenerateÿdoubleÿtemperature`week'ÿ=ÿu2ÿ+ÿ2ÿ*ÿrnormal()
    ÿÿ4.ÿ}

    .ÿquietlyÿreshapeÿlongÿexerciseÿtemperature,ÿi(study)ÿj(week)

    .ÿ
    .ÿ*
    .ÿ*ÿBeginÿhere
    .ÿ*
    .ÿrenameÿexerciseÿoutcome1

    .ÿrenameÿtemperatureÿoutcome2ÿ//ÿAreÿyouÿsure?

    .ÿquietlyÿreshapeÿlongÿoutcome,ÿi(studyÿweek)ÿj(type)

    .ÿ
    .ÿquietlyÿtabulateÿtype,ÿgenerate(type)

    .ÿ
    .ÿmixedÿoutcomeÿi.week##i.typeÿ||ÿstudy:ÿtype1ÿtype2,ÿnoconstantÿcovariance(unstructured)ÿ///
    >ÿÿÿÿÿÿÿÿÿremlÿresiduals(independent,ÿby(type))ÿnolrtestÿnofetableÿnolog

    Mixed-effectsÿREMLÿregressionÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿNumberÿofÿobsÿÿÿÿÿ=ÿÿÿÿÿ13,000
    Groupÿvariable:ÿstudyÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿNumberÿofÿgroupsÿÿ=ÿÿÿÿÿÿÿÿ250

    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿObsÿperÿgroup:
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿminÿ=ÿÿÿÿÿÿÿÿÿ52
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿavgÿ=ÿÿÿÿÿÿÿ52.0
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿmaxÿ=ÿÿÿÿÿÿÿÿÿ52

    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿWaldÿchi2(51)ÿÿÿÿÿ=ÿÿ101885.73
    Logÿrestricted-likelihoodÿ=ÿ-23801.742ÿÿÿÿÿÿÿÿÿÿProbÿ>ÿchi2ÿÿÿÿÿÿÿ=ÿÿÿÿÿ0.0000

    ------------------------------------------------------------------------------
    ÿÿRandom-effectsÿParametersÿÿ|ÿÿÿEstimateÿÿÿStd.ÿErr.ÿÿÿÿÿ[95%ÿConf.ÿInterval]
    -----------------------------+------------------------------------------------
    study:ÿUnstructuredÿÿÿÿÿÿÿÿÿÿ|
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿvar(type1)ÿ|ÿÿÿ.7897238ÿÿÿ.0742181ÿÿÿÿÿÿ.6568701ÿÿÿÿ.9494475
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿvar(type2)ÿ|ÿÿÿ4.406638ÿÿÿ.4088276ÿÿÿÿÿÿÿ3.67398ÿÿÿÿ5.285401
    ÿÿÿÿÿÿÿÿÿÿÿÿcov(type1,type2)ÿ|ÿÿÿ.7448146ÿÿÿ.1319014ÿÿÿÿÿÿ.4862926ÿÿÿÿ1.003337
    -----------------------------+------------------------------------------------
    Residual:ÿIndependent,ÿÿÿÿÿÿÿ|
    ÿÿÿÿbyÿtypeÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ|
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ1:ÿvar(e)ÿ|ÿÿÿ.9974162ÿÿÿ.0178781ÿÿÿÿÿÿ.9629841ÿÿÿÿ1.033079
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ2:ÿvar(e)ÿ|ÿÿÿ4.028403ÿÿÿ.0722068ÿÿÿÿÿÿ3.889338ÿÿÿÿ4.172441
    ------------------------------------------------------------------------------

    .ÿ
    .ÿquietlyÿmarginsÿi.week#i.type

    .ÿmarginsplotÿ,ÿnociÿ///
    >ÿÿÿÿÿÿÿÿÿplot1opts(lcolor(black)ÿmsymbol(none))ÿ///
    >ÿÿÿÿÿÿÿÿÿplot2opts(lcolor(black)ÿmsymbol(none))ÿ///
    >ÿÿÿÿÿÿÿÿÿtext(55ÿ13ÿ"DurationÿofÿExerciseÿ(Minutes)")ÿ///
    >ÿÿÿÿÿÿÿÿÿtext(15ÿ13ÿ"AmbientÿTemperatureÿ(°C)")ÿ///
    >ÿÿÿÿÿÿÿÿÿylabel(0(25)75,ÿangle(horizontal)ÿnogrid)ÿlegend(off)ÿ///
    >ÿÿÿÿÿÿÿÿÿytitle(LinearÿPredictions)ÿxtitle(Week)ÿtitle("")

    ÿÿVariablesÿthatÿuniquelyÿidentifyÿmargins:ÿweekÿtype

    .ÿquietlyÿgraphÿexportÿZhang.png

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .


    Click image for larger version

Name:	Zhang.png
Views:	1
Size:	10.4 KB
ID:	1319078

    Comment


    • #3
      Hi Joseph,

      Your second solution is brilliant! That works for me. I never thought of put two outcomes together.
      Thank you

      Xiaochen

      Comment

      Working...
      X