Statistics Assignment 1

Name: Hugh Richardson

ID Number: 24002972

Use the following R code to load and examine the structure of the dataset.

lambs <- read.csv("C:/Users/Naomi/Desktop/LambBehavour.csv", header=TRUE)

str(lambs)
## 'data.frame':    355 obs. of  6 variables:
##  $ lamb_tag       : int  321 322 315 316 559 162 163 405 406 336 ...
##  $ ewe_tag        : int  1 1 2 2 3 4 4 9 9 10 ...
##  $ Lamb_sex       : chr  "Ewe" "Ewe" "Ram" "Ewe" ...
##  $ time_to_contact: int  18 41 21 16 14 18 18 23 23 16 ...
##  $ time_to_follow : int  108 107 110 110 295 60 60 131 131 258 ...
##  $ contact        : chr  "Both moved" "Both moved" "Ewe to lamb" "Ewe to lamb" ...

EXERCISE 1: Data Summary [total: 10 Marks]

  1. What type of data is each of the variables; lamb_tag, Lamb_sex, time_to_follow and contact? [2 Marks]

lamb_tag=numerical; Lamb_sex=character; time_to_follow=numerical; contact=character

  1. How many male and female lambs were measured? [2 marks] Ewe=180; Ram=175
table(lambs$Lamb_sex)
## 
## Ewe Ram 
## 180 175
  1. What are the mean, median, and standard deviation for the variable time_to_contact? [3 Marks]

mean=38.34085; median=25; standard deviation=39.266

mean(lambs$time_to_contact)
## [1] 38.34085
median(lambs$time_to_contact)
## [1] 25
sd(lambs$time_to_contact)
## [1] 39.266
  1. Produce a histogram and a boxplot of time_to_contact. Make sure you label the axes appropriately. Comment on the shape of the distribution. [3 marks] The distribution is right-skewed
hist(lambs$time_to_contact,
     main="Histogram of 'time to contact'",
     xlab="time to contact")

boxplot(lambs$time_to_contact,
        main="Boxplot of 'time to contact'",
        ylab="time to contact")

EXERCISE 2: Sex and time to contact [total: 13 Marks]

  1. Produce side-by-side boxplots of time_to_contact for male and female lambs. Compare the two distributions. [3 marks]

The distributions of both sexs are similar, with a right skew on time to contact. There is a slightly larger range in the “ewe” group.

boxplot(time_to_contact ~ Lamb_sex, 
        data = lambs, 
        main = "Time to Contact by Lamb Sex", 
        xlab = "Lamb Sex",
        ylab = "Time to Contact")

  1. Calculate the standard deviation for time_to_contact for each sex of the lambs. [1 mark]

Ram=37.289; Ewe=41.12225

sd(lambs$time_to_contact[lambs$Lamb_sex=="Ram"])
## [1] 37.289
sd(lambs$time_to_contact[lambs$Lamb_sex=="Ewe"])
## [1] 41.12225
  1. Could the two population standard deviations be considered equal? Justify your answer. [2 marks]

Yes, the two population standard deviations could be considered equal. The ratio between the two standard deviations is 1.102798. This ratio is <2.0, so equal variances may be assumed for testing of the sample means.

sd(lambs$time_to_contact[lambs$Lamb_sex=="Ewe"])/
  sd(lambs$time_to_contact[lambs$Lamb_sex=="Ram"])
## [1] 1.102798
  1. Perform a t-test to see whether there is any evidence for a difference in the average time_to_contact of male and female lambs. Remember to check the conditions for the t-test before you perform it. Use 5% as the level of significance.

Null hypothesis: There is no difference in the true mean time_to_contact between male and female lambs. Alternative hypothesis: There is a difference in the true mean time_to_contact between male and female lambs. * What is the p-value?

t.test(time_to_contact ~ Lamb_sex, data=lambs, var.equal=T, conf.level=0.95)
## 
##  Two Sample t-test
## 
## data:  time_to_contact by Lamb_sex
## t = 0.86931, df = 353, p-value = 0.3853
## alternative hypothesis: true difference in means between group Ewe and group Ram is not equal to 0
## 95 percent confidence interval:
##  -4.576047 11.825888
## sample estimates:
## mean in group Ewe mean in group Ram 
##          40.12778          36.50286

p-value=0.3853 * What is your decision? with justification.

A p-value of 0.3853 is insufficient to reject the null hypothesis.

The statistical test result suggests there is no evidence for a difference in the average time_to_contact of male and female lambs. The difference observed in mean time_to_contact between male and female lambs is likely a result of sampling from the population and does not reflect true differences in the population.

  1. Explain how the 95% confidence interval in the test output, backs-up the conclusion for this hypothesis test. [1 mark]

The 95% confidence interval has a range that contains “0”. This means that the 95% confidence of the true difference between both groups may be a 0 value, which aligns with the null hypothesis.

  1. State the two assumptions for confidence intervals and hypothesis tests for means. Comment on the validity of these assumptions for the test you have just done. [2 marks]

Assumption 1 - Sample mean is approximately normally distributed. Assumption 2 - Sample is representative of the population of interest. The sample size was sufficiently large (n>30),meaning the central limit theorem holds and the sampling distribution of the mean may be considered to be approximately normal. We may consider the second assumption to be valid given that each data point was sampled randomly from the population of interest.

EXERCISE 3: time_to_contact, time_to_follow and contact [total: 5 Marks]

In this exercise you will explore the relationship between time_to_contact, time_to_follow and contact.

  1. Produce a scatter plot with time_to_contact on the vertical axis and time_to_follow on the horizontal axis. Use different plotting characters and/or colours to distinguish between the three different contact directions. Remember to include a legend. [3 marks]
plot(time_to_contact~time_to_follow, data=lambs,pch=5)
  points(lambs$time_to_follow[lambs$contact == "Both moved"],
       lambs$time_to_contact[lambs$contact == "Both moved"],
       col = "blue", pch = 5)

  points(lambs$time_to_follow[lambs$contact == "Lamb to ewe"],
       lambs$time_to_contact[lambs$contact == "Lamb to ewe"],
       col = "red", pch = 5)

  points(lambs$time_to_follow[lambs$contact == "Ewe to lamb"],
       lambs$time_to_contact[lambs$contact == "Ewe to lamb"],
       col = "green", pch = 5)

  legend(
    "topleft",
    legend=c("Both moved","Lamb to ewe", "Ewe to lamb"),
    col=c("blue","red","green"),
    pch=c(5,5,5),
    title="contact")

  title("scatter plot of 'time to contact' and 'time to follow' in lambs ")

  1. With reference to the scatter plot you have just created, describe the effect time_to_follow and contact direction have on the time_to_contact. [2 marks]

time_to_follow has a positive correlation with time_to_contact. This positive relationship is similarly observed when broken down into the different “contact” variables. However, the strength of the correlation between time_to_contact and time_to_follow is quite weak (0.43). When looking at this correlation at the different levels of “contact”, the “Ewe to lamb” variable has the weakest positive correlation of 0.40. This increases for “Both moved”, which has a correlation coefficient of 0.68, but is highest in “Lamb to ewe”, where the correlation coefficient is 0.81.

cor(
    lambs$time_to_contact,
    lambs$time_to_follow)
## [1] 0.4344611
cor(
    lambs$time_to_contact[lambs$contact=="Both moved"],
    lambs$time_to_follow[lambs$contact=="Both moved"]
)
## [1] 0.6767942
cor(
  lambs$time_to_contact[lambs$contact=="Ewe to lamb"],
  lambs$time_to_follow[lambs$contact=="Ewe to lamb"]
)
## [1] 0.3961282
cor(
  lambs$time_to_contact[lambs$contact=="Lamb to ewe"],
  lambs$time_to_follow[lambs$contact=="Lamb to ewe"]
)
## [1] 0.8058358

EXERCISE 4: Sex of the lamb and direction of contact [total: 8 Marks]

  1. Construct a table and side-by-side barplot to assess the relationship between direction of contact and the sex of the lamb.
my.table1 <- table(lambs$Lamb_sex,lambs$contact)
my.table1
##      
##       Both moved Ewe to lamb Lamb to ewe
##   Ewe         40         132           8
##   Ram         42         123          10
barplot(my.table1,beside=TRUE)

?barplot
## starting httpd help server ... done

The table and barplot suggest that there is no effect of the sex of the lamb on the direction of contact as there appears to be negligible differences between the sex at all levels of “contact”.

  1. Perform a \(\chi^2\) test for independence on this table. Use a 5% level of significance.
my.ChiSq1 <- chisq.test(lambs$Lamb_sex,lambs$contact)
my.ChiSq1
## 
##  Pearson's Chi-squared test
## 
## data:  lambs$Lamb_sex and lambs$contact
## X-squared = 0.51833, df = 2, p-value = 0.7717

Null hypothesis: direction of contact is independent of the sex of the lamb Alternative hypothesis: direction of contact is dependent on sex of the lamb

0.7717
## [1] 0.7717

There is insufficient evidence to reject the null hypothesis, given a p-value>0.05 (0.7717).

There is no impact of sex of lamb on the direction of contact. As a result, sex of the lamb need not be a consideration when evaluating the direction of contact exhibited by lambs.

  1. All of th expected values must be at least 5.
my.ChiSq1$expected
##               lambs$contact
## lambs$Lamb_sex Both moved Ewe to lamb Lamb to ewe
##            Ewe   41.57746    129.2958    9.126761
##            Ram   40.42254    125.7042    8.873239

We can assume the first assumption to be met, as the lowest expected value is 8.9.

  1. Sample must be representative of the population of interest. We can assume this condition to be met because random sampling was performed to obtain the sample dataset.