R code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Inspired by Peig & Green | |
# "New perspectives for estimating body condition from mass/length data: | |
# the scaled mass index as an alternative method" | |
# Oikos 118: 1883-1891, 2009 | |
# Author: Chen-Pan Liao | |
scaledMassIndex <- | |
function(x, y, x.0 = mean(x)) { | |
require(smatr) | |
require(magrittr) | |
require(MASS) | |
require(data.table) | |
logM.ols <- lm(log(y) ~ log(x)) | |
logM.rob <- rlm(log(y) ~ log(x), method = "M") | |
b.msa.ols <- coef(sma(log(y) ~ log(x)))[2] | |
b.msa.rob <- coef(sma(log(y) ~ log(x), robust = T))[2] | |
SMI.ols <- y * (x.0 / x) ^ b.msa.ols | |
SMI.rob <- y * (x.0 / x) ^ b.msa.rob | |
res <- data.frame(SMI.ols, SMI.rob, x, y) | |
pred.DT <- | |
data.table(x = seq(min(x), max(x), length = 100)) %>% | |
.[, y.ols := predict(logM.ols, newdata = .) %>% exp] %>% | |
.[, y.rob := predict(logM.rob, newdata = .) %>% exp] | |
attr(res, "b.msa") <- c(ols = b.msa.ols, rob = b.msa.rob) | |
return(res) | |
} |