size.anova10: Design of Experiments for ANOVA

Description Usage Arguments Details Value Note Author(s) References Examples

Description

This function provides access to several functions returning the optimal number of levels and / or observations in different types of One-Way, Two-Way and Three-Way ANOVA.

Usage

1
2
size.anova(model, hypothesis = "", assumption = "",
    a = NULL, b = NULL, c = NULL, n = NULL, alpha, beta, delta, cases)

Arguments

model

A character string describing the model, allowed characters are (>x) and the letters abcABC, capital letters stand for random factors, lower case letters for fixed factors, x means cross classification, > nested classification, brackets () are used to specify mixed model, the term in brackets has to come first. Spaces are allowed.

Examples: One-Way fixed: a, Two-Way: axB, a>b, Three-Way: axbxc, axBxC, a>b>c, (axb)>C, ...

hypothesis

Character string describiung Null hypothesis, can be omitted in most cases if it is clear that a test for no effects of factor A is performed, "a".

Other possibilities: "axb", "a>b", "c" and some more.

assumption

Character string. A few functions need an assumption on sigma, like "sigma_AB=0,b=c", see the referenced book until this page is updated.

a

Number of levels of fixed factor A

b

Number of levels of fixed factor B

c

Number of levels of fixed factor C

n

Number of Observations

alpha

Risk of 1st kind

beta

Risk of 2nd kind

delta

The minimum difference to be detected

cases

Specifies whether the "maximin" or "maximin" sizes are to be determined.

Details

see chapter 3 in the referenced book

Value

named integer giving the desired size(s)

Note

Depending on the selected model and hypothesis omit one or two of the sizes a, b, c, n. The function then tries to get its optimal value.

Author(s)

Dieter Rasch, Juergen Pilz, L.R. Verdooren, Albrecht Gebhardt, Minghui Wang

References

Dieter Rasch, Juergen Pilz, L.R. Verdooren, Albrecht Gebhardt: Optimal Experimental Design with R, Chapman and Hall/CRC, 2011

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
size.anova(model="a",a=4,
      alpha=0.05,beta=0.1, delta=2, case="maximin")
size.anova(model="a",a=4,
      alpha=0.05,beta=0.1, delta=2, case="minimin")

size.anova(model="axb", hypothesis="a", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="maximin")
size.anova(model="axb", hypothesis="a", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="maximin")

size.anova(model="axb", hypothesis="axb", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="minimin")
size.anova(model="axb", hypothesis="axb", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="minimin")

size.anova(model="axBxC",hypothesis="a",
           assumption="sigma_AC=0,b=c",a=6,n=2,
           alpha=0.05, beta=0.1, delta=0.5, cases="maximin")
size.anova(model="axBxC",hypothesis="a",
           assumption="sigma_AC=0,b=c",a=6,n=2,
           alpha=0.05, beta=0.1, delta=0.5, cases="minimin")

size.anova(model="a>B>c", hypothesis="c",a=6, b=2, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")
size.anova(model="a>B>c", hypothesis="c",a=6, b=20, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")

size.anova(model="a>B>c", hypothesis="c",a=6, b=NA, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")

size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")
size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="minimin")

size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")
size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="minimin")





























## Not run: 









###########10#
Your task is to optimize a minimum quantity lubrication (MQL) process on a turning machine with\
respect to the roughness (ra). You have four factors that you want to take into account: cutting\
speed (c), feed rate (f), depth of cut (d) and the nozzle diameter for the MQL.\
The current operation parameters are c=200 m/min, f= 0.15 mm/rev, d=0.8 mm, n = 4 mm\

```{r, echo=FALSE}
tabl <- " 
|        Parameter        | min. val.| max. val.|
|-------------------------|:--------:|:--------:|
| Cutting speed: c (m/min)|    50    |    450   |
| Feed rate: f (mm/rev)   |   0.01   |    0.29  |
| Depth of cut: d (mm)    |    0.1   |    1.7   |
| Nozzle diameter: n (mm) |     2    |    6     |
"
cat(tabl) # output the table in a format good for HTML/PDF/docx conversion
```
You will be asked to write down several plans in the following. These plans have all the same\
structure. The file starts with the Matrikelnumber. This row is followed by a row with the column\
names: c, f, d, n. In the next rows follows the plan with its levels. There are always 4 columns.\


## 10.1 - Screening Model without interactions 
Set up a first screening plan around the current parameter, to get an estimate about the machine\
performance. For this choose a resolution that assumes that there are no interactions and take\
the level range to be below 30proz of the full range. (Because you know that nonlinear behaviour\
could occur.) And evaluate the results of the screening with an appropriate model. (Do not\
validate model assumptions.)\

## 10.2 - Screening Model with two-fold-interactions  
After having this first overview set up a second plan with an appropriate resolution, (which?),
that considers also two, fold interactions. (But, is still linear.) And evaluate the results of the
screening with an appropriate model. (1 Model, 3 Plots) (Do not validate model assumptions.)

## 10.3 - Now set of a sequence of plans and try to find the optimum performance (smallest Ra).













size.anova(model="a",a=4,
      alpha=0.05,beta=0.1, delta=2, case="maximin")
size.anova(model="a",a=4,
      alpha=0.05,beta=0.1, delta=2, case="minimin")

size.anova(model="axb", hypothesis="a", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="maximin")
size.anova(model="axb", hypothesis="a", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="maximin")

size.anova(model="axb", hypothesis="axb", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="minimin")
size.anova(model="axb", hypothesis="axb", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="minimin")

size.anova(model="axBxC",hypothesis="a",
           assumption="sigma_AC=0,b=c",a=6,n=2,
           alpha=0.05, beta=0.1, delta=0.5, cases="maximin")
size.anova(model="axBxC",hypothesis="a",
           assumption="sigma_AC=0,b=c",a=6,n=2,
           alpha=0.05, beta=0.1, delta=0.5, cases="minimin")

size.anova(model="a>B>c", hypothesis="c",a=6, b=2, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")
size.anova(model="a>B>c", hypothesis="c",a=6, b=20, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")

size.anova(model="a>B>c", hypothesis="c",a=6, b=NA, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")

size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")
size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="minimin")

size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")
size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="minimin")





## 10.1 - Screening Model without interactions 
Set up a first screening plan around the current parameter, to get an estimate about the machine\
performance. For this choose a resolution that assumes that there are no interactions and take\
the level range to be below 30proz of the full range. (Because you know that nonlinear behaviour\
could occur.) And evaluate the results of the screening with an appropriate model. (Do not\
validate model assumptions.)\

### decide necessary number of experiments
resolution=3 as interactions are not of importance
```{r warning=F, message=F, error = T}
nex=nrow(FrF2(resolution = 3, nfactors=4,clear=TRUE, res3=TRUE))
nex
```
The number of experiments is `r nex`.

### Generate test plan
Generate a fractional factorial plan.
set factors for screening so that level range is below 30proz of the full range -> min. level= 0.8*current optimum, max.level=1.2*current optimum
```{r warning=F, message=F, error = T}
nr=1            # number of replications
plan1=FrF2(nruns=nex,nfactors=4,replication=nr,factor.names=list(c=c(200*.8,200*1.2),f=c(0.15*.8,0.15*1.2),d=c(0.8*.8,0.8*1.2),n=c(0.8*4,1.2*4)),randomize = T)
plan1
```
Show the aliasing
```{r}
aliasprint(plan1)
```
two-fold interactions are confounded as expected.

### Write test plan to file
```{r warning=F, message=F, error = T}
# First line writes the Matrikelnummer
write.table(matrikelnumber,file="plan1.csv",sep = ";", dec = ".",row.names = F, col.names = F, append = F)
# Second line writes the test plan
write.table(plan1,file="plan1.csv",sep = ";", dec = ".",row.names = F, col.names = T, append = T)
```

### Read data
Read data and have a first glance at them. (1st plot)
```{r warning=F, message=F, error = T}
dat1 = read.delim("plan1_res.csv", header = TRUE,sep = ";", dec = ".")
head(dat1)
```

#### first glance at the data
##### Check class of variables
```{r}
map_df(dat1, class) proz>proz kbl(.,"html") proz>proz   kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))
```

##### check for NA values
```{r}
dat1 proz>proz  summarise("number of NA"= sum(is.na(.)))proz>proz datatable(.,class = 'cell-border stripe')
```

##### Scatter Plot
```{r fig.height=9}
dat1 proz>proz ggplot(data=.,mapping = aes(x=time,y=ra)) +  geom_point() + ggtitle("Scatterplot")
```
The data show no suspicious elements in the scatterplot. There might be time drift.

H~0~ Distribution is stationary\
The significance level is set to  $\alpha = 5\proz$
```{r warning=F}
summary_kpss <- dat1 proz>proz  summarise(kpss_p=kpss.test(ra)$p.value)
summary_kpss proz>proz mutate_if(is.numeric, format, digits=2,nsmall = 1) proz>proz
  kbl(.,"html",align = "r",caption = "KPSS Test Results ") proz>proz   kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) 
```

H~0~ cannot be rejected because ($p>\alpha$ ).\
Data seem to be stationary.
Eliminate time as variable since no time drift was seen in KPSS-Test, so the reduced model does not require the time.
```{r}
dat1<-dat1[-1]
```
##### Visualize data
```{r message=F}
ggpairs(dat1,lower = list(continuous = wrap("smooth", alpha = 0.3, size=0.1)))
```
ra seems to have the most relevant correlation with c followed by f. The factors don't seem to correlate with each other.

### Set up a linear model for screening
H~01~: c has no effect\
H~02~: f has no effect\
H~03~: d has no effect\
H~04~: n has no effect\
significance level  $\alpha=0.05$\

```{r warning=F, message=F, error = T}
mod1=lm(ra~.,data=dat1) # linear model
summary(mod1)
```
c and f seem to be significant.\

check if a reduced model is appropriate with BIC.
```{r}
modBIC1<-stepAIC(mod1,k=log(nrow(dat1)),direction = "both",trace = 0)
summary(modBIC1)
```
BIC removes factor n, as it doesn't seem to be significant for the model.

#### check if factor d is significant, or the model can be reduced further
H~01~: c has no effect\
H~02~: f has no effect\
significance level  $\alpha=0.05$\

```{r warning=F, message=F, error = T}
mod1r=lm(ra~c+f,data=dat1) # linear model
summary(mod1r)
```
$p<\alpha$ of H~01~ and H~02~ cannot be rejected.\

##### compare models with anova
H~0~ models are equally fine.
significance level  $\alpha=0.05$\
```{r warning=F, message=F, error = T}
anova(modBIC1,mod1r)
```
$\because p<\alpha \Rightarrow$, I can't reject H~0~.\
The models seem to be equally fine. But we keep model with factor d, as adj.r.squared is higher than reduced model -> final model for first screening is modBIC1.

#### visualize effects
```{r warning=F, message=F, error = T}
MEPlot(mod1)
```











size.anova(model="a",a=4,
      alpha=0.05,beta=0.1, delta=2, case="maximin")
size.anova(model="a",a=4,
      alpha=0.05,beta=0.1, delta=2, case="minimin")

size.anova(model="axb", hypothesis="a", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="maximin")
size.anova(model="axb", hypothesis="a", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="maximin")

size.anova(model="axb", hypothesis="axb", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="minimin")
size.anova(model="axb", hypothesis="axb", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="minimin")

size.anova(model="axBxC",hypothesis="a",
           assumption="sigma_AC=0,b=c",a=6,n=2,
           alpha=0.05, beta=0.1, delta=0.5, cases="maximin")
size.anova(model="axBxC",hypothesis="a",
           assumption="sigma_AC=0,b=c",a=6,n=2,
           alpha=0.05, beta=0.1, delta=0.5, cases="minimin")

size.anova(model="a>B>c", hypothesis="c",a=6, b=2, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")
size.anova(model="a>B>c", hypothesis="c",a=6, b=20, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")

size.anova(model="a>B>c", hypothesis="c",a=6, b=NA, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")

size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")
size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="minimin")

size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")
size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="minimin")




## 10.2 - Screening Model with two-fold-interactions  
After having this first overview set up a second plan with an appropriate resolution, (which?),
that considers also two, fold interactions. (But, is still linear.) And evaluate the results of the
screening with an appropriate model. (1 Model, 3 Plots) (Do not validate model assumptions.)

### determine necessary number of experiments for model with two-fold-interaction but linear model (2 levels suffice)
```{r warning=F, message=F, error = T}
nex2=nrow(FrF2(resolution = 5, nfactors=4,clear=TRUE, res3=TRUE))
nex2
```
The number of experiments is `r nex2`.

### Generate Plan
Generate a fractional factorial plan.
```{r warning=F, message=F, error = T}
nr=1            # number of replications
plan2=FrF2(nruns=nex2,nfactors=4,replication=nr,factor.names=list(c=c(200*.8,200*1.2),f=c(0.15*.8,0.15*1.2),d=c(0.8*.8,0.8*1.2),n=c(0.8*4,1.2*4)),randomize = T)
```

Show the aliasing
```{r}
aliasprint(plan2)
plan2=as.data.frame(plan2) 
plan2=plan2[-5] # there is a last block column that has to be eliminated
plan2
```
There doesn't seem to be any aliasing.

### Write test plan to file
```{r warning=F, message=F, error = T}
# First line writes the Matrikelnummer
write.table(matrikelnumber,file="plan2.csv",sep = ";", dec = ".",row.names = F, col.names = F, append = F)
# Second line writes the test plan
write.table(plan2,file="plan2.csv",sep = ";", dec = ".",row.names = F, col.names = T, append = T)
```

### Read data
Read data and have a first glance at them. (1st plot)
```{r warning=F, message=F, error = T}
dat2 = read.delim("plan2_res.csv", header = TRUE,sep = ";", dec = ".")
datatable(dat2)
```

#### first glance at the data
##### Check class of variables
```{r}
map_df(dat2, class) proz>proz kbl(.,"html") proz>proz   kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))
```

##### check if there are NA values
```{r}
dat2 proz>proz  summarise("number of NA"= sum(is.na(.)))proz>proz datatable(.,class = 'cell-border stripe')
```

##### Scatter Plot
```{r fig.height=9}
dat2 proz>proz ggplot(data=.,mapping = aes(x=time,y=ra)) +  geom_point() + ggtitle("Scatterplot")
```
The data show no suspicious elements in the scatterplot. There might be slight time drift.

##### KPSs-Test
H~0~ Distribution is stationary\
The significance level is set to  $\alpha = 5\proz$

```{r warning=F}
summary_kpss <- dat2 proz>proz  summarise(kpss_p=kpss.test(ra)$p.value)
summary_kpss proz>proz mutate_if(is.numeric, format, digits=2,nsmall = 1) proz>proz
  kbl(.,"html",align = "r",caption = "KPSS Test Results ") proz>proz   kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) 
```
H~0~ cannot be rejected because ($p>\alpha$ ).\
Data seems to be stationary.

Eliminate time as variable since no time drift was seen initially, so the reduced model does not require the time
```{r}
dat2<-dat2[-1]
```

##### Visualize data
```{r message=F}
ggpairs(dat2,lower = list(continuous = wrap("smooth", alpha = 0.3, size=0.1)))
```
c seems to be relevant, f seems relevant but less so. d and n could be irrelevant.

### Set up a linear model
H~01~: c has no effect\
H~02~: f has no effect\
H~03~: d has no effect\
H~04~: n has no effect\
H~05~: cf has no effect\
H~06~: cd has no effect\
H~07~: cn has no effect\
H~08~: fd has no effect\
H~09~: fn has no effect\
H~010~: dn has no effect\
significance level  $\alpha=0.05$\

```{r warning=F, message=F, error = T}
mod2=lm(ra~.^2,data=dat2) # linear model
summary(mod2)
```
c, f, and interaction c:d seem to be most significant. But all p-values are > alpha -> all initial null-hypothesis can't be rejected.

#### visualize main effects
```{r warning=F, message=F, error = T}
MEPlot(mod2)
```
c and f seem to be most significant.

#### visualize interactions
```{r warning=F, message=F, error = T}
IAPlot(mod2)
```
all factors seem to have at least slight interactions, except interaction f:n.

#### pareto plot
```{r}
library(pid)
paretoPlot(mod2, xlab="Effect name", ylab="Magnitude of effect")
```











size.anova(model="a",a=4,
      alpha=0.05,beta=0.1, delta=2, case="maximin")
size.anova(model="a",a=4,
      alpha=0.05,beta=0.1, delta=2, case="minimin")

size.anova(model="axb", hypothesis="a", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="maximin")
size.anova(model="axb", hypothesis="a", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="maximin")

size.anova(model="axb", hypothesis="axb", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="minimin")
size.anova(model="axb", hypothesis="axb", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="minimin")

size.anova(model="axBxC",hypothesis="a",
           assumption="sigma_AC=0,b=c",a=6,n=2,
           alpha=0.05, beta=0.1, delta=0.5, cases="maximin")
size.anova(model="axBxC",hypothesis="a",
           assumption="sigma_AC=0,b=c",a=6,n=2,
           alpha=0.05, beta=0.1, delta=0.5, cases="minimin")

size.anova(model="a>B>c", hypothesis="c",a=6, b=2, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")
size.anova(model="a>B>c", hypothesis="c",a=6, b=20, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")

size.anova(model="a>B>c", hypothesis="c",a=6, b=NA, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")

size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")
size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="minimin")

size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")
size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="minimin")




## 10.3 - Now set of a sequence of plans and try to find the optimum performance (smallest Ra).
Generate fractional factorial plan with more replications for optimization
```{r warning=F, message=F, error = T}
nr=3            # number of replications
plan2a=FrF2(nruns=nex2,nfactors=4,replication=nr,factor.names=list(c=c(200*.8,200*1.2),f=c(0.15*.8,0.15*1.2),d=c(0.8*.8,0.8*1.2),n=c(0.8*4,1.2*5)),randomize = T)
```
Show the aliasing
```{r}
aliasprint(plan2a)
plan2a=as.data.frame(plan2a)
plan2a=plan2a[-5]  # there is a last block column that has to be eliminated
```
There doesn't seem to be any aliasing.

### Write test plan to file
```{r warning=F, message=F, error = T}
# First line writes the Matrikelnummer
write.table(matrikelnumber,file="plan2a.csv",sep = ";", dec = ".",row.names = F, col.names = F, append = F)
# Second line writes the test plan
write.table(plan2a,file="plan2a.csv",sep = ";", dec = ".",row.names = F, col.names = T, append = T)
```
### Read data
Read data and have a first glance at them. (1st plot)
```{r warning=F, message=F, error = T}
dat2a = read.delim("plan2a_res.csv", header = TRUE,sep = ";", dec = ".")
head(dat2)
```
#### Have first glance the data
##### Check class of variables
```{r}
map_df(dat2a, class) proz>proz kbl(.,"html") proz>proz   kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"))
```
##### check if there is an NA for any machine
```{r}
dat2a proz>proz  summarise("number of NA"= sum(is.na(.)))proz>proz datatable(.,class = 'cell-border stripe')
```
##### Scatter Plot
```{r fig.height=9}
dat2a proz>proz ggplot(data=.,mapping = aes(x=time,y=ra)) +  geom_point() + ggtitle("Scatterplot")
```
The data show no suspicious elements in the scatterplot. There doesn't seem to be any time drift..

H~0~ Distribution is stationary\
The significance level is set to  $\alpha = 5\proz$
```{r warning=F}
summary_kpss <- dat2a proz>proz  summarise(kpss_p=kpss.test(ra)$p.value)
summary_kpss proz>proz mutate_if(is.numeric, format, digits=2,nsmall = 1) proz>proz
  kbl(.,"html",align = "r",caption = "KPSS Test Results ") proz>proz   kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) 
```
H~0~ cannot be rejected because ($p>\alpha$ ).\
Data seems to be stationary.

Eliminate time as variable since no time drift was seen initially, so the reduced model does not require the time
```{r}
dat2a<-dat2a[-1]
```
##### Visualize data
```{r message=F}
ggpairs(dat2a,lower = list(continuous = wrap("smooth", alpha = 0.3, size=0.1)))
```
c and f seem to be relevant. But d and n could also be relevant.

### Set up a linear model
H~01~: c has no effect\
H~02~: f has no effect\
H~03~: d has no effect\
H~04~: n has no effect\
H~05~: cf has no effect\
H~06~: cd has no effect\
H~07~: cn has no effect\
H~08~: fd has no effect\
H~09~: fn has no effect\
H~010~: dn has no effect\
H~011~: cfd has no effect\
H~012~: cfn has no effect\
H~013~: cdf has no effect\
H~014~: fdn has no effect\
H~015~: cfdn has no effect\
significance level  $\alpha=0.05$\

```{r warning=F, message=F, error = T}
mod2a=lm(ra~.^2,data=dat2a) # linear model
summary(mod2a)
```
$p>\alpha \forall H_0$ except H~01~ therefore no factor seems to make a significant effect except factor c.\

#### visualize effects
```{r warning=F, message=F, error = T}
MEPlot(mod2a)
```
all effects seem to have a main effect on ra.

#### visualize interactions
```{r warning=F, message=F, error = T}
IAPlot(mod2a)
```
all factors seem to have at least slight interactions.

check if a reduced model is appropriate with BIC.
```{r}
mod2ar <- stepAIC(mod2a,k=log(nrow(dat2a)),direction = "both",trace = 0)
summary(mod2ar)
```
reduced model from BIC removes all interactions except d:n

### verify performance of reduced model
 H~0~ both models have the same quality.\
significance level  $\alpha=0.05$\

```{r}
anova(mod2a,mod2ar)
```
$p>\alpha \therefore H_0$ cannot be rejected.\
Both models seem to perform equally

## Find Optimum with rsm 
### First run
#### Generate coded data set
```{r warning=F, message=F, error = T}
do1<-dat2a
opt_x1 <- 200 #current opt param. stated in exercise of factor 1
opt_x2 <- 0.15 #current opt param. stated in exercise of factor 2
opt_x3 <- 0.8 #current opt param. stated in exercise of factor 3
opt_x4 <- 4 #current opt param. stated in exercise of factor 4
co1=list(x1 ~ (c - opt_x1)/(opt_x1*(1-0.8)), x2 ~ (f - opt_x2)/(opt_x2*(1-0.8)), x3 ~ (d - opt_x3)/(opt_x3*(1-0.8)), x4 ~ (n -opt_x4)/(opt_x4*(1-0.8))) #(orig - center) / divisor
do1c <- coded.data(do1, formulas = co1)
do1c
```
####  Set up a response surface model
```{r warning=F, message=F, error = T}
mo1=rsm(ra~FO(x1,x2,x3,x4)+TWI(x1,x2,x3,x4),data=do1c) # linear model
summary(mo1)
```
#### Plotting results
```{r warning=F, message=F, error = T}
par(mfrow = c(2,3))
contour(mo1, ~ x1 + x2 + x3 + x4,image = TRUE)
```
#### Get the direction of steepest descent and create test plan with points in distance steps of 0.5
```{r warning=F, message=F, error = T}
plans1=steepest(mo1,dist=seq(0,4,.5),descent = T) #descent=T for minimum search
plans1=plans1[-(1:6)] #remove unnecessary columns (keep only c,f,d,n columns)
plans1=plans1[-(5:6)]
nr=3
plans1=do.call("rbind", replicate(nr, plans1, simplify=F))
plans2=steepest(mo1,dist=seq(0,4,.5),descent = T) #descent=T for minimum search
plans2
```
#### Write test plan to file
```{r warning=F, message=F, error = T}
# First line writes the Matrikelnummer
write.table(matrikelnumber,file="plano1.csv",sep = ";", dec = ".",row.names = F, col.names = F, append = F)
# Second line writes the test plan
write.table(plans1,file="plano1.csv",sep = ";", dec = ".",row.names = F, col.names = T, append = T)
```
#### Read data from test plan with steepest descent
```{r warning=F, message=F, error = T}
dos1 = read.delim("plano1_res.csv", header = TRUE,sep = ";", dec = ".")
head(dos1)
```
#### compare steepest descent model with data to see where they deviate
```{r warning=F, message=F, error = T, fig.width=12}
dos1t=dos1[-1] # first remove time column
dos1v=cbind(rep("measured",nrow(dos1t)),dos1t) # add a name column to measurements
colnames(dos1v)<-c("data","c","f","d","n","ra")

plans1=steepest(mo1,dist=seq(0,4,.5),descent = T)
plans1=plans1[-(1:6)] # extract from the model, what is of interest
plans1=plans1[-(5)]   # extract from the model, what is of interest
plans1=cbind(rep("model",nrow(plans1)),plans1) # add a name column to steepest decent model
colnames(plans1)<-c("data","c","f","d","n","ra")
dos1v=rbind(dos1v,plans1) # combine model and measurement to one data set
ggline(data=dos1v, x = "c",y="ra", ylab = "ra",  add = "mean_sd", color = "data", palette = "jco")
```
The steepest decent and the data start to deviate at about c=276

##### new starting point
```{r warning=F, message=F, error = T}
newstart1=plans1[,-1][6,] #remove first column (data) with -1 and extract 6th row with c=~276 as new startpoint
newstart1=plans1[,-1][6,]
newstart1
```

### Second run
#### set up a central-composite design around new start point
```{r warning=F, message=F, error = T}
co2=list(x1 ~ (c - newstart1$c)/70, x2 ~ (f - newstart1$f)/0.07, x3 ~ (d-newstart1$d)/0.16, x4 ~ (n-newstart1$n)/.8) #(orig - center) / divisor, erstellt ccd um startpunkt z.b. für c: max(c)=startpunkt+70, min(c)=startpunkt-70
plan2=decode.data(ccd(4, n0 = 1,alpha = "rotatable", inscribed = TRUE, coding = co2)) #n0=1=1 centerpoint
plan2=plan2[-(1:2)]
plan2=plan2[-5]
nr=3
plan2=do.call("rbind", replicate(nr, plan2, simplify=F))
plan2=plan2[order(sample(1:nrow(plan2))),] # randomize design
```

#### Write test plan to file
```{r warning=F, message=F, error = T}
# First line writes the Matrikelnummer
write.table(matrikelnumber,file="plano2.csv",sep = ";", dec = ".",row.names = F, col.names = F, append = F)
# Second line writes the test plan
write.table(plan2,file="plano2.csv",sep = ";", dec = ".",row.names = F, col.names = T, append = T)
```

#### Read data
Read data and have a first glance at them. (1st plot)
```{r warning=F, message=F, error = T}
do2 = read.delim("plano2_res.csv", header = TRUE,sep = ";", dec = ".")
head(do2)
do2=do2[-1]
```

#### Generated coded data set
```{r warning=F, message=F, error = T}
do2c <- coded.data(do2, formulas = co2)
```

####  Set up a response surface model
```{r warning=F, message=F, error = T}
mo2=rsm(ra~SO(x1,x2,x3,x4),data=do2c) # square model
summary(mo2)
```
#### Plotting results
```{r warning=F, message=F, error = T}
par(mfrow = c(2,3))
contour(mo2, ~ x1 + x2 + x3 + x4,image = TRUE)
```
##### Get the direction of steepest descent with smaller distance steps 0.3
```{r warning=F, message=F, error = T}
plans2=steepest(mo2,dist=seq(0,2.1,.3),descent = T)
plans2=plans2[-(1:6)]
plans2=plans2[-(5:6)]
nr=3
plans2=do.call("rbind", replicate(nr, plans2, simplify=F))
```

#### Write test plan to file
```{r warning=F, message=F, error = T}
# First line writes the Matrikelnummer
write.table(matrikelnumber,file="plano3.csv",sep = ";", dec = ".",row.names = F, col.names = F, append = F)
# Second line writes the test plan
write.table(plans2,file="plano3.csv",sep = ";", dec = ".",row.names = F, col.names = T, append = T)
```
#### Read data
Read data and have a first glance at them. (1st plot)
```{r warning=F, message=F, error = T}
dos2 = read.delim("plano3_res.csv", header = TRUE,sep = ";", dec = ".")
head(dos2)
```

#### compare model with data
```{r warning=F, message=F, error = T, fig.width=15,fig.height=6}
dos2t=dos2[-1] # first remove time column
dos2v=cbind(rep("measured",nrow(dos2t)),dos2t) # add a name column to measurements
colnames(dos2v)<-c("data","c","f","d","n","ra")
plans2=steepest(mo2,dist=seq(0,2.1,.3),descent = T)
plans2=plans2[-(1:6)] # extract from the model, what is of interest
plans2=plans2[-(5)]   # extract from the model, what is of interest
plans2=cbind(rep("model",nrow(plans2)),plans2) # add a name column to steepest decent model
colnames(plans2)<-c("data","c","f","d","n","ra")
dos2v=rbind(dos2v,plans2) # combine model and measurement to one data set
ggline(data=dos2v, x = "c",y="ra", ylab = "ra",  add = "mean_sd", color = "data", palette = "jco")
```
The smallest value is reached for 283.38.

##### new starting point
```{r warning=F, message=F, error = T}
newstart2=plans2[,-1][8,]
rbind(newstart1,newstart2)
```
f seems to reach its minimum value

#### Third run
##### set up a central-composite designs 
```{r warning=F, message=F, error = T}
co3=list(x1 ~ (c - newstart2$c)/70, x2 ~ (f - newstart2$f)/0.01, x3 ~ (d-newstart2$d)/0.16,x4~(n-newstart2$n)/.4)
plan3=decode.data(ccd(4, n0 = 1,alpha = "rotatable", inscribed = TRUE, coding = co3))
plan3=plan3[-(1:2)]
plan3=plan3[-5]
nr=3
plan3=do.call("rbind", replicate(nr, plan3, simplify=F))
```

##### Write test plan to file
```{r warning=F, message=F, error = T}
# First line writes the Matrikelnummer
write.table(matrikelnumber,file="plano4.csv",sep = ";", dec = ".",row.names = F, col.names = F, append = F)
# Second line writes the test plan
write.table(plang3,file="plano4.csv",sep = ";", dec = ".",row.names = F, col.names = T, append = T)
```

##### Read data
Read data and have a first glance at them. (1st plot)
```{r warning=F, message=F, error = T}
do3 = read.delim("plano4_res.csv", header = TRUE,sep = ";", dec = ".")
head(do3)
do3=do3[-1]
```

##### Generate coded data set
```{r warning=F, message=F, error = T}
do3c <- coded.data(do3, formulas = co3)
```

#####  Set up a response surface model
```{r warning=F, message=F, error = T}
mo3=rsm(ra~SO(x1,x2,x3,x4),data=do3c) # linear model
summary(mo3)
```

##### Plotting results
```{r warning=F, message=F, error = T}
par(mfrow = c(2,3))
contour(mo3, ~ x1 + x2 + x3 + x4 ,image = TRUE)
```






















size.anova(model="a",a=4,
      alpha=0.05,beta=0.1, delta=2, case="maximin")
size.anova(model="a",a=4,
      alpha=0.05,beta=0.1, delta=2, case="minimin")

size.anova(model="axb", hypothesis="a", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="maximin")
size.anova(model="axb", hypothesis="a", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="maximin")

size.anova(model="axb", hypothesis="axb", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="minimin")
size.anova(model="axb", hypothesis="axb", a=6, b=4, 
           alpha=0.05,beta=0.1, delta=1, cases="minimin")

size.anova(model="axBxC",hypothesis="a",
           assumption="sigma_AC=0,b=c",a=6,n=2,
           alpha=0.05, beta=0.1, delta=0.5, cases="maximin")
size.anova(model="axBxC",hypothesis="a",
           assumption="sigma_AC=0,b=c",a=6,n=2,
           alpha=0.05, beta=0.1, delta=0.5, cases="minimin")

size.anova(model="a>B>c", hypothesis="c",a=6, b=2, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")
size.anova(model="a>B>c", hypothesis="c",a=6, b=20, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")

size.anova(model="a>B>c", hypothesis="c",a=6, b=NA, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")

size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")
size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="minimin")

size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="maximin")
size.anova(model="(axb)>c", hypothesis="a",a=6, b=5, c=4, 
           alpha=0.05, beta=0.1, delta=0.5, case="minimin")







## End(Not run)

maccaveli/e1072 documentation built on Feb. 13, 2022, 10:41 p.m.