Regressione in SAS : Proc Reg
Si vuole stabilire se esiste una dipendenza fra il flusso di un corso d’acqua (cioè la quantità di acqua che passa in un minuto) e la profondità del corso d’acqua. I dati sono i seguenti:
zona profond flusso
1 0.34 0.636
2 0.29 0.319
3 0.28 0.734
4 0.42 1.327
5 0.29 0.487
6 0.41 0.924
7 0.76 7.350
8 0.73 5.890
9 0.46 1.979
10 0.40 1.124
Il programma SAS per leggere i dati ed effettuare una prima analisi di regressione è il seguente :
data flusso;
input zona profond flusso;
datalines;
1 0.34 0.636 2 0.29 0.319 3 0.28 0.734 4 0.42 1.327 5 0.29 0.487 6 0.41 0.924 7 0.76 7.350 8 0.73 5.890 9 0.46 1.979 10 0.40 1.124
;
proc reg data=flusso;
model flusso= profond;
plot flusso* profond;
output out=regout p=flussopred r=flussores;
run;
Output SAS :
The REG Procedure Dependent Variable: flusso Number of Observations Read 10
Root MSE 0.60347 R-Square 0.9467 Dependent Mean 2.07700 Adj R-Sq 0.9400 Coeff Var 29.05490
Parameter Estimates Parameter Standard
Variable DF Estimate Error t Value Pr > |t|
Intercept 1 -3.98213 0.54298 -7.33 <.0001 profond 1 13.83363 1.16061 11.92 <.0001
f l usso = - 3. 9821 +13. 834 pr of ond
N 10 Rsq 0. 9467 Adj Rsq 0. 9400 RMSE 0. 6035
0 1 2 3 4 5 6 7 8
pr of ond
0. 25 0. 30 0. 35 0. 40 0. 45 0. 50 0. 55 0. 60 0. 65 0. 70 0. 75 0. 80
Per ottenere il grafico dei residui :
symbol v=dot;
proc gplot data=regout;
plot residui*predetti/vref=0;
run;
Resi dual
- 0. 8 - 0. 7 - 0. 6 - 0. 5 - 0. 4 - 0. 3 - 0. 2 - 0. 1 0. 0 0. 1 0. 2 0. 3 0. 4 0. 5 0. 6 0. 7 0. 8 0. 9
Pr edi ct ed Val ue of f l usso
- 1 0 1 2 3 4 5 6 7
Si può già intravedere che la dipendenza lineare non è marcata; questo si osserva ancora meglio tramite il grafico dei residui di un modello in cui si è supposta una dipendenza lineare.
I dati e il precedente grafico dei residui possono indurre a supporre una dipendenza quadratica; si può quindi costruire un modello polinomiale del secondo ordine del tipo:
y = β0 + β1 x + β2 x2+ ε in cui le variabili esplicative sono due, X e X2. Il programma SAS è il seguente :
data flusso;
set flusso;
sqprof=profond**2;
proc reg data=flusso;
model flusso= profond sqprof;
output out=regout p = predetti r = residui;
proc gplot data=sqregout;
plot residui*predetti/vref=0;
run;
quit;
The REG Procedure
Model: MODEL1 Dependent Variable: flusso
Number of Observations Read 10
Analysis of Variance
Sum of Mean
Source DF Squares Square F Value Pr > F
Model 2 54.10549 27.05275 346.50 <.0001 Error 7 0.54652 0.07807
Corrected Total 9 54.65201
Root MSE 0.27942 R-Square 0.9900 Dependent Mean 2.07700 Adj R-Sq 0.9871 Coeff Var 13.45294
Parameter Estimates
Parameter Standard
Variable DF Estimate Error t Value Pr > |t|
Intercept 1 1.68269 1.05912 1.59 0.1561 profond 1 -10.86091 4.51711 -2.40 0.0472 sqprof 1 23.53522 4.27447 5.51 0.0009
GRAFICO DEI RESIDUI
Resi dual
- 0. 5 - 0. 4 - 0. 3 - 0. 2 - 0. 1 0. 0 0. 1 0. 2 0. 3 0. 4
Pr edi ct ed Val ue of f l usso
0 1 2 3 4 5 6 7 8
Il grafico dei residui della regressione polinomiale del secondo ordine presenta già un andamento migliore ma si possono provare altri modelli ad esempio:
• √y = β0 + β1 x + ε
• log(y) = β0 + β1 log(x) + ε
Il primo di questi due modelli è del tutto simile al modello 2, mentre il secondo è motivato dal fatto che i due valori con il flusso e la profondità più alti sono quelli che si discostano maggiormente dalla linearità rispetto agli altri dati e il logaritmo “schiaccia”i valori più alti.
data flusso;
set flusso;
logprof=log(profond);
logflusso=log(flusso);
sqrflusso=sqrt(flusso);
proc reg data=flusso;
model sqrflusso= profond ; output out=sqrtregout p = predetti r = residui;
proc gplot data=sqrtregout;
plot residui*predetti/vref=0;
run;
proc reg data=flusso;
model logflusso= logprof ; output out=logregout p = predetti r = residui;
proc gplot data=logregout;
plot residui*predetti/vref=0;
run;
quit;
The REG Procedure
Model: MODEL1 Dependent Variable: sqrflusso
Number of Observations Read 10
Analysis of Variance
Sum of Mean
Source DF Squares Square F Value Pr > F
Model 1 4.67505 4.67505 286.72 <.0001 Error 8 0.13044 0.01631
Corrected Total 9 4.80550
Root MSE 0.12769 R-Square 0.9729 Dependent Mean 1.26351 Adj R-Sq 0.9695 Coeff Var 10.10623
Parameter Estimates
Parameter Standard
Variable DF Estimate Error t Value Pr > |t|
Intercept 1 -0.55785 0.11489 -4.86 0.0013 profond 1 4.15836 0.24558 16.93 <.0001
Resi dual
- 0. 19 - 0. 18 - 0. 17 - 0. 16 - 0. 15 - 0. 14 - 0. 13 - 0. 12 - 0. 11 - 0. 10 - 0. 09 - 0. 08 - 0. 07 - 0. 06 - 0. 05 - 0. 04 - 0. 03 - 0. 02 - 0. 01 0. 00 0. 01 0. 02 0. 03 0. 04 0. 05 0. 06 0. 07 0. 08 0. 09 0. 10 0. 11 0. 12 0. 13 0. 14 0. 15 0. 16 0. 17 0. 18 0. 19 0. 20 0. 21 0. 22 0. 23 0. 24 0. 25 0. 26
Pr edi ct ed Val ue of sqr f l usso
0. 6 0. 7 0. 8 0. 9 1. 0 1. 1 1. 2 1. 3 1. 4 1. 5 1. 6 1. 7 1. 8 1. 9 2. 0 2. 1 2. 2 2. 3 2. 4 2. 5 2. 6 2. 7
The REG Procedure Model: MODEL1 Dependent Variable: logflusso
Number of Observations Read 10 Number of Observations Used 10
Analysis of Variance Sum of Mean
Source DF Squares Square F Value Pr > F
Model 1 8.77270 8.77270 121.24 <.0001 Error 8 0.57886 0.07236
Corrected Total 9 9.35156
Root MSE 0.26899 R-Square 0.9381 Dependent Mean 0.21475 Adj R-Sq 0.9304 Coeff Var 125.26096
Parameter Estimates
Parameter Standard
Variable DF Estimate Error t Value Pr > |t|
Intercept 1 2.66614 0.23833 11.19 <.0001 logprof 1 2.76413 0.25103 11.01 <.0001
Resi dual
- 0. 4 - 0. 3 - 0. 2 - 0. 1 0. 0 0. 1 0. 2 0. 3 0. 4 0. 5 0. 6
Pr edi ct ed Val ue of l ogf l usso
- 1 0 1 2