Set up

In this analysis, you will be using a dataset which records the hunting returns of individual Ache men. Data were recorded between 1981 and 2007. The data lives in the rethinking package, but it is included here as a separate .csv file. Start by loading the packages you will need to explore the data: tidyverse and table1, as well as the data (I’ll write the code to read in the data for you).

ache <- read.csv("ache-hunting.csv")
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## âś” dplyr     1.1.4     âś” readr     2.1.5
## âś” forcats   1.0.0     âś” stringr   1.5.1
## âś” ggplot2   3.4.4     âś” tibble    3.2.1
## âś” lubridate 1.9.3     âś” tidyr     1.3.0
## âś” purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## âś– dplyr::filter() masks stats::filter()
## âś– dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(table1)
## 
## Attaching package: 'table1'
## 
## The following objects are masked from 'package:base':
## 
##     units, units<-

1. Getting familiar with the data

Start by taking a look at the structure of the dataframe and the first few rows. The datatype column is coded as follows: 1 if the duration of the trip was known; and 3 if the duration of the trip was not know.

Q1.1. What is the unit of observation for this dataframe? What variables are recorded about the unit of observation?

Unit of observation is individual trips

head(ache)
##   X month day year   id age kg.meat hours datatype
## 1 1    10   2 1981 3043  67     0.0  6.97        1
## 2 2    10   3 1981 3043  67     0.0  9.00        1
## 3 3    10   4 1981 3043  67     0.0  1.55        1
## 4 4    10   5 1981 3043  67     4.5  8.00        1
## 5 5    10   6 1981 3043  67     0.0  3.00        1
## 6 6    10   7 1981 3043  67     0.0  7.50        1

Q1.2. What are the vector and variable types for the following variables: age, kg.meat, hours, and datatype?

age: character, discrete kg.meat: character,continuous hours: character, continuous datatype: character, nominal

typeof("age")
## [1] "character"
typeof("kg.meat")
## [1] "character"
typeof("hours")
## [1] "character"
typeof("datatype")
## [1] "character"

Q1.3. Are the vector types appropriate for each of the variable types? If not, coerce the variables to better-suited vector types.

Hint: To save a new variable in a dataframe, use the mutate function from the tidyverse or save a new variable using the following syntax: dataframe$newvar <- function(dataframe$oldvar)

age <- as.integer(ache$age)
kg.meat <- as.numeric(ache$kg.meat)
datatype <- as.logical(ache$datatype)
hours <- as.numeric(ache$hours)

2. Summary statistics

Create a set of summary statistics for the following variables: age, kg.meat, hours, and datatype using thetable1 package.

Q2.1. Set informative labels for each of these variables

label(ache$age) <- "Age"
label(ache$kg.meat) <- "Meat(kg)"
label(ache$datatype) <- "TripDuration"
label(ache$hours) <- "Hours"

Q2.2. Produce a table that summarizes these variables.

Hint: make sure that the output summaries make sense for the different types of variables. If not, consider adjusting the variable types, either by coercing them before using table1 or by coercing them inside the table1 function.

table1(~kg.meat + age + hours + datatype, data=ache)
Overall
(N=14364)
Meat(kg)
Mean (SD) 3.21 (6.36)
Median [Min, Max] 0 [0, 191]
Age
Mean (SD) 46.4 (13.6)
Median [Min, Max] 47.0 [11.0, 75.0]
Hours
Mean (SD) 6.84 (2.19)
Median [Min, Max] 7.10 [0.300, 14.0]
Missing 12050 (83.9%)
TripDuration
Mean (SD) 2.67 (0.744)
Median [Min, Max] 3.00 [1.00, 3.00]

Q2.3. Use the table to answer the following questions: On average, how old are the hunters who engage in these hunts on average? How old are the youngest and the oldest huntere recorded in the dataset? What age range includes 68% of observations in this dataset (One standard deviation on both sides of the mean)?

Average is 46.4 years old Youngest is 11 years old & oldest is 75 years old Range is 32.8 - 60.

Q2.4. Look at the summary statistics for kg.meat. Are the mean and median similar? What does this suggest about the distribution of the data?

No, the mean (3.21kg) and median (0kg) are not the same, this difference is due to the large number of unsuccessful hunts; the successful hunts, are very successful and are extreme values pulling the mean up from the median. Data is relatively skewed by the extremes.

Q2.5. Create two separate count tables: one for year, and the other for month. Was the data collected evenly over the study period? Is there any seasonality in the observations i.e., does the data disproportionately cover certain months?

The data was not distributed evenly over the study period. It ranged from 0.1204 in 2000 to 0.001531607 in 1984. This is a range of approximately 12% to 0.15%. This is quite a range yearly. However, the distribution over the months were relatively similar with a range from 6.95% to 9.97% over the average of the 12 months in a year.

table(ache$year)
## 
## 1980 1981 1982 1984 1985 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 
##  600  264  560   22  112   77  381 1023 1036 1556 1397 1730 1633  931  609  620 
## 2005 2006 2007 
##  902  792  119
table(ache$month)
## 
##    1    2    3    4    5    6    7    8    9   10   11   12 
## 1304 1199 1105 1344 1108 1270 1433  999 1080 1408 1080 1034
proportions(table(ache$year))
## 
##        1980        1981        1982        1984        1985        1994 
## 0.041771094 0.018379282 0.038986355 0.001531607 0.007797271 0.005360624 
##        1995        1996        1997        1998        1999        2000 
## 0.026524645 0.071219716 0.072124756 0.108326371 0.097257031 0.120439989 
##        2001        2002        2003        2004        2005        2006 
## 0.113686995 0.064814815 0.042397661 0.043163464 0.062795879 0.055137845 
##        2007 
## 0.008284600
proportions(table(ache$month))
## 
##          1          2          3          4          5          6          7 
## 0.09078251 0.08347257 0.07692843 0.09356725 0.07713729 0.08841548 0.09976330 
##          8          9         10         11         12 
## 0.06954887 0.07518797 0.09802283 0.07518797 0.07198552

3. Data visualization

Q3.1. Check your intuition about the distribution of the data for kg.meat. Consider the type and number of the variable(s) that you are trying to plot to select the correct visualization. Make sure you set a binwidth that makes sense in this context.

Hint: Consider that this variable measures the return of a hunt conducted by a single hunter, and that 1 kg = ~2lb.

ggplot(ache, aes(x = kg.meat))+
  geom_histogram(binwidth = 6)

Q3.2. Perhaps skill of the hunter increases the chance that a hunt will be successful, such that older hunters tend to return more meat? To explore this, make a plot that shows the relationship between the age of the hunter (x-axis) and the amount of meat returned by a hunt (y-axis). Then add both scattergram and smoothed linear regression line to the plot. Add an informative title and appropriate axes labels.

ache%>%
  ggplot(aes(age, kg.meat)) + geom_point() + geom_smooth()
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

Q3.3. Using the plot you’ve just produced, do you think that there is a relationship between age of the hunter and the yield of the hunt?

No, there is no relationship. The regression line appears to be flat and run parallel to the x-axis. All the data appears to follow a pattern of no association between age and hunt yield.