I have collected data on an animal that is either in its burrow (0) or out foraging (1). Each data point has a time stamp associated with it.

What I want to know is, what percentage of time does the animal spend in the burrow?

Example data

Date  Time Foraging
10/1  10:00   1
10/1  10:30   0
10/1  13:30   0
. . . 
10/1  17:00   1

Here is some sample code that will create a toy data set:

state = rep(seq(0,1),5000)
time = NULL
for (i in 1:length(state)){
    if (state[i]==0) {time[i] = rpois(1,4)*10}
    if (state[i] == 1) {time[i] = rexp(1,1) * 10}

df = data.frame(state,time)

the dataset df represents state (0 = "in burrow", 1 = "foraging") and a time spent doing the activity. I wrote the code so that the animal spends ~ 40 minutes in its burrow and 10 minutes foraging, drawing from different distributions. I am assuming that I should be able to sample from the toy data set and recover the values. If I can do this, I may be reasonably sure of the estimates that I obtain from the collected data.

I thought that I would do a simple binomial, but the data points are not equidistant, so then I thought possibly a poisson distribution, but that would get me a rate.

  • $\begingroup$ An extremely simple starting point would be to assume the animal is out the entire length of a segment foraging = 1 and in for foraging = 0. Now the sumproduct of interval length * foraging = 1 over the entire length of the experiment is your initial estimate. As for parameterization, percentages are often estimated as beta-distributed random variables. You could break your data up by days and use maximum likelihood, or you can set up a Bayesian model with a beta (1, 1) prior which IIRC is uniform on 0, 1. $\endgroup$
    – Avraham
    Sep 26, 2016 at 16:03
  • $\begingroup$ Two issues come to mind: 1) What determines the data sampling? Is it independent of the animal's location? (i.e. burrow vs. foraging) 2) For evenly sampled data, would they really be independent? It could be that in your situation, the Gambler's fallacy is actually true. $\endgroup$
    – GeoMatt22
    Sep 26, 2016 at 16:09
  • $\begingroup$ 1) the data sampling is random (read opportunistic), and it is independent of the animal's location. 2) For evenly sampled, I think that I would be able to apply a time to each observation. For example, if I sampled every hour and the animal had a probability of .5 of being in its burrow, then I could assume that it was foraging half the time. $\endgroup$
    – user44796
    Sep 26, 2016 at 18:32
  • $\begingroup$ A reasonable starting point might be a continuous-time, two-state Markov model with instantaneous transition rates $\lambda_{01}$ and $\lambda_{10}$ per unit time. The likelihood of the data would be straightforward to write down and maximise at least numerically. This would account for the dependency between subsequent observations and produce an estimate of the long run probability of being in state 1, $\lambda_{10}/(\lambda_{01}+\lambda_{10})$ along with its uncertainty (either by bootstrapping or relying on asymptotic approximations). Of course, this is only one of many possible models $\endgroup$ Sep 26, 2016 at 18:44
  • $\begingroup$ How would I structure the data? Would I have a column of transitions and a second column of time? For example, [0->1, 20 min] or [1<-0, 45 min]. Would I also include 0->0 and 1->1? My experience with markov chains is obviously limited. $\endgroup$
    – user44796
    Sep 26, 2016 at 19:54

2 Answers 2


A simple continuous-time, discrete-state Markov model would be to assume that $$P(Y(t+dt)=1|Y(t)=0)=\lambda_{01}dt$$ and $$P(Y(t+dt)=0|Y(t)=1)=\lambda_{10}dt.$$ Let $p(t)=P(Y(t)=1)$. From the above assumptions it follows that $$ \frac{dp}{dt} = -\lambda_{10}p + \lambda_{01}(1-p). \tag{1} $$ Suppose the state is known at some time $t_0$, say $p(t_0)=1$ or $p(t_0)=0$. With this initial condition, the solution of (1) is $$ p(t) = p_\infty + (p(t_0) - p_\infty)e^{-\lambda(t-t_0)} $$ where $p_\infty = \lambda_{01}/\lambda$ and $\lambda = \lambda_{01}+\lambda_{10}$.

Let $y_i \in \{0,1\}$ be the observed state at time point $t_i$, $i=1,2,\dots,n$. From the Markov property of the model, the likelihood function becomes \begin{align} L(p_\infty,\lambda) &= P(Y(t_1)=y_1 \cap \dots \cap Y(t_n) = y_n) \\ &= P(Y(t_1)=y_1)\prod_{i=2}^n P(Y(t_i)=y_i|Y(t_{i-1})=y_{i-1}) \\ &= f(y_1;p_\infty)\prod_{i=2}^n f(y_i; p_\infty + (y_{i-1} - p_\infty)e^{-\lambda(t_i-t_{i-1})}) \end{align} where $f(y;p)$ is the Bernoulli probability mass function with parameter $p$ (dbinom( ,size=1, ) below). Numerical maximisation of the log of this can be implemented in R as follows:

lnL <- function(par,y,t) {
  pinf <- par[1]
  lambda <- par[2]
  n <- length(y)
  i <- 2:n
  tmp <- dbinom(y[1], 1, pinf, log=TRUE) + 
         sum(dbinom(y[i], 1, pinf + (y[i-1]-pinf)*exp(-lambda*(t[i]-t[i-1])), log=TRUE))

# fake data with some autocorrelation
y <- c(1,1,1,0,0,1,0,0,0,1) 
t <- c(1,2,3,4,5,6,7,8,9,15)

# fit the model
fit <- optim(c(.5,1),lnL,y=y,t=t,hessian=TRUE)
fit$par # MLEs of p_infinity and lambda = lambda_10 + labmda_01
sqrt(diag(solve(fit$hessian))) # approximate standard errors 

## Estimate based on possibly incorrect binomial model
n <- length(y)
(phat <- sum(y)/n)

Note that this model assumes that the times spent in each state are exponentially distributed which may not be very realistic.


I thought that I would do a simple binomial, but the data points are not equidistant

But if you don't know anything about how often the animal changes state, or at what times it's most likely to change state, or anything else like that, then there's no way to exploit the timing information. The best you can do is divide the number of 1s by the number of observations. This corresponds to a model in which the animal's probability of being in the burrow at any given moment is constant over time.

  • $\begingroup$ I don't think this is exactly true. I think that my situation is similar to work sampling that is used in occupational analyses, but not sure if that approach is directly transferable. $\endgroup$
    – user44796
    Sep 26, 2016 at 19:04
  • $\begingroup$ @user44796 I just don't see how you're going to be able to exploit the timing information without making some assumptions about the underlying process. Feel free to prove me wrong. $\endgroup$ Sep 26, 2016 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.