Machine Learning is a subset of Artificial Intelligence (AI), which is used to create intelligent systems that are able to learn without being programmed explicitly. In machine learning, we create algorithms and models which is used by an intelligent system to predict outcomes based on particular patterns or trends which are observed from the given data. Machine learning follows a unique principle of using data and the outcomes from the data to predict the rules which are stored in a model. This model is then used to predict outcomes from a different set of data. In R programming the environment for machine learning can be set easily through RStudio.
Setting up an environment for machine learning using Anaconda
Step 2: Open Anaconda Navigator and click the Install button for Rstudio.
Step 3: After installation, create a new environment. Anaconda will then send a prompt asking to enter a name for the new environment and the lunch the R studio.
Running R commands
Method 1: R commands can run from the console provided in R studio. After opening Rstudio simply type R commands to the console.
Method 2: R commands can be stored in a file and can be executed in an anaconda prompt. This can be achieved by the following steps.
- Open an anaconda prompt
- Go to the directory where the R file is located
- Activate the anaconda environment by using the command:
conda activate <ENVIRONMENT_NAME>
- Run the file by using the command:
Installing machine learning packages in R
Packages help make code easier to write as they contain a set of predefined functions that perform various tasks. The most used machine learning packages are Caret, e1071, net, kernlab, and randomforest. There are two methods that can be used to install these packages for your R program.
Method 1: Installing Packages through Rstudio
- Open Rstudio and click the Install Packages option under Tools which is present in the menubar.
- Enter the names of all the packages you want to install separated by spaces or commas and then click install.
- Open an Anaconda prompt.
- Switch the environment to the environment you used for Rstudio using the command:
conda activate <ENVIRONMENT_NAME>
- Enter the command r to open the R console.
- Install the required packages using the command:
install.packages(c("<PACKAGE_1>", "<PACKAGE_2>", ..., "<PACKAGE_N>"))
- Difference Between Artificial Intelligence vs Machine Learning vs Deep Learning
- Difference Between Machine Learning and Deep Learning
- Setting up the environment in Scala
- How Artificial Intelligence (AI) and Machine Learning(ML) Transforming Endpoint Security?
- How Machine Learning and Artificial Intelligence Will Impact Global Industries in 2020?
- Difference Between Data mining and Machine learning
- Difference Between Business Intelligence and Machine Learning
- Difference between Big Data and Machine Learning
- Difference between Data Science and Machine Learning
- Introduction to Machine Learning in R
- How to get the value of System Property and Environment Variable in Java?
- How to set PHP development environment in windows ?
- Environment Setup in Ruby
- ES6 | Environment Setup
- Clear the Console and the Environment in R Studio
- Deep Learning in R Programming
- Supervised and Unsupervised Learning in R Programming
- Getting and Setting Length of the Vectors in R Programming - length() Function
- Amazon summer internship (Hospitality, Work, Learning and Perks)
Method 2: Installing Packages through Anaconda prompt/Rstudio console
While downloading the packages you might be prompted to choose a CRAN mirror. It is recommended to choose the location closest to you for a faster download.
Machine Learning packages in R
There are many R libraries that contain a host of functions, tools, and methods to manage and analyze data. Each of these libraries has a particular focus with some libraries managing image and textual data, data manipulation, data visualization, web crawling, machine learning, and so on. Here let’s discuss some of the important machine learning packages by demonstrating an example.
Preparing the Data Set:
Before using these packages first of all import the data set into RStudio, cleaning the data set, and split the data into train and test data set. Download the CSV file from this link.
CARET: Caret stands for classification and regression training. The CARET package is used for performing classification and for regression tasks. It consists of many other built-in packages.
ggplot2: R is most famous for its visualization library ggplot2. It provides an aesthetic set of graphics that are also interactive. The ggplot2 package is used for creating plots and for visualising data.
randomForest: The randomForest package allows us to use the random forest algorithm easily.
nnet: The nnet package uses neural networks in deep learning to create layers which help in training and predicting models. The loss (the difference between the actual value and predicted value) decreases after every iteration of training.
e1071: The e1071 package is used to implement the support vector machines, naive bayes algorithm and many other algorithms.
rpart: The rpart package is used to partition data. It is used for classification and regression tasks. The resultant model is in the form of a binary tree.
dplyr: Like rpart the dplyr package is also a data manipulation package. It helps manipulate data by using functions such as filter, select, and arrange.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.