Sas 简明教程

SAS - Scatter Plots

散点图是一种使用笛卡尔平面上绘制的两个变量的值的图形。它通常用于找出两个变量之间的关系。在 SAS 中,我们使用 PROC SGSCATTER 来创建散点图。

A scatterplot is a type of graph which uses values from two variables plotted in a Cartesian plane. It is usually used to find out the relationship between two variables. In SAS we use PROC SGSCATTER to create scatterplots.

请注意,我们在第一个示例中创建名为 CARS1 的数据集,并为所有后续数据集使用相同的数据集。此数据集在 SAS 会话结束之前一直保留在工作库中。

Please note that we create the data set named CARS1 in the first example and use the same data set for all the subsequent data sets. This data set remains in the work library till the end of the SAS session.

Syntax

在 SAS 中创建散点图的基本语法是 −

The basic syntax to create a scatter-plot in SAS is −

PROC sgscatter  DATA = DATASET;
   PLOT VARIABLE_1 * VARIABLE_2
   / datalabel = VARIABLE group = VARIABLE;
RUN;

以下是所用参数的描述 −

Following is the description of parameters used −

  1. DATASET is the name of data set.

  2. VARIABLE is the variable used from the dataset.

Simple Scatterplot

在简单的散点图中,我们从数据集中选择两个变量,并根据第三个变量将其分组。我们还可以标记这些数据。结果显示了两个变量在 Cartesian plane. 中的分散情况

In a simple scatterplot we choose two variables form the dataset and group them with respect a third variable. We can also label the data. The result shows how the two variables are scattered in the Cartesian plane.

Example

PROC SQL;
create table CARS1 as
SELECT make, model, type, invoice, horsepower, length, weight
FROM
SASHELP.CARS
WHERE make in ('Audi','BMW')
;
RUN;

TITLE 'Scatterplot - Two Variables';
PROC sgscatter  DATA = CARS1;
   PLOT horsepower*Invoice
   / datalabel = make group = type grid;
   title 'Horsepower vs. Invoice for car makers by types';
RUN;

当我们执行以上代码时,我们将得到以下输出:

When we execute the above code, we get the following output −

sactter plot1

Scatterplot with Prediction

我们可以使用估算参数通过围绕数值绘制椭圆来预测相关性强度。在该过程中,我们使用以下所示附加选项来绘制椭圆。

we can use an estimation parameter to predict the strength of correlation between by drawing an ellipse around the values. We use the additional options in the procedure to draw the ellipse as shown below.

Example

proc sgscatter data = cars1;
compare y = Invoice  x = (horsepower length)
         / group = type  ellipse =(alpha = 0.05 type = predicted);
title
'Average Invoice vs. horsepower for cars by length';
title2
'-- with 95% prediction ellipse --'
;
format
Invoice dollar6.0;
run;

当我们执行以上代码时,我们将得到以下输出:

When we execute the above code, we get the following output −

sactter plot eliipse

Scatter Matrix

通过将其分组为对对,我们也可以拥有涉及多个变量的散点图。在下例中,我们考虑了三个变量并绘制了散点矩阵。我们得到了 3 对结果矩阵。

We can also have a scatterplot involving more than two variables by grouping them into pairs. In the example below we consider three variables and draw a scatter plot matrix. We get 3 pairs of resulting matrix.

Example

PROC sgscatter  DATA = CARS1;
   matrix horsepower invoice length
   / group = type;

   title 'Horsepower vs. Invoice vs. Length for car makers by types';
RUN;

当我们执行以上代码时,我们将得到以下输出:

When we execute the above code, we get the following output −

sactter plot matrix