Quantcast

different fit lines for different groups

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

different fit lines for different groups

Kirill Orlov
Is it possible to customize via GPL a scatterplot with 2 groups of points (defined by a grouping variable) so that fit lines for the 2 groups be different functions. For example, linear fit for GROUP=1 cloud and cubic fit for GROUP=2 cloud.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: different fit lines for different groups

Andy W

Here is the best solution I could come up with. In a nutshell it makes two grouping variables and then maps the one group to a 100% transparent element. Then it just has two element calls in the GPL (in this example one for linear and one for quadratic).

Of course the most flexible solution would be to actually fit the models for each group and put the predicted values as a new variable in the dataset, but this didn't work out too badly (the legend didn't even turn out that badly).


**********************************************.
set seed = 10.
input program.
loop #i = 1 to 100.
if #i <= 50 group = 0.
if #i > 50 group = 1.
end case.
end loop.
end file.
end input program.
dataset name sim.
execute.

compute x = RV.NORM(0,1).
if group = 0 outcome =  x + RV.NORM(0,0.1).
if group = 1 outcome =  x**2 + RV.NORM(0,0.1).

compute group_square = (group = 1).
compute group_linear = (group = 0).
formats all (F1.0).
exe.


DATASET ACTIVATE sim.
* Chart Builder.
GGRAPH
  /GRAPHDATASET NAME="graphdataset" VARIABLES=x outcome group_square group_linear group
    MISSING=LISTWISE REPORTMISSING=NO
  /GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
  SOURCE: s=userSource(id("graphdataset"))
  DATA: x=col(source(s), name("x"))
  DATA: outcome=col(source(s), name("outcome"))
  DATA: group=col(source(s), name("group"), unit.category())
  DATA: group_square=col(source(s), name("group_square"), unit.category())
  DATA: group_linear=col(source(s), name("group_linear"), unit.category())
  GUIDE: axis(dim(1), label("x"))
  GUIDE: axis(dim(2), label("outcome"))
  GUIDE: legend(aesthetic(aesthetic.transparency), null())     
  SCALE: cat(aesthetic(aesthetic.transparency), map(("0", transparency."1.0"), ("1", transparency."0.0")))
  ELEMENT: point(position(x*outcome), color.exterior(group))
  ELEMENT: line(position(smooth.linear(x*outcome)), transparency.interior(group_linear), color.interior(group))
  ELEMENT: line(position(smooth.quadratic(x*outcome)), transparency.interior(group_square), color.interior(group))
END GPL.
**********************************************.

Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: different fit lines for different groups

ViAnn Beadle
In reply to this post by Kirill Orlov

Use CASESTOVARS to restructure the file and then specify two ELEMENT statements.

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Kirill Orlov
Sent: Monday, July 30, 2012 1:21 AM
To: [hidden email]
Subject: different fit lines for different groups

 

Is it possible to customize via GPL a scatterplot with 2 groups of points (defined by a grouping variable) so that fit lines for the 2 groups be different functions. For example, linear fit for GROUP=1 cloud and cubic fit for GROUP=2 cloud.

Loading...