HYPOTHESES TESTING
pacman::p_load(tidyverse,Lock5Data,ISLR,wooldridge,expss,hablar,rio)
#Two tailed test of population mean with known variance
#Ho: μ = 30
#Ha: μ ≠ 30
mu <- 30
sigma <- 5
n <- 30
alfa <- 0.10
zcrit <- qnorm(p=alfa/2)
x_bar1 <- ((zcrit*sigma)+(mu*sqrt(n)))/(sqrt(n));x_bar1
## [1] 28.49846
x_bar2 <- ((-zcrit*sigma)+(mu*sqrt(n)))/(sqrt(n));x_bar2
## [1] 31.50154
#z <- (x_bar-mu)/(sigma/(sqrt(n)));z
#pvalue= 2*(1-pnorm(abs(z))); pvalue
#At a value of less than 28.49846 or greater than 31.50154 for x bar, the null hypotheses can be rejected, which would mean that at the 10% significance level, the mean monthly expenditures on bakery products for all Americans is not equal to $30
#Upper tail test of population mean with known variance
#Ho: μ <= 0.10
#Ha: μ > 0.10
mu <- 0.10
sigma <- 0.03
n <- 30
alfa <- 0.05
zcrit <- qnorm(p=alfa, lower.tail = FALSE)
x_bar <- ((zcrit*sigma)+(mu*sqrt(n)))/(sqrt(n));x_bar
## [1] 0.1090092
#z <- (x_bar-mu)/(sigma/(sqrt(n)))
#pvalue= pnorm(z,lower.tail= FALSE); pvalue
#At a value of more than 0.1090092 (10.9%) for x bar, the null hypotheses can be rejected, which would mean that at the 5% significance level, heating costs increased faster than inflation.
#Test of population mean with unknown variance
n=30
xbarra=55.7
s=sqrt(64.8)
alfa=0.01
mu0=50
#Rejection region
Tcal=(xbarra-mu0)/(s/sqrt(n))
#p-value
p=pt(abs(Tcal),n-1,lower.tail=F); p
## [1] 0.0002780401
pv=ifelse(p<0.5,2*p,2*(1-p));pv
## [1] 0.0005560802
ifelse(pv<alfa,"reject H0","Don't reject H0")
## [1] "reject H0"
#pvalue < level of significance
#Rejection of H0, the people surveyed would be willing to pay more than the 50 pounds initially proposed to obtain the software
#Test of population mean with known variance
n= 48
xbarra=59500
sigma=5000
mu0=60000
alfa= 0.05
Zcal=(xbarra-mu0)/(sigma/sqrt(n))
p=pnorm(Zcal,lower.tail= F)
ifelse(p<alfa,"reject H0","Don't reject H0")
## [1] "Don't reject H0"
#pvalue > level of significance
#Do not reject H0, there is not enough evidence to affirm that what the manufacturer declares differs from the results in the sample.
#Ho: μ >= 3
#Ha: μ < 3
miu <- 3
desv <- 1
n <- 50
xbar <- 2.75
z <- ((xbar-miu)/(desv/sqrt(n)))
pnorm(z,lower.tail = TRUE)
## [1] 0.03854994
#pvalue < level of significance
#We reject the null hypotheses, which means that the mean waiting time is less than 3 minutes
#Ho: μ >= 42.3
#Ha: μ < 42.3
miu <- 42.3
s <- 2.7
n <- 24
xbar <- 40.6
t <- (xbar-miu)/(s/(sqrt(n)))
pvalue= pt(t,df=n-1, lower.tail= TRUE); pvalue
## [1] 0.00261788
#pvalue < level of significance
#We reject the null hypotheses, which means that at a 10% significance level, we can conclude that the assembly time using the new method is faster.
Mu <- 40000
Xbarra <- 50000
S <- 10000
n <- 10
alfa <- 0.05
#Case of study: 1 mean with unknown variance
#test: T-student
#Ho: Xbarra <= 40000
#H1: Xbarra > 40000
Tcalc <- ((Xbarra - Mu)/(S/sqrt(n)))
Tcalc
## [1] 3.162278
df <- n-1
Tcrit <- qt(1-(alfa/2), df)
Tcalc > Tcrit
## [1] TRUE
#The above expression is true, so the null hypothesis is rejected with 95% confidence
#It is 95% accurate to say that Wilmington residents earn more than the national average
# H0: Mu = 45000
x_bar <- 45500
mu <- 45000
sigma <- 3000
n <- 120
z <- (x_bar - mu) / (sigma/ sqrt(n))
p.value <- pnorm(z, lower.tail = FALSE); p.value
## [1] 0.03394458
#This means that H0 is false, so with .10 significance level its certain to say that the mean income is not equal to 45000.
#Ho: μ >= 10
#H1: μ < 10
t=(10-9)/(2.8*sqrt(50))
t
## [1] 0.05050763
xbar= 9
mu = 10
sigma = 2.8
n = 50
z <- (xbar - mu) / (sigma / sqrt(n))
z
## [1] -2.525381
pvalue <- pnorm(z, lower.tail = TRUE); pvalue
## [1] 0.00577864
#pvalue < level of significance
#We reject the null hypotheses, which means that with 5% level of significance we can conclude that those joining Weight Reducers on average will lose less than 10 pounds
#Ho: μ >= 7
#Ha: μ < 7
xbar2 = 6.8
mu2 = 7
s = 0.9
n2 = 50
t <- (xbar2-mu2)/(s/(sqrt(n2)))
pvalue2= pt(t,df=n-1, lower.tail= TRUE); pvalue2
## [1] 0.06126869
#Level of significance = 5%
#pvalue > level of significance
#At a 5% significance level, we don't reject the null hypotheses, which means that we cannot conclude that students at West Virginia sleep less than the typical American