楼主: bayes
16466 19

[原创博文] 散点图+回归线图 [推广有奖]

11
guoluo 发表于 2011-5-11 23:01:34
  1. data a;
  2. input y x;
  3. datalines;
  4. 0.69 8.48
  5. 1.10 8.29
  6. 1.39 8.28
  7. 1.61 8.27
  8. 1.79 8.25
  9. 1.95 8.06
  10. 2.08 8.06
  11. 2.20 7.85
  12. 2.30 7.75
  13. 2.40 7.73
  14. ;

  15. proc reg data=a outest=est noprint;
  16. model y=x;
  17. run;
  18. quit;

  19. data _null_;
  20. set est;
  21. call symputx('a',put(Intercept,7.4));
  22. call symputx('b',put(x,7.4));
  23. run;


  24. proc template;
  25.   define statgraph reg;
  26.   mvar a b;
  27.   begingraph;
  28.     entrytitle 'Linear Regression';
  29.     layout overlay;
  30.         entry 'y=' a b 'x' /valign=top;
  31.         scatterplot y=y x=x;
  32.         regressionplot y=y x=x;
  33.         endlayout;
  34.   endgraph;
  35.   end;
  36. run;

  37. proc sgrender data=a template=reg;
  38. run;
复制代码

12
sas_user 发表于 2011-5-11 23:13:34
谢谢,很高兴能帮上忙。

13
bayes 发表于 2011-5-12 13:19:28
11# guoluo 很感谢,这两个proc都不太熟悉,学习了。

现在又有个新问题。
我在散点图上发现有些点我不想加入回归中。
也就是说,散点图要画所有的点,以防遗漏,但是回归线图,我只用符合特定条件的点。
比如我加入第三列z,回归线图只拟合z>5的x和y。

这样好像proc reg就不管用了,因为他貌似不能在model命令后面加入where语句。
不知道这时该用什么proc才能解决?

data:

y           x          z
0.69     8.48     10
1.10     8.29     9
1.39     8.28     8
1.61     8.27     7
1.79     8.25     6
1.95     8.06     5
2.08     8.06     4
2.20     7.85     3
2.30     7.75     2
2.40     7.73     1

14
guoluo 发表于 2011-5-12 19:46:38
  1. data a;
  2. input y x z;
  3. if z > 5 then do;
  4.   y1 = y;
  5.   x1 = x;
  6. end;
  7. datalines;
  8. 0.69 8.48 10
  9. 1.10 8.29 9
  10. 1.39 8.28 8
  11. 1.61 8.27 7
  12. 1.79 8.25 6
  13. 1.95 8.06 5
  14. 2.08 8.06 4
  15. 2.20 7.85 3
  16. 2.30 7.75 2
  17. 2.40 7.73 1
  18. ;

  19. proc reg data=a outest=est noprint;
  20. model y1=x1;
  21. run;
  22. quit;

  23. data _null_;
  24. set est;
  25. call symputx('a',put(Intercept,7.4));
  26. call symputx('b',put(x1,7.4));
  27. run;


  28. proc template;
  29.   define statgraph reg1;
  30.   mvar a b;
  31.   begingraph;
  32.     entrytitle 'Linear Regression';
  33.     layout overlay;
  34.           if(b<0)
  35.         entry 'y=' a b 'x' /valign=top;
  36.           else
  37.             entry 'y=' a '+' b 'x' /valign=top;
  38.           endif;
  39.       scatterplot y=y x=x;
  40.       regressionplot y=y1 x=x1;
  41.     endlayout;
  42.   endgraph;
  43.   end;
  44. run;

  45. proc sgrender data=a template=reg1;
  46. run;
复制代码

15
gennielz 发表于 2011-5-12 21:16:05
受益匪浅~~

16
sas_user 发表于 2011-5-13 03:56:56
学习了。谢谢

17
sekiray 发表于 2011-5-15 00:40:34
thanks for the info~~helps a lot!!!

18
complicated 在职认证  发表于 2013-4-11 16:06:38
感谢!学到了!
密码被盗??

19
ariman911 发表于 2013-4-11 17:04:09
要將散布圖跟迴歸畫在一起
可以用SGPLOT Procedure
proc sgplot;
scatter x=x y=y;
reg x=x y=y;
run;

如果要選擇z>5的
可以先利用REG Procedure將預測值output
再利用SGPLOT畫散步圖跟線圖
data test;
input y  x z;
k=_n_;
cards;
0.69     8.48 10
1.10     8.29 9
1.39     8.28 8
1.61     8.27 7
1.79     8.25 6
1.95     8.06 5
2.08     8.06 4
2.20     7.85 3
2.30     7.75 2
2.40     7.73 6
;
proc reg;
where z>5;
model y=x;
output out=xx p=pred;
proc print data=xx;
run;
data all;
merge test xx;
by k;
proc  print;
run;
proc sgplot;
scatter x=x y=y;
series x=x y=pred;
run;

20
tianwk 发表于 2019-7-13 00:21:46
thanks for sharing

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-29 08:19