Archive

Archive for the ‘R’ Category

Simple MMRE function in R

December 15, 2011 Leave a comment

After a while reading through R tutorials, here’s a simple function in R to calculate MMRE

function(data)
{
    n = nrow(data);
    total_mre = 0;
    for(i in 1:n)
    {
        current_x = data[i,1];
        current_y = data[i,2];
        temp_data = data[-i,];
        temp_series_x = temp_data[,1];
        temp_series_y = temp_data[,2];
        
        // this bit took me relatively more time to figure out how to 
        // extract data from atomic vectors
        formular = matrix(coef(lm(temp_series_y~temp_series_x)), 1, 2);

        m = formular[1,2];
        c = formular[1,1];
        pred = m*current_x + c;
        mre = abs(current_y - pred)/current_y;
        total_mre = total_mre + mre;
    }
    mmre = (total_mre)/n;
    return (mmre);
}
Categories: R