rmachine-learninglinear-regressiontest-data

Validate Accuracy of Test Data


I have fit my model with my training data and tested the accuracy of the model using r squared.

However, I want to test the accuracy of the model with my test data, how to do this?

My predicted value is continuous. Quite new to this so open to suggestions.

LR_swim <- lm(racetime_mins ~ event_month +gender + place +
             clocktime_mins +handicap_mins +
              Wind_Speed_knots+ 
             Air_Temp_Celsius +Water_Temp_Celsius +Wave_Height_m,
               data = SwimmingTrain) 
           family=gaussian(link = "identity")
summary(LR_swim)
rsq(LR_swim) #Returns-  0.9722331

#Predict Race_Time Using Test Data
 pred_LR <- predict(LR_swim, SwimmingTest, type ="response")
#Add predicted Race_Times back into the test dataset.
SwimmingTest$Pred_RaceTime <- pred_LR

Solution

  • To start with, as already pointed out in the comments, the term accuracy is actually reserved for classification problems. What you are actually referring to is the performance of your model. And truth is, for regression problems (such as yours), there are several such performance measures available.

    For good or bad, R^2 is still the standard measure in several implementations; nevertheless, it may be helpful to keep in mind what I have argued elsewhere:

    the whole R-squared concept comes in fact directly from the world of statistics, where the emphasis is on interpretative models, and it has little use in machine learning contexts, where the emphasis is clearly on predictive models; at least AFAIK, and beyond some very introductory courses, I have never (I mean never...) seen a predictive modeling problem where the R-squared is used for any kind of performance assessment; neither it's an accident that popular machine learning introductions, such as Andrew Ng's Machine Learning at Coursera, do not even bother to mention it. And, as noted in the Github thread above (emphasis added):

    In particular when using a test set, it's a bit unclear to me what the R^2 means.

    with which I certainly concur.

    There are several other performance measures that are arguably more suitable in a predictive task, such as yours; and most of them can be implemented with a simple line of R code. So, for some dummy data:

    preds <- c(1.0, 2.0, 9.5)
    actuals <- c(0.9, 2.1, 10.0)
    

    the mean squared error (MSE) is simply

    mean((preds-actuals)^2)
    # [1] 0.09
    

    while the mean absolute error (MAE), is

    mean(abs(preds-actuals))
    # [1] 0.2333333
    

    and the root mean squared error (RMSE) is simply the square root of the MSE, i.e.:

    sqrt(mean((preds-actuals)^2))
    # [1] 0.3
    

    These measures are arguably more useful for assessing the performance on unseen data. The last two have an additional advantage of being in the same scale as your original data (not the case for MSE).