## ----lossdata, message=FALSE-------------------------------------------------- d <- agrodata::data_ibli("marsabit_losses") d[1:5, c(1:6, 9:10)] ## ----lossvariables, echo=FALSE------------------------------------------------ vars <- c("hhid", "sublocation", "year", "month", "animal", "cause", "cause_other", "where", "totalloss", "adultloss", "unique household ID", "sublocation name", "year of loss event", "month of loss event", "animal species", "cause of loss", "other causes not on list", "at base or satellite camp?", "number of animals lost?", "number of adult animals lost (>= 3 years for cattle/camel, >= 6 months for sheep/goat)") vars <- matrix(vars, ncol=2) colnames(vars) <- c("name", "description") knitr::kable(vars, caption="Livestock los variables") ## ----animals------------------------------------------------------------------ table(d$animal) x <- tapply(d$totalloss, d$animal, sum, na.rm=TRUE) barplot(x, col=rainbow(3), las=1) ## ----animals2----------------------------------------------------------------- x tlu <- c(camel=1.4, cattle=1, shoat=0.1) xtlu <- x * tlu barplot(xtlu, col=rainbow(3), las=1) ## ----tlu---------------------------------------------------------------------- animals <- match(trimws(d$animal), c("Goat/Sheep", "Camel", "Cattle")) anitlu <- tlu[animals] youngloss <- d$totalloss - d$adultloss d$tluloss <- (d$adultloss + youngloss/2) * anitlu ## ----tluloss------------------------------------------------------------------ a <- aggregate(d[, "tluloss", drop=FALSE], d[, c("year", "month")], sum, na.rm=TRUE) a <- a[order(a$year, a$month), ] time <- as.Date(paste(a$year, a$month, 15, sep="-")) plot(time, a$tluloss, ylab="Loss (TLU)") ## ----tab---------------------------------------------------------------------- xtabs(tluloss ~ month+year, d) ## ----barplot1----------------------------------------------------------------- cause <- tapply(d$tluloss, d$cause, sum, na.rm=TRUE) x <- sort(round(cause)) par(mai=c(1,2,1,1)) barplot(x, horiz=T, las=1) ## ----barplot2----------------------------------------------------------------- i <- d$cause %in% c("Starvation/Drought", "Disease") d$cause2 <- "Other" d$cause2[i] <- d$cause[i] d$cause2[d$cause2 == "Starvation/Drought"] <- "Starvation" ## ----mort40------------------------------------------------------------------- yc <- aggregate(d[, "tluloss", drop=FALSE], d[, c("cause2", "year")], sum, na.rm=TRUE) head(yc) ## ----mort50------------------------------------------------------------------- r <- reshape(yc, direction="wide", idvar="cause2", timevar="year") colnames(r) <- gsub("tluloss.", "", colnames(r)) r <- as.matrix(r) rownames(r) <- r[,1] barplot(r[,-1], legend = rownames(r), las=2, args.leg=list(cex=1))