gianni lavaredo
2012-May-31 22:20 UTC
[R] function to calculate a SMAPE (Symmetric mean absolute percentage error) to avoid the possibility of an inflation caused by zero values in the series
Dear Reseacher, i find this modified version of SMAPE at pag 13 formula "msSMAPE" to to avoid the possibility of an inflation caused by zero values in the series using a Si component in the denominator of the symmetric MAPE http://www.stat.iastate.edu/preprint/articles/2004-10.pdf I wrote a function but I wish eventually correct error, change or improve with your suggestions. Please feel free to add comments or remarks because I am not a mathematicians and probably i wrong to write this function Thanks in advance Gianni SMAPEper <- function(x,y){mean((200*(abs(x-y)))/(x+y))} obs <- c(10,20,0,40) pred <-c(5,6,0,8) SMAPEper(obs,pred) msSMAPE <- function(obs,pred){ M <- numeric(length(obs)) for (i in 2:length(obs)) { n <- i-1 M[i] <- mean(obs[1:n]) } sum1 <- (abs(obs[1]-pred[1]))/(0.5*(abs(pred[1])+abs(obs[1]))) for (i in 2:length(obs)) { n <- i-1 sum2 <- 0 for (k in 1:n) {sum2 <- sum2+abs(obs[k]-M[k])} S <- sum2/n sum1 <- sum1+(abs(obs[i]-pred[i]))/(0.5*(abs(pred[i])+abs(obs[i]))+S) } my.SMAPE <- sum1/length(obs) return(my.SMAPE) } msSMAPE(obs,pred) [[alternative HTML version deleted]]