#Number 1

t_test <- function(xbar, miu,stadev, n, alfa, pilihan_uji="upper" ){
  df <- n-1
  t_value <- (xbar - miu)/ (stadev/sqrt(n))
  
  if (pilihan_uji == "two_sided") {
    p_value <- 2*pt(abs(t_value), df)
  } else if (pilihan_uji == "lower") {
    p_value <- pt(t_value, df)
  } else if (pilihan_uji == "upper") {
    p_value <- (1-pt(t_value, df))
  } else {
    warning("pilihan Uji yang anda masukkan tidak sesuai")
  }
  
  return(list(p_value = p_value))
}

#Number 2

xbar = 16
miu = 16.43
stadev = 0.8
n = 15
alfa = 0.05


lower <- t_test(xbar,miu,stadev,n,alfa, pilihan_uji="lower")
paste("Uji Lower Tail:\n")
## [1] "Uji Lower Tail:\n"
paste("P-Value:", lower$p_value, "\n")
## [1] "P-Value: 0.0280993701831434 \n"
if(lower$p_value<alfa){
  print("H0 Ditolak")
}else{
  print("H0 Gagal Ditolak")
}
## [1] "H0 Ditolak"
upper <- t_test(xbar,miu,stadev,n,alfa, pilihan_uji="upper")
paste("Uji Upper Tail:\n")
## [1] "Uji Upper Tail:\n"
paste("P-Value:", upper$p_value, "\n")
## [1] "P-Value: 0.971900629816857 \n"
if(upper$p_value<alfa){
  print("H0 Ditolak")
}else{
  print("H0 Gagal Ditolak")
}
## [1] "H0 Gagal Ditolak"
two_sided <- t_test(xbar,miu,stadev,n,alfa, pilihan_uji="two_sided")
paste("Uji Two Sided:\n")
## [1] "Uji Two Sided:\n"
paste("P-Value:", two_sided$p_value, "\n")
## [1] "P-Value: 1.94380125963371 \n"
if(two_sided$p_value<alfa){
  print("H0 Ditolak")
}else{
  print("H0 Gagal Ditolak")
}
## [1] "H0 Gagal Ditolak"

#Number 3

cek_integer<- function(vector, int) {
  if(round(int)!=int || is.numeric(vector)!=TRUE){
    warning("angka yang anda masukkan bukan bilangan bulat")
  }
  if(int %in% vector){
    return(TRUE)
  }else{
    return(FALSE)
  }
  
}


cek_integer(c(1:100),100.5)
## Warning in cek_integer(c(1:100), 100.5): angka yang anda masukkan bukan
## bilangan bulat
## [1] FALSE
cek_integer(c(1:100),100)
## [1] TRUE
cek_integer(c(1:100),101)
## [1] FALSE

#Number 4

library(numbers)
## Warning: package 'numbers' was built under R version 4.3.3
cek_prima <- function(a){
  if(isPrime(a)){
    print("TRUE")
  }else{
    print("FALSE")
  }
}
cek_prima(2)
## [1] "TRUE"

#Number 5

Faktorial <- function(n){
  if(n==0){
    return(1)
  } else{
    z = 1
    for(i in 1:n){
      z = z * i
    }
    return(z)
  }
}
Faktorial(3)
## [1] 6
Faktorial(0)
## [1] 1

#Number 6

data("iris")
p <- iris
mean_max_scalling <- function(z){
  xnew <- (z-min(z))/(max(z)-min(z))
  return(xnew)
}

Hasil <- mean_max_scalling(p$Sepal.Length)
Hasil
##   [1] 0.22222222 0.16666667 0.11111111 0.08333333 0.19444444 0.30555556
##   [7] 0.08333333 0.19444444 0.02777778 0.16666667 0.30555556 0.13888889
##  [13] 0.13888889 0.00000000 0.41666667 0.38888889 0.30555556 0.22222222
##  [19] 0.38888889 0.22222222 0.30555556 0.22222222 0.08333333 0.22222222
##  [25] 0.13888889 0.19444444 0.19444444 0.25000000 0.25000000 0.11111111
##  [31] 0.13888889 0.30555556 0.25000000 0.33333333 0.16666667 0.19444444
##  [37] 0.33333333 0.16666667 0.02777778 0.22222222 0.19444444 0.05555556
##  [43] 0.02777778 0.19444444 0.22222222 0.13888889 0.22222222 0.08333333
##  [49] 0.27777778 0.19444444 0.75000000 0.58333333 0.72222222 0.33333333
##  [55] 0.61111111 0.38888889 0.55555556 0.16666667 0.63888889 0.25000000
##  [61] 0.19444444 0.44444444 0.47222222 0.50000000 0.36111111 0.66666667
##  [67] 0.36111111 0.41666667 0.52777778 0.36111111 0.44444444 0.50000000
##  [73] 0.55555556 0.50000000 0.58333333 0.63888889 0.69444444 0.66666667
##  [79] 0.47222222 0.38888889 0.33333333 0.33333333 0.41666667 0.47222222
##  [85] 0.30555556 0.47222222 0.66666667 0.55555556 0.36111111 0.33333333
##  [91] 0.33333333 0.50000000 0.41666667 0.19444444 0.36111111 0.38888889
##  [97] 0.38888889 0.52777778 0.22222222 0.38888889 0.55555556 0.41666667
## [103] 0.77777778 0.55555556 0.61111111 0.91666667 0.16666667 0.83333333
## [109] 0.66666667 0.80555556 0.61111111 0.58333333 0.69444444 0.38888889
## [115] 0.41666667 0.58333333 0.61111111 0.94444444 0.94444444 0.47222222
## [121] 0.72222222 0.36111111 0.94444444 0.55555556 0.66666667 0.80555556
## [127] 0.52777778 0.50000000 0.58333333 0.80555556 0.86111111 1.00000000
## [133] 0.58333333 0.55555556 0.50000000 0.94444444 0.55555556 0.58333333
## [139] 0.47222222 0.72222222 0.66666667 0.72222222 0.41666667 0.69444444
## [145] 0.66666667 0.66666667 0.55555556 0.61111111 0.52777778 0.44444444
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
library(ggpubr)
## Warning: package 'ggpubr' was built under R version 4.3.3
qqnorm(Hasil)

ggdensity(Hasil, main="Density Plot")

Dari plot diatas dapat diketahui bahwa data hasil scaling berdistribusi normal

#Number 7

p <- iris
ggplot(p, aes(Species, Sepal.Length, fill = Species)) +
  geom_boxplot() +
  labs(title = "Boxplot")

#Number 8

data("swiss")
k <- swiss
k <- transform(k, IPM = (log(swiss$Education)+log(swiss$Fertility)+log(swiss$Agriculture))^(1/3)*10)
k
##              Fertility Agriculture Examination Education Catholic
## Courtelary        80.2        17.0          15        12     9.96
## Delemont          83.1        45.1           6         9    84.84
## Franches-Mnt      92.5        39.7           5         5    93.40
## Moutier           85.8        36.5          12         7    33.77
## Neuveville        76.9        43.5          17        15     5.16
## Porrentruy        76.1        35.3           9         7    90.57
## Broye             83.8        70.2          16         7    92.85
## Glane             92.4        67.8          14         8    97.16
## Gruyere           82.4        53.3          12         7    97.67
## Sarine            82.9        45.2          16        13    91.38
## Veveyse           87.1        64.5          14         6    98.61
## Aigle             64.1        62.0          21        12     8.52
## Aubonne           66.9        67.5          14         7     2.27
## Avenches          68.9        60.7          19        12     4.43
## Cossonay          61.7        69.3          22         5     2.82
## Echallens         68.3        72.6          18         2    24.20
## Grandson          71.7        34.0          17         8     3.30
## Lausanne          55.7        19.4          26        28    12.11
## La Vallee         54.3        15.2          31        20     2.15
## Lavaux            65.1        73.0          19         9     2.84
## Morges            65.5        59.8          22        10     5.23
## Moudon            65.0        55.1          14         3     4.52
## Nyone             56.6        50.9          22        12    15.14
## Orbe              57.4        54.1          20         6     4.20
## Oron              72.5        71.2          12         1     2.40
## Payerne           74.2        58.1          14         8     5.23
## Paysd'enhaut      72.0        63.5           6         3     2.56
## Rolle             60.5        60.8          16        10     7.72
## Vevey             58.3        26.8          25        19    18.46
## Yverdon           65.4        49.5          15         8     6.10
## Conthey           75.5        85.9           3         2    99.71
## Entremont         69.3        84.9           7         6    99.68
## Herens            77.3        89.7           5         2   100.00
## Martigwy          70.5        78.2          12         6    98.96
## Monthey           79.4        64.9           7         3    98.22
## St Maurice        65.0        75.9           9         9    99.06
## Sierre            92.2        84.6           3         3    99.46
## Sion              79.3        63.1          13        13    96.83
## Boudry            70.4        38.4          26        12     5.62
## La Chauxdfnd      65.7         7.7          29        11    13.79
## Le Locle          72.7        16.7          22        13    11.22
## Neuchatel         64.4        17.6          35        32    16.92
## Val de Ruz        77.6        37.6          15         7     4.97
## ValdeTravers      67.6        18.7          25         7     8.65
## V. De Geneve      35.0         1.2          37        53    42.34
## Rive Droite       44.7        46.6          16        29    50.43
## Rive Gauche       42.8        27.7          22        29    58.33
##              Infant.Mortality      IPM
## Courtelary               22.2 21.32865
## Delemont                 22.2 21.84614
## Franches-Mnt             20.2 21.41284
## Moutier                  20.3 21.54093
## Neuveville               20.6 22.12009
## Porrentruy               26.6 21.43016
## Broye                    23.6 21.98463
## Glane                    24.9 22.11928
## Gruyere                  21.0 21.78119
## Sarine                   24.4 22.09988
## Veveyse                  24.5 21.84567
## Aigle                    16.5 22.08540
## Aubonne                  19.1 21.80070
## Avenches                 22.7 22.12021
## Cossonay                 18.7 21.52290
## Echallens                21.2 20.95533
## Grandson                 20.0 21.45659
## Lausanne                 20.2 21.76996
## La Vallee                10.8 21.33518
## Lavaux                   20.0 22.01074
## Morges                   18.0 21.95005
## Moudon                   22.4 21.01597
## Nyone                    16.7 21.86333
## Orbe                     15.3 21.42348
## Oron                     21.0 20.44748
## Payerne                  23.8 21.86165
## Paysd'enhaut             18.0 21.19866
## Rolle                    16.3 21.90650
## Vevey                    20.9 21.75657
## Yverdon                  22.5 21.66002
## Conthey                  15.1 21.15715
## Entremont                19.8 21.87789
## Herens                   18.3 21.20681
## Martigwy                 19.4 21.83250
## Monthey                  20.2 21.28703
## St Maurice               17.8 22.03646
## Sierre                   16.3 21.58771
## Sion                     18.1 22.29554
## Boudry                   20.3 21.81887
## La Chauxdfnd             20.5 20.50721
## Le Locle                 18.9 21.30228
## Neuchatel                23.0 21.89673
## Val de Ruz               20.0 21.48998
## ValdeTravers             19.5 20.86845
## V. De Geneve             18.0 19.75361
## Rive Droite              18.2 22.24578
## Rive Gauche              19.3 21.85948

#Number 9

kotadiatasratarata <- subset(k, IPM > mean(k$IPM))
kotadibawahratarata <- subset(k, IPM <= mean(k$IPM))

summary(kotadiatasratarata)
##    Fertility      Agriculture     Examination      Education    
##  Min.   :42.80   Min.   :17.60   Min.   : 6.00   Min.   : 6.00  
##  1st Qu.:64.17   1st Qu.:45.12   1st Qu.:14.00   1st Qu.: 8.00  
##  Median :67.90   Median :58.95   Median :16.00   Median :10.00  
##  Mean   :69.08   Mean   :54.25   Mean   :17.23   Mean   :12.92  
##  3rd Qu.:78.70   3rd Qu.:66.75   3rd Qu.:21.75   3rd Qu.:13.00  
##  Max.   :92.40   Max.   :84.90   Max.   :35.00   Max.   :32.00  
##     Catholic     Infant.Mortality      IPM       
##  Min.   : 2.27   Min.   :16.30    Min.   :21.66  
##  1st Qu.: 5.74   1st Qu.:18.43    1st Qu.:21.84  
##  Median :17.69   Median :20.25    Median :21.89  
##  Mean   :45.44   Mean   :20.53    Mean   :21.94  
##  3rd Qu.:95.83   3rd Qu.:22.65    3rd Qu.:22.07  
##  Max.   :99.68   Max.   :24.90    Max.   :22.30
summary(kotadibawahratarata)
##    Fertility      Agriculture     Examination      Education     
##  Min.   :35.00   Min.   : 1.20   Min.   : 3.00   Min.   : 1.000  
##  1st Qu.:65.70   1st Qu.:18.70   1st Qu.: 7.00   1st Qu.: 3.000  
##  Median :72.50   Median :39.70   Median :15.00   Median : 6.000  
##  Mean   :71.45   Mean   :46.21   Mean   :15.57   Mean   : 8.571  
##  3rd Qu.:77.60   3rd Qu.:69.30   3rd Qu.:22.00   3rd Qu.: 8.000  
##  Max.   :92.50   Max.   :89.70   Max.   :37.00   Max.   :53.000  
##     Catholic      Infant.Mortality      IPM       
##  Min.   :  2.15   Min.   :10.80    Min.   :19.75  
##  1st Qu.:  4.20   1st Qu.:18.00    1st Qu.:21.02  
##  Median : 11.22   Median :20.00    Median :21.30  
##  Mean   : 35.82   Mean   :19.21    Mean   :21.15  
##  3rd Qu.: 90.57   3rd Qu.:20.50    3rd Qu.:21.43  
##  Max.   :100.00   Max.   :26.60    Max.   :21.59