--- title: "Analysis of step length distribution" output: html_notebook --- ##Title: Primates adjust movement strategies due to changing food availability ##Abstract: Animals are hypothesized to search their environments in predictable ways depending on the distribution of resources. Evenly distributed foods are thought to be best exploited with random Brownian movements; while foods that are patchy or unevenly distributed require non-Brownian strategies, such as Lévy walks. Thus, when food distribution changes due to seasonal variation, animals should show concomitant changes in their search strategies. We examined this issue in six monkey species from Africa and Mexico: three frugivores and three folivores. We hypothesized that the more patchily distributed fruit would result in frugivores showing more levy-like patterns of motion, while folivores, with their more homogenous leaf food supply, would show Brownian patterns of motion. Five of six species conformed to the overall movement pattern predicted by their primary dietary item. For folivorous black howler monkeys (Alouatta pigra), ursine colobus (Colobus vellerosus), and red colobus (Procolobus rufomitratus), Brownian movement was supported. Two frugivores (spider monkeys, Ateles geoffroyi yucatanensis, and grey-cheeked mangabeys, Lophocebus albigena) showed Lévy walks, as predicted, but frugivorous vervet monkeys (Chlorocebus pygerythrus) showed a Brownian walk. Additionally, we test whether seasonal variation in the spatial availability of food support environmentally driven changes movement patterns. Four of five species tested for seasonal variation showed adjustments in their search strategies between the rainy and dry seasons. This study provides support for the notion that food distribution determines search strategies and that animal movement patterns are flexible, mirroring changes in the environment. ##Analysis: Below is the anaysis of step length distributions from 6 primate species. 1.0) Libraries used ```{r} library(poweRlaw) library(dplyr) library(ggplot2) ``` 2.0) Select dataset ```{r} ##clear environment variables rm(list=ls()) ##import data: column of step lengths steplengths <- read.csv("data/stepLengths.csv", header=TRUE) #all names(steplengths) ##select which primate and season: e.g., howler monkey, dry season = hmdry df.steps <- steplengths %>% select(vall) %>% na.omit() step <- df.steps[,1] ``` 3.0) Visualize the data ```{r} summary(step) hist(step) ``` 4.0) Create a power law distribution and estimate xmin and alpha ```{r} #fit a power law distribution to the data power.law <- conpl$new(step) #create a new power law distribution fit <- estimate_xmin(power.law) #estimate best fit to observed data (goodness-of-fit using KS test to choose xmin, maximum likelihood to get alpha) fit #pars[1] is alpha power.law$setXmin(fit) #give power.law object the best fit parameters #estimate and visualize uncertainty around estimates: Xmin, alpha #fit.boot <- bootstrap(power.law) #par(mfrow=c(1,2)) #plot(fit.boot$bootstraps$xmin) #plot(fit.boot$bootstraps$pars) ``` 5.0) Test if power law hypothesis holds (goodness-of-fit) ```{r} fit.test <- bootstrap_p(power.law) #Newman paper: this is the goodness-of-fit test page 675 section 4.1 fit.test$p #Newman paper: p-values > than 0.1 suggest power-law distribution is plausable ``` 6.0) Compare Power law distribution to other plausable fits (lognormal, exponential) ```{r} ##create other best fit distributions #exponential exp <- conexp$new(step) #create new exponetial distribution exp$setXmin(power.law$xmin) #set x-min based on power-law fit fit.exp <- estimate_pars(exp) #find best fit rate parameter exp$setPars(fit.exp) #set best fit rate parameter #lognormal lnorm <- conlnorm$new(step) #create log normal distribution lnorm$setXmin(power.law$xmin) #set x-min based on power law fit fit.lnorm <- estimate_pars(lnorm) #find best mean/sd parameters lnorm$setPars(fit.lnorm) #set best fit parameters ##compare between power law and other potential distributions results.table <- matrix(ncol = 3) #power law vs. exponential comp.exp <- compare_distributions(power.law, exp) results.table <- rbind(results.table, c("powerLaw_vs_exponetial",comp.exp$test_statistic,comp.exp$p_two_sided)) #power law vs. lognormal comp.lnorm <- compare_distributions(power.law, lnorm) results.table <- rbind(results.table, c("powerLaw_vs_lognormal",comp.lnorm$test_statistic,comp.lnorm$p_two_sided)) #exponential vs. lognormal comp.lnorm.exp <- compare_distributions(exp, lnorm) results.table <- rbind(results.table, c("exponential_vs_lognormal",comp.lnorm.exp$test_statistic,comp.lnorm.exp$p_two_sided)) #plot results results.table <- as.data.frame(results.table[-1,]) names(results.table) <- c("Comparison","test statistic", "p-value") results.table ###Choose best fit distribution # - if p-value < 0.05 the distribution do not fit equally well to the observed data # - if the test statistic is positive the first distribution fits best (negative the second fits best) ``` Plot the comparisons ```{r} plot(power.law, ylab='CDF',xlab='Step length (m)') lines(power.law, col=2, lwd=4) lines(lnorm, col=3, lwd=2) lines(exp, col=4, lwd=2) legend("bottomleft", inset=.05, c("Exponential","Lognormal","Power law"), fill=c(4,3,2), horiz=TRUE) ``` Plot the comparisons as a sliding bar ```{r} arrow <- read.csv("data/arrowData.csv") ggplot(arrow,aes(x=pwr_v_exp,y=1,color=seasons))+facet_grid(species~.)+geom_point(size=5)+ geom_vline(xintercept=0,linetype="longdash")+ geom_hline(yintercept=1)+theme_bw()+theme_classic()+ labs(x="Ratio of the log-likelihoods")+ theme(axis.text.y=element_blank(),axis.title.y=element_blank(),axis.ticks.y=element_blank()) ```