1

I have a data set containing the step count of cows from a 4 week trial where each animal was exposed to treatment A or treatment B at the beginning of week 2, and want to know how the step rate of the two treatment groups changed each week compared to week 1.

How do I add an offset to my model to do this?

The model I am running before adding the offset is this:

mod.1 <- glmmTMB(Step.count ~ Week*Treatment + (1|Cow.ID), data = data.df, family = poisson) 

Here is an example of my data

data.1 <- data.frame(Cow.ID = rep(1, 20),
       Week = sample(c(1,2,3,4), 20, replace = TRUE),
       Treatment = sample(c("infected"), 20, replace = TRUE),
       Step.count = rpois(20, 60.1))

data.2 <- data.frame(Cow.ID = rep(2, 20),
                 Week = sample(c(1,2,3,4), 20, replace = TRUE),
                 Treatment = sample(c("infected"), 20, replace = TRUE),
                 Step.count = rpois(20, 60.1))

data.3 <- data.frame(Cow.ID = rep(3, 20),
                 Week = sample(c(1,2,3,4), 20, replace = TRUE),
                 Treatment = sample(c("non-infected"), 20, replace = TRUE),
                 Step.count = rpois(20, 60.1))


data.4 <- data.frame(Cow.ID = rep(4, 20),
                 Week = sample(c(1,2,3,4), 20, replace = TRUE),
                 Treatment = sample(c("non-infected"), 20, replace = TRUE),
                 Step.count = rpois(20, 60.1))


sample.df <- rbind(data.1, data.2, data.3, data.4)
alex
  • 153
  • 1
  • 10
  • Hi alex, can you perhaps share an example of the dataframe you are working on to make your example [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? – alex_jwb90 Aug 20 '20 at 16:49
  • Hi, yes I have edited my question above with code to simulate a similar data set – alex Aug 20 '20 at 17:12

1 Answers1

1

Hard to say without an example of your data, but assuming that you have a datafame something like this

library(dplyr)

cows <- tibble(
    Cow.Id = rep(1:4, times = 5),
    Week = rep(1:5, each = 4),
    Step.count = floor(runif(20, 100,200)),
    Treatment = rep(c('A','B','A','B'), times = 5),
)

Then, you can easily calculate a column of Step.count.offset for each cow like this:

cows.clean <- cows %>%
    group_by(Cow.Id) %>%
    arrange(Week) %>%
    mutate(
        Step.count.offset = Step.count - first(Step.count)
    ) %>%
    ungroup()
alex_jwb90
  • 1,663
  • 1
  • 11
  • 20
  • The data that you simulated is the same as the data frame I have and this seems like it should work! Thank you! Am I right in thinking I then add this into my model like this: mod.1 <- glmmTMB(Step.count ~ Week*Treatment + (1|Cow.ID), data = data.df, offset = Step.count.offset, family = poisson)? – alex Aug 20 '20 at 17:18
  • Phew, I'm sorry have no idea about the offset param in the function you're using – alex_jwb90 Aug 20 '20 at 18:30
  • When running this on my actual data it does not work. Sorry, I should have said that step count was measured hourly for every cow each week. So every animal has 23 observations per day so 161 observations per week. Is it possible to add an offset if you have multiple observations for each animal each week? – alex Aug 20 '20 at 18:39
  • Is it possible to create an offset using a dataframe that looks something like this: cows <- tibble( Cow.Id = rep(1:4, times = 12), Week = rep(1:4, each = 12), Step.count = floor(runif(48, 100,200)), Treatment = rep(c('A','B','A','B'), times = 12), ) – alex Aug 21 '20 at 11:28
  • So, you need keep that granularity and offset each later observation against the respective hour & weekday in the first week? Or just summarise the step count by week and then work on steps per week? – alex_jwb90 Aug 21 '20 at 14:54
  • In the first case, how do you represent hour and day in your dataset? Do you have a timestamp? Or two extra columns for "hour" and "weekday"? – alex_jwb90 Aug 21 '20 at 14:58
  • Yes I have both. So I have Hour, Day, Hour/Date and Week variables in my dataframe. – alex Aug 21 '20 at 16:56
  • How about you then `group_by(Cow.Id, Day, Hour)` (assuming that `Day` is the weekday, not the day of the month)? Then the rows within each group-block would be the weeks, for a given combination of `cow x weekday x hour` and the offsetting happens against the first observation in week1 – alex_jwb90 Aug 21 '20 at 18:48