Nama : Novan Adi Nugroho

NIM : 211910848

Kelas : 3SE3

TUGAS ANALISIS PEUBAH GANDA PERTEMUAN 7

Import File

library(readxl)
dt <- read_xlsx("Tugas APG Pertemuan 7.xlsx")
colnames(dt) <- c("i","closed","open")
dt2 <- dt[,2:3]

Vektor rata-rata dari data asli

(cm <- colMeans(dt2))
##    closed      open 
## 0.1283333 0.1638095

Menghitung matriks variance-covariance

(S <- cov(dt2))
##             closed        open
## closed 0.010053252 0.009535772
## open   0.009535772 0.016190012

Menghitung invers matriks variance-covariance

(solve(S))
##           closed      open
## closed  225.3893 -132.7523
## open   -132.7523  139.9564

Membuat chi-square Plot

# calaculate the statistical square distances
(d <- apply(dt2,MARGIN = 1,function(dt2) +
            t(dt2-cm) %*% solve(S) %*% (dt2-cm)))
##  [1]  1.9182482  0.3424485  1.3293288  0.2707763  0.6257666  0.1873365
##  [7]  0.3418183  0.6257666  0.3418183  0.2707763  0.5456976  1.1844913
## [13]  1.6346824 13.7969703  0.1199894  0.6364931  1.2294244  0.2707763
## [19]  1.6346824 11.8008871  0.1199894  0.6257666  1.0208562  0.8288197
## [25]  0.2119385  3.8013379  0.2119385  0.3424485  0.3418183  0.8972369
## [31]  0.2707763  2.9416357  0.3350118  3.0306589  1.6537157  0.5573147
## [37]  0.6523064  3.6845396  2.9329147  8.7828361  8.9074764  0.7404843
# construct a chi-square plot of the ordered distances
par(mfrow = c(1,1))
plot(qchisq((1:nrow(dt2)-1/2)/nrow(dt2),df=2), sort(d),
     xlab = expression(paste(chi[2]^2, "Quantile")),
     ylab = "Ordered Distances",
     main = "Chi-square Plot for (Closed,Open)"); abline(a=0, b=1)

Terlihat bahwa data tidak berdistribusi normal bivariat, tidak berada pada garis lurus maka solusinya dilakukan transformasi

Transformasi berdasarkan soal

dtnew <- dt2^(1/4)

Vektor rata-rata dari data transformasi

(cmnew <- colMeans(dtnew))
##    closed      open 
## 0.5642575 0.6029812

Menghitung matriks variance-covariance dari data transformasi

(Snew <- cov(dtnew))
##            closed       open
## closed 0.01435023 0.01171547
## open   0.01171547 0.01454530

invers S

solve(Snew)
##           closed      open
## closed  203.4981 -163.9069
## open   -163.9069  200.7691

Membuat chi-square Plot dari data transformasi

# calaculate the statistical square distances in transformation data
(dnew <- apply(dtnew,MARGIN = 1,function(dtnew) +
                t(dtnew-cmnew) %*% solve(Snew) %*% (dtnew-cmnew)))
##  [1] 1.85004223 0.36916666 1.40305802 0.30680988 0.81362789 0.27683757
##  [7] 0.23962769 0.81362789 0.23962769 0.30680988 0.63376962 2.57931130
## [13] 5.71246681 9.50831671 0.03341089 0.91023045 2.46903199 0.30680988
## [19] 5.71246681 5.29252541 0.03341089 0.81362789 1.54506989 1.20045883
## [25] 0.39311905 3.86069165 0.39311905 0.36916666 0.23962769 1.09984515
## [31] 0.30680988 3.94506381 0.51430566 2.16261800 6.35993678 0.83188830
## [37] 0.83738765 2.63136002 2.18040137 4.03149749 7.16362319 1.30939580
# construct a chi-square plot of the ordered distances
par(mfrow = c(1,1))
plot(qchisq((1:nrow(dtnew)-1/2)/nrow(dtnew),df=2), sort(dnew),
     xlab = expression(paste(chi[2]^2, "Quantile")),
     ylab = "Ordered Distances",
     main = "Chi-square Plot for (Closed,Open) Transformation Data"); abline(a=0, b=1)

Scatter Plot Data Transformasi

plot(dtnew$closed,dtnew$open); abline(a=0, b=1)

Eigen value

ev <- eigen(Snew)
# extract components
(values <- ev$values)
## [1] 0.026163638 0.002731895

Vector eigen

ev$vectors
##           [,1]       [,2]
## [1,] 0.7041574 -0.7100439
## [2,] 0.7100439  0.7041574

Membuat Bivariat Ellips

# Make bivariat ellips
#install.packages("MVQuickGraphs")
library(MVQuickGraphs)
## Warning: package 'MVQuickGraphs' was built under R version 4.0.5
confidenceEllipse(X.mean = cmnew,
                  eig = ev,n = 42,p = 2,alpha = 0.05)

```