The original address; http://xccds.github.io/2011/09/caret.html/
In the case of data mining, we will use many of the expansion packages in R, each with different functions and functions. It would be convenient if they could be combined to apply. The caret package (classification and Regression Training) is a comprehensive toolkit created to solve data training for classification and regression problems. The following example illustrates the application of several core steps of data mining.
The data involved in this example is a medical experimental data, after loading the data can be found that its sample number is 528, the number of arguments is 342,MDRRDESCR as the argument data frame, mdrrclass as the dependent variable.
Library (caret)
Data (MDRR) is a very variable number of variables involved in this sample, which requires a preliminary dimensionality reduction. One of the variables that needs to be removed is a constant argument, or an independent variable with a minimal variance, and the corresponding command is Nearzerovar, and you can see that the new dataset's arguments are reduced to 297.
Zerovar=nearzerovar (MDRRDESCR)
Newdata1=mdrrdescr[,-zerovar] Another class that needs to be removed is a variable that is strongly related to other arguments, and the corresponding command is findcorrelation. There may also be multiple collinearity problems in the arguments, which can be found using the Findlinearcombos command. The self-variable is reduced to 94 after this processing.
Descrcorr = Cor (NEWDATA1)
Highcorr = Findcorrelation (Descrcorr, 0.90)
Newdata2 = newdata1[,-highcorr]
Comboinfo = Findlinearcombos (NEWDATA2)
newdata2=newdata2[,-comboinfo$remove] We also need to standardize the data and complement the missing values, which can be used with the preprocess command, which is the normalized data Its advanced features include two methods for predicting missing values using K-nearest neighbors and bagging decision trees. In addition, it can perform Cox Power transformation and principal component extraction.
Process = preprocess (NEWDATA2)
NEWDATA3 = Predict (Process, NEWDATA2) finally divides the data with createdatapartition, divides it into 75% training samples and 25% test samples, Similar commands include createresample for simple self-service sampling, and createfolds to generate multiple cross-examination samples.
Intrain = Createdatapartition (mdrrclass, p = 3/4, list = FALSE)
Trainx = Newdata3[intrain,]
TESTX = Newdata3[-intrain,]
Trainy = Mdrrclass[intrain]
testy = Mdrrclass[-intrain] The sample data can also be graphically observed before modeling, for example, to draw a box plot for the first two variables
Featureplot (trainx[,1:2],trainy,plot= ' box ')