How to perform z-test and t-test using R? Explain with an example
To perform z- test, let us take data first.
############## 1 sample z test ##########
fabric=read.csv(file.choose())
Fabric
Here fabric is the dataset
Now to perform Normality test, we can perform Anderson-Darling Test which is the following
##### Normality Test##################
library(nortest)
ad.test(fabric$Fabric_length) ###Anderson-Darling test
Here Anderson Darling test can be performed by reading a library nortest.
########### 1 sample z-test ##########
Let us perform two sided test which is 1 sample z-test
z.test(fabric$Fabric_length, alternative = "two.sided", mu = 0, sigma.x = 4,
sigma.y = NULL, conf.level = 0.95)
############## 1 sample t test ##########
Now we will be performing t-test in another dataset called bolt_d which contains diameter of bolts.
bolt_d=read.csv(file.choose())
##### Normality Test##################
library(nortest)
ad.test(bolt_d$Diameter) ###Anderson-Darling test
########### 1 sample t-test ##########
To perform 1 sample t-test, we can do the following
t.test(bolt_d$Diameter, mu = 0, alternative = "two.sided")
############2 sample T Test ##################
Promotion<-read_excel(file.choose()) # Promotion.xlsx
attach(Promotion)
colnames(Promotion)<-c("Credit","Promotion.Type","InterestRateWaiver","StandardPromotion")
# Changing column names
View(Promotion)
attach(Promotion)
#############Normality test###############
shapiro.test(InterestRateWaiver)
# p-value = 0.2246 >0.05 so p high null fly => It follows normal distribution
shapiro.test(StandardPromotion)
# p-value = 0.1916 >0.05 so p high null fly => It follows normal distribution
#############Variance test###############
var.test(InterestRateWaiver,StandardPromotion)#variance test
# p-value = 0.653 > 0.05 so p high null fly => Equal variances
############2 sample T Test ##################
t.test(InterestRateWaiver,StandardPromotion,alternative = "two.sided",conf.level = 0.95,correct = TRUE)#two sample T.Test
# alternative = "two.sided" means we are checking for equal and unequal
# means
# null Hypothesis -> Equal means
# Alternate Hypothesis -> Unequal Hypothesis
# p-value = 0.02523 < 0>
# unequal means
?t.test
t.test(InterestRateWaiver,StandardPromotion,alternative = "greater",var.equal = T)
# alternative = "greater means true difference is greater than 0
# Null Hypothesis -> (InterestRateWaiver-StandardPromotion) < 0>
# Alternative Hypothesis -> (StandardPromotion - InterestRateWaiver) > 0
# p-value = 0.01211 < 0> p low null go => accept alternate hypothesis
# InterestRateWaiver better promotion than StandardPromotion