KELOMPOK:
2
NAMA/NO.
- M. ARIEF EKO PRATAMA/27 (12.7261)
ABSEN (NIM):
- NADYA AMALIA/29 (12.7276) - RIVAN DESTYANUGRAHA/32 (12.7351) - RIZKI RAMADHANI ARIF T./33 (12.7357) - SISKA AMBARWATI/35 (12.7386)
KELAS:
4KS2
Tugas Kelompok Mata Kuliah Analisis Peubah Ganda (KS) SEKOLAH TINGGI ILMU STATISTIK JAKARTA, 2016
Page 2 of 9
Function Uji Multivariate Inferensia Vektor Rata-rata 1 populasi HotellingsT2 <- function(x, rata, alfa) UseMethod("HotellingsT2") HotellingsT2.count <- function(x,mu,sig) { #masukkan data, nilai mu, dan nilai signifikansi ##nilai t2 varY <- var(x) d <- matrix(NA,ncol = ncol(x),nrow = 1) for (i in 1:ncol(x)) { d[i] <- mean(x[,i]) - mu[i] } t2 <- nrow(x) * d %*% solve(varY) %*% t(d) chsq <- qchisq(1 - sig,ncol(x)) T2table <- ncol(x)*(nrow(x)-1)*qf(1-sig,ncol(x),nrow(x)-ncol(x))/ (nrow(x)-ncol(x)) res = FALSE if (t2 > T2table) { res = TRUE } list( mu = mu, Thitung = t2, ChSqTabel = T2table, result = res ) } HotellingsT2.default <- function(x,rata,alfa) { y <- as.matrix(x) rata <- t(as.matrix(rata)) test1 <- HotellingsT2.count(y,rata,alfa) test1$t.squared <- format(round(as.numeric(test1$Thitung),4), nsmall=4) test1$c.squared <- format(round(as.numeric(test1$ChSqTabel),4), nsmall=4) class(test1) <- "HotellingsT2" test1 } print.HotellingsT2 <- function(x, ...) { cat("==================================================================\n\n") cat("T Hotelling 1 Population\n\n") cat("H0: mu = [ ") cat(paste(x$mu," ")) cat("]\n") cat("H1: mu != [ ") cat(paste(x$mu," ")) cat("]\n\n") cat("Test:\n") r1 <- cbind(" T-Squared",x$t.squared) r2 <- cbind("T-2 Table", x$c.squared) cat(cbind(r1,"\n",r2)) cat("\n\nDecision: ") if (x$result) cat("T-Squared > Hotelling's T2 Table, Reject H0\n") else cat("T-Squared < Hotelling's T2 Table, Fail to Reject H0\n") cat("\n=================================================================\n")}
Page 3 of 9
Fungsi di atas digunakan dengan data pada slide pertemuan 5 APG halaman 26 tabel 3.3 > x <- matrix(c(35,3.5,2.80,35,4.9,2.70,40,30.0,4.38,10,2.8,3.21,6,2.7,2.73,2 0,2.8,2.81,35,4.6,2.88,35,10.9,2.90,35,8.0,3.28,30,1.6,3.20), byrow=TRUE, nco l=3) > mean <- c(15.0,6.0,2.85) > HotellingsT2(x,mean,0.05) ================================================================== T Hotelling 1 Population H0: mu = [ 15 H1: mu != [ 15
6 6
2.85 ] 2.85 ]
Test: T-Squared 24.5589 T-2 Table 16.7663 Decision: T-Squared > Hotelling's T2 Table, Reject H0 ==================================================================
Page 4 of 9
Function Uji Multivariate Inferensia Vektor Rata-rata 1 atau 2 Populasi V arians Diketahui. HotellingsT2Var <- function(x, rata, var, alfa) UseMethod("HotellingsT2Var") HotellingsT2Var.count <- function(x,mu,sigma,sig) { d <- as.matrix(colMeans(x) - mu) z2 <- nrow(x) * (t(d) %*% solve(sigma) %*% d) ch <- qchisq(1 - sig,2) res = FALSE if(z2>ch) res=TRUE list(z2 = z2, chitabel = ch, result = res) } HotellingsT2Var.default <- function(x,miu,var,sig) { x <- as.matrix(x) miu <- as.matrix(miu) var <- as.matrix(var) test2 <- HotellingsT2Var.count(x,miu,var,sig) test2$t.squared <- format(round(as.numeric(test2$z2),4), nsmall=4) test2$c.squared <- format(round(as.numeric(test2$chitabel),4), nsmall=4) class(test2) <- "HotellingsT2Var" test2 } print.HotellingsT2Var <- function(x, ...) { cat("==================================================================\n\n") cat("Z-Squared 2 Population\nVariance Known\n\n") cat("H0: mu = [ ") cat(paste(x$mu," ")) cat("]\n") cat("H1: mu != [ ") cat(paste(x$mu," ")) cat("]\n\n") cat("Test:\n") r1 <- cbind(" Z-Squared", x$t.squared) r2 <- cbind("Chi-2 Tab", x$c.squared) cat(as.matrix(cbind(r1,"\n",r2))) cat("\n\nDecision: ") if (x$result) cat("Z-Squared > Chi-Square Table, Reject H0\n") else cat("Z-Squared < Chi-Square Table, Fail to Reject H0\n") cat("\n==================================================================\n") }
Page 5 of 9
Fungsi di atas digunakan dengan data pada slide pertemuan 5 APG halaman 12 tabel 3.1 > x1<-c(69,74,68,70,72,67,66,70,76,68,72,79,74,67,66,71,74,75,75,76) > y1<-c(153,175,155,135,172,150,115,137,200,130,140,265,185,112,140,150,165,1 85,210,220) > dat <- cbind(x1,y1) > mean1 <- c(70,170) > var1 <- matrix(c(20,100,100,1000), byrow=TRUE, ncol=2) > HotellingsT2Var(dat,mean1,var1,0.05) ================================================================== Z-Squared 2 Population Variance Known H0: mu = [ 70 H1: mu != [ 70
170 ] 170 ]
Test: Z-Squared 8.4026 Chi-2 Tab 5.9915 Decision: Z-Squared > Chi-Square Table, Reject H0 ==================================================================
Page 6 of 9
Function Uji Multivariate Inferensia Vektor Rata-rata 2 p opulasi Varians Tidak Diketahui namun Diasumsikan Sama HotellingsT2VarS <- function(x1, x2, alfa) UseMethod("HotellingsT2VarS") HotellingsT2VarS.count <- function(x1,x2,alfa) { n1 <- nrow(x1) p1 <- ncol(x1) n2 <- nrow(x2) p2 <- ncol(x2) x.bar.1 <- colMeans(x1) x.bar.2 <- colMeans(x2) s1 <- var(x1) s2 <- var(x2) sp <- (1 / (n1 + n2 - 2)) * (((n1 - 1) * s1) + ((n2 - 1) * s2)) x.bar.d <- x.bar.1 - x.bar.2 t.sq <- (n1 * n2 / (n1 + n2)) * (t(x.bar.d) %*% solve(sp) %*% x.bar.d) c.sq <- (((n1 + n2 - 2) * p1) / (n1 + n2 - p1 - 1)) * qf((1 - alfa),p1, (n1+n2 - p1 - 1)) res <- FALSE if (t.sq > c.sq) { res <- TRUE } list(t.squared = t.sq, c.squared = c.sq, result = res) } HotellingsT2VarS.default <- function(x1,x2,alfa) { x1.conv <- as.matrix(x1) x2.conv <- as.matrix(x2) test3 <- HotellingsT2VarS.count(x1.conv,x2.conv,alfa) test3$t.squared <- format(round(as.numeric(test3$t.squared),4), nsmall=4) test3$c.squared <- format(round(as.numeric(test3$c.squared),4), nsmall=4) class(test3) <- "HotellingsT2VarS" test3 } print.HotellingsT2VarS <- function(x, ...) { cat("==================================================================\n\n") cat("T Hotelling 2 Population\nVariance Unknown, Assumed Same\n\n") cat("H0: mu1 = mu2\n") cat("H1: mu1 != mu2\n\n") cat("Test:\n") r1 <- cbind(" T-Squared", x$t.squared) r2 <- cbind("T-2 Table", x$c.squared) cat(as.matrix(cbind(r1,"\n",r2))) cat("\n\nDecision: ") if (x$result) cat("T-Squared > Hotelling's T2 Table, Reject H0\n") else cat("T-Squared < Hotelling's T2 Table, Fail to Reject H0\n") cat("\n==================================================================\n") }
Page 7 of 9
Fungsi di atas digunakan dengan data pada slide pertemuan 5 APG halaman 34 tabel 5.1 > x3 <- read.csv(file = "C:/Users/rdmbe/Documents/Tingkat 4KS2/APG/male.csv", header=TRUE,sep=";") > x4 <- read.csv(file = "C:/Users/rdmbe/Documents/Tingkat 4KS2/APG/fem.csv",h eader=TRUE,sep=",") > HotellingsT2VarS(x3,x4,0.01) ================================================================== T Hotelling 2 Population Variance Unknown, Assumed Same H0: mu1 = mu2 H1: mu1 != mu2 Test: T-Squared 97.6015 T-2 Table 15.3632 Decision: T-Squared > Hotelling's T2 Table, Reject H0 ==================================================================
Page 8 of 9
Function Uji Multivariate Inferensia Vektor Rata-rata 2 populasi Varians Tidak Diketahui dan Diasumsikan Tidak Sama HotellingsT2VarD <- function(x1, x2, alfa) UseMethod("HotellingsT2VarD") HotellingsT2VarD.count <- function(mat1, mat2, alfa) { xbar1 <- colMeans(mat1) xbar2 <- colMeans(mat2) S1 <- var(mat1) S2 <- var(mat2) n1 <- nrow(mat1) n2 <- nrow(mat2) 1 / n1 * S1 T2 <- as.numeric(t(xbar1 - xbar2) %*% solve((S1 / n1) + (S2 / n2)) %*% (xbar1 - xbar2)) CsquareTabel <- qchisq(1 - alfa, df = ncol(mat1)) if (T2 > CsquareTabel) { res = TRUE } else { res = FALSE } list(Thitung = T2, Ttabel = CsquareTabel, result = res) } HotellingsT2VarD.default <- function(x1,x2,alfa) { x1.conv <- as.matrix(x1) x2.conv <- as.matrix(x2) test4 <- HotellingsT2VarD.count(x1.conv,x2.conv,alfa) test4$t.squared <- format(round(as.numeric(test4$Thitung),4), nsmall=4) test4$c.squared <- format(round(as.numeric(test4$Ttabel),4), nsmall=4) class(test4) <- "HotellingsT2VarD" test4 } print.HotellingsT2VarD <- function(x, ...) { cat("==================================================================\n\n") cat("T Hotelling 2 Population\nVariance Unknown, Assumed Different\n\n") cat("H0: mu1 = mu2\n") cat("H1: mu1 != mu2\n\n") cat("Test:\n") r1 <- cbind(" T-Squared",x$t.squared) r2 <- cbind("Chi-2 Tab", x$c.squared) cat(as.matrix(cbind(r1,"\n",r2))) cat("\n\nDecision: ") if (x$result) cat("T-Squared > Chi-Square Table, Reject H0\n") else cat("T-Squared < Chi-Square Table, Fail to Reject H0\n") cat("\n=================================================================\n")}
Page 9 of 9
Fungsi di atas digunakan dengan data pada slide pertemuan 5 APG halaman 34 tabel 5.1 > x3 <- read.csv(file = "C:/Users/rdmbe/Documents/Tingkat 4KS2/APG/male.csv", header=TRUE,sep=";") > x4 <- read.csv(file = "C:/Users/rdmbe/Documents/Tingkat 4KS2/APG/fem.csv",h eader=TRUE,sep=",") > HotellingsT2VarD(x3,x4,0.05) ================================================================== T Hotelling 2 Population Variance Unknown, Assumed Different H0: mu1 = mu2 H1: mu1 != mu2 Test: T-Squared 97.6015 Chi-2 Tab 9.4877 Decision: T-Squared > Chi-Square Table, Reject H0 ==================================================================