### Problem 6.8

CM<-c(rep(1,6),rep(2,6))
Time<-c(rep(12,12),rep(18,12))
obs<-c(21,23,20,22,28,26,25,24,29,26,25,27,37,38,35,39,38,36,31,29,30,34,33,35)
data.frame(CM,Time,obs)
##    CM Time obs
## 1   1   12  21
## 2   1   12  23
## 3   1   12  20
## 4   1   12  22
## 5   1   12  28
## 6   1   12  26
## 7   2   12  25
## 8   2   12  24
## 9   2   12  29
## 10  2   12  26
## 11  2   12  25
## 12  2   12  27
## 13  1   18  37
## 14  1   18  38
## 15  1   18  35
## 16  1   18  39
## 17  1   18  38
## 18  1   18  36
## 19  2   18  31
## 20  2   18  29
## 21  2   18  30
## 22  2   18  34
## 23  2   18  33
## 24  2   18  35
dat<-data.frame(CM,Time,obs)

model<-lm(obs~CM*Time,data=dat)
coef(model)
## (Intercept)          CM        Time     CM:Time 
##  -22.666667   18.333333    3.611111   -1.305556
## halfnormal(model)
plot(model)

## hat values (leverages) are all = 0.1666667
##  and there are no factor predictors; no plot no. 5

summary(model)
## 
## Call:
## lm.default(formula = obs ~ CM * Time, data = dat)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.333 -1.500 -0.250  1.208  4.667 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -22.6667     7.4391  -3.047 0.006365 ** 
## CM           18.3333     4.7049   3.897 0.000896 ***
## Time          3.6111     0.4863   7.426 3.61e-07 ***
## CM:Time      -1.3056     0.3076  -4.245 0.000397 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.26 on 20 degrees of freedom
## Multiple R-squared:  0.8713, Adjusted R-squared:  0.852 
## F-statistic: 45.12 on 3 and 20 DF,  p-value: 4.346e-09
interaction.plot(dat$CM,dat$Time,dat$obs)

## Comments: 
## Model is significant p value is less than 0.05
## Factors Time and (Culture Medium: Time) interaction are significant
## The variance is slightly unequal as shown plots in the plot of residuals.
## The F value of F = 45.12 indicates that the model is significant 
## Growth rate is affected by factor Time and the CM:Time interaction.
## Problem 6.12
A<-c(-1,1,-1,1)
B<-c(-1,-1,1,1)
R<-c(14.037,13.880,14.821,14.888,
     16.165,13.860,14.757,14.921,
     13.972,14.032,14.843,14.415,
     13.907,13.914,14.878,14.932)

data.frame(A,B,R)
##     A  B      R
## 1  -1 -1 14.037
## 2   1 -1 13.880
## 3  -1  1 14.821
## 4   1  1 14.888
## 5  -1 -1 16.165
## 6   1 -1 13.860
## 7  -1  1 14.757
## 8   1  1 14.921
## 9  -1 -1 13.972
## 10  1 -1 14.032
## 11 -1  1 14.843
## 12  1  1 14.415
## 13 -1 -1 13.907
## 14  1 -1 13.914
## 15 -1  1 14.878
## 16  1  1 14.932
dat1<-data.frame(A,B,R)

model1<-lm(R~A*B,data=dat1)
coef(model1)
## (Intercept)           A           B         A:B 
##   14.513875   -0.158625    0.293000    0.140750
halfnormal(model1)
## Warning in halfnormal.lm(model1): halfnormal not recommended for models with
## more residual df than model df
## 
## Significant effects (alpha=0.05, Lenth method):
## [1] e7 B  A

plot(model1)

## hat values (leverages) are all = 0.25
##  and there are no factor predictors; no plot no. 5

summary(model1)
## 
## Call:
## lm.default(formula = R ~ A * B, data = dat1)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.61325 -0.14431 -0.00563  0.10188  1.64475 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  14.5139     0.1412 102.783   <2e-16 ***
## A            -0.1586     0.1412  -1.123   0.2833    
## B             0.2930     0.1412   2.075   0.0602 .  
## A:B           0.1407     0.1412   0.997   0.3386    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5648 on 12 degrees of freedom
## Multiple R-squared:  0.3535, Adjusted R-squared:  0.1918 
## F-statistic: 2.187 on 3 and 12 DF,  p-value: 0.1425
interaction.plot(dat1$A,dat1$B,dat1$R)

## Comments:
## F-statistic: 2.187 on 3 and 12 DF,  p-value: 0.1425 indicate that the model is not significant
## P values are larger than alpha of 0.05. This implies that there are no significant factors in the model.


## Part c: Write down a regression equation that could be used to predict epitaxial layer thickness over the region of arsenic
## flow rate A and deposition time B used in this experiment.

## Thickness = 14.51 - 0.158625*A + 0.293000*B + 0.140750*A*B

## Part d: Analyze the residuals. Are there any residuals that should cause concern? 

## There is an outlier falls outside the group in the normal probability plot and the plot of residual.

## Part e: Discuss how you might deal with the potential outlier found in part (d).
## Replace the outlier value with the average of the observations from that experimental design.

R_outlier<-c(14.037,13.880,14.821,14.888,
             13.972,13.860,14.757,14.921,
             13.972,14.032,14.843,14.415,
             13.907,13.914,14.878,14.932)

data.frame(A,B,R_outlier)
##     A  B R_outlier
## 1  -1 -1    14.037
## 2   1 -1    13.880
## 3  -1  1    14.821
## 4   1  1    14.888
## 5  -1 -1    13.972
## 6   1 -1    13.860
## 7  -1  1    14.757
## 8   1  1    14.921
## 9  -1 -1    13.972
## 10  1 -1    14.032
## 11 -1  1    14.843
## 12  1  1    14.415
## 13 -1 -1    13.907
## 14  1 -1    13.914
## 15 -1  1    14.878
## 16  1  1    14.932
dat2<-data.frame(A,B,R_outlier)

model2<-lm(R_outlier~A*B,data=dat2)
coef(model2)
## (Intercept)           A           B         A:B 
##  14.3768125  -0.0215625   0.4300625   0.0036875
halfnormal(model2)
## Warning in halfnormal.lm(model2): halfnormal not recommended for models with
## more residual df than model df
## 
## Significant effects (alpha=0.05, Lenth method):
## [1] B  e4

plot(model2)

## hat values (leverages) are all = 0.25
##  and there are no factor predictors; no plot no. 5

summary(model2)
## 
## Call:
## lm.default(formula = R_outlier ~ A * B, data = dat2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.3740 -0.0465  0.0000  0.0735  0.1430 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 14.376813   0.033969 423.237  < 2e-16 ***
## A           -0.021563   0.033969  -0.635    0.537    
## B            0.430062   0.033969  12.661 2.66e-08 ***
## A:B          0.003688   0.033969   0.109    0.915    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1359 on 12 degrees of freedom
## Multiple R-squared:  0.9305, Adjusted R-squared:  0.9131 
## F-statistic: 53.57 on 3 and 12 DF,  p-value: 3.2e-07
interaction.plot(dat2$A,dat2$B,dat2$R_outlier)

## Thickness = 14.38 - 0.022*A + 0.43*B + 0.0037*A*B

R_outlierr<-c(14.037,13.880,14.821,14.888,
             14.165,13.860,14.757,14.921,
             13.972,14.032,14.843,14.415,
             13.907,13.914,14.878,14.932)

data.frame(A,B,R_outlierr)
##     A  B R_outlierr
## 1  -1 -1     14.037
## 2   1 -1     13.880
## 3  -1  1     14.821
## 4   1  1     14.888
## 5  -1 -1     14.165
## 6   1 -1     13.860
## 7  -1  1     14.757
## 8   1  1     14.921
## 9  -1 -1     13.972
## 10  1 -1     14.032
## 11 -1  1     14.843
## 12  1  1     14.415
## 13 -1 -1     13.907
## 14  1 -1     13.914
## 15 -1  1     14.878
## 16  1  1     14.932
dat2r<-data.frame(A,B,R_outlierr)

model2r<-lm(R_outlierr~A*B,data=dat2r)
coef(model2r)
## (Intercept)           A           B         A:B 
##   14.388875   -0.033625    0.418000    0.015750
halfnormal(model2r)
## Warning in halfnormal.lm(model2r): halfnormal not recommended for models with
## more residual df than model df
## 
## Significant effects (alpha=0.05, Lenth method):
## [1] B

plot(model2r)

## hat values (leverages) are all = 0.25
##  and there are no factor predictors; no plot no. 5

summary(model2r) 
## 
## Call:
## lm.default(formula = R_outlierr ~ A * B, data = dat2r)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.37400 -0.05156  0.00650  0.10188  0.14475 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 14.38888    0.03605 399.171  < 2e-16 ***
## A           -0.03363    0.03605  -0.933    0.369    
## B            0.41800    0.03605  11.596 7.08e-08 ***
## A:B          0.01575    0.03605   0.437    0.670    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1442 on 12 degrees of freedom
## Multiple R-squared:  0.9187, Adjusted R-squared:  0.8983 
## F-statistic: 45.18 on 3 and 12 DF,  p-value: 8.192e-07
interaction.plot(dat2r$A,dat2r$B,dat2r$R_outlierr)

## Thickness = 14.39 - 0.34*A + 0.42*B + 0.016*A*B

## Significant effects (alpha=0.05, Lenth method): [1] B 
## Replaced observation 2 with the average of the remaining observations in replicate II.
## another outlier showed up. maybe there is an issue with data recording. New outlier must be investigated.
## Another outlier showed up. Further investigation is required. 
## Problem 6.21
Lp<-c(rep(10,1),rep(30,1))
Type<-c(rep("Mallet",2),rep("Cavity",2))
Break<-c(rep("Straight",4),rep("Breaking",4))
Slope<-c(rep("Level",8),rep("Downhill",8))

dis<-c(10.0,0.0,4.0,0.0,0.0,5.0,6.5,16.5,4.5,19.5,15.0,41.5,8.0,21.5,0.0,18.0,
       18.0,16.5,6.0,10.0,0.0,20.5,18.5,4.5,18.0,18.0,16.0,39.0,4.5,10.5,0.0,5.0,
       14.0,4.5,1.0,34.0,18.5,18.0,7.5,0.0,14.5,16.0,8.5,6.5,6.5,6.5,0.0,7.0,
       12.5,17.5,14.5,11.0,19.5,20.0,6.0,23.5,10.0,5.5,0.0,3.5,10.0,0.0,4.5,10.0,
       19.0,20.5,12.0,25.5,16.0,29.5,0.0,8.0,0.0,10.0,0.5,7.0,13.0,15.5,1.0,32.5,
       16.0,17.5,14.0,21.5,15.0,19.0,10.0,8.0,17.5,7.0,9.0,8.5,41.0,24.0,4.0,18.5,
       18.5,33.0,5.0,0.0,11.0,10.0,0.0,8.0,6.0,36.0,3.0,36.0,14.0,16.0,6.5,8.0)
data.frame(Lp,Type,Break,Slope,dis)
##     Lp   Type    Break    Slope  dis
## 1   10 Mallet Straight    Level 10.0
## 2   30 Mallet Straight    Level  0.0
## 3   10 Cavity Straight    Level  4.0
## 4   30 Cavity Straight    Level  0.0
## 5   10 Mallet Breaking    Level  0.0
## 6   30 Mallet Breaking    Level  5.0
## 7   10 Cavity Breaking    Level  6.5
## 8   30 Cavity Breaking    Level 16.5
## 9   10 Mallet Straight Downhill  4.5
## 10  30 Mallet Straight Downhill 19.5
## 11  10 Cavity Straight Downhill 15.0
## 12  30 Cavity Straight Downhill 41.5
## 13  10 Mallet Breaking Downhill  8.0
## 14  30 Mallet Breaking Downhill 21.5
## 15  10 Cavity Breaking Downhill  0.0
## 16  30 Cavity Breaking Downhill 18.0
## 17  10 Mallet Straight    Level 18.0
## 18  30 Mallet Straight    Level 16.5
## 19  10 Cavity Straight    Level  6.0
## 20  30 Cavity Straight    Level 10.0
## 21  10 Mallet Breaking    Level  0.0
## 22  30 Mallet Breaking    Level 20.5
## 23  10 Cavity Breaking    Level 18.5
## 24  30 Cavity Breaking    Level  4.5
## 25  10 Mallet Straight Downhill 18.0
## 26  30 Mallet Straight Downhill 18.0
## 27  10 Cavity Straight Downhill 16.0
## 28  30 Cavity Straight Downhill 39.0
## 29  10 Mallet Breaking Downhill  4.5
## 30  30 Mallet Breaking Downhill 10.5
## 31  10 Cavity Breaking Downhill  0.0
## 32  30 Cavity Breaking Downhill  5.0
## 33  10 Mallet Straight    Level 14.0
## 34  30 Mallet Straight    Level  4.5
## 35  10 Cavity Straight    Level  1.0
## 36  30 Cavity Straight    Level 34.0
## 37  10 Mallet Breaking    Level 18.5
## 38  30 Mallet Breaking    Level 18.0
## 39  10 Cavity Breaking    Level  7.5
## 40  30 Cavity Breaking    Level  0.0
## 41  10 Mallet Straight Downhill 14.5
## 42  30 Mallet Straight Downhill 16.0
## 43  10 Cavity Straight Downhill  8.5
## 44  30 Cavity Straight Downhill  6.5
## 45  10 Mallet Breaking Downhill  6.5
## 46  30 Mallet Breaking Downhill  6.5
## 47  10 Cavity Breaking Downhill  0.0
## 48  30 Cavity Breaking Downhill  7.0
## 49  10 Mallet Straight    Level 12.5
## 50  30 Mallet Straight    Level 17.5
## 51  10 Cavity Straight    Level 14.5
## 52  30 Cavity Straight    Level 11.0
## 53  10 Mallet Breaking    Level 19.5
## 54  30 Mallet Breaking    Level 20.0
## 55  10 Cavity Breaking    Level  6.0
## 56  30 Cavity Breaking    Level 23.5
## 57  10 Mallet Straight Downhill 10.0
## 58  30 Mallet Straight Downhill  5.5
## 59  10 Cavity Straight Downhill  0.0
## 60  30 Cavity Straight Downhill  3.5
## 61  10 Mallet Breaking Downhill 10.0
## 62  30 Mallet Breaking Downhill  0.0
## 63  10 Cavity Breaking Downhill  4.5
## 64  30 Cavity Breaking Downhill 10.0
## 65  10 Mallet Straight    Level 19.0
## 66  30 Mallet Straight    Level 20.5
## 67  10 Cavity Straight    Level 12.0
## 68  30 Cavity Straight    Level 25.5
## 69  10 Mallet Breaking    Level 16.0
## 70  30 Mallet Breaking    Level 29.5
## 71  10 Cavity Breaking    Level  0.0
## 72  30 Cavity Breaking    Level  8.0
## 73  10 Mallet Straight Downhill  0.0
## 74  30 Mallet Straight Downhill 10.0
## 75  10 Cavity Straight Downhill  0.5
## 76  30 Cavity Straight Downhill  7.0
## 77  10 Mallet Breaking Downhill 13.0
## 78  30 Mallet Breaking Downhill 15.5
## 79  10 Cavity Breaking Downhill  1.0
## 80  30 Cavity Breaking Downhill 32.5
## 81  10 Mallet Straight    Level 16.0
## 82  30 Mallet Straight    Level 17.5
## 83  10 Cavity Straight    Level 14.0
## 84  30 Cavity Straight    Level 21.5
## 85  10 Mallet Breaking    Level 15.0
## 86  30 Mallet Breaking    Level 19.0
## 87  10 Cavity Breaking    Level 10.0
## 88  30 Cavity Breaking    Level  8.0
## 89  10 Mallet Straight Downhill 17.5
## 90  30 Mallet Straight Downhill  7.0
## 91  10 Cavity Straight Downhill  9.0
## 92  30 Cavity Straight Downhill  8.5
## 93  10 Mallet Breaking Downhill 41.0
## 94  30 Mallet Breaking Downhill 24.0
## 95  10 Cavity Breaking Downhill  4.0
## 96  30 Cavity Breaking Downhill 18.5
## 97  10 Mallet Straight    Level 18.5
## 98  30 Mallet Straight    Level 33.0
## 99  10 Cavity Straight    Level  5.0
## 100 30 Cavity Straight    Level  0.0
## 101 10 Mallet Breaking    Level 11.0
## 102 30 Mallet Breaking    Level 10.0
## 103 10 Cavity Breaking    Level  0.0
## 104 30 Cavity Breaking    Level  8.0
## 105 10 Mallet Straight Downhill  6.0
## 106 30 Mallet Straight Downhill 36.0
## 107 10 Cavity Straight Downhill  3.0
## 108 30 Cavity Straight Downhill 36.0
## 109 10 Mallet Breaking Downhill 14.0
## 110 30 Mallet Breaking Downhill 16.0
## 111 10 Cavity Breaking Downhill  6.5
## 112 30 Cavity Breaking Downhill  8.0
dat_golf<-data.frame(Lp,Type,Break,Slope,dis)

model_golf<-lm(dis~Lp+Type,data=dat_golf)
coef(model_golf)
## (Intercept)          Lp  TypeMallet 
##   4.7142857   0.2861607   3.7232143
## halfnormal(model_golf)
plot(model_golf)

## hat values (leverages) are all = 0.02678571
##  and there are no factor predictors; no plot no. 5

summary(model_golf)
## 
## Call:
## lm.default(formula = dis ~ Lp + Type, data = dat_golf)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -17.022  -6.632  -1.076   4.701  29.701 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  4.71429    2.13206   2.211  0.02911 * 
## Lp           0.28616    0.08704   3.288  0.00136 **
## TypeMallet   3.72321    1.74082   2.139  0.03469 * 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 9.212 on 109 degrees of freedom
## Multiple R-squared:  0.1237, Adjusted R-squared:  0.1076 
## F-statistic: 7.692 on 2 and 109 DF,  p-value: 0.0007504
## Part a: Analyze the data from this experiment. Which factors significantly affect putting performance?
  ## The half normal probability plot of effects idicate that only factors Lp and Type are significant
  ## Length of putt in feets and type of putter Mallet or cavity have significant effect on putting.

##  Call:
## lm.default(formula = dis ~ Lp * Type * Break * Slope, data = dat_golf)

## Residuals:
##   Min      1Q  Median      3Q     Max 
## -16.786  -6.036  -0.250   4.250  27.143 

## Coefficients:
##                                       Estimate Std. Error t value Pr(>|t|)  
## (Intercept)                             -3.6429     5.5621  -0.655   0.5141  
## Lp                                       0.5929     0.2487   2.383   0.0191 *
## TypeMallet                              17.7143     7.8660   2.252   0.0266 *
## BreakStraight                            4.6429     7.8660   0.590   0.5564  
## SlopeLevel                               9.1429     7.8660   1.162   0.2480  
## Lp:TypeMallet                           -0.6143     0.3518  -1.746   0.0840 .
## Lp:BreakStraight                         0.0500     0.3518   0.142   0.8873  
## TypeMallet:BreakStraight               -11.6071    11.1242  -1.043   0.2994  
## Lp:SlopeLevel                           -0.4500     0.3518  -1.279   0.2039  
## TypeMallet:SlopeLevel                  -14.7857    11.1242  -1.329   0.1869  
## BreakStraight:SlopeLevel                -5.3214    11.1242  -0.478   0.6335  
## Lp:TypeMallet:BreakStraight              0.2679     0.4975   0.538   0.5915  
## Lp:TypeMallet:SlopeLevel                 0.7714     0.4975   1.551   0.1243  
## Lp:BreakStraight:SlopeLevel              0.1321     0.4975   0.266   0.7911  
## TypeMallet:BreakStraight:SlopeLevel     19.1786    15.7319   1.219   0.2258  
## Lp:TypeMallet:BreakStraight:SlopeLevel  -0.7393     0.7036  -1.051   0.2960  
## ---
##  Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

## Residual standard error: 9.307 on 96 degrees of freedom
## Multiple R-squared:  0.2121, Adjusted R-squared:  0.08898 
## F-statistic: 1.723 on 15 and 96 DF,  p-value: 0.0589

## There is 0.089% chance that F values is large due to noise in the model.
##                                       Estimate Std. Error t value Pr(>|t|)  
## (Intercept)                             -3.6429     5.5621  -0.655   0.5141  
## Lp                                       0.5929     0.2487   2.383   0.0191 *
## TypeMallet                              17.7143     7.8660   2.252   0.0266 *  

## P value of 0.0191 and 0.0266 for factor Lp and Type of putter are both less than alpha value 0.05 
## This means that Lenght of putt and Type of putter are significant.


## We then pull up all significant factors LP and Type
model_golf1<-aov(dis~Lp+Type,data=dat_golf)
summary(model_golf)
## 
## Call:
## lm.default(formula = dis ~ Lp + Type, data = dat_golf)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -17.022  -6.632  -1.076   4.701  29.701 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  4.71429    2.13206   2.211  0.02911 * 
## Lp           0.28616    0.08704   3.288  0.00136 **
## TypeMallet   3.72321    1.74082   2.139  0.03469 * 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 9.212 on 109 degrees of freedom
## Multiple R-squared:  0.1237, Adjusted R-squared:  0.1076 
## F-statistic: 7.692 on 2 and 109 DF,  p-value: 0.0007504
summary(model_golf1)
##              Df Sum Sq Mean Sq F value  Pr(>F)   
## Lp            1    917   917.1  10.809 0.00136 **
## Type          1    388   388.1   4.574 0.03469 * 
## Residuals   109   9249    84.9                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
interaction.plot(dat_golf$Lp,dat_golf$Type, dat_golf$dis)

## Part b: Analyze the residuals from this experiment. Are there any indications of model inadequacy?
  ## The normality assumption is violated. 
  ## The variance is slightly inequal.  
  ## Action: Take square root transformation and check for normality and equal variance assumptions again. 
  ## then generate residual plots . 
  
dis_trans<-c(sqrt(dis))
data.frame(Lp,Type,Break,Slope,dis_trans)
##     Lp   Type    Break    Slope dis_trans
## 1   10 Mallet Straight    Level 3.1622777
## 2   30 Mallet Straight    Level 0.0000000
## 3   10 Cavity Straight    Level 2.0000000
## 4   30 Cavity Straight    Level 0.0000000
## 5   10 Mallet Breaking    Level 0.0000000
## 6   30 Mallet Breaking    Level 2.2360680
## 7   10 Cavity Breaking    Level 2.5495098
## 8   30 Cavity Breaking    Level 4.0620192
## 9   10 Mallet Straight Downhill 2.1213203
## 10  30 Mallet Straight Downhill 4.4158804
## 11  10 Cavity Straight Downhill 3.8729833
## 12  30 Cavity Straight Downhill 6.4420494
## 13  10 Mallet Breaking Downhill 2.8284271
## 14  30 Mallet Breaking Downhill 4.6368092
## 15  10 Cavity Breaking Downhill 0.0000000
## 16  30 Cavity Breaking Downhill 4.2426407
## 17  10 Mallet Straight    Level 4.2426407
## 18  30 Mallet Straight    Level 4.0620192
## 19  10 Cavity Straight    Level 2.4494897
## 20  30 Cavity Straight    Level 3.1622777
## 21  10 Mallet Breaking    Level 0.0000000
## 22  30 Mallet Breaking    Level 4.5276926
## 23  10 Cavity Breaking    Level 4.3011626
## 24  30 Cavity Breaking    Level 2.1213203
## 25  10 Mallet Straight Downhill 4.2426407
## 26  30 Mallet Straight Downhill 4.2426407
## 27  10 Cavity Straight Downhill 4.0000000
## 28  30 Cavity Straight Downhill 6.2449980
## 29  10 Mallet Breaking Downhill 2.1213203
## 30  30 Mallet Breaking Downhill 3.2403703
## 31  10 Cavity Breaking Downhill 0.0000000
## 32  30 Cavity Breaking Downhill 2.2360680
## 33  10 Mallet Straight    Level 3.7416574
## 34  30 Mallet Straight    Level 2.1213203
## 35  10 Cavity Straight    Level 1.0000000
## 36  30 Cavity Straight    Level 5.8309519
## 37  10 Mallet Breaking    Level 4.3011626
## 38  30 Mallet Breaking    Level 4.2426407
## 39  10 Cavity Breaking    Level 2.7386128
## 40  30 Cavity Breaking    Level 0.0000000
## 41  10 Mallet Straight Downhill 3.8078866
## 42  30 Mallet Straight Downhill 4.0000000
## 43  10 Cavity Straight Downhill 2.9154759
## 44  30 Cavity Straight Downhill 2.5495098
## 45  10 Mallet Breaking Downhill 2.5495098
## 46  30 Mallet Breaking Downhill 2.5495098
## 47  10 Cavity Breaking Downhill 0.0000000
## 48  30 Cavity Breaking Downhill 2.6457513
## 49  10 Mallet Straight    Level 3.5355339
## 50  30 Mallet Straight    Level 4.1833001
## 51  10 Cavity Straight    Level 3.8078866
## 52  30 Cavity Straight    Level 3.3166248
## 53  10 Mallet Breaking    Level 4.4158804
## 54  30 Mallet Breaking    Level 4.4721360
## 55  10 Cavity Breaking    Level 2.4494897
## 56  30 Cavity Breaking    Level 4.8476799
## 57  10 Mallet Straight Downhill 3.1622777
## 58  30 Mallet Straight Downhill 2.3452079
## 59  10 Cavity Straight Downhill 0.0000000
## 60  30 Cavity Straight Downhill 1.8708287
## 61  10 Mallet Breaking Downhill 3.1622777
## 62  30 Mallet Breaking Downhill 0.0000000
## 63  10 Cavity Breaking Downhill 2.1213203
## 64  30 Cavity Breaking Downhill 3.1622777
## 65  10 Mallet Straight    Level 4.3588989
## 66  30 Mallet Straight    Level 4.5276926
## 67  10 Cavity Straight    Level 3.4641016
## 68  30 Cavity Straight    Level 5.0497525
## 69  10 Mallet Breaking    Level 4.0000000
## 70  30 Mallet Breaking    Level 5.4313902
## 71  10 Cavity Breaking    Level 0.0000000
## 72  30 Cavity Breaking    Level 2.8284271
## 73  10 Mallet Straight Downhill 0.0000000
## 74  30 Mallet Straight Downhill 3.1622777
## 75  10 Cavity Straight Downhill 0.7071068
## 76  30 Cavity Straight Downhill 2.6457513
## 77  10 Mallet Breaking Downhill 3.6055513
## 78  30 Mallet Breaking Downhill 3.9370039
## 79  10 Cavity Breaking Downhill 1.0000000
## 80  30 Cavity Breaking Downhill 5.7008771
## 81  10 Mallet Straight    Level 4.0000000
## 82  30 Mallet Straight    Level 4.1833001
## 83  10 Cavity Straight    Level 3.7416574
## 84  30 Cavity Straight    Level 4.6368092
## 85  10 Mallet Breaking    Level 3.8729833
## 86  30 Mallet Breaking    Level 4.3588989
## 87  10 Cavity Breaking    Level 3.1622777
## 88  30 Cavity Breaking    Level 2.8284271
## 89  10 Mallet Straight Downhill 4.1833001
## 90  30 Mallet Straight Downhill 2.6457513
## 91  10 Cavity Straight Downhill 3.0000000
## 92  30 Cavity Straight Downhill 2.9154759
## 93  10 Mallet Breaking Downhill 6.4031242
## 94  30 Mallet Breaking Downhill 4.8989795
## 95  10 Cavity Breaking Downhill 2.0000000
## 96  30 Cavity Breaking Downhill 4.3011626
## 97  10 Mallet Straight    Level 4.3011626
## 98  30 Mallet Straight    Level 5.7445626
## 99  10 Cavity Straight    Level 2.2360680
## 100 30 Cavity Straight    Level 0.0000000
## 101 10 Mallet Breaking    Level 3.3166248
## 102 30 Mallet Breaking    Level 3.1622777
## 103 10 Cavity Breaking    Level 0.0000000
## 104 30 Cavity Breaking    Level 2.8284271
## 105 10 Mallet Straight Downhill 2.4494897
## 106 30 Mallet Straight Downhill 6.0000000
## 107 10 Cavity Straight Downhill 1.7320508
## 108 30 Cavity Straight Downhill 6.0000000
## 109 10 Mallet Breaking Downhill 3.7416574
## 110 30 Mallet Breaking Downhill 4.0000000
## 111 10 Cavity Breaking Downhill 2.5495098
## 112 30 Cavity Breaking Downhill 2.8284271
dat_golf2<-data.frame(Lp,Type,Break,Slope,dis_trans)

model_golf2<-lm(dis_trans~Lp*Type*Break*Slope,data=dat_golf2)
coef(model_golf2)
##                            (Intercept)                                     Lp 
##                            -0.15033673                             0.12461696 
##                             TypeMallet                          BreakStraight 
##                             3.71983177                             1.57992513 
##                             SlopeLevel                          Lp:TypeMallet 
##                             2.01368366                            -0.13282550 
##                       Lp:BreakStraight               TypeMallet:BreakStraight 
##                            -0.03575270                            -2.78592107 
##                          Lp:SlopeLevel                  TypeMallet:SlopeLevel 
##                            -0.09379376                            -3.34826087 
##               BreakStraight:SlopeLevel            Lp:TypeMallet:BreakStraight 
##                            -1.00747250                             0.09285297 
##               Lp:TypeMallet:SlopeLevel            Lp:BreakStraight:SlopeLevel 
##                             0.16289124                             0.02848102 
##    TypeMallet:BreakStraight:SlopeLevel Lp:TypeMallet:BreakStraight:SlopeLevel 
##                             4.06457337                            -0.16447007
##  halfnormal(model_golf2)
plot(model_golf2)

## hat values (leverages) are all = 0.1428571
##  and there are no factor predictors; no plot no. 5

summary(model_golf2)  
## 
## Call:
## lm.default(formula = dis_trans ~ Lp * Type * Break * Slope, data = dat_golf2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.5460 -0.9090  0.2177  0.9994  2.9157 
## 
## Coefficients:
##                                        Estimate Std. Error t value Pr(>|t|)   
## (Intercept)                            -0.15034    0.92189  -0.163  0.87080   
## Lp                                      0.12462    0.04123   3.023  0.00321 **
## TypeMallet                              3.71983    1.30374   2.853  0.00530 **
## BreakStraight                           1.57993    1.30374   1.212  0.22855   
## SlopeLevel                              2.01368    1.30374   1.545  0.12575   
## Lp:TypeMallet                          -0.13283    0.05831  -2.278  0.02494 * 
## Lp:BreakStraight                       -0.03575    0.05831  -0.613  0.54119   
## TypeMallet:BreakStraight               -2.78592    1.84377  -1.511  0.13408   
## Lp:SlopeLevel                          -0.09379    0.05831  -1.609  0.11097   
## TypeMallet:SlopeLevel                  -3.34826    1.84377  -1.816  0.07249 . 
## BreakStraight:SlopeLevel               -1.00747    1.84377  -0.546  0.58605   
## Lp:TypeMallet:BreakStraight             0.09285    0.08246   1.126  0.26293   
## Lp:TypeMallet:SlopeLevel                0.16289    0.08246   1.975  0.05108 . 
## Lp:BreakStraight:SlopeLevel             0.02848    0.08246   0.345  0.73054   
## TypeMallet:BreakStraight:SlopeLevel     4.06457    2.60749   1.559  0.12233   
## Lp:TypeMallet:BreakStraight:SlopeLevel -0.16447    0.11661  -1.410  0.16165   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.543 on 96 degrees of freedom
## Multiple R-squared:  0.2279, Adjusted R-squared:  0.1073 
## F-statistic: 1.889 on 15 and 96 DF,  p-value: 0.0335
model_golf2b<-lm(dis_trans~Lp+Type,data=dat_golf2)
model_golf2bb<-aov(dis_trans~Lp+Type,data=dat_golf2)
coef(model_golf2b)
## (Intercept)          Lp  TypeMallet 
##  1.85530859  0.04392853  0.74746603
## halfnormal(model_golf2b)
plot(model_golf2b)

## hat values (leverages) are all = 0.02678571
##  and there are no factor predictors; no plot no. 5

summary(model_golf2bb)
##              Df Sum Sq Mean Sq F value  Pr(>F)   
## Lp            1  21.61  21.613   9.109 0.00317 **
## Type          1  15.64  15.644   6.593 0.01159 * 
## Residuals   109 258.63   2.373                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(model_golf2b)
## 
## Call:
## lm.default(formula = dis_trans ~ Lp + Type, data = dat_golf2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.9206 -0.7584  0.1492  0.9630  3.3611 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.85531    0.35653   5.204 9.25e-07 ***
## Lp           0.04393    0.01456   3.018  0.00317 ** 
## TypeMallet   0.74747    0.29110   2.568  0.01159 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.54 on 109 degrees of freedom
## Multiple R-squared:  0.1259, Adjusted R-squared:  0.1099 
## F-statistic: 7.851 on 2 and 109 DF,  p-value: 0.0006527
## Generate interaction plots

interaction.plot(dat_golf$Lp,dat_golf$Type, dat_golf$dis)

interaction.plot(dat_golf2$Lp,dat_golf2$Type, dat_golf2$dis_trans)

## Conclusion: 
## Interaction plot indicate that length of putt of 10 foot and the type cavity-back putter reduce the mean distance after transformation.
## Problem 6.36

A<-c(-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1)
B<-c(-1,-1,1,1,-1,-1,1,1,-1,-1,1,1,-1,-1,1,1)
C<-c(-1,-1,-1,-1,1,1,1,1,-1,-1,-1,-1,1,1,1,1)
D<-c(-1,-1,-1,-1,-1,-1,-1,-1,1,1,1,1,1,1,1,1)

Resistivity<-c(1.92,11.28,1.09,5.75,2.13,9.53,1.03,5.35,1.60,11.73,1.16,4.68,2.16,9.11,1.07,5.30)
data.frame(A,B,C,D,Resistivity)
##     A  B  C  D Resistivity
## 1  -1 -1 -1 -1        1.92
## 2   1 -1 -1 -1       11.28
## 3  -1  1 -1 -1        1.09
## 4   1  1 -1 -1        5.75
## 5  -1 -1  1 -1        2.13
## 6   1 -1  1 -1        9.53
## 7  -1  1  1 -1        1.03
## 8   1  1  1 -1        5.35
## 9  -1 -1 -1  1        1.60
## 10  1 -1 -1  1       11.73
## 11 -1  1 -1  1        1.16
## 12  1  1 -1  1        4.68
## 13 -1 -1  1  1        2.16
## 14  1 -1  1  1        9.11
## 15 -1  1  1  1        1.07
## 16  1  1  1  1        5.30
dat_Resistivity<-data.frame(A,B,C,D,Resistivity)

model_Resistivity<-lm(Resistivity~A*B*C*D,data=dat_Resistivity)
coef(model_Resistivity)
## (Intercept)           A           B           C           D         A:B 
##    4.680625    3.160625   -1.501875   -0.220625   -0.079375   -1.069375 
##         A:C         B:C         A:D         B:D         C:D       A:B:C 
##   -0.298125    0.229375   -0.056875   -0.046875    0.029375    0.344375 
##       A:B:D       A:C:D       B:C:D     A:B:C:D 
##   -0.096875   -0.010625    0.094375    0.141875
## halfnormal(model_Resistivity)
## plot(model_Resistivity)
summary(model_Resistivity)
## 
## Call:
## lm.default(formula = Resistivity ~ A * B * C * D, data = dat_Resistivity)
## 
## Residuals:
## ALL 16 residuals are 0: no residual degrees of freedom!
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  4.68062        NaN     NaN      NaN
## A            3.16062        NaN     NaN      NaN
## B           -1.50187        NaN     NaN      NaN
## C           -0.22062        NaN     NaN      NaN
## D           -0.07937        NaN     NaN      NaN
## A:B         -1.06938        NaN     NaN      NaN
## A:C         -0.29812        NaN     NaN      NaN
## B:C          0.22937        NaN     NaN      NaN
## A:D         -0.05687        NaN     NaN      NaN
## B:D         -0.04688        NaN     NaN      NaN
## C:D          0.02937        NaN     NaN      NaN
## A:B:C        0.34437        NaN     NaN      NaN
## A:B:D       -0.09688        NaN     NaN      NaN
## A:C:D       -0.01063        NaN     NaN      NaN
## B:C:D        0.09438        NaN     NaN      NaN
## A:B:C:D      0.14188        NaN     NaN      NaN
## 
## Residual standard error: NaN on 0 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:    NaN 
## F-statistic:   NaN on 15 and 0 DF,  p-value: NA
## Part a: Estimate the factor effects. Plot the effect estimates on a normal probability plot and select a tentative model.
## > halfnormal(model_Resistivity)

## Significant effects (alpha=0.05, Lenth method): [1] A     B     A:B    

## Part b: Fit the model identified in part (a) and analyze the residuals. Is there any indication of model inadequacy?
  ## The normal probability plot of residuals is not satisfactory. The plots of residual versus predicted, 
  ## residual versus factor A, and the residual versus factor B are funnel shaped indicating non-constant variance.
model_Resistivity1<-lm(Resistivity~A+B+A*B,data=dat_Resistivity)
model_Resistivity2<-aov(Resistivity~A+B+A*B,data=dat_Resistivity)
coef(model_Resistivity1)
## (Intercept)           A           B         A:B 
##    4.680625    3.160625   -1.501875   -1.069375
halfnormal(model_Resistivity1)
## Warning in halfnormal.lm(model_Resistivity1): halfnormal not recommended for
## models with more residual df than model df
## 
## Significant effects (alpha=0.05, Lenth method):
## [1] A   B   A:B

plot(model_Resistivity1)

## hat values (leverages) are all = 0.25
##  and there are no factor predictors; no plot no. 5

summary(model_Resistivity2)
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## A            1 159.83  159.83  333.09 4.05e-10 ***
## B            1  36.09   36.09   75.21 1.63e-06 ***
## A:B          1  18.30   18.30   38.13 4.76e-05 ***
## Residuals   12   5.76    0.48                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(model_Resistivity)
## 
## Call:
## lm.default(formula = Resistivity ~ A * B * C * D, data = dat_Resistivity)
## 
## Residuals:
## ALL 16 residuals are 0: no residual degrees of freedom!
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  4.68062        NaN     NaN      NaN
## A            3.16062        NaN     NaN      NaN
## B           -1.50187        NaN     NaN      NaN
## C           -0.22062        NaN     NaN      NaN
## D           -0.07937        NaN     NaN      NaN
## A:B         -1.06938        NaN     NaN      NaN
## A:C         -0.29812        NaN     NaN      NaN
## B:C          0.22937        NaN     NaN      NaN
## A:D         -0.05687        NaN     NaN      NaN
## B:D         -0.04688        NaN     NaN      NaN
## C:D          0.02937        NaN     NaN      NaN
## A:B:C        0.34437        NaN     NaN      NaN
## A:B:D       -0.09688        NaN     NaN      NaN
## A:C:D       -0.01063        NaN     NaN      NaN
## B:C:D        0.09438        NaN     NaN      NaN
## A:B:C:D      0.14188        NaN     NaN      NaN
## 
## Residual standard error: NaN on 0 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:    NaN 
## F-statistic:   NaN on 15 and 0 DF,  p-value: NA
##  Part c: Repeat the analysis from parts (a) and (b) using ln (y) as the response variable. Is there an indication that the transformation has been useful?
  
Resistivity_trans<-c(log(Resistivity))  
data.frame(A,B,C,D,Resistivity_trans)
##     A  B  C  D Resistivity_trans
## 1  -1 -1 -1 -1        0.65232519
## 2   1 -1 -1 -1        2.42303125
## 3  -1  1 -1 -1        0.08617770
## 4   1  1 -1 -1        1.74919985
## 5  -1 -1  1 -1        0.75612198
## 6   1 -1  1 -1        2.25444472
## 7  -1  1  1 -1        0.02955880
## 8   1  1  1 -1        1.67709656
## 9  -1 -1 -1  1        0.47000363
## 10  1 -1 -1  1        2.46214966
## 11 -1  1 -1  1        0.14842001
## 12  1  1 -1  1        1.54329811
## 13 -1 -1  1  1        0.77010822
## 14  1 -1  1  1        2.20937271
## 15 -1  1  1  1        0.06765865
## 16  1  1  1  1        1.66770682
dat_Resistivity_trans<-data.frame(A,B,C,D,Resistivity_trans)

model_Resistivity_trans<-lm(Resistivity_trans~A*B*C*D,data=dat_Resistivity_trans)
coef(model_Resistivity_trans)
##  (Intercept)            A            B            C            D          A:B 
##  1.185417116  0.812870345 -0.314277554 -0.006408558 -0.018077390 -0.024684570 
##          A:C          B:C          A:D          B:D          C:D        A:B:C 
## -0.039723700 -0.004225796 -0.009578245  0.003708723  0.017780432  0.063434408 
##        A:B:D        A:C:D        B:C:D      A:B:C:D 
## -0.029875960 -0.003740235  0.003765760  0.031322043
halfnormal(model_Resistivity_trans)
## 
## Significant effects (alpha=0.05, Lenth method):
## [1] A     B     A:B:C

## plot(model_Resistivity_trans)

## Part d: Fit a model in terms of the coded variables that can be used to predict the resistivity.

model_Resistivity_trans3<-lm(Resistivity_trans~A*B,data=dat_Resistivity_trans)
model_Resistivity_trans4<-aov(Resistivity_trans~A*B,data=dat_Resistivity_trans)
coef(model_Resistivity_trans3)
## (Intercept)           A           B         A:B 
##  1.18541712  0.81287034 -0.31427755 -0.02468457
halfnormal(model_Resistivity_trans3)
## Warning in halfnormal.lm(model_Resistivity_trans3): halfnormal not recommended
## for models with more residual df than model df
## 
## Significant effects (alpha=0.05, Lenth method):
## [1] A B

plot(model_Resistivity_trans3)

## hat values (leverages) are all = 0.25
##  and there are no factor predictors; no plot no. 5

summary(model_Resistivity_trans4)
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## A            1 10.572  10.572  954.04 8.33e-13 ***
## B            1  1.580   1.580  142.61 5.10e-08 ***
## A:B          1  0.010   0.010    0.88    0.367    
## Residuals   12  0.133   0.011                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(model_Resistivity_trans3)
## 
## Call:
## lm.default(formula = Resistivity_trans ~ A * B, data = dat_Resistivity_trans)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.192136 -0.060747  0.005803  0.086805  0.124900 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.18542    0.02632  45.044 9.34e-15 ***
## A            0.81287    0.02632  30.888 8.33e-13 ***
## B           -0.31428    0.02632 -11.942 5.10e-08 ***
## A:B         -0.02468    0.02632  -0.938    0.367    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1053 on 12 degrees of freedom
## Multiple R-squared:  0.9892, Adjusted R-squared:  0.9865 
## F-statistic: 365.8 on 3 and 12 DF,  p-value: 4.672e-12
## Problem 6.39 Quality and Reliability Engineering

A<-c(-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1)
B<-c(-1,-1,1,1,-1,-1,1,1,-1,-1,1,1,-1,-1,1,1,-1,-1,1,1,-1,-1,1,1,-1,-1,1,1,-1,-1,1,1)
C<-c(-1,-1,-1,-1,1,1,1,1,-1,-1,-1,-1,1,1,1,1,-1,-1,-1,-1,1,1,1,1,-1,-1,-1,-1,1,1,1,1)
D<-c(-1,-1,-1,-1,-1,-1,-1,-1,1,1,1,1,1,1,1,1,-1,-1,-1,-1,-1,-1,-1,-1,1,1,1,1,1,1,1,1)
E<-c(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)

y<-c(8.11,5.56,5.77,5.82,9.17,7.8,3.23,5.69,8.82,14.23,9.2,8.94,8.68,11.49,6.25,9.12,
     7.93,5,7.47,12,9.86,3.65,6.4,11.61,12.43,17.55,8.87, 25.38,13.06,18.85,11.78,26.05)

data.frame(A,B,C,D,E,y)
##     A  B  C  D  E     y
## 1  -1 -1 -1 -1 -1  8.11
## 2   1 -1 -1 -1 -1  5.56
## 3  -1  1 -1 -1 -1  5.77
## 4   1  1 -1 -1 -1  5.82
## 5  -1 -1  1 -1 -1  9.17
## 6   1 -1  1 -1 -1  7.80
## 7  -1  1  1 -1 -1  3.23
## 8   1  1  1 -1 -1  5.69
## 9  -1 -1 -1  1 -1  8.82
## 10  1 -1 -1  1 -1 14.23
## 11 -1  1 -1  1 -1  9.20
## 12  1  1 -1  1 -1  8.94
## 13 -1 -1  1  1 -1  8.68
## 14  1 -1  1  1 -1 11.49
## 15 -1  1  1  1 -1  6.25
## 16  1  1  1  1 -1  9.12
## 17 -1 -1 -1 -1  1  7.93
## 18  1 -1 -1 -1  1  5.00
## 19 -1  1 -1 -1  1  7.47
## 20  1  1 -1 -1  1 12.00
## 21 -1 -1  1 -1  1  9.86
## 22  1 -1  1 -1  1  3.65
## 23 -1  1  1 -1  1  6.40
## 24  1  1  1 -1  1 11.61
## 25 -1 -1 -1  1  1 12.43
## 26  1 -1 -1  1  1 17.55
## 27 -1  1 -1  1  1  8.87
## 28  1  1 -1  1  1 25.38
## 29 -1 -1  1  1  1 13.06
## 30  1 -1  1  1  1 18.85
## 31 -1  1  1  1  1 11.78
## 32  1  1  1  1  1 26.05
dat_Quality<-data.frame(A,B,C,D,E,y)

model_Quality<-lm(y~A*B*C*D*E,data=dat_Quality)
coef(model_Quality)
## (Intercept)           A           B           C           D           E 
##  10.1803125   1.6159375   0.0434375  -0.0121875   2.9884375   2.1878125 
##         A:B         A:C         B:C         A:D         B:D         C:D 
##   1.2365625  -0.0015625  -0.1953125   1.6665625  -0.0134375   0.0034375 
##         A:E         B:E         C:E         D:E       A:B:C       A:B:D 
##   1.0271875   1.2834375   0.3015625   1.3896875   0.2503125  -0.3453125 
##       A:C:D       B:C:D       A:B:E       A:C:E       B:C:E       A:D:E 
##  -0.0634375   0.3053125   1.1853125  -0.2590625   0.1709375   0.9015625 
##       B:D:E       C:D:E     A:B:C:D     A:B:C:E     A:B:D:E     A:C:D:E 
##  -0.0396875   0.3959375  -0.0740625  -0.1846875   0.4071875   0.1278125 
##     B:C:D:E   A:B:C:D:E 
##  -0.0746875  -0.3553125
## halfnormal(model_Quality)
## plot(model_Quality)

## Part a: Analyze the data from this experiment. Identify the significant factors and interactions.

model_Quality1<-lm(y~A+B+D+E+A*B+A*D+A*E+B*E+D*E+ A*B*E+A*D*E,data=dat_Quality)
model_Quality2<-aov(y~A+B+D+E+A*B+A*D+A*E+B*E+D*E+ A*B*E+A*D*E,data=dat_Quality)
coef(model_Quality1)
## (Intercept)           A           B           D           E         A:B 
##  10.1803125   1.6159375   0.0434375   2.9884375   2.1878125   1.2365625 
##         A:D         A:E         B:E         D:E       A:B:E       A:D:E 
##   1.6665625   1.0271875   1.2834375   1.3896875   1.1853125   0.9015625
halfnormal(model_Quality1)
## Warning in halfnormal.lm(model_Quality1): halfnormal not recommended for models
## with more residual df than model df
## 
## Significant effects (alpha=0.05, Lenth method):
##  [1] D     E     A:D   A     D:E   B:E   A:B   A:B:E A:E   A:D:E e10

plot(model_Quality1)

## hat values (leverages) are all = 0.375
##  and there are no factor predictors; no plot no. 5

summary(model_Quality2)
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## A            1  83.56   83.56  51.362 6.10e-07 ***
## B            1   0.06    0.06   0.037 0.849178    
## D            1 285.78  285.78 175.664 2.30e-11 ***
## E            1 153.17  153.17  94.149 5.24e-09 ***
## A:B          1  48.93   48.93  30.076 2.28e-05 ***
## A:D          1  88.88   88.88  54.631 3.87e-07 ***
## A:E          1  33.76   33.76  20.754 0.000192 ***
## B:E          1  52.71   52.71  32.400 1.43e-05 ***
## D:E          1  61.80   61.80  37.986 5.07e-06 ***
## A:B:E        1  44.96   44.96  27.635 3.82e-05 ***
## A:D:E        1  26.01   26.01  15.988 0.000706 ***
## Residuals   20  32.54    1.63                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(model_Quality1)
## 
## Call:
## lm.default(formula = y ~ A + B + D + E + A * B + A * D + A * 
##     E + B * E + D * E + A * B * E + A * D * E, data = dat_Quality)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.04875 -0.67375 -0.00687  0.65281  2.25375 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 10.18031    0.22548  45.150  < 2e-16 ***
## A            1.61594    0.22548   7.167 6.10e-07 ***
## B            0.04344    0.22548   0.193 0.849178    
## D            2.98844    0.22548  13.254 2.30e-11 ***
## E            2.18781    0.22548   9.703 5.24e-09 ***
## A:B          1.23656    0.22548   5.484 2.28e-05 ***
## A:D          1.66656    0.22548   7.391 3.87e-07 ***
## A:E          1.02719    0.22548   4.556 0.000192 ***
## B:E          1.28344    0.22548   5.692 1.43e-05 ***
## D:E          1.38969    0.22548   6.163 5.07e-06 ***
## A:B:E        1.18531    0.22548   5.257 3.82e-05 ***
## A:D:E        0.90156    0.22548   3.998 0.000706 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.275 on 20 degrees of freedom
## Multiple R-squared:  0.9643, Adjusted R-squared:  0.9447 
## F-statistic: 49.15 on 11 and 20 DF,  p-value: 5.069e-12
## Part b: Analyze the residuals from this experiment. Are there any indications of model inadequacy or violations of the assumptions?
  
## The residual plots do not identify any concerns with model adequacy or the violations of the assumptions.

## Part c: One of the factors from this experiment does not seem to be important. If you drop this factor, 
## what type of design remains? Analyze the data using the full factorial model for only the four
## active factors. Compare your results with those obtained in part (a).

## The resulting experimental design is a replicated 2^4 full factorial design. The ANOVA is shown below. 
## The factor names in the output below were modified to match the factor names in the original problem. 
## The same factors are significant below as were significant in the original analysis.

model_Quality3<-lm(y~A+B+D+E+A*B+A*D+A*E+B*D+ B*E+D*E+ A*B*D+A*D*E+A*D*E+B*D*E+A*B*D*E,data=dat_Quality)
model_Quality4<-aov(y~A+B+D+E+A*B+A*D+A*E+B*D+ B*E+D*E+ A*B*D+A*D*E+A*D*E+B*D*E+A*B*D*E,data=dat_Quality)
coef(model_Quality3)
## (Intercept)           A           B           D           E         A:B 
##  10.1803125   1.6159375   0.0434375   2.9884375   2.1878125   1.2365625 
##         A:D         A:E         B:D         B:E         D:E       A:B:D 
##   1.6665625   1.0271875  -0.0134375   1.2834375   1.3896875  -0.3453125 
##       A:D:E       B:D:E       A:B:E     A:B:D:E 
##   0.9015625  -0.0396875   1.1853125   0.4071875
halfnormal(model_Quality3)
## 
## Significant effects (alpha=0.05, Lenth method):
##  [1] D     E     A:D   A     D:E   B:E   A:B   A:B:E A:E   A:D:E e10

plot(model_Quality3)

## hat values (leverages) are all = 0.5
##  and there are no factor predictors; no plot no. 5

summary(model_Quality4)
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## A            1  83.56   83.56  57.233 1.14e-06 ***
## B            1   0.06    0.06   0.041 0.841418    
## D            1 285.78  285.78 195.742 2.16e-10 ***
## E            1 153.17  153.17 104.910 1.97e-08 ***
## A:B          1  48.93   48.93  33.514 2.77e-05 ***
## A:D          1  88.88   88.88  60.875 7.66e-07 ***
## A:E          1  33.76   33.76  23.126 0.000193 ***
## B:D          1   0.01    0.01   0.004 0.950618    
## B:E          1  52.71   52.71  36.103 1.82e-05 ***
## D:E          1  61.80   61.80  42.328 7.24e-06 ***
## A:B:D        1   3.82    3.82   2.613 0.125501    
## A:D:E        1  26.01   26.01  17.815 0.000650 ***
## B:D:E        1   0.05    0.05   0.035 0.854935    
## A:B:E        1  44.96   44.96  30.794 4.40e-05 ***
## A:B:D:E      1   5.31    5.31   3.634 0.074735 .  
## Residuals   16  23.36    1.46                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(model_Quality3)
## 
## Call:
## lm.default(formula = y ~ A + B + D + E + A * B + A * D + A * 
##     E + B * D + B * E + D * E + A * B * D + A * D * E + A * D * 
##     E + B * D * E + A * B * D * E, data = dat_Quality)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.4750 -0.5637  0.0000  0.5637  1.4750 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 10.18031    0.21360  47.661  < 2e-16 ***
## A            1.61594    0.21360   7.565 1.14e-06 ***
## B            0.04344    0.21360   0.203 0.841418    
## D            2.98844    0.21360  13.991 2.16e-10 ***
## E            2.18781    0.21360  10.243 1.97e-08 ***
## A:B          1.23656    0.21360   5.789 2.77e-05 ***
## A:D          1.66656    0.21360   7.802 7.66e-07 ***
## A:E          1.02719    0.21360   4.809 0.000193 ***
## B:D         -0.01344    0.21360  -0.063 0.950618    
## B:E          1.28344    0.21360   6.009 1.82e-05 ***
## D:E          1.38969    0.21360   6.506 7.24e-06 ***
## A:B:D       -0.34531    0.21360  -1.617 0.125501    
## A:D:E        0.90156    0.21360   4.221 0.000650 ***
## B:D:E       -0.03969    0.21360  -0.186 0.854935    
## A:B:E        1.18531    0.21360   5.549 4.40e-05 ***
## A:B:D:E      0.40719    0.21360   1.906 0.074735 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.208 on 16 degrees of freedom
## Multiple R-squared:  0.9744, Adjusted R-squared:  0.9504 
## F-statistic: 40.58 on 15 and 16 DF,  p-value: 7.07e-10
## (d) Find the settings of the active factors that maximize the predicted response.

## Cube model can be constructed such that factors E is at +1 and C at 0 and response of other factors, A, B, and D are at +1.