#Packages

#install.packages("tidyverse")
#install.packages("ggpubr")
#install.packages("readxl")
#install.packages("sjPlot")
#install.packages("fBasics")
#install.packages("lmtest")
#install.packages("zoo")
#install.packages("sciplot")
#install.packages("lsr")
#install.packages("gplots")

##memanggil package
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.4     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(sjPlot)
library(fBasics)
## Loading required package: timeDate
## Loading required package: timeSeries
library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following object is masked from 'package:timeSeries':
## 
##     time<-
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(car)
## Loading required package: carData
## Registered S3 methods overwritten by 'car':
##   method                          from
##   influence.merMod                lme4
##   cooks.distance.influence.merMod lme4
##   dfbeta.influence.merMod         lme4
##   dfbetas.influence.merMod        lme4
## 
## Attaching package: 'car'
## The following object is masked from 'package:fBasics':
## 
##     densityPlot
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following object is masked from 'package:purrr':
## 
##     some
library(readxl)
library(ggpubr)
library(MASS)
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
## 
##     select
library(sjmisc)
## Install package "strengejacke" from GitHub (`devtools::install_github("strengejacke/strengejacke")`) to load all sj-packages at once!
## 
## Attaching package: 'sjmisc'
## The following object is masked from 'package:purrr':
## 
##     is_empty
## The following object is masked from 'package:tidyr':
## 
##     replace_na
## The following object is masked from 'package:tibble':
## 
##     add_case
library(sciplot)
library(lsr)
library(gplots)
## 
## Attaching package: 'gplots'
## The following object is masked from 'package:stats':
## 
##     lowess
#Import Dataset
#Data yang kita miliki memiliki format .xlsx . Untuk megimport data tersebut, kita dapat
#menggunakan fungsi read_excel dari library readxl .
metal = read_excel("soal 1 faktorial ral.xlsx")

head(metal)
## # A tibble: 6 x 4
##   FeedRate DepthOfCut SurfaceFinish Ulangan
##      <dbl>      <dbl>         <dbl>   <dbl>
## 1      0.2       0.15            74       1
## 2      0.2       0.15            64       2
## 3      0.2       0.15            60       3
## 4      0.2       0.18            79       1
## 5      0.2       0.18            68       2
## 6      0.2       0.18            73       3
str(metal)
## tibble [36 x 4] (S3: tbl_df/tbl/data.frame)
##  $ FeedRate     : num [1:36] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 ...
##  $ DepthOfCut   : num [1:36] 0.15 0.15 0.15 0.18 0.18 0.18 0.2 0.2 0.2 0.25 ...
##  $ SurfaceFinish: num [1:36] 74 64 60 79 68 73 82 88 92 99 ...
##  $ Ulangan      : num [1:36] 1 2 3 1 2 3 1 2 3 1 ...
#Mengubah kolom faktor ke bentuk factor

metal$DepthOfCut<- as.factor(metal$DepthOfCut)
metal$FeedRate<- as.factor(metal$FeedRate)

#Menampilkan hasil ANOVA

anova_bat <- aov(SurfaceFinish~FeedRate+DepthOfCut+FeedRate:DepthOfCut,data=metal)
summary(anova_bat)
##                     Df Sum Sq Mean Sq F value   Pr(>F)    
## FeedRate             2 3160.5  1580.2  55.018 1.09e-09 ***
## DepthOfCut           3 2125.1   708.4  24.663 1.65e-07 ***
## FeedRate:DepthOfCut  6  557.1    92.8   3.232    0.018 *  
## Residuals           24  689.3    28.7                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Catatan: 1. FeedRate:DepthOfCut menandakan interaksi antara Feed Rate dan Depth Of Cut

#Plot Interaksi

lineplot.CI(x.factor=metal$FeedRate,response=metal$SurfaceFinish,group=metal$DepthOfCut,ci.fun=ciMean,xlab="FeedRate",ylab="SurfaceFinish",legend=TRUE, x.leg="bottomright")

#Uji Asumsi ANOVA
#Menggunakan Grafik
plot_model(anova_bat,type = "diag")
## [[1]]

## 
## [[2]]
## `geom_smooth()` using formula 'y ~ x'

## 
## [[3]]

## 
## [[4]]
## `geom_smooth()` using formula 'y ~ x'

res <- residuals(anova_bat)
res_order <- data.frame(order=seq_along(res),
                        residual=res
)
plot_scatter(res_order,x = order,y=residual)+geom_hline(yintercept = 0)

res <- residuals(anova_bat)
ksnormTest(res)
## 
## Title:
##  One-sample Kolmogorov-Smirnov test
## 
## Test Results:
##   STATISTIC:
##     D: 0.3513
##   P VALUE:
##     Alternative Two-Sided: 0.0001774 
##     Alternative      Less: 8.87e-05 
##     Alternative   Greater: 0.0001526 
## 
## Description:
##  Wed Feb 17 19:39:41 2021 by user: Reni_Amelia
print(adTest(res))
## 
## Title:
##  Anderson - Darling Normality Test
## 
## Test Results:
##   STATISTIC:
##     A: 0.3259
##   P VALUE:
##     0.5097 
## 
## Description:
##  Wed Feb 17 19:39:41 2021 by user: Reni_Amelia
shapiroTest(res)
## 
## Title:
##  Shapiro - Wilk Normality Test
## 
## Test Results:
##   STATISTIC:
##     W: 0.9705
##   P VALUE:
##     0.4397 
## 
## Description:
##  Wed Feb 17 19:39:41 2021 by user: Reni_Amelia
bptest(SurfaceFinish~FeedRate+DepthOfCut+FeedRate:DepthOfCut,data=metal,studentize = F)
## 
##  Breusch-Pagan test
## 
## data:  SurfaceFinish ~ FeedRate + DepthOfCut + FeedRate:DepthOfCut
## BP = 6.6135, df = 11, p-value = 0.8295
leveneTest(SurfaceFinish~FeedRate*DepthOfCut*FeedRate:DepthOfCut,data=metal)
## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value Pr(>F)
## group 11  0.3209 0.9733
##       24