How do I run weighted least squares on the data below using PROC REG where the weights are chosen to account for unequal variances?
data one;
input X @;
do i= 1 to 4;
input Y @;
output;
end;
drop i;
datalines;
2.5 7.5 9.5 8.0 8.5
5.0 11.0 12.0 9.0 10.0
7.5 11.0 16.0 12.5 14.0
10.0 16.5 14.5 21.5 19.0
;
run;
proc reg data=one plots=all;
model Y=X / p r clm cli influence;
run;
It's fairly simple to do when you have replicates, as in your example data:
/* Compute variance estimates of dependent variable Y for each value of X */
proc sql;
create table three as
select *,
1/var(y) as invVar
from one
group by x;
quit;
/* Weighted regression */
proc reg data=three plots=all;
model Y=X / p r clm cli influence;
weight invVar;
run;
quit;
Use the WEIGHT statement in PROC REG.
I think this is all explained in the documentation.
A WEIGHT statement names a variable in the input data set with values that are relative weights for a weighted least squares fit. If the weight value is proportional to the reciprocal of the variance for each observation, then the weighted estimates are the best linear unbiased estimates (BLUE).
Of course, for the VERY SIMPLE data set you provide, there is no such variable. You could compute the variance for each observation, and then make a new variable to use in the WEIGHT statement which is the reciprocal of the variance.
It's fairly simple to do when you have replicates, as in your example data:
/* Compute variance estimates of dependent variable Y for each value of X */
proc sql;
create table three as
select *,
1/var(y) as invVar
from one
group by x;
quit;
/* Weighted regression */
proc reg data=three plots=all;
model Y=X / p r clm cli influence;
weight invVar;
run;
quit;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.