###Practicals 5
#Task 1
# Null hypothesis is that the bees do not prefer any color. Hence, the probabilities of
# visitation of flowers of different colors should be equal. We have 4 colors, which makes
# p = 0.25 for each.

?chisq.test
test.res<-chisq.test(c(10,25,18,6), p = rep(0.25, 4))

test.res
# Chi-squared test for given probabilities
# 
# data:  c(10, 25, 18, 6)
# X-squared = 14.559, df = 3, p-value = 0.002235

## Conclusion: There is a significant difference in bee preference for different colors 
# Note, that we cannot directly test which color is preferred and which avoided.
# Nevertheless, we can display the difference between observed and expected values, to
# see excess or deficiency of visits in comparison to null hypothesis
test.res$observed-test.res$expected
# [1] -4.75 10.25  3.25 -8.75

# This shows that yellow flowers were most preferred while green most avoided.

#Task 2

## Null hypothesis is based on the Mendelian rule. Thus, we expect the ratio 9:3:3:1, which
# must be scaled to sum up to 1 to obtain probabilities.

chisq.test(c(125,60,50,12), p = c(9/16, 3/16, 3/16, 1/16))
# Chi-squared test for given probabilities
# 
# data:  c(125, 60, 50, 12)
# X-squared = 6.5025, df = 3, p-value = 0.08956

# Conclusion: The observed numbers of offspring do not differ significantly from expectations
# based on the Mendelian rules.

#Task 3
## Null hypothesis states that that the number of prey is proportional to the 
# prey abundance 
# Total pry abundance:
sum(c(1200, 200,650, 200 ))
# 2250

chisq.test(c(41,5,30,2), p = c(1200, 200,650, 200)/2250)
# 	Chi-squared test for given probabilities
# 
# data:  c(41, 5, 30, 2)
# X-squared = 6.5322, df = 3, p-value = 0.0884

## Conclusion: No significant preference for the analyzed prey types was identified in 
# the lizards. 

## Note here, that from the pragmatic point of view, the non-significant 
# result is likely caused by a low number of observations. The researchers should therefore
# repeat the sampling and analyze the lizard feeding habits over a larger area or over a longer
# period to get more data.


#Task 4
pchisq(5.04, df=3, lower.tail = F )
# 0.1688915

# alternatively
1-pchisq(5.04, df=3, lower.tail = T )

#Task 5
qchisq(0.05, 3, lower.tail = F)
# [1] 7.814728
qchisq(0.01, 3, lower.tail = F)
# [1] 11.34487

# Note that a higher value of chi-sq. is needed to achieve lower type I error.

qchisq(0.05, 8, lower.tail = F)
# [1] 15.50731
qchisq(0.01, 8, lower.tail = F)
# [1] 20.09024

# Note here that a higher value of chi-sq. is needed to acheve the same type I error as
# in tests with lower df.

#Task 6

# Null hypothesis is rejected without any computation. If you observe something which was not
# expected by the null hypothesis (i.e. null hypothesis prohibited such observation), null hypothesis
# is rejected. Note here, that this does not apply vice versa - if an observation is expected under 
# null hypothesis but not observed, the computation needs to be done because such situation may 
# happen by chance.