library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.5.2

1. MEMBACA DATA & RESTRUKTURISASI

data <- read.csv("C:/Users/ASUS/OneDrive/Dokumen/rbsl/cropdata_updated.csv")
head (data)
##   crop.ID  soil_type Seedling.Stage MOI temp humidity result
## 1   Wheat Black Soil    Germination   1   25       80      1
## 2   Wheat Black Soil    Germination   2   26       77      1
## 3   Wheat Black Soil    Germination   3   27       74      1
## 4   Wheat Black Soil    Germination   4   28       71      1
## 5   Wheat Black Soil    Germination   5   29       68      1
## 6   Wheat Black Soil    Germination   6   30       65      1
colnames(data)
## [1] "crop.ID"        "soil_type"      "Seedling.Stage" "MOI"           
## [5] "temp"           "humidity"       "result"
unique(data$crop.ID)
## [1] "Wheat"  "Potato" "Carrot" "Tomato" "Chilli"
subset_data <- data %>%
  filter(crop.ID %in% c("Wheat","Potato","Carrot","Tomato","Chilli"))
subset_data <- data %>%
  filter(crop.ID %in% c("Wheat","Potato","Carrot","Tomato", "Chilli")) %>%
  filter(Seedling.Stage == "Germination") %>%     # Kunci di 1 fase pertumbuhan
  group_by(crop.ID) %>%
  sample_n(5) %>%                                 # Ambil 5 sampel dari kondisi yang sudah seragam
  ungroup()
subset_data
## # A tibble: 25 × 7
##    crop.ID soil_type   Seedling.Stage   MOI  temp humidity result
##    <chr>   <chr>       <chr>          <int> <int>    <dbl>  <int>
##  1 Carrot  Loam Soil   Germination       58    38     37        1
##  2 Carrot  Clay Soil   Germination       55    37     42        1
##  3 Carrot  Sandy Soil  Germination        6    19     85        0
##  4 Carrot  Clay Soil   Germination       63    45     18        1
##  5 Carrot  Loam Soil   Germination       25    27     72        1
##  6 Chilli  Chalky Soil Germination       28    16     88        0
##  7 Chilli  Red Soil    Germination        2    20     84        0
##  8 Chilli  Chalky Soil Germination       66    46     23.3      1
##  9 Chilli  Red Soil    Germination       25    27     75        1
## 10 Chilli  Chalky Soil Germination       50    30     66.5      1
## # ℹ 15 more rows
subset_data$perlakuan <- factor(subset_data$crop.ID,
  levels = c("Wheat","Potato","Carrot","Tomato","Chilli"),
  labels = c("A","B","C","D","E"))
latin_square <- matrix(c(
  "A","B","C","D","E",
  "B","C","D","E","A",
  "C","D","E","A","B",
  "D","E","A","B","C",
  "E","A","B","C","D"
), 
nrow = 5, byrow = TRUE)
latin_square
##      [,1] [,2] [,3] [,4] [,5]
## [1,] "A"  "B"  "C"  "D"  "E" 
## [2,] "B"  "C"  "D"  "E"  "A" 
## [3,] "C"  "D"  "E"  "A"  "B" 
## [4,] "D"  "E"  "A"  "B"  "C" 
## [5,] "E"  "A"  "B"  "C"  "D"
data_long <- as.data.frame(as.table(latin_square))
colnames(data_long) <- c("baris","kolom","perlakuan")
data_long_sorted <- data_long %>% arrange(perlakuan)
subset_data_sorted <- subset_data %>% arrange(perlakuan)
data_long_sorted$respon <- subset_data_sorted$temp
data_rbsl <- data_long_sorted %>% arrange(baris, kolom)
data_rbsl$baris <- as.factor(data_rbsl$baris)
data_rbsl$kolom <- as.factor(data_rbsl$kolom)
data_rbsl
##    baris kolom perlakuan respon
## 1      A     A         A     20
## 2      A     B         B     21
## 3      A     C         C     19
## 4      A     D         D     35
## 5      A     E         E     30
## 6      B     A         B     33
## 7      B     B         C     37
## 8      B     C         D     21
## 9      B     D         E     27
## 10     B     E         A     45
## 11     C     A         C     38
## 12     C     B         D     17
## 13     C     C         E     46
## 14     C     D         A     43
## 15     C     E         B     21
## 16     D     A         D     26
## 17     D     B         E     20
## 18     D     C         A     31
## 19     D     D         B     46
## 20     D     E         C     27
## 21     E     A         E     16
## 22     E     B         A     37
## 23     E     C         B     28
## 24     E     D         C     45
## 25     E     E         D     14
# gabungkan huruf + nilai
data_rbsl$isi <- paste0(data_rbsl$respon, " (", data_rbsl$perlakuan, ")")

# ubah jadi bentuk matriks 5x5
tabel_kotak <- matrix(data_rbsl$isi, nrow = 5, byrow = TRUE)

tabel_kotak
##      [,1]     [,2]     [,3]     [,4]     [,5]    
## [1,] "20 (A)" "21 (B)" "19 (C)" "35 (D)" "30 (E)"
## [2,] "33 (B)" "37 (C)" "21 (D)" "27 (E)" "45 (A)"
## [3,] "38 (C)" "17 (D)" "46 (E)" "43 (A)" "21 (B)"
## [4,] "26 (D)" "20 (E)" "31 (A)" "46 (B)" "27 (C)"
## [5,] "16 (E)" "37 (A)" "28 (B)" "45 (C)" "14 (D)"
data_rbsl$baris <- as.factor(data_rbsl$baris)
data_rbsl$kolom <- as.factor(data_rbsl$kolom)
data_rbsl$perlakuan <- as.factor(data_rbsl$perlakuan)
model <- aov(respon ~ baris + kolom + perlakuan, data = data_rbsl)
summary(model)
##             Df Sum Sq Mean Sq F value Pr(>F)
## baris        4  221.8   55.46   0.527  0.718
## kolom        4  582.6  145.66   1.385  0.297
## perlakuan    4  482.6  120.66   1.147  0.381
## Residuals   12 1261.9  105.16
shapiro.test(residuals(model))
## 
##  Shapiro-Wilk normality test
## 
## data:  residuals(model)
## W = 0.96171, p-value = 0.4495
aggregate(respon ~ perlakuan, data = data_rbsl, mean)
##   perlakuan respon
## 1         A   35.2
## 2         B   29.8
## 3         C   33.2
## 4         D   22.6
## 5         E   27.8
boxplot(respon ~ perlakuan, data = data_rbsl)