###Practicals 7
#Task 1
people<-read.delim2("clipboard")
summary(people)

means<-aggregate(people$height, list(people$sex, people$eye.color), mean)
x.coords<-barplot(means$x, ylim=c(0,200))

library(sciplot)
ses<-aggregate(people$height, list(people$sex, people$eye.color), se)
ses

dfs<-aggregate(people$height, list(people$sex, people$eye.color), length)
dfs$x<-dfs$x-1

CI.low<-means$x+ses$x*qt(0.025,dfs$x, lower.tail=T)
CI.upper<-means$x+ses$x*qt(0.025,dfs$x, lower.tail=F)

arrows(x0=x.coords, y0=CI.low, y1=CI.upper, angle=90, code=3)

##Task 2

ran<-data.frame(auri=c(5,6,4,6,5,3), nemor=c(7,8,9,6,8,10))
ran

var.test(ran$auri, ran$nemor)
# F test to compare two variances
# 
# data:  ran$auri and ran$nemor
# F = 0.68333, num df = 5, denom df = 5, p-value = 0.6863
# alternative hypothesis: true ratio of variances is not equal to 1
# 95 percent confidence interval:
#   0.09561948 4.88336092
# sample estimates:
#   ratio of variances 
# 0.6833333 

t.test(ran$auri, ran$nemor)
# Welch Two Sample t-test
# 
# data:  ran$auri and ran$nemor
# t = -4.2274, df = 9.6582, p-value = 0.001892
# alternative hypothesis: true difference in means is not equal to 0
# 95 percent confidence interval:
#   -4.843747 -1.489586
# sample estimates:
#   mean of x mean of y 
# 4.833333  8.000000 


means<-apply(ran,2,mean)
x.coords<-barplot(means, ylim=c(0,10))

ses<-apply(ran,2,se)
dfs<-apply(ran,2,length)-1

CI.lower=means+ses*qt(0.025,dfs,lower.tail = T)
CI.upper=means+ses*qt(0.025,dfs,lower.tail = F)
arrows(x0=x.coords, y0=CI.lower, y1=CI.upper, angle=90, code=3)

boxplot(ran$auri, ran$nemor)

## Task 3
fert<-data.frame(block=1:5, F=c(23,25,36, 19, 32), NF=c(20,24,33,18,21))

t.test(fert$F, fert$NF, paired = T)
# Paired t-test
# 
# data:  fert$F and fert$NF
# t = 2.0488, df = 4, p-value = 0.1098
# alternative hypothesis: true difference in means is not equal to 0
# 95 percent confidence interval:
#   -1.349539  8.949539
# sample estimates:
#   mean of the differences 
# 3.8 


t.test(fert$F-fert$NF)
# One Sample t-test
# 
# data:  fert$F - fert$NF
# t = 2.0488, df = 4, p-value = 0.1098
# alternative hypothesis: true mean is not equal to 0
# 95 percent confidence interval:
#   -1.349539  8.949539
# sample estimates:
#   mean of x 
# 3.8 


