*** linear regression example; /** chapter 5 in "applied statistics and the sas programming language" */ *** pages 160~182; /* input data into SAS */ DATA CarMPG; INPUT Weight MPG; DATALINES; 4.36 16.9 4.054 15.5 3.605 19.2 3.94 18.5 2.155 30 2.56 27.5 2.3 27.2 2.23 30.9 2.83 20.3 3.14 17 2.795 21.6 3.41 16.2 3.38 20.6 3.07 20.8 3.62 18.6 3.41 18.1 3.84 17 3.725 17.6 3.955 16.5 3.83 18.2 2.585 26.5 2.91 21.9 1.975 34.1 1.915 35.1 2.67 27.4 1.99 31.5 2.135 29.5 2.67 28.4 2.595 28.8 2.7 26.8 2.556 33.5 2.2 34.2 2.02 31.8 2.13 37.3 2.19 30.5 2.815 22 2.6 21.5 1.925 31.9 ; RUN; /* Print data */ PROC PRINT DATA=CarMPG; TITLE "CarMPG"; VAR Weight MPG; RUN; /* Alternative way: Read data from file */ DATA CarMPG1; FILENAME CarMPG1 'C:\Users\Jie Yang\Documents\stat481\SAS\exercise836.TXT'; INFILE CarMPG1; INPUT Weight1 MPG1; RUN; PROC PRINT DATA=CarMPG1; TITLE "CarMPG1"; VAR Weight1 MPG1; RUN; /* end of alternative way */ /* Producing a scatter plot */ PROC GPLOT DATA=CarMPG; TITLE "Scatter Plot"; PLOT MPG*Weight; RUN; /* linear regression */ PROC REG DATA=CarMPG; TITLE "Regression Line for MPG-Weight Data"; MODEL MPG = Weight; RUN; /* scatter plot and regression line */ SYMBOL VALUE=DOT COLOR=BLACK; PROC REG DATA=CarMPG; TITLE "Scatter Plot and Regression Line"; MODEL MPG = Weight; PLOT MPG*Weight; RUN; /* residual plot 1 */ PROC REG DATA=CarMPG; TITLE "Residual Plot"; MODEL MPG = Weight; PLOT RESIDUAL.*Weight; RUN; /* residual plot 2*/ PROC REG DATA=CarMPG; TITLE "Residual Plot"; MODEL MPG = Weight; PLOT RESIDUAL.*PRED.; RUN; /* adding quadratic term and transformed variable */ /* input data into SAS */ DATA CarMPGnew; INPUT Weight MPG; Weight2 = Weight**2; GPM = 100/MPG; DATALINES; 4.36 16.9 4.054 15.5 3.605 19.2 3.94 18.5 2.155 30 2.56 27.5 2.3 27.2 2.23 30.9 2.83 20.3 3.14 17 2.795 21.6 3.41 16.2 3.38 20.6 3.07 20.8 3.62 18.6 3.41 18.1 3.84 17 3.725 17.6 3.955 16.5 3.83 18.2 2.585 26.5 2.91 21.9 1.975 34.1 1.915 35.1 2.67 27.4 1.99 31.5 2.135 29.5 2.67 28.4 2.595 28.8 2.7 26.8 2.556 33.5 2.2 34.2 2.02 31.8 2.13 37.3 2.19 30.5 2.815 22 2.6 21.5 1.925 31.9 ; RUN; /* Print data */ PROC PRINT DATA=CarMPGnew; TITLE "CarMPGnew"; VAR Weight Weight2 MPG GPM; RUN; /* residual plot of model 1*/ PROC REG DATA=CarMPGnew; TITLE "Residual Plot"; MODEL MPG = Weight Weight2; PLOT RESIDUAL.*PRED.; RUN; /* residual plot of model 2*/ PROC REG DATA=CarMPGnew; TITLE "Residual Plot"; MODEL GPM = Weight; PLOT RESIDUAL.*PRED.; RUN;