如何用Sas生成这个R图

时间:2013-11-30 10:10:54

标签: r sas

同时,我绕着它努力,我的解决方案如下。如果您了解更好的方法,请告诉我

我想在SAS中生成以下回归图:

enter image description here

点是实验结果,蓝点是男性,红点是女性。黑线是通过所有点的简单回归拟合。虚线用回归模型制作,其中包含预测变量性别(无交互项),如Y = alpha + beta_1.X_1i + beta_2.X_2i,其中beta_2i为性别的虚拟变量。红色虚线是该模型对女性的回归,蓝色是男性的回归模型。

这是我现在用来获取回归参数的SAS代码,它给出了与R中相同的结果:

PROC REG DATA= datasalary;
MODEL Salary=Yearsemployed gender / CLB R DW DWPROB;
OUTPUT out=pred p=Yhat RSTUDENT=Residuals L95M=Low U95M=High ;;
RUN;

如何在Sas制作情节? 提前致谢! 卡斯帕

解决方案,非常繁琐,所以也许有更简单的方法

enter image description here

我通过将回归系数输出到全局参数,然后在SGPlot过程中使用该全局参数来完成此操作。

PROC REG DATA= datasalary outest=est_model2;
MODEL Salary=Yearsemployed gender / CLB R DW DWPROB;
OUTPUT out=pred p=Yhat RSTUDENT=Residuals L95M=Low U95M=High;
RUN;

%LET model2_alpha = 0;
%LET model2_beta1 = 0;
%LET model2_beta2 = 0;

Data _null_;
Set est_model2;
 call symput("model2_alpha",Intercept);
 call symput("model2_beta1",Yearsemployed);
 call symput("model2_beta2",Gender);
 call symput("model_2_intercept_males", Intercept + Gender);
Run;
%put &model2_alpha;
%put &model2_beta1;
%put &model2_beta2;
%put &model_2_intercept_males;

PROC SGPlot Data = datasalary;
    Scatter X=Yearsemployed Y=Salary / Group = Gender;
    Lineparm x=0 y=&model1_alpha slope=&model1_beta1 / LINEATTRS=(Color= "Black");
    Lineparm x=0 y=&model2_alpha slope=&model2_beta1 / LINEATTRS=(Color= "Maroon" Pattern= MediumDash);
    Lineparm x=0 y=&model_2_intercept_males  slope=&model2_beta1 / LINEATTRS=(Color= "VIB   " Pattern= MediumDash);
    XAXIS LABEL = 'Salary' VALUES = (0 TO 30 BY 5);
    YAXIS LABEL = 'Years experience' VALUES = (0 TO 35 BY 5);
    TITLE 'Linear regression with two predictors and no interaction terms';
 Run;

1 个答案:

答案 0 :(得分:0)

@DomPazz和@Astro是正确的,你想要做的不是直接来自SGPLOT,也不会直接来自PROC REG。虽然PROC REG确实具有一些绘图功能,但它无法在同一图表上绘制多个模型(至少从v9.4开始)。您的解决方法是合理的,但您可以添加一些内容以使绘图更接近于R,特别是轴语句中的grid选项。

proc reg data = datasalary outest = est_model2;
    model salary = yearsemployed gender / clb r dw dwprob;
    output out=pred p=yhat rstudent=residuals l95m=low u95m=high;
run;
quit;

data _null_;
    set est_model2;
    call symputx('model2_beta0', intercept);
    call symputx('model2_beta1', yearsemployed);
    call symputx('model2_beta2', gender);
    call symputx('model2_intercept_males', intercept+gender);
run;

title 'Linear regression with two predictors and no interaction terms';

proc sgplot data = datasalary noautolegend;
    reg x = yearsemployed y = salary /
        group = gender
        lineattrs = (color=black pattern=solid);
    lineparm x = 0 y = &model2_beta0 slope = &model2_beta1 /
        lineattrs = (color=red pattern=mediumdash);
    lineparm x = 0 y = &model2_intercept_males slope = &model2_beta1 /
        lineattrs = (color=blue pattern=mediumdash);
    xaxis label = 'Salary' values = (0 to 30 by 5)
        grid gridattrs = (color=gray pattern=shortdash);
    yaxis label = 'Years Experience' values = (0 to 35 by 5)
        grid gridattrs = (color=gray pattern=shortdash);
run;

title;

这里我假设你的代码中的model1引用了一个只包含截距和yearsemployed的简单线性回归模型。在这种情况下,您可以使用REG语句获取该模型的散点图和回归线。请注意,SGPLOT中的REG语句仅支持简单的线性回归模型。