chi-squared test

s <- c(10,15,20)
s / sum(s)
## [1] 0.2222222 0.3333333 0.4444444
obsfreq <- c(10,15,20)
nullprobs <- c(0.333,0.333,0.334)
chisq.test(obsfreq, p = nullprobs)
## 
##  Chi-squared test for given probabilities
## 
## data:  obsfreq
## X-squared = 3.3018, df = 2, p-value = 0.1919
obsfreq <- c(10,10,10)
nullprobs <- c(0.333,0.333,0.334)
chisq.test(obsfreq, p = nullprobs)
## 
##  Chi-squared test for given probabilities
## 
## data:  obsfreq
## X-squared = 5.994e-05, df = 2, p-value = 1
curl::curl_download('https://raw.githubusercontent.com/ywchiu/fda_course/main/cdc.Rdata', 'cdc.Rdata')
load('cdc.Rdata')


ftable <- table(cdc$smoke100, cdc$gender)
mosaicplot(ftable, col = c('blue', 'red'))

ftable2<- table(c(1,1,1,1,0,0,0,0), c('m','m','f', 'f','m','m','f', 'f'))
ftable2
##    
##     f m
##   0 2 2
##   1 2 2
chisq.test(ftable)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  ftable
## X-squared = 204.6, df = 1, p-value < 2.2e-16
chisq.test(ftable2)
## Warning in chisq.test(ftable2): Chi-squared approximation may be incorrect
## 
##  Pearson's Chi-squared test
## 
## data:  ftable2
## X-squared = 0, df = 1, p-value = 1
obsfreq <- matrix(c(10,20, 5,10,20,40),nrow=2,ncol=3)

chisq.test(obsfreq)
## 
##  Pearson's Chi-squared test
## 
## data:  obsfreq
## X-squared = 0, df = 2, p-value = 1
obsfreq <- matrix(c(20,30, 5,10,40,40),nrow=2,ncol=3)
obsfreq
##      [,1] [,2] [,3]
## [1,]   20    5   40
## [2,]   30   10   40
chisq.test(obsfreq)
## 
##  Pearson's Chi-squared test
## 
## data:  obsfreq
## X-squared = 2.1378, df = 2, p-value = 0.3434
data(iris)
head(iris)
chisq.test(x = iris$Petal.Width, y = iris$Species)
## Warning in chisq.test(x = iris$Petal.Width, y = iris$Species): Chi-squared
## approximation may be incorrect
## 
##  Pearson's Chi-squared test
## 
## data:  iris$Petal.Width and iris$Species
## X-squared = 271.75, df = 42, p-value < 2.2e-16

Wilcoxon Rank Sum Test

placebo <- c(7,5,6,4,12)
new_drug <- c(3,6,4,2,1 )

boxplot(list(placebo,new_drug))

wilcox.test(placebo, new_drug)
## Warning in wilcox.test.default(placebo, new_drug): cannot compute exact p-value
## with ties
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  placebo and new_drug
## W = 22, p-value = 0.05855
## alternative hypothesis: true location shift is not equal to 0
placebo <- c(6,7,8,9,10)
new_drug <- c(1,2,3,4,5 )
wilcox.test(placebo, new_drug)
## 
##  Wilcoxon rank sum exact test
## 
## data:  placebo and new_drug
## W = 25, p-value = 0.007937
## alternative hypothesis: true location shift is not equal to 0
placebo <- c(2,4,6,8,10)
new_drug <- c(1,3,5,7,9 )
wilcox.test(placebo, new_drug)
## 
##  Wilcoxon rank sum exact test
## 
## data:  placebo and new_drug
## W = 15, p-value = 0.6905
## alternative hypothesis: true location shift is not equal to 0
placebo <- c(7,8,9,10,11,12)
new_drug <- c(1,2,3,4,5,6)
wilcox.test(placebo, new_drug)
## 
##  Wilcoxon rank sum exact test
## 
## data:  placebo and new_drug
## W = 36, p-value = 0.002165
## alternative hypothesis: true location shift is not equal to 0
binom.test(2, 8, alternative = 'less')
## 
##  Exact binomial test
## 
## data:  2 and 8
## number of successes = 2, number of trials = 8, p-value = 0.1445
## alternative hypothesis: true probability of success is less than 0.5
## 95 percent confidence interval:
##  0.0000000 0.5996894
## sample estimates:
## probability of success 
##                   0.25
before <- c(85,70,40,65,80,75,55,20)
after <- c(75,50,50,40,20,65,40,25)
wilcox.test(before, after,paired=TRUE)
## Warning in wilcox.test.default(before, after, paired = TRUE): cannot compute
## exact p-value with ties
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  before and after
## V = 32, p-value = 0.05747
## alternative hypothesis: true location shift is not equal to 0
albumin <- c(3.1, 2.6, 2.9, 
             3.8, 4.1, 2.9, 3.4,4.2,
             4,5.5, 5, 4.8)
group <- c(1 ,1 ,1 ,
           2 ,2 ,2 ,2 ,2 ,
           3 ,3 ,3 ,3)
boxplot(albumin ~ group)

kruskal.test(albumin~ group)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  albumin by group
## Kruskal-Wallis chi-squared = 7.5495, df = 2, p-value = 0.02294

ANOVA

wt1 <- c(8,9,6,7,3)
wt2 <- c(2,4,3,5,1)
wt3 <- c(3,5,4,2,3)
wt4 <- c(2,2,-1,0,3)
weights <- c(wt1, wt2, wt3, wt4)
group   <- c(rep(1,5),rep(2,5),rep(3,5),rep(4,5))

rep(4,5)
## [1] 4 4 4 4 4
boxplot(weights ~ group )

oneway.test(weights ~ group , var.equal = TRUE)
## 
##  One-way analysis of means
## 
## data:  weights and group
## F = 8.5593, num df = 3, denom df = 16, p-value = 0.001278
MaleA <- c(22,25,26,27,24)
FemaleA <- c(21,19,18,24,25)

MaleB <- c(14, 17, 19, 20, 17)
FemaleB <- c(21, 20 , 23, 27, 25)

MaleC <- c(15, 16, 19, 14, 12)
FemaleC <- c(37, 34, 36, 26, 29)

timespan <- c(MaleA, FemaleA, MaleB, FemaleB, MaleC, FemaleC)

sex <- c(rep('Male', 5),rep('Female', 5),rep('Male', 5),
rep('Female', 5),rep('Male', 5),rep('Female', 5))

treatment <- c(rep('A',10),rep('B',10),rep('C',10))


#treatment
boxplot(timespan ~ sex)

boxplot(timespan ~ treatment)

par(mfrow=c(1,2))
boxplot(timespan ~ sex)
boxplot(timespan ~ treatment)

anova_res <- aov(timespan~factor(sex)*factor(treatment))
summary(anova_res)
##                               Df Sum Sq Mean Sq F value   Pr(>F)    
## factor(sex)                    1  320.1   320.1  34.609 4.55e-06 ***
## factor(treatment)              2   68.6    34.3   3.708   0.0395 *  
## factor(sex):factor(treatment)  2  532.5   266.2  28.782 4.21e-07 ***
## Residuals                     24  222.0     9.3                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1