Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 37 ยท Statistics and Reporting

PROC CORR and PROC REG

Measure relationships with PROC CORR (Pearson, Spearman, matrices) and fit linear models with PROC REG: MODEL, R-square, coefficients, residuals, OUTPUT and diagnostics, with Python stand-ins.

Once you know what single variables look like, the next question is how they relate. PROC CORR measures the strength of linear (or rank) association between variables. PROC REG fits a straight-line model that predicts one variable from others. Both come in nearly every statistical programming job.

SAS cannot run on this site; sas blocks are read-only and I print no real SAS output. The TryIts are Python stand-ins (standard library) that compute the same quantities from first principles.

PROC CORR

SAS
proc corr data=work.cars;
  var weight mpg horsepower;
run;

By default SAS prints simple statistics (N, mean, std, sum, min, max) for each variable and a Pearson correlation matrix with p-values under each coefficient (the test of "true correlation is zero"). By default SAS uses pairwise deletion: each coefficient uses all rows where both of its variables are present, so the N can differ from cell to cell. Read the N. The nomiss option switches to listwise deletion (only rows complete in every variable).

Options:

Option Use
spearman / kendall Rank correlations, robust to outliers and monotone curves
with a b; statement Correlate var variables against the with list only
nosimple Skip the simple-statistics table
nomiss Use only rows complete in all variables
plots=matrix(histogram) Scatter plot matrix
plots=scatter(ellipse=prediction) Scatter plots with ellipse
outp=work.corrs Save the matrix as a data set
SAS
proc corr data=work.cars spearman plots=matrix(histogram);
  var weight mpg;
  with horsepower;
run;

Correlation values lie between -1 and 1. Near 0 means no linear relationship; the sign gives direction. A correlation does not prove causation, and one outlier can create or destroy it, so always plot.

Stand-in: Pearson and Spearman

Python Stand-in: correlation by hand

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A single bad point wrecks Pearson far more than Spearman, which is why it pays to run both and plot.

PROC REG

Simple linear regression fits y = b0 + b1 * x:

SAS
proc reg data=work.cars;
  model mpg = weight;
run;
quit;

Like PROC SQL, PROC REG is interactive: it ends with quit;, and you can run more model statements before it. The output has:

  • Analysis of Variance: model and error sums of squares, the overall F test and its p-value.
  • Fit statistics: Root MSE (typical residual size), R-Square (share of variance explained), Adjusted R-Square (penalized for extra predictors).
  • Parameter Estimates: the intercept and slope with standard errors, t values and p-values. The slope says: "a one-unit increase in weight changes the expected mpg by this much".

For several predictors, list them on the right: model mpg = weight horsepower cylinders;. Then check multicollinearity with / vif (variance inflation factors; values above about 5-10 mean predictors overlap too much) and choose variables with selection=stepwise (forward, backward) or by judgment.

SAS
proc reg data=work.cars plots(only)=(diagnostics residuals fit);
  model mpg = weight horsepower / vif clb;
  output out=work.preds predicted=pred residual=resid r=stud_resid;
run;
quit;
  • clb gives confidence limits for the coefficients.
  • output out= stores predictions and residuals per observation.
  • The default ODS plots show residuals against predicted values, a Q-Q plot, and influence measures (Cook's D, leverage), which tell you whether the assumptions hold.

PROC REG handles numeric predictors only. Categorical variables need dummy columns made in a DATA step, or you use PROC GLM or PROC GLMSELECT with a class statement. Logistic regression for 0/1 outcomes is PROC LOGISTIC; generalized models are PROC GENMOD.

Stand-in: least squares

Python Stand-in: slope, intercept and R-square

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The residuals should look like random noise with mean zero; if they curve or fan out, a straight line is the wrong model. Predicting at x=10 is extrapolation past the data and should be treated with caution.

Residuals and assumptions

Ordinary regression assumes:

  1. A linear relationship (check the residual-vs-predicted plot).
  2. Independent errors (a time series needs different tools).
  3. Constant variance (look for a funnel shape).
  4. Roughly normal residuals (Q-Q plot), important mainly for small samples.
  5. No extreme influential points (Cook's D).

A common fix for curvature is to transform a variable (log(x)) or add a squared term in a DATA step.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does a Pearson correlation of -0.9 indicate?

    The sign is the direction and the magnitude the strength; correlation alone never proves causation.
  2. 2. How does a PROC REG step end?

    PROC REG is interactive and stays active until quit; so several MODEL statements can follow.
  3. 3. What does R-Square measure?

    R-Square is 1 minus the ratio of residual to total sum of squares.
  4. 4. Which MODEL option reports variance inflation factors?

    VIF requests variance inflation factors to detect multicollinearity.
  5. 5. Why use Spearman instead of Pearson?

    Rank-based coefficients are not distorted by one extreme value and capture any monotone relationship.

Summary

  • PROC CORR gives Pearson (default), Spearman and Kendall coefficients with p-values; check each cell's N.
  • PROC REG fits model y = x1 x2 ...; read R-Square, Root MSE and parameter estimates.
  • Use vif, clb, output out= and the diagnostic plots to check the model.
  • PROC REG needs numeric predictors; use GLM or LOGISTIC for classes and binary outcomes.
  • Plot your data: one outlier or one curve can invalidate a summary number.

Next: ODS and Output Destinations