*************************************************************************************************** R Examples Examples you can copy and paste the code from into your R workspace ************************************************************************************************** Example 1: Run the lm procedure to compute a regression model predicting miles per gallon (mpg) from engine dispacement (cid) and save the results in a data structure called lm0: lm0<-lm(mpg ~ cid) *************************************************************************************************** Example 2: Same as example 1, but we do not necessarily assume that we have "attached" the data frame with the values of the independent and dependent variables (epa in this case): lm0<-lm(epa$mpg ~ epa$cid) If you run the first version and get an error message about mpg or cid not found, try the second form, or enter attach(epa) **************************************************************************************************** Example 3: Run a two-way ANOVA with interaction predicting mileage (mpg) from city/highway (C.H) and car/truck (car.truck): First use aov() so we can run Tukey's test as a follow up for individual differences: lm0<-aov(mpg ~ C.H*car.truck) drop1(lm0,~.,test="F") TukeyHSD(lm0) Next, use lm() so we can get estimates of the differences: lm0<-lm(mpg ~ C.H*car.truck) summary(lm0) **************************************************************************************************** Example 4: Run a two-way ANOVA with interaction predicting mileage (mpg) from city/highway (C.H) and car/truck (car.truck) adjusting for engine displacement (i.e., using engine displacement (cid) as a covariate). lm0<-lm(mpg ~ C.H*car.truck+cid) drop1(lm0,~.,test="F") summary(lm0) **************************************************************************************************** Example 5: Run a multiple regression using rated horsepower (rhp), engine displacement (cid), and vehicle weight (etw) to predict mileage (mpg): lm0<-lm(mpg ~ rhp+cid+etw) drop1(lm0,~.,test="F") summary(lm0) *******************************************************************************************************************