bears <- read.csv(“bears.csv”,header=TRUE,stringsAsFactors=TRUE) str(bears)

bears <- read.csv("bears.csv")

#’1. What type of data is each of the variables; Weight, Sex, Month and Age? [2 Marks]

Weight-Integer Month-Integer Age-Integer Sex-Factor

#’2. How many male and female bears were measured? [1 mark] There are 35 females and 65 males

#’3. How many bears were measured each month? [1 Mark] # No missingness observed in the data therefore a 100 entries would indicate 100 bears

#’4. Produce a histogram and a boxplot of Weight. Make sure you label the axes appropriately. Comment on the shape of the distribution.[3 marks]

A minor right skew with some signs of bimodality observed. It can be explored further by plotting the variables individually.

#’

str(bears)
## 'data.frame':    100 obs. of  10 variables:
##  $ ID     : int  301 601 394 238 655 598 39 545 638 41 ...
##  $ Age    : int  87 17 26 70 8 54 19 45 33 29 ...
##  $ Month  : int  8 5 6 9 8 11 7 9 8 5 ...
##  $ Sex    : chr  "M" "M" "M" "M" ...
##  $ Head.L : int  41 29 30 38 25 38 25 33 30 30 ...
##  $ Head.W : int  23 13 21 17 11 18 13 17 15 15 ...
##  $ Neck.G : int  80 43 47 71 25 67 38 53 48 46 ...
##  $ Length : int  190 135 153 198 110 175 114 152 136 157 ...
##  $ Chest.G: int  119 77 81 114 61 118 58 88 81 79 ...
##  $ Weight : int  159 51 55 151 13 134 30 83 56 55 ...
bears$Sex <- (bears$Sex)
summary(bears$Sex)
##    Length     Class      Mode 
##       100 character character
bears$Month <- as.factor(bears$Month)
summary(bears$Month)
##  4  5  6  7  8  9 10 11 
##  6  7  6  9 26 20 13 13
barplot(summary(bears$Month))

hist(bears$Weight,
     main="Bear weight Histogram",#titile
     xlab="weight(kg)", #x axis
     ylab= "Frequency" # y axis
)

boxplot(bears$Weight,
     main="Bear weight Boxplot",#titile
     ylab="weight(kg)")

EXERCISE 2: Sex and Weight [total: 10 Marks]

#’ #’1. Produce a histogram of the weights for the male bears. Describe the shape of the distribution. [1 mark]

#’2. Produce a histogram of the weights for the female bears. Describe the shape of the distribution. [1 mark]

#’3. Produce side-by-side boxplots of weight for male and female bears. Compare the two distributions. [2 marks]

A skew still present for males and mostly a symmetrical distribution for female bears.

male_bears <- subset(bears, Sex == "M")

hist(male_bears$Weight,
     main= "Histogram of weight for Male Bears",
     xlab= "Weight (kg",
     ylab= " Frequency"
)

female_bears <- subset(bears, Sex == "F")

hist(female_bears$Weight,
     main= "Histogram of weight for Female Bears",
     xlab= "Weight (kg",
     ylab= " Frequency"
)

boxplot(Weight ~ Sex,
        data = bears,
        main = "Boxplot of Weight by Gender",
        xlab = "Gender",
        ylab = "Weight (kg)"
)

#’4. Perform a t-test to see whether there is any evidence for a difference in the average weight of male and female black bears.

#’ * What are the hypotheses?

The null would be there is no difference between the two genders. The t-test shall indicate a 95% confidence level thus depicting whether the null hypothesis can be rejected or not.

#’ * What is the p-value?

p-value = 0.0001115

#’ * What is your conclusion? Explain. [3 marks]

The null hypothesis can be rejected.There is a statistical significance between the weight of male and female bears at 0.05 significance levels as the p value obtained is 0.000115. Since the p-value obtained is 0.00115,therefore,if there were truly no difference in the average weight between male and female bears.

#’5. Explain how the 95% confidence interval adds to the conclusion for this hypothesis test. [1 mark]

The 95% confidence interval allows a range of values within which the true difference in means between male bears and female bears lie.For this particular case,the 95% confidence interval for the difference in means ranges from; 95 percent confidence interval: -51.43949 -17.48359 Even if the sampling process is repeated,approximately 95% of those intervals would contain the true difference in means. The difference in means can be depicted as statistically significant since the interval does not include zero. All in all, the 95% confidence interval reinforces the conclusion drawn from the p-value.

#’6. 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]

Assumptions

Independence-to ensure the observations are independent of each other-in this case any relations seems unlikely

Normality-both groups do have a normal distribution with some skew for male a a few outliers for the female groups. This is needed to ensure the validity of the results.

t.test(female_bears$Weight,male_bears$Weight)
## 
##  Welch Two Sample t-test
## 
## data:  female_bears$Weight and male_bears$Weight
## t = -4.0285, df = 97.086, p-value = 0.0001115
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -51.43949 -17.48359
## sample estimates:
## mean of x mean of y 
##   67.4000  101.8615

EXERCISE 3: Age, Sex and Weight [total: 4 Marks] #’ #‘It might be expected that weight is related to the age and sex of the bear. #’ #’1. Produce a scatterplot showing the relationship between the weight and age of the bears. Use different plotting characters and/or colours to distinguish between the males and females. [2 marks]

plot(bears$Weight, bears$Age,
     col= ifelse(bears$Sex == "M", "blue", "red"),
     pch= ifelse(bears$Sex == "M", 16,17),
     xlab= "Weight", ylab = "Age",
     main = "Scatterplot of Weight vs Age by Gender")
     legend("topright",legend = c("Male", "Female"),col = c("blue", "red"),pch =  c(16,17),title = "Gender")

plot(bears$Age,bears$Weight,
     col= ifelse(bears$Sex == "M", "blue", "red"),
     pch= ifelse(bears$Sex == "M", 16,17),
     xlab= "Age", ylab = "Weight",
     main = "Scatterplot of  Age vs Weight by Gender")
     legend("topright",legend = c("Male", "Female"),col = c("blue", "red"),pch =  c(16,17),title = "Gender")

#’2. With reference to the scatterplot you have just created, describe the effect age and sex have on the weight of the bears. [2 marks]

The second plot is clearer (that is age vs weight by gender),it is seen that female bears do not gain weight at a slower rate compared to the male bears and do not reach the same heavier weights with increase in age.(Note:outliers observed however this could be an input error) An increase in weight is observed with age for both groups however for female bears, this is mostly observed after a certain age (approx 70kg)

EXERCISE 4: Month and Weight [total: 10 Marks] #’ #’1. Produce a set of side-by-side boxplots on the same graph showing the weights of the bears surveyed each month. Compare the distribution of weights across the months. [2 marks]

boxplot(Weight ~ Month,
        data = bears,
        main = "Boxplot of Weight by Month",
        xlab = "Month",
        ylab = "Weight (kg)"
        
)

#’2. Produce a 95% confidence interval for the average weight of the bears in the month with the smallest median weight. Interpret the confidence interval. [2 marks]

#95 percent confidence interval: 29.30673 77.26470 95% confident that average mean weight for this month falls within this

# finding the smallest median month 
smallest_median_month <- names(sort(tapply(bears$Weight, bears$Month, median))[1])

#creating a subset
smallest_median_month_data <- subset(bears, Month == smallest_median_month)

#calculating the CI
t.test(smallest_median_month_data$Weight)
## 
##  One Sample t-test
## 
## data:  smallest_median_month_data$Weight
## t = 5.4375, df = 6, p-value = 0.001606
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  29.30673 77.26470
## sample estimates:
## mean of x 
##  53.28571

#’3. Produce a 95% confidence interval for the average weight of the bears in the month with the largest median weight. Interpret the confidence interval. [2 marks]

#95 percent confidence interval: 83.21942 140.01135

95% confident that average mean weight for this month falls within this

# finding the largest median month
largest_median_month <- names(sort(tapply(bears$Weight, bears$Month, median), decreasing = TRUE)[1])

#creating a subset
largest_median_month_data <- subset(bears, Month == largest_median_month)

#calculating the CI
t.test(largest_median_month_data$Weight)
## 
##  One Sample t-test
## 
## data:  largest_median_month_data$Weight
## t = 8.5642, df = 12, p-value = 1.859e-06
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##   83.21942 140.01135
## sample estimates:
## mean of x 
##  111.6154

#’4. State the two assumptions for constructing confidence intervals. Comment on the validity of these assumptions for the confidence intervals you have just produced. [2 marks]

The distribution of the values in the population is normally distributed The values in the population are independent from each other

#’5. Based ONLY on your confidence intervals, is there evidence of a difference in the average weight of the bears measured in those two months? Explain. [2 marks]

The confidence interval for the average weight of bears in these two months do not overlap,it suggests that there may indeed be a difference in the average weight of bears measured in those two months.However,to make a definitive conclusion,a formal statistical hypothesis test would need to be conducted to determine if the difference is statistically significant

EXERCISE 5: Chi-Square [5 Marks] #’ #‘1. Construct a table to assess whether the month the bear is observed depends on the sex of the bear. #’ * What does this table suggest about the relationship between sex and month? [2 Marks]

table(bears$Month,bears$Sex)
##     
##       F  M
##   4   4  2
##   5   4  3
##   6   1  5
##   7   1  8
##   8  12 14
##   9   6 14
##   10  4  9
##   11  3 10

*August observed the highest number of both genders sightings. More females observed in the earlier months and later months comapred to June and July.More males observed than female sightings in general(except the first two9 months). #’2. Perform a \(\chi^2\) test for independence on this table.

x <-(table(bears$Month,bears$Sex))
chisq.test (x)
## Warning in chisq.test(x): Chi-squared approximation may be incorrect
## 
##  Pearson's Chi-squared test
## 
## data:  x
## X-squared = 9.8536, df = 7, p-value = 0.197

#’ * What are the hypotheses?

Null hypothesis:The month the bear is observed is independent of the sex of the bear. Alternative hypothesis: The month the bear is observed depends on the sex of the bear

#’ * Are the assumptions satisfied?

Random Sampling;the data is obtained from a random sample or the population is well defined

Independence;each observation is independent of the others

Expected cell count;the expected frequency for each cell in the contingency table is at least 5 in most cases,and no cell should have an expected frequency of less than 1

#’ * What is your conclusion? Explain. [3 Marks]

Fail to reject null hypothesis as p is over 0.05. Insufficient evidence to conclude that the month the bear is observed depends on the sex of the bear