/************************************************** SAS Example Showing Polynomial Regression and a Model with an Interaction Filename: interaction.sas ***************************************************/ options pageno=1 nodate; title; libname lib1 "c:\kwelch\510\2004\sas examples"; data htwt; set lib1.htwt; age2 = age*age; centage = age-16.4; centage2 = centage*centage; if sex="m" then male=1; if sex="f" then male=0; agesex=age*male; agesex2=age*age*male; cagesex=centage*male; cagesex2=centage*centage*male; run; proc means; title "Descriptive Statistics for HtWt Example"; run; proc freq; tables age centage; title "Distribution of Age and Centered Age for HtWt Example"; run; proc corr; var height weight age centage; title "Correlations for HtWt Example"; run; /********************************************* Set up SAS/GRAPH environment Note: symbol1 statement sets up quadratic regression line (interpol=rq). **********************************************/ goptions reset=all; goptions device=win target=winprtm; symbol1 color=black interpol=rq value=dot; /*Note: not necessary to sort by age for regression fit, but necessary for spline*/ proc sort data=htwt; by age; run; proc gplot data=htwt; where age < 20; plot height*age; title "Quadratic Fit for Age"; run;quit; proc gplot data=htwt; where age < 20; plot height*centage; title "Quadratic Fit for Centered Age"; run;quit; proc reg; where age < 20; model height = age age2; title "Regression Model for HtWt Example"; title2 "Age and Age-Squared"; run;quit; proc reg; where age < 20; model height = centage centage2; plot residual.*predicted.; output out=regdat1 p=predict1 r=resid1; title "Regression Model for HtWt Example"; title2 "Centered Age and Centered Age-Squared"; run;quit; proc univariate data=regdat1 plot normal; var resid1; histogram; title "Residuals from Centered Age Regression Model"; run; /********************************************* Model with Age and Sex. Note: 2 symbol statements. One for each sex. **********************************************/ goptions reset=all; goptions device=win target=winprtm; symbol1 color=black interpol=rq value=dot line=1; symbol2 color=black interpol=rq value=star line=3; proc gplot data=htwt; where age < 20; plot height*age=sex; title "Quadratic Fit for Age"; run;quit; proc gplot data=htwt; where age < 20; plot height*centage=sex; title "Quadratic Fit for Centered Age"; run;quit; proc reg; where age < 20; model height = age age2 male agesex agesex2; title "Regression Model for HtWt Example"; title2 "Age and Age-Squared Plus Sex"; run;quit; proc reg; where age < 20; model height = centage centage2 male cagesex cagesex2; plot residual.*predicted.; title "Regression Model for HtWt Example"; title2 "Centered Age and Centered Age-Squared Plus Sex"; run;quit; proc reg; where age < 20; model height = centage centage2 male cagesex; plot residual.*predicted.; output out=regdat2 p=predict2 r=resid2; title "Regression Model for HtWt Example"; title2 "Centered Age and Centered Age-Squared Plus Sex"; title3 "Simpler Model"; run;quit; proc univariate data=regdat2 plot normal; var resid2; histogram; title "Residuals from Centered Age Regression Model"; title2 "No Interaction Between Cage2 and Sex"; run;