Skip to content

Instantly share code, notes, and snippets.

@kumeS
Last active November 20, 2022 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kumeS/486d049718bb981f980db651d550b59b to your computer and use it in GitHub Desktop.
Save kumeS/486d049718bb981f980db651d550b59b to your computer and use it in GitHub Desktop.
Calculate residuals
Calc_residuals <- function(original, predicted, digits=4){
#diff
d0 <- original - predicted
#MSE
d1 <- mean((d0)^2)
#MAE
d2 <- mean(abs(d0))
#RMSE
d3 <- sqrt(mean((d0)^2))
#MAPE
d4a <- abs((d0/original))
d4b <- mean(d4a[!is.infinite(d4a)])*100
#R2
d5 <- round(1-(sum((d0)^2)/sum((original-mean(original))^2)), digits)
return(data.frame(mse=round(d1, digits=digits),
mae=round(d2, digits=digits),
rmse=round(d3, digits=digits),
MAPE=round(d4b, digits=digits),
R2=round(d5, digits=digits),
row.names = 1))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment