#=============================================================================== # Name : cv # Author(s) : José Cláudio Faria/UESC/DCET # Walmes Marques Zeviani UFPR/DE # Date : 23/03/2014 06:42:20 # Version : v6 # Aim : Calculate coefficient variation from lm, aov and # aovlist objects #=============================================================================== # Arguments: # av : an lm, aov or aovlist object # round : rounds the values of cv to the specified number of decimal places (default 2) cv <- function(av, round=2) { if(is.null(av)) stop('Please, check the parameter!') if(inherits(av, 'lm')) { qmee <- with(av, sum(residuals^2) / df.residual) m <- mean(av$fitted.values) cv <- 100 * sqrt(qmee) / m names(cv) <- 'cv' return(round(cv, round)) } if(inherits(av, 'aovlist')) { sm.av <- summary(av) qmee <- sapply(sm.av, function(i){ i[[1]]["Residuals", 3] }) qmee <- qmee[!is.na(qmee)] m <- av$'(Intercept)'$coefficients[[1]] cv <- 100 * sqrt(qmee) / m names(cv) <- paste('cv(', letters[1:length(qmee)], ')', sep='') return(round(cv, round)) } }