Scaling the same continuous feature both in train and test I'm building a classification model to predict some target variable.
I have only one continuous feature (age) that I am interested in scaling. I split my data into train and test sets, I scale this feature separately in both the train and test (using the scale() function in R), and only then do I train the model, using the caret package.
I've been told this is not entirely correct, I should scale this feature in the test set according to the train set, and I'm not sure what this means exactly. How do I scale a feature in the test set according to the train set?
I'm dealing with the test and train as entirely separate datasets so that I won't have information leakage. If this is not the way to do it, how should I apply it in R?
NOTE - I have many continuous variables in the data, however, I want to scale only the age feature.
Here is my code, I'm using gradient boosting as my classification algorithm:
for (i in (1:10)) {
  print(i)
  set.seed(i)
  
  IND = createDataPartition(y = MYData$Target_feature, p=0.8, list = FALSE)
  TRAIN_set = MYData[IND, ]
  TEST_set = MYData[-IND,]
  
  TRAIN_set$age = scale(TRAIN_set$age)
  TEST_set$age = scale(TEST_set$age)
  
  GBMModel <- train(Target_feature~., data = TRAIN_set,
                    method = "gbm",
                    metric="ROC",
                    trControl = ctrlCV,
                    tuneGrid = gbmGRID,
                    verbose = FALSE
                    
  )
  
  
  AUCs_Trn[i] = auc(roc(TRAIN_set$Target_feature,predict(GBMModel,TRAIN_set, type='prob')[,1]))
  AUCs_Tst[i] = auc(roc(TEST_set$Target_feature,predict(GBMModel,TEST_set, type='prob')[,1]))
  
}


For example, for standard scaling, you need to use $\mu_{train}, \sigma_{train}$ while applying scaling to the test set. You should not calculate $\mu_{test},\sigma_{test}$.
More generally, the scaling should be done using the parameters estimated from the training set.
In caret, the example in this page shows you how yo use it using the preprocess method for train and test afterwards: https://www.rdocumentation.org/packages/caret/versions/6.0-92/topics/preProcess
