Big difference in randomTree accuracy with train and test sets I've created a model using randomForest for the following dataset: https://archive.ics.uci.edu/ml/datasets/Contraceptive+Method+Choice

The thing i'm questioning is that the results of the model when used on my training and testing sets are vastly different.

library(randomForest)
library(caret)
df &lt;- read.csv('cmc.csv')


Changing values to factors

df$Wife.s.education &lt;- as.factor(df$Wife.s.education)
df$Husband.s.education &lt;- as.factor(df$Husband.s.education)
df$Wife.s.religion &lt;- as.factor(df$Wife.s.religion)
df$Wife.s.now.working. &lt;- as.factor(df$Wife.s.now.working.)
df$Husband.s.occupation &lt;- as.factor(df$Husband.s.occupation)
df$Standard.of.living.index &lt;- as.factor(df$Standard.of.living.index)
df$Media.exposure &lt;- as.factor(df$Media.exposure)
#add string representation for readiblilty 
df[df$Contraceptive.method.used == 1,]$Contraceptive.method.used &lt;- "No-use"
df[df$Contraceptive.method.used == 2,]$Contraceptive.method.used &lt;- "Long-term"
df[df$Contraceptive.method.used == 3,]$Contraceptive.method.used &lt;- "Short-term"
df$Contraceptive.method.used &lt;- as.factor(df$Contraceptive.method.used)


Splitting data:

set.seed(47)
ind &lt;- sample(2, nrow(df), replace = TRUE, prob = c(0.7,0.3))
inTrain &lt;- createDataPartition(y=df$Contraceptive.method.used, p=.7, list = FALSE)
training = df[ind==1,]
testing = df[ind==2,]


Creating model:

model &lt;- randomForest(Contraceptive.method.used ~., data = training, proximity=TRUE)


#Prediction &amp; Confusion Matrix - training data
p1 &lt;- predict(model, training)
confusionMatrix(p1, training$Contraceptive.method.used)


Confusion Matrix Results (training):

          Reference
Prediction   Long-term No-use Short-term
  Long-term        208      9         18
  No-use             4    408          5
  Short-term        16     14        338

Overall Statistics

               Accuracy : 0.9353          
                 95% CI : (0.9184, 0.9496)
    No Information Rate : 0.4225          
    P-Value [Acc &gt; NIR] : &lt; 2e-16         

                  Kappa : 0.9002          

 Mcnemar's Test P-Value : 0.09773    


#Prediction &amp; Confusion Matrix - testing data
p2 &lt;- predict(model, testing)
confusionMatrix(p2, testing$Contraceptive.method.used)


Confusion Matrix Results (testing)

    Reference
Prediction   Long-term No-use Short-term
  Long-term         42     11         23
  No-use            27    122         47
  Short-term        36     65         80

Overall Statistics

               Accuracy : 0.5386          
                 95% CI : (0.4915, 0.5853)
    No Information Rate : 0.4371          
    P-Value [Acc &gt; NIR] : 8.972e-06       

                  Kappa : 0.2788          

 Mcnemar's Test P-Value : 0.005869  


As we can see theres is a massive change in the two results, if i print my model I get the following results:

     Type of random forest: classification
                     Number of trees: 500
No. of variables tried at each split: 3

        OOB estimate of  error rate: 46.96%
Confusion matrix:
           Long-term No-use Short-term class.error
Long-term         76     63         89   0.6666667
No-use            45    282        104   0.3457077
Short-term        72    106        183   0.4930748


This makes me belief that the test results are correct however I'm not sure why there is such a big difference when used on the training set, is this due to overfitting? If so how can this be handled?

Any guidance would be awesome.
Overfitting is a common problem in random forest, you can use cross Validation methods (Cross validation is a technique to build models that are not prone to overfitting) example - K-fold cross validation, stratified k fold.. 

Try this using train package-
control &lt;- trainControl(method = "cv", number = 5)
and add this as a control parameter to the model.