cars <- read.csv("~/RPI/Classes/Design of Experiments/R/FFD Cars 3.0.csv", header=TRUE)
#reads in the data from the csv file 'FFD Cars 3.0.csv' and assigns it to the dataframe 'cars'
#Summary of Data
head(cars)
## Engine Weight Acceleration Year Country Cylinders mpg
## 1 1 1 -1 1 -1 1 18
## 2 1 1 1 -1 -1 1 15
## 3 1 -1 -1 -1 1 1 18
## 4 -1 -1 -1 -1 1 1 16
## 5 1 -1 1 1 1 1 17
## 6 1 1 1 1 1 1 15
#displays the first 6 sets of variables
tail(cars)
## Engine Weight Acceleration Year Country Cylinders mpg
## 123 1 1 1 1 1 -1 16
## 124 -1 1 -1 1 -1 -1 15
## 125 -1 1 -1 1 1 -1 16
## 126 -1 -1 -1 -1 -1 -1 14
## 127 -1 -1 -1 1 1 1 17
## 128 1 -1 -1 1 1 1 16
#displays the last 6 sets of variables
summary(cars)
## Engine Weight Acceleration Year Country
## Min. :-1.00000 Min. :-1 Min. :-1 Min. :-1 Min. :-1
## 1st Qu.:-1.00000 1st Qu.:-1 1st Qu.:-1 1st Qu.:-1 1st Qu.:-1
## Median : 1.00000 Median : 0 Median : 0 Median : 0 Median : 0
## Mean : 0.03125 Mean : 0 Mean : 0 Mean : 0 Mean : 0
## 3rd Qu.: 1.00000 3rd Qu.: 1 3rd Qu.: 1 3rd Qu.: 1 3rd Qu.: 1
## Max. : 1.00000 Max. : 1 Max. : 1 Max. : 1 Max. : 1
## Cylinders mpg
## Min. :-1 Min. :10.00
## 1st Qu.:-1 1st Qu.:15.00
## Median : 0 Median :19.00
## Mean : 0 Mean :19.88
## 3rd Qu.: 1 3rd Qu.:25.00
## Max. : 1 Max. :35.00
#displays a summary of the variables
#Assign the data types
cars$engine=as.factor(cars$Engine)
#makes the variable "engine" a factor
cars$weight=as.factor(cars$Weight)
#makes the variable "weight" a factor
cars$acceleration=as.factor(cars$Acceleration)
#makes the variable "acceleration" a factor
cars$year=as.factor(cars$Year)
#makes the variable "year" a factor
cars$country=as.factor(cars$Country)
#makes the variable "country" a factor
cars$cylinders=as.factor(cars$Cylinders)
#makes the variable "cylinders" a factor
#Boxplot
boxplot(mpg~engine,data=cars, xlab="Engine", ylab="Fuel Mileage (mpg)")
title("Engine Miles Per Gallon")
#boxplot of the mpg data from the factor "Engine"
boxplot(mpg~weight,data=cars, xlab="Weight (lbs)", ylab="Fuel Mileage (mpg)")
title("Weight Miles Per Gallon")
#boxplot of the mpg data from the factor "Weight"
boxplot(mpg~acceleration,data=cars, xlab="Acceleration Time", ylab="Fuel Mileage (mpg)")
title("Acceleration Miles Per Gallon")
#boxplot of the mpg data from the factor "Acceleration"
boxplot(mpg~year,data=cars, xlab="Model Year", ylab="Fuel Mileage (mpg)")
title("Year Miles Per Gallon")
#boxplot of the mpg data from the factor "Year"
boxplot(mpg~country,data=cars, xlab="Country of Origin", ylab="Fuel Mileage (mpg)")
title("Country Miles Per Gallon")
#boxplot of the mpg data from the factor "Country"
boxplot(mpg~cylinders,data=cars, xlab="Number of Cylinders", ylab="Fuel Mileage (mpg)")
title("Cylinders Miles Per Gallon")
#boxplot of the mpg data from the factor "Cylinders"
# ANOVA
model = aov(mpg~engine+weight+acceleration+year+country+cylinders,data=cars)
anova(model)
## Analysis of Variance Table
##
## Response: mpg
## Df Sum Sq Mean Sq F value Pr(>F)
## engine 1 145.7 145.707 4.4236 0.03752 *
## weight 1 2.1 2.070 0.0628 0.80248
## acceleration 1 23.0 23.046 0.6996 0.40455
## year 1 0.5 0.544 0.0165 0.89798
## country 1 2.1 2.090 0.0634 0.80156
## cylinders 1 120.9 120.948 3.6719 0.05770 .
## Residuals 121 3985.6 32.939
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#performs an anova test
library(FrF2)
## Loading required package: DoE.base
## Loading required package: grid
## Loading required package: conf.design
##
## Attaching package: 'DoE.base'
##
## The following objects are masked from 'package:stats':
##
## aov, lm
##
## The following object is masked from 'package:graphics':
##
## plot.design
#loads the R package "FrF2" needed to run a fractional factorial design
FFD = FrF2(32,nfactors=6,estimable=formula("~Engine+Weight+Acceleration+Year+Country+Cylinders+Engine:(Weight+Acceleration+Year+Country+Cylinders)"), factor.names=c("Engine","Weight","Acceleration","Year","Country","Cylinders"), res4=TRUE, clear=FALSE)
#generates an array for a 2^(6-1) fractional factorial design (32 runs)
#since factor names are used, factor.names must be used
#the resolution of this design is 4 to estimate the main effects unconfounded by other main effects or two-factor interactions. Two-factor interaction effects, however, are aliased with each other.
FFD
## Engine Weight Acceleration Year Country Cylinders
## 1 -1 -1 -1 1 1 -1
## 2 -1 1 -1 1 1 1
## 3 -1 1 1 1 1 -1
## 4 -1 1 -1 -1 -1 1
## 5 1 1 -1 -1 1 1
## 6 -1 -1 1 1 1 1
## 7 1 1 1 -1 1 -1
## 8 1 -1 1 1 -1 1
## 9 -1 -1 1 1 -1 -1
## 10 1 -1 -1 1 1 1
## 11 -1 1 1 -1 -1 -1
## 12 1 1 -1 1 1 -1
## 13 1 1 1 1 1 1
## 14 1 -1 1 1 1 -1
## 15 1 1 -1 -1 -1 -1
## 16 1 -1 -1 -1 -1 1
## 17 -1 -1 -1 1 -1 1
## 18 1 -1 1 -1 1 1
## 19 1 1 1 -1 -1 1
## 20 -1 -1 -1 -1 -1 -1
## 21 -1 -1 1 -1 -1 1
## 22 -1 1 1 1 -1 1
## 23 -1 1 1 -1 1 1
## 24 1 -1 -1 -1 1 -1
## 25 -1 1 -1 -1 1 -1
## 26 1 1 1 1 -1 -1
## 27 1 -1 1 -1 -1 -1
## 28 1 -1 -1 1 -1 -1
## 29 -1 1 -1 1 -1 -1
## 30 1 1 -1 1 -1 1
## 31 -1 -1 -1 -1 1 1
## 32 -1 -1 1 -1 1 -1
## class=design, type= FrF2.estimable
#displays the fractional factorial design
aliasprint(FFD)
## $legend
## [1] A=Engine B=Weight C=Acceleration D=Year
## [5] E=Country F=Cylinders
##
## [[2]]
## [1] no aliasing among main effects and 2fis
#identifies any aliasing in the design
FFDdata = merge(FFD, cars, by=c("Engine","Weight","Acceleration","Year","Country","Cylinders"), all = FALSE)
#combines the FFD matrix with the data in cars
FFDdata
## Engine Weight Acceleration Year Country Cylinders mpg engine weight
## 1 -1 -1 -1 -1 -1 -1 14 -1 -1
## 2 -1 -1 -1 -1 -1 -1 13 -1 -1
## 3 -1 -1 -1 -1 1 1 16 -1 -1
## 4 -1 -1 -1 -1 1 1 23 -1 -1
## 5 -1 -1 -1 1 -1 1 14 -1 -1
## 6 -1 -1 -1 1 -1 1 12 -1 -1
## 7 -1 -1 -1 1 1 -1 25 -1 -1
## 8 -1 -1 -1 1 1 -1 19 -1 -1
## 9 -1 -1 1 -1 -1 1 15 -1 -1
## 10 -1 -1 1 -1 -1 1 24 -1 -1
## 11 -1 -1 1 -1 1 -1 14 -1 -1
## 12 -1 -1 1 -1 1 -1 16 -1 -1
## 13 -1 -1 1 1 -1 -1 16 -1 -1
## 14 -1 -1 1 1 -1 -1 32 -1 -1
## 15 -1 -1 1 1 1 1 18 -1 -1
## 16 -1 -1 1 1 1 1 14 -1 -1
## 17 -1 1 -1 -1 1 -1 14 -1 1
## 18 -1 1 -1 -1 1 -1 12 -1 1
## 19 -1 1 -1 1 -1 -1 20 -1 1
## 20 -1 1 -1 1 -1 -1 15 -1 1
## 21 -1 1 -1 1 1 1 24 -1 1
## 22 -1 1 -1 1 1 1 30 -1 1
## 23 -1 1 1 -1 -1 -1 11 -1 1
## 24 -1 1 1 -1 -1 -1 10 -1 1
## 25 -1 1 1 -1 1 1 21 -1 1
## 26 -1 1 1 -1 1 1 15 -1 1
## 27 -1 1 1 1 -1 1 13 -1 1
## 28 -1 1 1 1 -1 1 29 -1 1
## 29 -1 1 1 1 1 -1 24 -1 1
## 30 -1 1 1 1 1 -1 21 -1 1
## 31 1 -1 -1 -1 -1 1 15 1 -1
## 32 1 -1 -1 -1 -1 1 25 1 -1
## 33 1 -1 -1 -1 1 -1 25 1 -1
## 34 1 -1 -1 -1 1 -1 31 1 -1
## 35 1 -1 -1 1 -1 -1 17 1 -1
## 36 1 -1 -1 1 -1 -1 25 1 -1
## 37 1 -1 -1 1 1 1 24 1 -1
## 38 1 -1 -1 1 1 1 16 1 -1
## 39 1 -1 1 -1 -1 -1 19 1 -1
## 40 1 -1 1 -1 -1 -1 28 1 -1
## 41 1 -1 1 -1 1 1 18 1 -1
## 42 1 -1 1 -1 1 1 26 1 -1
## 43 1 -1 1 1 -1 1 26 1 -1
## 44 1 -1 1 1 -1 1 22 1 -1
## 45 1 -1 1 1 1 -1 18 1 -1
## 46 1 -1 1 1 1 -1 14 1 -1
## 47 1 1 -1 -1 -1 -1 22 1 1
## 48 1 1 -1 -1 -1 -1 21 1 1
## 49 1 1 -1 -1 1 1 32 1 1
## 50 1 1 -1 -1 1 1 18 1 1
## 51 1 1 -1 1 -1 1 18 1 1
## 52 1 1 -1 1 -1 1 16 1 1
## 53 1 1 -1 1 1 -1 10 1 1
## 54 1 1 -1 1 1 -1 20 1 1
## 55 1 1 1 -1 -1 1 18 1 1
## 56 1 1 1 -1 -1 1 15 1 1
## 57 1 1 1 -1 1 -1 27 1 1
## 58 1 1 1 -1 1 -1 21 1 1
## 59 1 1 1 1 -1 -1 30 1 1
## 60 1 1 1 1 -1 -1 26 1 1
## 61 1 1 1 1 1 1 11 1 1
## 62 1 1 1 1 1 1 15 1 1
## acceleration year country cylinders
## 1 -1 -1 -1 -1
## 2 -1 -1 -1 -1
## 3 -1 -1 1 1
## 4 -1 -1 1 1
## 5 -1 1 -1 1
## 6 -1 1 -1 1
## 7 -1 1 1 -1
## 8 -1 1 1 -1
## 9 1 -1 -1 1
## 10 1 -1 -1 1
## 11 1 -1 1 -1
## 12 1 -1 1 -1
## 13 1 1 -1 -1
## 14 1 1 -1 -1
## 15 1 1 1 1
## 16 1 1 1 1
## 17 -1 -1 1 -1
## 18 -1 -1 1 -1
## 19 -1 1 -1 -1
## 20 -1 1 -1 -1
## 21 -1 1 1 1
## 22 -1 1 1 1
## 23 1 -1 -1 -1
## 24 1 -1 -1 -1
## 25 1 -1 1 1
## 26 1 -1 1 1
## 27 1 1 -1 1
## 28 1 1 -1 1
## 29 1 1 1 -1
## 30 1 1 1 -1
## 31 -1 -1 -1 1
## 32 -1 -1 -1 1
## 33 -1 -1 1 -1
## 34 -1 -1 1 -1
## 35 -1 1 -1 -1
## 36 -1 1 -1 -1
## 37 -1 1 1 1
## 38 -1 1 1 1
## 39 1 -1 -1 -1
## 40 1 -1 -1 -1
## 41 1 -1 1 1
## 42 1 -1 1 1
## 43 1 1 -1 1
## 44 1 1 -1 1
## 45 1 1 1 -1
## 46 1 1 1 -1
## 47 -1 -1 -1 -1
## 48 -1 -1 -1 -1
## 49 -1 -1 1 1
## 50 -1 -1 1 1
## 51 -1 1 -1 1
## 52 -1 1 -1 1
## 53 -1 1 1 -1
## 54 -1 1 1 -1
## 55 1 -1 -1 1
## 56 1 -1 -1 1
## 57 1 -1 1 -1
## 58 1 -1 1 -1
## 59 1 1 -1 -1
## 60 1 1 -1 -1
## 61 1 1 1 1
## 62 1 1 1 1
FFDdata2 = unique( FFDdata[ , 1:6])
#elimates any repeated rows for columns 1 through 6
FFDdata2
## Engine Weight Acceleration Year Country Cylinders
## 1 -1 -1 -1 -1 -1 -1
## 3 -1 -1 -1 -1 1 1
## 5 -1 -1 -1 1 -1 1
## 7 -1 -1 -1 1 1 -1
## 9 -1 -1 1 -1 -1 1
## 11 -1 -1 1 -1 1 -1
## 13 -1 -1 1 1 -1 -1
## 15 -1 -1 1 1 1 1
## 17 -1 1 -1 -1 1 -1
## 19 -1 1 -1 1 -1 -1
## 21 -1 1 -1 1 1 1
## 23 -1 1 1 -1 -1 -1
## 25 -1 1 1 -1 1 1
## 27 -1 1 1 1 -1 1
## 29 -1 1 1 1 1 -1
## 31 1 -1 -1 -1 -1 1
## 33 1 -1 -1 -1 1 -1
## 35 1 -1 -1 1 -1 -1
## 37 1 -1 -1 1 1 1
## 39 1 -1 1 -1 -1 -1
## 41 1 -1 1 -1 1 1
## 43 1 -1 1 1 -1 1
## 45 1 -1 1 1 1 -1
## 47 1 1 -1 -1 -1 -1
## 49 1 1 -1 -1 1 1
## 51 1 1 -1 1 -1 1
## 53 1 1 -1 1 1 -1
## 55 1 1 1 -1 -1 1
## 57 1 1 1 -1 1 -1
## 59 1 1 1 1 -1 -1
## 61 1 1 1 1 1 1
mpgdata = FFDdata$mpg[index=c(1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61)]
#creates a column of the fuel mileage data that corresponds to the unique rows
FFDmpg = cbind(FFDdata2,mpgdata)
#cobines FFDdata2 with the column of fuel mileage data
FFDmpg
## Engine Weight Acceleration Year Country Cylinders mpgdata
## 1 -1 -1 -1 -1 -1 -1 14
## 3 -1 -1 -1 -1 1 1 16
## 5 -1 -1 -1 1 -1 1 14
## 7 -1 -1 -1 1 1 -1 25
## 9 -1 -1 1 -1 -1 1 15
## 11 -1 -1 1 -1 1 -1 14
## 13 -1 -1 1 1 -1 -1 16
## 15 -1 -1 1 1 1 1 18
## 17 -1 1 -1 -1 1 -1 14
## 19 -1 1 -1 1 -1 -1 20
## 21 -1 1 -1 1 1 1 24
## 23 -1 1 1 -1 -1 -1 11
## 25 -1 1 1 -1 1 1 21
## 27 -1 1 1 1 -1 1 13
## 29 -1 1 1 1 1 -1 24
## 31 1 -1 -1 -1 -1 1 15
## 33 1 -1 -1 -1 1 -1 25
## 35 1 -1 -1 1 -1 -1 17
## 37 1 -1 -1 1 1 1 24
## 39 1 -1 1 -1 -1 -1 19
## 41 1 -1 1 -1 1 1 18
## 43 1 -1 1 1 -1 1 26
## 45 1 -1 1 1 1 -1 18
## 47 1 1 -1 -1 -1 -1 22
## 49 1 1 -1 -1 1 1 32
## 51 1 1 -1 1 -1 1 18
## 53 1 1 -1 1 1 -1 10
## 55 1 1 1 -1 -1 1 18
## 57 1 1 1 -1 1 -1 27
## 59 1 1 1 1 -1 -1 30
## 61 1 1 1 1 1 1 11
FFDmodel = aov(mpgdata~Engine+Weight+Acceleration+Year+Country+Cylinders,data=FFDmpg)
anova(FFDmodel)
## Analysis of Variance Table
##
## Response: mpgdata
## Df Sum Sq Mean Sq F value Pr(>F)
## Engine 1 87.32 87.317 2.5448 0.1237
## Weight 1 10.79 10.787 0.3144 0.5802
## Acceleration 1 2.56 2.562 0.0747 0.7870
## Year 1 2.50 2.501 0.0729 0.7895
## Country 1 40.01 40.006 1.1660 0.2910
## Cylinders 1 1.36 1.357 0.0395 0.8440
## Residuals 24 823.47 34.311
#performs an anova test
shapiro.test(FFDmpg$mpgdata)
##
## Shapiro-Wilk normality test
##
## data: FFDmpg$mpgdata
## W = 0.9582, p-value = 0.2613
#Q-Q Plots
#Model Year
qqnorm(residuals(FFDmodel), main="Normal Q-Q Plot", ylab="Fuel Mileage (mpg) Residuals")
qqline(residuals(FFDmodel))
#produces a Q-Q normal plot with a normal fit line
#Model Year
plot(fitted(FFDmodel),residuals(FFDmodel), main="Residual vs Fitted Plot")
#Produces a residual plot