# How to apply a trained Random Forest model to a new data set in R?

So I have a data set that is essentially football players statistics in 2017 and 2018. I have trained my model to use the 2017 data to predict the 2018 number of touchdowns. My code is below:

set.seed(1)
data.rf <- randomForest(2018_td ~ ., data = data, proximity = TRUE)


In my data set, I had the actual # of touchdowns in 2018, and trained a random forest algorithm to predict that value. Now, I want to apply the trained random forest on the same 2018 data set, but to predict 2019 # of TD, that I don't have.

I'm not sure if I'm missing something or if I have a fundamentally wrong understanding of how RF works. How would I go about predicting those 2019 values from my data.rf model?

• Hey I could not get from your question if you already have another dataset for 2019 and you want to label them. If that is the case then you can feed the dataset to Random Forest Classifier to get labels for 2019. But if you don't have anything about 2019 and you just want to predict, then you should look for 'time series analysis'. The one I use and love (because it can plot really cool graphs as well) is Facebook's Prophet library which can predict future based on recent observations. – naïveRSA Jun 18 '19 at 13:13

You need to setup another data frame that has the unlabeled 2019 observations. Assuming you have multiple predictors, your new data would have the exact same columns as your 2018 data but with no 2019_td column (since you presumably don't know anything about the 2019 season). That is, the players in 2019 that you wish to predict on, along with their predictors, but no target column.
Once you have the data frame, you can use predict(trained_model, newdata) to score your new predictions.