### Task 1
mean(people$height)
# [1] 179.2391

var(people$height)
# [1] 81.36067

sqrt(var(people$height))

sd(people$height)
# [1] 9.020015


tapply(people$height, list(people$eye.color, people$sex),
       mean)
#              F        M
# blue  173.5000 180.3333
# brown 175.8571 185.9167

# tapply produces a table, 
#while  with aggregate, you can make a nice dataframe, especially if
# the column names are adjusted

cat.means<-aggregate(people$height, list(people$eye.color, people$sex),
       mean)
names(cat.means)<-names(people)
names(cat.means)[3]<-"mean.height"
cat.means


cat.vars<-aggregate(people$height, list(people$eye.color, people$sex),
                     var)
cat.vars
#   Group.1 Group.2         x
# 1    blue       F 113.66667
# 2   brown       F  53.14286
# 3    blue       M  54.66667
# 4   brown       M  74.04167


cat.sd<-aggregate(people$height, list(people$eye.color, people$sex),
                    sd)
cat.sd
#   Group.1 Group.2         x
# 1    blue       F 10.661457
# 2   brown       F  7.289915
# 3    blue       M  7.393691
# 4   brown       M  8.604747



install.packages("sciplot") # the library sciplot is needed for the se function
library(sciplot)

cat.se<-aggregate(people$height, list(people$eye.color, people$sex),
                  se)
cat.se

# alternatrively, a new function, can be defined. 
std.err <- function(x) sd(x)/sqrt(length(x))
cat.se2<-aggregate(people$height, list(people$eye.color, people$sex),
                  std.err)
cat.se2

# Task 2
plants<-rnorm(n = 10, mean = 25.5, sd=2.4)# generation of the sample


?rnorm

mean(plants)
se(plants)

25.5+2*se(plants)
25.5-2*se(plants)

sd(plants)

plants.2<-plants # Copying the plants vector to another vector called plants.2
plants.2[2]<-plants[2]*10 # Replacing the second value of plants.2 by itt value multiplied by 10


#Comparison of means
mean(plants)
mean(plants.2)

hist(plants.2)

#Comparison of medians
median(plants)
median(plants.2)

#Trying the same for a 10x larger sample
plants<-rnorm(n=100, mean=25.5, sd=2.4)
plants.2<-plants
plants.2[2]<-plants[2]*10


se(plants)

mean(plants)
mean(plants.2)



###Task 3

# likelihood 
1/2*1/2*1/2
# 0.125

# probability - guess


#Task A

pnorm(q=100, mean=81, sd=9, lower.tail=F)
# [1] 0.01738138

#Task B
pnorm(q=50, mean=81, sd=9, lower.tail=T)
# [1] 0.0002861171


#Task C
#lower limit
qnorm(p=0.005, mean=179, sd=11, lower.tail=T)
# [1] 150.6659

#upper limit
qnorm(p=0.005, mean=179, sd=11, lower.tail=F)
# [1] 207.3341

# Task D
qnorm(p=0.95, mean=179, sd=11, lower.tail=T)
# [1] 197.0934

# Task E
pnorm(q=200, mean=179, sd=11, lower.tail=F)*550
 # 15.46885

# Task F
1-
pnorm(q=170, mean=179, sd=11, lower.tail=T)-
pnorm(q=190, mean=179, sd=11, lower.tail=F)
# [1] 0.6347181

# Task G

qnorm(p=0.9, mean=40, sd=sqrt(6), lower.tail = T)
# [1] 43.13915
