Skip to content

Instantly share code, notes, and snippets.

@kumeS
Last active Nov 20, 2022
Embed
What would you like to do?
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