# 1. Pendahuluan

Tujuan Praktikum:

1. Memahami konsep uji vektor nilai tengah untuk dua populasi.

2. Mampu melakukan uji hipotesis dan membentuk selang kepercayaan untuk:

3. Mampu menginterpretasikan hasil output R dalam konteks masalah.

2. Perbandingan Dua Vektor Nilai Tengah Sampel Berpasangan

Input Data

# membangkitkan data dengan set seed 34
library(readxl)
## Warning: package 'readxl' was built under R version 4.4.3
dataset <- read_excel("C:/Users/LENOVO/Downloads/Latihan Data Pertemuan 4.xlsx", sheet = 1)
dataset
## # A tibble: 15 × 4
##    `Kata Formal` `Kata Kerja Formal` `Kata Informal` `Kata Kerja Informal`
##            <dbl>               <dbl>           <dbl>                 <dbl>
##  1           137                  15             148                    20
##  2           164                  25             159                    24
##  3           224                  27             144                    19
##  4           208                  33             103                    18
##  5           178                  24             121                    17
##  6           128                  20              89                    11
##  7           154                  18             119                    17
##  8           158                  16             123                    13
##  9           102                  21              76                    16
## 10           214                  25             217                    29
## 11           209                  24             148                    22
## 12           151                  16             151                    21
## 13           123                  13              83                     7
## 14           161                  22             135                    20
## 15           175                  23             178                    15

Mengganti seluruh nilai data dengan data bangkitan menggunakan set seed 34

set.seed(34)

# Banyak baris data
n <- 15

# Bangkitkan data acak
dataset2 <- data.frame(
  `Jumlah Gol` = sample(100:230, n, replace = TRUE),
  `Gol Jarak Jauh` = sample(10:35, n, replace = TRUE),
  `Jumlah Poin` = sample(70:220, n, replace = TRUE),
  `Poin Jarak Jauh` = sample(7:30, n, replace = TRUE)
)

# Tampilkan data
print(dataset2)
##    Jumlah.Gol Gol.Jarak.Jauh Jumlah.Poin Poin.Jarak.Jauh
## 1         109             13         173              17
## 2         149             22          91              19
## 3         107             29         181              30
## 4         185             34         144              27
## 5         199             22         177              23
## 6         128             27         128              30
## 7         110             14         106              30
## 8         225             18         216               9
## 9         198             31         116              13
## 10        160             13         214              12
## 11        166             18          82              14
## 12        132             34         188              17
## 13        190             21         191              20
## 14        145             31         187              18
## 15        108             29         151              18

Menghitung Selisih Jumlah Gol dan Poin

d_Gol = dataset2[,1]-dataset2[,3]
d_Gol_Jarak_Jauh = dataset2[,2]-dataset2[,4]

X = data.frame(d_Gol,d_Gol_Jarak_Jauh)

Menghitung vektor rataan dan matriks covarians

xbar = apply(X, 2, mean)
xbar
##            d_Gol d_Gol_Jarak_Jauh 
##        -2.266667         3.933333
cov_m = cov(X)
cov_m
##                       d_Gol d_Gol_Jarak_Jauh
## d_Gol            2754.78095         50.40952
## d_Gol_Jarak_Jauh   50.40952         79.35238

Uji T2 Hotelling Dua Populasi Sampel Berpasangan

library(MVTests)
## Warning: package 'MVTests' was built under R version 4.4.3
## 
## Attaching package: 'MVTests'
## The following object is masked from 'package:datasets':
## 
##     iris
mean0 = c(0,0)
result = OneSampleHT2(X,mu0=mean0,alpha=0.05)
summary(result)
##        One Sample Hotelling T Square Test 
## 
## Hotelling T Sqaure Statistic = 3.049613 
##  F value = 1.416 , df1 = 2 , df2 = 13 , p-value: 0.278 
## 
##                    Descriptive Statistics
## 
##           d_Gol d_Gol_Jarak_Jauh
## N     15.000000        15.000000
## Means -2.266667         3.933333
## Sd    52.486007         8.907995
## 
## 
##                  Detection important variable(s)
## 
##                       Lower    Upper Mu0 Important Variables?
## d_Gol            -41.065157 36.53182   0                FALSE
## d_Gol_Jarak_Jauh  -2.651598 10.51827   0                FALSE

Selang Kepercayaan Simultan

result$CI
##                       Lower    Upper Mu0 Important Variables?
## d_Gol            -41.065157 36.53182   0                FALSE
## d_Gol_Jarak_Jauh  -2.651598 10.51827   0                FALSE

Selang Kepercayaan Bonferroni

bon = function(mu,S,n,alpha,k){
 p = length(mu)
 lower = mu[k] - sqrt(S[k,k]/n) * abs(qt(alpha/(2*p), df=n-1))
 upper = mu[k] + sqrt(S[k,k]/n) * abs(qt(alpha/(2*p), df=n-1))
 c(lower = lower,upper = upper)
}

n = nrow(X)
#Gol
bon(xbar, cov_m,n,0.05,1)
## lower.d_Gol upper.d_Gol 
##   -36.27592    31.74259
#Jarak Jauh
bon(xbar, cov_m,n,0.05,2)
## lower.d_Gol_Jarak_Jauh upper.d_Gol_Jarak_Jauh 
##              -1.838763               9.705430

3. Perbandingan Dua Vektor Nilai Tengah Sampel Saling Bebas Ragam Sama

library(readxl)
dataset <- read_excel("C:/Users/LENOVO/Downloads/Latihan Data Pertemuan 4.xlsx", sheet = 2)
dataset
## # A tibble: 20 × 4
##    `CAT TWK` `CAT TIU` `MANUAL TWK` `MANUAL TIU`
##        <dbl>     <dbl>        <dbl>        <dbl>
##  1        54        77           58           84
##  2        76        58           49           60
##  3        55        58           83           69
##  4        79        63           55           60
##  5        84        37           85           89
##  6        60        45           90           68
##  7        73        60           42           36
##  8        81        56           85           57
##  9        65        80           80           46
## 10        54        42           89           51
## 11        67        42           40           65
## 12        51        64           71           64
## 13        56        62           81           82
## 14        50        41           40           50
## 15        58        43           80           77
## 16        82        46           75           35
## 17        61        48           41           55
## 18        60        43           54           61
## 19        45        56           48           50
## 20        80        38           69           43
set.seed(34)
n <- 20 
# Bangkitkan data acak
Digital_Sistolik  <- round(rnorm(n, mean=120, sd=15))
Digital_Diastolik <- round(rnorm(n, mean=80, sd=10))

Manual_Sistolik   <- pmin(pmax(round(rnorm(n, mean=122, sd=12)), 0), 250)
Manual_Diastolik  <- pmin(pmax(round(rnorm(n, mean=82, sd=8)),  0), 150)

dataset_kesehatan <- data.frame(
  Digital_Sistolik,
  Digital_Diastolik,
  Manual_Sistolik,
  Manual_Diastolik
)

dataset_kesehatan
##    Digital_Sistolik Digital_Diastolik Manual_Sistolik Manual_Diastolik
## 1               118                65             114               74
## 2               138               105             115               93
## 3               109                69              94               72
## 4               111                66             100               68
## 5               116                84             121               76
## 6               113                88             133               84
## 7               130                71             124               89
## 8               107                75             145               87
## 9               136                78             112               87
## 10              120                92             131               78
## 11              114                72             135               86
## 12              131                92             109               76
## 13              117                68             113               78
## 14              136                80             121              101
## 15              126                87             110               79
## 16              140                75             134               73
## 17              120                82             110               82
## 18              113                81             137               70
## 19               92                81             122               71
## 20              116                80             137               88
# Memisahkan Data dari Populasi 1 dan 2
data_cat = dataset_kesehatan[,1:2]
data_man = dataset_kesehatan[,3:4]

Menghitung vektor rataan dan matriks covarians

xbar1 = apply(data_cat, 2, mean)
xbar1
##  Digital_Sistolik Digital_Diastolik 
##            120.15             79.55
xbar2 = apply(data_man, 2, mean)
xbar2
##  Manual_Sistolik Manual_Diastolik 
##           120.85            80.60
cov_m1 = cov(data_cat)
cov_m1
##                   Digital_Sistolik Digital_Diastolik
## Digital_Sistolik         148.76579          38.01842
## Digital_Diastolik         38.01842         101.52368
cov_m2 = cov(data_man)
cov_m2
##                  Manual_Sistolik Manual_Diastolik
## Manual_Sistolik        183.81842         31.51579
## Manual_Diastolik        31.51579         74.56842
n1 = nrow(data_cat)
n2 = nrow(data_man)

# S Gabungan
s_gab = ((n1-1)*cov_m1+(n2-1)*cov_m2)/(n1+n2-2)
s_gab
##                   Digital_Sistolik Digital_Diastolik
## Digital_Sistolik         166.29211          34.76711
## Digital_Diastolik         34.76711          88.04605

Uji T2 Hotelling Dua Populasi Sampel Saling Bebas Ragam Sama

library(Hotelling)
## Loading required package: corpcor
t2_homogen = hotelling.test(data_cat,data_man,var.equal=TRUE)
t2_homogen
## Test stat:  0.13056 
## Numerator df:  2 
## Denominator df:  37 
## P-value:  0.9385

Selang Kepercayaan Simultan

T.ci = function(mu1, mu2, S_gab, n1, n2, avec=rep(1,length(mu)), level=0.95){
p = length(mu1)
mu = mu1-mu2
cval = qf(level, p, n1+n2-p-1) * p * (n1+n2-2) / (n1+n2-p-1)
zhat = crossprod(avec, mu)
zvar = crossprod(avec, S_gab %*% avec)* (1/n1+1/n2)
const = sqrt(cval * zvar)
c(lower = zhat - const, upper = zhat + const)
}
#TWK
T.ci(xbar1, xbar2, s_gab, n1,n2, avec=c(1,0),level=0.95)
##      lower      upper 
## -11.239304   9.839304
#TIU
T.ci(xbar1, xbar2, s_gab, n1,n2, avec=c(0,1),level=0.95)
##     lower     upper 
## -8.718864  6.618864

Selang Kepercayaan Bonferroni

bon = function(mu1, mu2 ,S, n1, n2, alpha, k){
 p = length(mu1)
 mu = mu1-mu2
 lower = mu[k] - sqrt((S[k,k]) *(1/n1+1/n2))* abs(qt(alpha/(2*p), df=n1+n2-2))
 upper = mu[k] + sqrt((S[k,k]) *(1/n1+1/n2))* abs(qt(alpha/(2*p), df=n1+n2-2))
 ci = c(lower = lower,upper = upper)
 names(ci)= c("lower","upper")
 ci
}
#TWK
bon(xbar1, xbar2, s_gab, n1, n2,0.05,1)
##      lower      upper 
## -10.216665   8.816665
#TIU
bon(xbar1, xbar2, s_gab, n1, n2,0.05,2)
##     lower     upper 
## -7.974747  5.874747

4. Perbandingan Dua Vektor Nilai Tengah Sampel Saling Bebas Ragam Tidak Sama

Input Data

library(readxl)
dataset <- read_excel("C:/Users/LENOVO/Downloads/Latihan Data Pertemuan 4.xlsx", sheet = 2)
dataset
## # A tibble: 20 × 4
##    `CAT TWK` `CAT TIU` `MANUAL TWK` `MANUAL TIU`
##        <dbl>     <dbl>        <dbl>        <dbl>
##  1        54        77           58           84
##  2        76        58           49           60
##  3        55        58           83           69
##  4        79        63           55           60
##  5        84        37           85           89
##  6        60        45           90           68
##  7        73        60           42           36
##  8        81        56           85           57
##  9        65        80           80           46
## 10        54        42           89           51
## 11        67        42           40           65
## 12        51        64           71           64
## 13        56        62           81           82
## 14        50        41           40           50
## 15        58        43           80           77
## 16        82        46           75           35
## 17        61        48           41           55
## 18        60        43           54           61
## 19        45        56           48           50
## 20        80        38           69           43
# Memisahkan Data dari Populasi 1 dan 2
data_cat = dataset[,1:2]
data_man = dataset[,3:4]

Menghitung vektor rataan dan matriks covarians

xbar1 = apply(data_cat, 2, mean)
xbar1
## CAT TWK CAT TIU 
##   64.55   52.95
xbar2 = apply(data_man, 2, mean)
xbar2
## MANUAL TWK MANUAL TIU 
##      65.75      60.10
cov_m1 = cov(data_cat)
cov_m1
##           CAT TWK   CAT TIU
## CAT TWK 151.10263 -26.28684
## CAT TIU -26.28684 154.15526
cov_m2 = cov(data_man)
cov_m2
##            MANUAL TWK MANUAL TIU
## MANUAL TWK  333.77632   81.86842
## MANUAL TIU   81.86842  230.41053
n1 = nrow(data_cat)
n2 = nrow(data_man)

Uji T2 Hotelling Dua Populasi Sampel Saling Bebas Ragam Sama

library(Hotelling)
t2_not_homogen = hotelling.test(data_cat,data_man,var.equal=FALSE)
t2_not_homogen
## Test stat:  2.6599 
## Numerator df:  2 
## Denominator df:  34.1358976944273 
## P-value:  0.287

Selang Kepercayaan Simultan

T.ci = function(mu1, mu2, S1, S2, n1, n2, avec=rep(1,length(mu)), level=0.95){
p = length(mu1)
mu = mu1-mu2
cval = qchisq(level, p)
zhat = crossprod(avec, mu)
zvar = crossprod(avec, S1 %*% avec)/n1 + crossprod(avec, S2 %*% avec)/n2
const = sqrt(cval * zvar)
c(lower = zhat - const, upper = zhat + const)
}

#TWK
T.ci(xbar1, xbar2, cov_m1, cov_m2, n1,n2, avec=c(1,0),level=0.95)
##     lower     upper 
## -13.25225  10.85225
#TWK
T.ci(xbar1, xbar2, cov_m1, cov_m2, n1,n2, avec=c(1,0),level=0.95)
##     lower     upper 
## -13.25225  10.85225
#TIU
T.ci(xbar1, xbar2, cov_m1, cov_m2, n1,n2, avec=c(0,1),level=0.95)
##      lower      upper 
## -17.883388   3.583388