In R, you can perform various machine learning tasks, including decision trees, random forests, and support vector machines. Here are some examples: ## Decision trees # Load the rpart package library(rpart) # Generate some data set.seed(123) x <- matrix(rnorm(1000), ncol = 5) y <- factor(sample(0:1, 200, replace = TRUE)) # Fit a decision tree model model <- rpart(y
., data = as.data.frame(x))
printcp(model) # View the model complexity parameter table
# Plot the decision tree
plot(model, compress = TRUE)
In this code, we first load the `rpart` package. We then generate some data using the `matrix()` function and a binary outcome variable using the `factor()` function. We fit a decision tree model using the `rpart()` function, specifying the formula for the outcome variable and the predictor variables using the `data` argument. We can view the complexity parameter table using the `printcp()` function. We then plot the decision tree using the `plot()` function, setting the `compress` argument to `TRUE` to remove redundant nodes.
## Random forests
# Load the randomForest package
library(randomForest)
# Generate some data
set.seed(123)
x <- matrix(rnorm(1000), ncol = 5)
y <- factor(sample(0:1, 200, replace = TRUE))
#Fit a random forest model
model <- randomForest(x, y)
print(model) # View the model summary
# Make predictions for new data
new_x <- matrix(rnorm(15), ncol = 5)
new_y <- predict(model, newdata = new_x)
In this code, we first load the `randomForest` package. We then generate some data using the `matrix()` function and a binary outcome variable using the `factor()` function. We fit a random forest model using the `randomForest()` function, specifying the predictor variables and the outcome variable using the `x` and `y` arguments. We can view a summary of the model using the `print()` function applied to the model object. We then make predictions for new data using the `predict()` function, passing in a new data matrix with the predictor variables.
## Support vector machines
# Load the e1071 package
library(e1071)
# Generate some data
set.seed(123)
x <- matrix(rnorm(1000), ncol = 5)
y <- factor(sample(0:1, 200, replace = TRUE))
# Fit a support vector machine model
model <- svm(x, y)
print(model) # View the model summary
# Make predictions for new data
new_x <- matrix(rnorm(15), ncol = 5)
new_y <- predict(model, newdata = new_x)
In this code, we first load the `e1071` package. We then generate some data using the `matrix()` function and a binary outcome variable using the `factor()` function. We fit a support vector machine model using the `svm()` function, specifying the predictor variables and the outcome variable using the `x` and `y` arguments. We can view a summary of the model using the `print()` function applied to the model object. We then make predictions for new data using the `predict()` function, passing in a new data matrix with the predictor variables.
Note that there are many other types of machine learning algorithms available in R, including neural networks, gradient boosting, and deep learning models. You can find more information on these functions in the R documentation or online tutorials and resources.