********************************************;
* VitD.SAS ******************************;
********************************************;
options pageno=1 nodate;
ods graphics on;
ods listing gpath="c:\temp"; * specify the gpath to a directory in which you want graphics files stored.
if this is unspecified, the files may or may not go to a directory to which
you have write access. If not, then they will not be written correctly and
also will not appear in your pdf or html output file;
ods pdf file="mypath\vitD.pdf"; * reset mypath to a folder on your flash drive;
data vitD;
input dose $ D;
cards;
A 74.61
A 53.53
A 47.86
A 64.10
A 103.53
A 98.06
A 39.12
A 73.41
B 72.56
B 54.21
B 95.97
B 107.44
B 73.60
B 77.16
B 67.09
B 76.59
C 37.91
C 65.04
C 47.62
C 66.95
C 68.49
C 61.94
C 82.44
C 56.47
D 33.79
D 44.71
D 52.17
D 81.58
D 29.11
D 51.75
D 71.32
D 45.64
;
run;
title 'One-way Anova for Vit D Data';
title2 'Assuming equally spaced doses';
proc glm data=vitD;
class dose;
model D=dose /clparm;
contrast 'linear dose' dose -3 -1 1 3;
contrast 'quadratic dose' dose 1 -1 -1 1;
contrast 'cubic dose' dose -1 3 -3 1;
contrast 'nonlinear dose' dose 1 -1 -1 1,
dose -1 3 -3 1;
lsmeans dose / cl;
run;
* For the exercise, uncomment the rest of this program. Then run the PROC IML
portion of the code below to get the right orthogonal polynomial contrast coefficients
given that dose is not equally spaced. Then, specify those contrasts coefficients in the call to
PROC GLM below and run the remainder of the program;
/*
title 'Use PROC IML to get non-equally spaced orth poly coefficients';
proc iml;
d={2000, 1000, 400, 0}; * the true non-equally spaced doses;
P=orpol(d); * orpol is a built-in function in PROC IML;
print P; * the second, third and fourth columns of P are the linear, quadratic and cubic contrasts for the true
unequally spaced levels of dose;
run;
quit; * quit statement exits out of PROC IML;
title 'One-way Anova for Vit D Data';
title2 'Using true spacing of doses';
proc glm data=vitD ;
class dose;
model D=dose /clparm;
contrast 'linear dose' dose .7632818 .0995585 -.298675 -.564165; * I've done the linear one for you;
contrast 'quadratic dose' dose c1 c2 c3 c4;
contrast 'cubic dose' dose c1 c2 c3 c4;
contrast 'nonlinear dose' dose c1 c2 c3 c4,
dose c1 c2 c3 c4;
lsmeans dose / cl;
ods output lsmeans=mnout; * output the means from the lsmeans statement to a new dataset called mnout;
run;
* notice that the horizontal axis in the graphs produced by the previous call to PROC GLM
treat the four doses as equally spaced even though they are not. This gives a misleading impression
about the linearity/nonlinearity of the dose effect. Even if the values of dose had been quantitative
and unequally spaced (e.g., 2000, 1000, 400 and 0), the plots would still misleadingly show the values of
dose to be equally spaced. This is because dose appears on the CLASS statement and is therefore treated as
qualitative, even though it is not. If we want a true
picture of the quantitative effects of dose, we should replot using the actual values of dose.
That is done below;
title 'The datset mnout, which contains estimated means for each dose';
title2;
proc print data=mnout;
run;
* The numeric values of dose are not in the mnout dataset, so create a new version of mnout, called mnout2
that first copies mnout into it, and then creates the numeric values of dose in a new variable called ndose;
data mnout2; * a new dataset;
set mnout; * copy the old dataset mnout into mnout2;
if (dose='A') then ndose=2000;
else if (dose='B') then ndose=1000;
else if (dose='C') then ndose=400;
else if (dose='D') then ndose=0;
run;
symbol1 value=star color=black interpol=none;
title 'Plot of estimated means for each level of dose';
proc gplot data=mnout2;
plot lsmean*ndose;
run;
*/
ods graphics off;
ods pdf close;