#install.packages("tidyverse")
#install.packages("ggpubr")
#install.packages("sjPlot")
#install.packages("fBasics")
#install.packages("lmtest")
#install.packages("car")

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(ggpubr)
library(sjPlot)
## Registered S3 methods overwritten by 'lme4':
##   method                          from
##   cooks.distance.influence.merMod car 
##   influence.merMod                car 
##   dfbeta.influence.merMod         car 
##   dfbetas.influence.merMod        car
## #refugeeswelcome
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
## 
## 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(agricolae)
## 
## Attaching package: 'agricolae'
## The following objects are masked from 'package:timeDate':
## 
##     kurtosis, skewness
#import file excel
Oyster <- readxl::read_excel("soal 4 Ancova.xlsx")
head(Oyster,10)
## # A tibble: 10 x 4
##    Location Replication Initial_Weight Final_Weight
##       <dbl>       <dbl>          <dbl>        <dbl>
##  1        1           1           27.2         32.6
##  2        1           2           32           36.6
##  3        1           3           33           37.7
##  4        1           4           26.8         31  
##  5        2           1           28.6         33.8
##  6        2           2           26.8         31.7
##  7        2           3           26.5         30.7
##  8        2           4           26.8         30.4
##  9        3           1           28.6         35.2
## 10        3           2           22.4         29.1
#mengubah setiap faKtor dan ulangan ke dalam bentuk factor
Oyster <- Oyster %>% mutate_if(is.character,as.factor) %>% mutate(Location=as.factor(Location))
Oyster <- Oyster %>% mutate_if(is.character,as.factor) %>% mutate(Replication=as.factor(Replication))
head(Oyster,10)
## # A tibble: 10 x 4
##    Location Replication Initial_Weight Final_Weight
##    <fct>    <fct>                <dbl>        <dbl>
##  1 1        1                     27.2         32.6
##  2 1        2                     32           36.6
##  3 1        3                     33           37.7
##  4 1        4                     26.8         31  
##  5 2        1                     28.6         33.8
##  6 2        2                     26.8         31.7
##  7 2        3                     26.5         30.7
##  8 2        4                     26.8         30.4
##  9 3        1                     28.6         35.2
## 10 3        2                     22.4         29.1
# Memeriksa keliniearan
ggscatter(data = Oyster,x = "Initial_Weight",y="Final_Weight")

# Ancova
anova_Oyster <-aov(Final_Weight ~ Initial_Weight+Location,data=Oyster)
Anova(anova_Oyster,type="III")
## Anova Table (Type III tests)
## 
## Response: Final_Weight
##                 Sum Sq Df  F value    Pr(>F)    
## (Intercept)      0.733  1   2.4319 0.1412046    
## Initial_Weight 156.040  1 517.3840 1.867e-12 ***
## Location        12.089  4  10.0212 0.0004819 ***
## Residuals        4.222 14                       
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Menggunakan Grafik
plot_model(anova_Oyster,type = "diag")
## [[1]]

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

## 
## [[3]]

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

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

# Uji Normalitas
ksnormTest(res)
## 
## Title:
##  One-sample Kolmogorov-Smirnov test
## 
## Test Results:
##   STATISTIC:
##     D: 0.3022
##   P VALUE:
##     Alternative Two-Sided: 0.04071 
##     Alternative      Less: 0.02036 
##     Alternative   Greater: 0.1894 
## 
## Description:
##  Wed Feb 17 19:25:06 2021 by user: Reni_Amelia
print(adTest(res))
## 
## Title:
##  Anderson - Darling Normality Test
## 
## Test Results:
##   STATISTIC:
##     A: 1.1052
##   P VALUE:
##     0.005178 
## 
## Description:
##  Wed Feb 17 19:25:06 2021 by user: Reni_Amelia
shapiroTest(res)
## 
## Title:
##  Shapiro - Wilk Normality Test
## 
## Test Results:
##   STATISTIC:
##     W: 0.8907
##   P VALUE:
##     0.02774 
## 
## Description:
##  Wed Feb 17 19:25:06 2021 by user: Reni_Amelia
# Uji Homogenitas Ragam
bptest(Final_Weight ~ Initial_Weight+Location,data=Oyster,
       studentize = F)
## 
##  Breusch-Pagan test
## 
## data:  Final_Weight ~ Initial_Weight + Location
## BP = 1.7635, df = 5, p-value = 0.8808