A=c(45.3, 66.3, 52.5, 51.4, 53.4, 60.1, 49.1, 64.5, 51.0, 52.7)
B=c(55.2, 40.3, 46.5, 55.6, 48.9, 46.7, 53.4, 43.2)
value=c(A,B);Group=c(rep("A",10),rep("B",8));dat=as.data.frame(cbind(Group,value))
dat$value=as.numeric(as.character(dat$value))
str(dat)
## 'data.frame': 18 obs. of 2 variables:
## $ Group: Factor w/ 2 levels "A","B": 1 1 1 1 1 1 1 1 1 1 ...
## $ value: num 45.3 66.3 52.5 51.4 53.4 60.1 49.1 64.5 51 52.7 ...
par(mfrow=c(1,2)); plot(density(A),col="blue"); plot(density(B),col="blue")
t.test(A,B) # Assumption: paired=F, var.equal=F, mu=0
##
## Welch Two Sample t-test
##
## data: A and B
## t = 2.0178, df = 15.961, p-value = 0.06074
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.2999224 12.1099224
## sample estimates:
## mean of x mean of y
## 54.630 48.725
var.test(value~Group)$p.value
## [1] 0.6353104
t.test(dat$value~dat$Group) # Similar with command t.test(A,B)
##
## Welch Two Sample t-test
##
## data: dat$value by dat$Group
## t = 2.0178, df = 15.961, p-value = 0.06074
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.2999224 12.1099224
## sample estimates:
## mean in group A mean in group B
## 54.630 48.725
t.test(dat$value~dat$Group,var.equal=T)
##
## Two Sample t-test
##
## data: dat$value by dat$Group
## t = 1.9742, df = 16, p-value = 0.06588
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.4356878 12.2456878
## sample estimates:
## mean in group A mean in group B
## 54.630 48.725
Ref: Phan tich du lieu voi R (NVT) pp:365-366
set.seed(123) # The random samples may different with those created by Prof. NVT
n1=length(A); n2=length(B)
difference=numeric(10000) # Create a space to store values of "difference"
no.effect=0 # Create a space to store values of "no.effect"
for(i in 1:10000){ # loop 10000 times
bt.A=sample(A,n1,replace=T)
bt.B=sample(B,n2,replace=T)
difference[i]=mean(bt.A)-mean(bt.B)
if (difference [i]<0) no.effect = no.effect+1
}
no.effect/10000 # number of samples with "difference" <0
## [1] 0.0168
quantile(difference,probs=c(0.025,0.5,0.975)) # 95% interval of "difference"
## 2.5% 50% 97.5%
## 0.48750 5.86000 11.50269
plot(density(difference),col="blue")