### Practicals #2
# Task 1
library(readxl)# loads the package readxl
people<-read_excel("people.xlsx")# imports the people dataset from excel file into a tibble called people 

mean(people$height)# Overall mean 
# [1] 179.2391
var(people$height) # Overall variance
# [1] 81.36067
sd(people$height) # Standard deviation
# [1] 9.020015

means.agg<-aggregate(people$height, list(people$eye.color, people$sex),
          mean)# Computing of the mean for categories defined by the color and sex variables
# This returdns a data frame, where the values are saved together with the categorizing variables
names(means.agg)<-names(people)#Renaming the column names in aggregated data

means.agg
#   eye.color sex   height
# 1      blue   F 173.5000
# 2     brown   F 175.8571
# 3      blue   M 180.3333
# 4     brown   M 185.9167

# For variance, the approach is identical, just the function var is used in aggregate instead of mean
var.agg<-aggregate(people$height, list(people$eye.color, people$sex),
          var)
names(var.agg)<-c(names(people)[1:2], "height.variance")#Renaming the column names in aggregated data
var.agg
#   eye.color sex height.variance
# 1      blue   F       113.66667
# 2     brown   F        53.14286
# 3      blue   M        54.66667
# 4     brown   M        74.04167

sqrt(var.agg$height.variance) #Standard deviations = square roots of variances
# [1] 10.661457  7.289915  7.393691  8.604747

# SDs may also be added to the var.agg data frame
var.agg$sd<-sqrt(var.agg$height.variance)

var.agg
#   eye.color sex height.variance        sd
# 1      blue   F       113.66667 10.661457
# 2     brown   F        53.14286  7.289915
# 3      blue   M        54.66667  7.393691
# 4     brown   M        74.04167  8.604747

# Standard errors of the mean - the situation is a bit complicated by missing function in base R
# The corresponding function in available in the package sciplot, though
install.packages("sciplot")#Installing package sciplot which contains the se function
library(sciplot)#loading the sciplot package

# Subsequent steps are the same as with mean and var
se.agg<-aggregate(people$height, list(people$eye.color, people$sex),
          se)
names(se.agg)<-c(names(people)[1:2], "height.se")
se.agg
#   eye.color sex height.se
# 1      blue   F  5.330729
# 2     brown   F  2.755329
# 3      blue   M  3.018462
# 4     brown   M  3.512873


# Task 2
sam<-rnorm(n=10, mean=25.5, sd=2.4) # Generation of the sample 
mean(sam) # Sample mean

var(sam) #Variance 

se(sam) # Standard error of the mean

median(sam) # Median

sam[4]<-sam[4]*10 #Multiplication of the fourth number in vector sam by 10

# recomputed values of the mean and se
mean(sam)
var(sam)
se(sam)

median(sam)

# Repeating task 2 with n = 100
sam<-rnorm(100, mean=25.5, sd=2.4)
mean(sam)
var(sam)
se(sam)

sam[4]<-sam[4]*10
mean(sam)
var(sam)
se(sam)

# Task 4
# a
pnorm(q=100, mean=81, sd=9, lower.tail = F) # Uses a normal distribution with m = 81 and sd =9
# to compute the probability of a value (quantile) being higher than 100 
# lower.tail needs to be set to F, becuase we arte interested in the upper tail

# This is the result:
# 0.01738138

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

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

qnorm(p=0.1, mean=40, sd=sqrt(6), lower.tail = F)
# Here, we use the qnorm function which computes quantiles (i.e. values) from known probabilities
# We want to have 90% probability to catch the train, so we need to include 90% of the distribution to the 
# the interval. It is importnat to keet in mind that the longer we leave before the train departure
# the higher is the probability to catch it.
