How to add custom (mixed model) regression line to a scatterplot?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to add custom (mixed model) regression line to a scatterplot?

Alexander
Hello all,

I have computed a mixed model (with the MIXED command) with one fixed term
that has a quadratic effect. I would now like to plot a scatterplot of all
data, and then put the quadratic line on top of this plot.

I know a "normal" regression line (i.e. from a standard linear model) can be
added and have found a number of tutorials (it's simply an additional
checkbox), but I can't figure out how to do it with a mixed model.

I think there are two options:
1.) Maybe there is a way to directly plot the mixed model regression line
from the MIXED procedure.
2.) Maybe I can first plot the scatterplot, then, in a second command,
create a second data set of predicted values and overlay that on the
original plot in a separate Syntax command.

Does anyone know how to best accomplish this?

Thanks in advance,
Alex



--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: How to add custom (mixed model) regression line to a scatterplot?

Andy W
Option 2 is how I would approach it. If you create an example set of data and
show your MIXED model someone on the list could likely show you how to make
that graph.



-----
Andy W
[hidden email]
http://andrewpwheeler.wordpress.com/
--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: How to add custom (mixed model) regression line to a scatterplot?

Robert L
In reply to this post by Alexander
Alternative 2 would be my choice, but I suspect the LMM procedure would not work directly since there is no option for saving the model needed for making predctions. But in the Generalized LMM though this saving option is available. So setting up your analysis there, saving the model in an XML-file followed by using the Scoring Wizard found under Utilities should do the trick.

Robert

-----Ursprungligt meddelande-----
Från: SPSSX(r) Discussion [mailto:[hidden email]] För Alexander
Skickat: den 20 oktober 2017 15:06
Till: [hidden email]
Ämne: How to add custom (mixed model) regression line to a scatterplot?

Hello all,

I have computed a mixed model (with the MIXED command) with one fixed term that has a quadratic effect. I would now like to plot a scatterplot of all data, and then put the quadratic line on top of this plot.

I know a "normal" regression line (i.e. from a standard linear model) can be added and have found a number of tutorials (it's simply an additional checkbox), but I can't figure out how to do it with a mixed model.

I think there are two options:
1.) Maybe there is a way to directly plot the mixed model regression line from the MIXED procedure.
2.) Maybe I can first plot the scatterplot, then, in a second command, create a second data set of predicted values and overlay that on the original plot in a separate Syntax command.

Does anyone know how to best accomplish this?

Thanks in advance,
Alex



--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Robert Lundqvist
Reply | Threaded
Open this post in threaded view
|

Re: How to add custom (mixed model) regression line to a scatterplot?

Alexander
In reply to this post by Andy W
That makes sense. Here is a sample syntax piece that shows how far I can get:

(It's the mixed model and the graph without any quadratic regression line)





--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: How to add custom (mixed model) regression line to a scatterplot?

Alexander
It seems that my code (wrapped in a "raw" tag) did not go through. I'll post
it again in plain text:

* Encoding: UTF-8.
DATA LIST /PersonID 1 sex 3 (A) age 5-6 height 8-10.
BEGIN DATA
1 m 10 150
1 m 12 160
1 m 14 170
1 m 28 171
2 f 12 140
2 f 15 164
2 f 80 163
3 f 12 134
3 f 18 154
3 f 22 153
END DATA.

COMPUTE age_squared = age*age.
EXECUTE.

MIXED height BY sex WITH age age_squared
 /FIXED = sex age age_squared | SSTYPE(3)
 /RANDOM = INTERCEPT age | SUBJECT(PersonID) COVTYPE(ID)
 /PRINT SOLUTION TESTCOV.




GGRAPH
  /GRAPHDATASET NAME="graphdataset" VARIABLES=age height PersonID
MISSING=LISTWISE REPORTMISSING=NO
  /GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
  SOURCE: s=userSource(id("graphdataset"))
  DATA: age=col(source(s), name("age"))
  DATA: height=col(source(s), name("height"))
  DATA: PersonID=col(source(s), name("PersonID"), unit.category())
  GUIDE: axis(dim(1), label("age"))
  GUIDE: axis(dim(2), label("height"))
  GUIDE: legend(aesthetic(aesthetic.color.interior), label("PersonID"))
  GUIDE: text.title(label("Mehrere Linien  von height Schritt: age Schritt:
PersonID"))
  ELEMENT: line(position(age*height), color.interior(PersonID),
missing.wings())
END GPL.



--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: How to add custom (mixed model) regression line to a scatterplot?

Andy W
Because MIXED does not let you save a file to score, I'm going to suggest
doing the old school, insert missing data to make the predicted curve nice
and filled in. What happens if you just predict with the data, it won't show
the parabola nicely and will look like a set of piecewise lines with so few
of points. If you intersperse missing data though (that does not impact the
regression) your lines will look nicer.

Here is an example.

******************************************************************.
* Encoding: UTF-8.
DATA LIST /PersonID 1 sex 3 (A) age 5-6 height 8-10.
BEGIN DATA
1 m 10 150
1 m 11 .
1 m 12 160
1 m 14 170
1 m 28 171
2 f 12 140
2 f 15 164
2 f 80 163
3 f 12 134
3 f 18 154
3 f 22 153
END DATA.
DATASET NAME Traj.

*I am going to suggest something tricky, adding in missing data at different
points.
DATASET DECLARE MisData.
AGGREGATE OUTFILE='MisData'
  /BREAK PersonID sex
  /MinAge = MIN(age)
  /MaxAge = MAX(age).
DATASET ACTIVATE MisData.

*Compute various points along.
*Just specify an arbitrary number of points.
*Here 20.
COMPUTE #Delta = (MaxAge - MinAge)/21.
VECTOR Age(20,F2.0).
LOOP #i = 1 TO 20.
  COMPUTE Age(#i) = MinAge + #Delta*#i.
END LOOP.
VARSTOCASES /MAKE Age FROM Age1 TO Age20 /DROP MinAge MaxAge.
ADD FILES FILE = *
  /FILE = 'Traj'.

SORT CASES BY PersonID Age.

COMPUTE age_squared = age*age.
EXECUTE.

MIXED height BY sex WITH age age_squared
 /FIXED = sex age age_squared | SSTYPE(3)
 /RANDOM = INTERCEPT age | SUBJECT(PersonID) COVTYPE(ID)
 /PRINT SOLUTION TESTCOV
 /SAVE PRED(PredTraj).

FORMATS Height PredTraj (F3.0).

*This is what it would look like if you did not make up the intermediate age
points.
GGRAPH
  /GRAPHDATASET NAME="graphdataset" VARIABLES=age height PersonID PredTraj
        MISSING=LISTWISE REPORTMISSING=NO
  /GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
  SOURCE: s=userSource(id("graphdataset"))
  DATA: age=col(source(s), name("age"))
  DATA: height=col(source(s), name("height"))
  DATA: PredTraj=col(source(s), name("PredTraj"))
  DATA: PersonID=col(source(s), name("PersonID"), unit.category())
  COORD: rect(dim(1,2), wrap())
  GUIDE: axis(dim(1), label("Age"))
  GUIDE: axis(dim(2), label("Height"))
  GUIDE: axis(dim(3), opposite())
  GUIDE: legend(aesthetic(aesthetic.color.interior), null())
  ELEMENT: line(position(age*PredTraj*PersonID), color.interior(PersonID))
  ELEMENT: point(position(age*height*PersonID), color.interior(PersonID))
END GPL.

*This has the parabola nicely filled in.
GGRAPH
  /GRAPHDATASET NAME="graphdataset" VARIABLES=age height PersonID PredTraj
        MISSING=VARIABLEWISE REPORTMISSING=NO
  /GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
  SOURCE: s=userSource(id("graphdataset"))
  DATA: age=col(source(s), name("age"))
  DATA: height=col(source(s), name("height"))
  DATA: PredTraj=col(source(s), name("PredTraj"))
  DATA: PersonID=col(source(s), name("PersonID"), unit.category())
  COORD: rect(dim(1,2), wrap())
  GUIDE: axis(dim(1), label("Age"))
  GUIDE: axis(dim(2), label("Height"))
  GUIDE: axis(dim(3), opposite())
  GUIDE: legend(aesthetic(aesthetic.color.interior), null())
  ELEMENT: line(position(age*PredTraj*PersonID), color.interior(PersonID))
  ELEMENT: point(position(age*height*PersonID), color.interior(PersonID))
END GPL.
******************************************************************.

<http://spssx-discussion.1045642.n5.nabble.com/file/t329824/Growth.png>

If you want the lines to be interpolated through a specific set of ages,
instead of just limited to within the domain of the measured data, insert
something like:

COMPUTE MinAge = 20.
COMPUTE MaxAge = 80.

Just before the "COMPUTE #Delta" line.

Final note -- did you intend for the squared term not to be random as well?
It forces everyone to shrink in this example!




-----
Andy W
[hidden email]
http://andrewpwheeler.wordpress.com/
--
Sent from: http://spssx-discussion.1045642.n5.nabble.com/

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/