Minggu, 03 Desember 2023

Kode Program : R Studio (Identifikasi Outlier, Uji Normalitas, Uji Homogenitas Varians, Independent Sample T-Test, dll)

Kode Program : R Studio (Identifikasi Outlier, Uji Normalitas, Uji Homogenitas Varians, Independent Sample T-Test, dll).

#generate normal random distribution for 20 samples

nilaiPreKlEksp <- rnorm(20, mean=55, sd=4.3)

nilaiPostKlEksp <- rnorm(20, mean=82, sd=4)


nilaiPreKlKontr <- rnorm(20, mean=68, sd=3.2)

nilaiPostKlKontr <- rnorm(20, mean=78, sd=3)


nilaiPreKlEksp

nilaiPostKlEksp

nilaiPreKlKontr

nilaiPostKlKontr


library(readr)

IndependentSampleTTest <- read_csv("D:/HAVIZUL DATA 2023/2023 Desain dan Analisas Eksperimen/IndependentSampleTTest.csv")

IndependentSampleTTest

print(IndependentSampleTTest, n=40)


#getwd() #Shows the default working directory 

IndependentSampleTTest <- read.csv("D:/HAVIZUL DATA 2023/2023 Desain dan Analisas Eksperimen/IndependentSampleTTest.txt")

IndependentSampleTTest

class(IndependentSampleTTest)


library(readxl)

IndependentSampleTTest <- read_excel("D:/HAVIZUL DATA 2023/2023 Desain dan Analisas Eksperimen/IndependentSampleTTest.xlsx", sheet = "Sheet1")

print(IndependentSampleTTest, n = 40)


nilaiKelEksp <- subset(IndependentSampleTTest$Nilai, IndependentSampleTTest$Kelompok == "E")

nilaiKelEksp

nilaiKelKontr <- subset(IndependentSampleTTest$Nilai, IndependentSampleTTest$Kelompok == "K")

nilaiKelKontr


#Uji normalitas dengan shapiro wilk

shapiro.test(nilaiKelEksp)

shapiro.test(nilaiKelKontr)


#Uji normalitas dengan visual

library("ggplot2")

library("ggpubr")


ggdensity(nilaiKelEksp, fill = "green")

ggqqplot(nilaiKelEksp)

ggdensity(nilaiKelKontr, fill = "blue")

ggqqplot(nilaiKelKontr)


hist(nilaiKelEksp)

hist(nilaiKelKontr)


#Uji Homogenitas Variansi (F-Test)

#var.test(IndependentSampleTTest$Nilai~IndependentSampleTTest$Kelompok, data = IndependentSampleTTest)

var.test(Nilai~Kelompok, data = IndependentSampleTTest)


#Uji Homogenitas Varians dengan Bartlett

bartlett.test(Nilai~Kelompok, data = IndependentSampleTTest)



#Uji Homogenitas Variansi dengan Levene Test

library(carData)

library(car)

leveneTest(Nilai~Kelompok, data = IndependentSampleTTest)


#Visualisasi Variability 

#Sumber : https://cran.r-project.org/web/packages/VCA/vignettes/VCA_package_vignette.html

#install.packages("VCA")

#library(VCA)

#data("VCAdata1")

#VCAdata1

#datS5 <- subset(VCAdata1, sample==5)

#datS5

#varPlot(form = Nilai~Kelompok, Data = IndependentSampleTTest)


#INDEPENDENT SAMPLE T-TEST

#Data deskriptif

IndependentSampleTTest %>% group_by(Kelompok) %>% get_summary_stats(Nilai, type = "mean_sd")


#Visualisasi dengan boxplot

ggboxplot(IndependentSampleTTest, x = "Kelompok", y = "Nilai", ylab = "Nilai (Gain Score)", xlab = "Kelompok", add = "jitter")


#Identifikasi nilai outlier 

#Cara Manual (sumber: https://www.reneshbedre.com/blog/find-outliers.html) :

# get median

med = median(nilaiKelEksp)

# subtract median from each value of x and get absolute deviation

abs_dev = abs(nilaiKelEksp-med)

# get MAD

mad = 1.4826 * median(abs_dev)

# get threshold values for outliers

Tmin = med-(3*mad) 

Tmax = med+(3*mad) 

# find outlier

nilaiKelEksp[which(nilaiKelEksp < Tmin | nilaiKelEksp > Tmax)]

# remove outlier

#x[which(x > Tmin & x < Tmax)]


# get median

med = median(nilaiKelKontr)

# subtract median from each value of x and get absolute deviation

abs_dev = abs(nilaiKelKontr-med)

# get MAD

mad = 1.4826 * median(abs_dev)

# get threshold values for outliers

Tmin = med-(3*mad) 

Tmax = med+(3*mad) 

# find outlier

nilaiKelKontr[which(nilaiKelKontr < Tmin | nilaiKelKontr > Tmax)]

# remove outlier

#x[which(x > Tmin & x < Tmax)]


#Cara otomatis (mengidentifikasi outliers berdasarkan kelompok) (Sumber : Buku Alboukadel Kassambara)

library(rstatix)

IndependentSampleTTest %>% 

  group_by(Kelompok) %>% 

  identify_outliers(Nilai)


#Uji Normalitas Shapiro Wilk by groups

IndependentSampleTTest %>% group_by(Kelompok) %>% shapiro_test(Nilai)

#Draw qqplot by group

ggqqplot(IndependentSampleTTest, x = "Nilai", facet.by = "Kelompok")


#Uji kesetaraan variansi dengan Levene's Test

IndependentSampleTTest %>% levene_test(Nilai~Kelompok)


#Independent Sample T-Test untuk variansi sama : STUDENT T-TEST

IndependentSampleTTest %>% t_test(Nilai ~ Kelompok, var.equal = TRUE, detailed = TRUE, alternative = "greater") %>% add_significance()


#Independent Sample T-Test untuk variansi tidak sama : WELTCH T-TEST

IndependentSampleTTest %>% t_test(Nilai ~ Kelompok, var.equal = FALSE, detailed = TRUE, alternative = "greater") %>% add_significance()





Tidak ada komentar:

Posting Komentar