# Estimating population trends in R
# https://tidyecology.com/posts/population-trend-estimation/
#
# The R code of this Tidy Ecology tutorial, chunk by chunk, in the order of the post.
# Run it top to bottom in a fresh R session. Code the post shows but does not run
# is commented out. The prose, and the numbers it quotes, are in the post itself.
# Post last modified: 2026-10-07

## ---- setup ----
suppressPackageStartupMessages({library(mgcv); library(ggplot2); library(dplyr)})
ink<-"#16241d"; body<-"#2c3a31"; forest<-"#275139"; gold<-"#c9b458"
paper<-"#f5f4ee"; line<-"#dad9ca"; faint<-"#5d6b61"; aban<-"#b5534e"; mown<-"#2f8f63"
theme_te <- function(){
  theme_minimal(base_size=12)+
  theme(plot.background=element_rect(fill=paper,colour=NA),
        panel.background=element_rect(fill=paper,colour=NA),
        panel.grid.minor=element_blank(),
        panel.grid.major=element_line(colour=line,linewidth=.3),
        axis.title=element_text(colour=ink), axis.text=element_text(colour=faint),
        plot.title=element_text(colour=ink,size=12,face="bold"),
        strip.text=element_text(colour=ink,face="bold"),
        legend.position="bottom", legend.title=element_text(colour=ink),
        legend.text=element_text(colour=body))
}

## ---- simulate ----
n_t<-30; year<-1:n_t
logmu <- log(90) - 0.9*sin(pi*(year-1)/(n_t-1))   # dip to about 37, then recover
set.seed(311); count <- rpois(n_t, exp(logmu))
dat <- data.frame(year, count)

## ---- fit ----
glin <- glm(count ~ year, family=poisson, data=dat)
gam1 <- gam(count ~ s(year), family=poisson, data=dat, method="REML")

sl <- summary(glin)$coefficients["year", ]
edf <- sum(gam1$edf[-1]); de <- summary(gam1)$dev.expl
c(pct_per_yr = round(100*(exp(sl["Estimate"])-1), 2),
  lin_p      = round(sl["Pr(>|z|)"], 2),
  gam_edf    = round(edf, 1),
  gam_dev_expl = round(de, 2),
  dAIC       = round(AIC(glin)-AIC(gam1), 0))

## ---- fig-trend ----
gg <- data.frame(year=seq(1,n_t,length.out=200))
pr <- predict(gam1, gg, type="link", se.fit=TRUE)
gg$fit<-exp(pr$fit); gg$lo<-exp(pr$fit-1.96*pr$se.fit); gg$hi<-exp(pr$fit+1.96*pr$se.fit)
gl <- data.frame(year=seq(1,n_t,length.out=200)); gl$fit <- exp(predict(glin, gl))
ggplot()+
  geom_ribbon(data=gg, aes(year, ymin=lo, ymax=hi), fill=forest, alpha=.15)+
  geom_point(data=dat, aes(year, count), colour=ink, size=2, alpha=.85)+
  geom_line(data=gl, aes(year, fit, colour="Linear trend"), linewidth=1)+
  geom_line(data=gg, aes(year, fit, colour="GAM smooth"), linewidth=1)+
  scale_colour_manual(values=c("Linear trend"=gold,"GAM smooth"=forest))+
  labs(x="year", y="count", colour=NULL,
       title="A flat linear trend hides a crash and recovery")+
  theme_te()

## ---- lowcount ----
T2<-25; yr2<-1:T2; logmu2 <- log(9) - 0.075*(yr2-1)
set.seed(69); y2 <- rpois(T2, exp(logmu2))
dat2 <- data.frame(year=yr2, count=y2)

gp <- glm(count ~ year, family=poisson, data=dat2)         # count model
o1 <- lm(log(count+1)   ~ year, data=dat2)                 # log(y+1) OLS
o5 <- lm(log(count+0.5) ~ year, data=dat2)                 # log(y+0.5) OLS
round(c(n_zeros=sum(y2==0),
        poisson_pct=100*(exp(coef(gp)["year"])-1),
        logp1_pct=100*(exp(coef(o1)["year"])-1),
        logp0.5_pct=100*(exp(coef(o5)["year"])-1)), 2)

## ---- fig-count ----
gg2 <- data.frame(year=seq(1,T2,length.out=200))
gg2$pois <- exp(predict(gp, gg2))
gg2$logols <- exp(predict(o1, gg2)) - 1
ggplot()+
  geom_point(data=dat2, aes(year, count), colour=ink, size=2, alpha=.85)+
  geom_line(data=gg2, aes(year, pois, colour="Poisson GLM"), linewidth=1)+
  geom_line(data=gg2, aes(year, logols, colour="log(y+1) OLS"), linewidth=1, linetype="22")+
  scale_colour_manual(values=c("Poisson GLM"=forest,"log(y+1) OLS"=aban))+
  labs(x="year", y="count", colour=NULL,
       title="Count model against log-transformed regression")+
  theme_te()
