## Non-parametric Test

set.seed(123)
population <- rnorm(100)
hist(population)

boxplot(population)

set.seed(123)
populations2 <- rpois(100, lambda = 1)
hist(populations2)

boxplot(populations2)

## Mann Whitney U Test

placebo  <- c(7,5,6,4,12)
new_drug <- c(3,6,4,2,1 )
wilcox.test(placebo, new_drug, alternative = 'greater')
## Warning in wilcox.test.default(placebo, new_drug, alternative = "greater"):
## cannot compute exact p-value with ties
##
##  Wilcoxon rank sum test with continuity correction
##
## data:  placebo and new_drug
## W = 22, p-value = 0.02928
## alternative hypothesis: true location shift is greater than 0

## Sign Test & Wilcoxon Signed Rank Test

binom.test(0, 8, alternative = 'less')
##
##  Exact binomial test
##
## data:  0 and 8
## number of successes = 0, number of trials = 8, p-value = 0.003906
## alternative hypothesis: true probability of success is less than 0.5
## 95 percent confidence interval:
##  0.000000 0.312344
## sample estimates:
## probability of success
##                      0
# 0.003906 << 0.05
# Reject H0
# The treatment is effective

binom.test(4, 8, alternative = 'less')
##
##  Exact binomial test
##
## data:  4 and 8
## number of successes = 4, number of trials = 8, p-value = 0.6367
## alternative hypothesis: true probability of success is less than 0.5
## 95 percent confidence interval:
##  0.0000000 0.8070971
## sample estimates:
## probability of success
##                    0.5
# 0.6367 > 0.05
# Reject H1
# The treatment is not effective

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
# 0.1445 > 0.05
# Reject H1
# The treatment is not effective

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
# p= 0.05747 > 0.05
# reject H1
# treatment is not effective

## K-W Test

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
## 0.02294 < 0.5
## Reject H0
##

## 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)
weights
##  [1]  8  9  6  7  3  2  4  3  5  1  3  5  4  2  3  2  2 -1  0  3
group <- c(rep(1,5),rep(2,5),rep(3,5),rep(4,5))
group
##  [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
boxplot(weights ~ group)
abline(h = mean(wt1), col='red' )
abline(h = mean(wt2), col='blue' )
abline(h = mean(wt3), col='orange' )
abline(h = mean(wt4), col='green' )

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
a <- aov(weights ~ group)
summary(a)
##             Df Sum Sq Mean Sq F value   Pr(>F)
## group        1  62.41   62.41   18.56 0.000424 ***
## Residuals   18  60.54    3.36
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
?aov
## starting httpd help server ... done
?oneway.test

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))

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