Split data into train and test sets
Introduction to Data Science
Data science is one of the hottest and most in-demand fields today. At a high level, data science is the practice of extracting insights and knowledge from data using a combination of mathematics, statistics, and computer science. Data scientists collect data, analyze it to find patterns and trends, build predictive models, and communicate insights to drive business decisions.
If you‘re interested in becoming a data scientist or just learning the fundamentals, one of the first things you‘ll need to do is gain some programming skills. While there are several great languages for data science including Python, one of the most popular is R. R is an open-source programming language and software environment designed for statistical computing, data analysis, and creating data visualizations. It has a huge ecosystem of packages and libraries that allow you to do everything from cleaning and manipulating data to building machine learning models.
In this tutorial, we‘ll cover all the basics you need to get started learning data science and R from the ground up. Even if you‘re completely new to programming, by the end of this guide you‘ll have a solid foundation to begin your data science journey. Let‘s dive in!
Setting Up Your R Environment
The first step is to install R and RStudio on your computer:
-
Download and install the latest version of R from CRAN (Comprehensive R Archive Network). Choose the download link for your operating system.
-
Next, download and install RStudio, a powerful integrated development environment (IDE) that makes it much easier to write and run R code. You can get the free open-source desktop version of RStudio from their website. Select the RStudio Desktop version for your OS.
Once you have both R and RStudio installed, open up RStudio. You should see a window with three panes:
-
Console: This is the R console where you can type R commands and view output. You can use it as an interactive way to run R code line by line.
-
Source: This is a text editor where you can write and edit longer R scripts and programs.
-
Environment: This pane displays the objects (data, variables, functions, etc.) currently loaded into memory in your R session.
-
Plots/Help: This multipurpose pane can display plots/graphs you create, help documentation, files, and other output.
With RStudio open, you‘re ready to start learning R and using it for data science!
R Packages for Data Science
One of the best things about R is the huge number of open-source packages created by the community that provide pre-built functions and features for almost anything you want to do with data. Here are some of the most essential packages you should install:
-
tidyverse: This is a collection of packages for data manipulation, exploration and visualization that all follow a consistent design philosophy and work well together. Key packages in the tidyverse include dplyr, ggplot2, and tidyr. Install it with
install.packages("tidyverse") -
caret: Short for Classification And REgression Training, caret is a set of functions that help streamline the process of building and testing predictive models. Install with
install.packages("caret") -
data.table: Provides a high-performance version of base R‘s data.frame for working with large datasets. Install with
install.packages("data.table") -
stringr: Makes it easy to manipulate and work with string data. Install with
install.packages("stringr") -
lubridate: Has tools to parse, manipulate, and do arithmetic with date and time data. Install with
install.packages("lubridate")
To use a package after installing it, you have to load it in your R session or script with library(packagename). For example:
library(tidyverse)
library(caret)
It‘s a good idea to put all your library() calls at the top of your R script. We‘ll use several of these packages throughout the rest of this tutorial.
R Programming Basics
To really unleash the power of R and make use of the packages above, you need to understand some fundamentals of programming in R:
Objects and data types
In R, an object is anything that can be assigned a value or name. The most basic objects are:
– Numeric (real numbers)
– Character (text strings)
– Integer (whole numbers)
– Logical (TRUE/FALSE)
– Factor (categorical data)
You assign an object a value using either = or <-. For example:
x = 10
name <- "John"
is_student <- TRUE
Some other important objects in R:
- Vector: ordered collection of elements of the same type
- List: ordered collection of elements of different types
- Matrix: 2-dimensional collection of elements of the same type
- Data frame: 2-dimensional collection of elements of different types (like a table)
Functions
A function is a set of instructions that takes an input, does some computation, and returns an output. Many are built into R, but you can also define your own. Here‘s an example of a function to calculate the mean of a vector:
calc_mean <- function(x) {
total <- sum(x)
n <- length(x)
mean <- total / n
return(mean)
}
Control Structures
Control structures allow you to add logic to control the flow of an R program:
- if and else test a condition and execute different code based on whether the condition is true or false
- for loops repeat a block of code a fixed number of times
- while loops repeat a block of code as long as a condition is true
Here‘s an example using an if/else statement:
x <- 15
if (x > 10) {
print("x is greater than 10")
} else {
print("x is less than or equal to 10")
}
Now that you have some basic R programming concepts down, let‘s see how we can use them with some real data!
Importing and Exploring Data
Data for analysis can come in many different formats, but some of the most common are CSV (comma-separated values) files, Excel spreadsheets, and data from databases. R has built-in functions to read data from all these sources. Use read.csv() to import a CSV file:
mydata <- read.csv("data.csv")
Use the head() function to take a quick look at the first few rows of data:
head(mydata)
Some other useful functions for getting to know a new dataset:
str(mydata) # displays the structure of the data
summary(mydata) # shows summary statistics
dim(mydata) # dimensions (rows and columns)
names(mydata) # column names
table(mydata$color) # frequency counts of a factor variable
Data Visualization
After exploring a dataset, a natural next step is to create some visualizations to better understand patterns and relationships in the data. While base R has some plotting capabilities, I recommend using the ggplot2 package. ggplot2 follows a "grammar of graphics" that allows you to build up plots layer by layer in a logical way.
Here‘s an example of creating a scatter plot with ggplot2:
library(ggplot2)
ggplot(mydata, aes(x=height, y=weight)) +
geom_point(color="blue") +
labs(title="Height vs Weight",
x ="Height (in)",
y = "Weight (lbs)")
And here‘s how to make a bar chart showing counts by category:
ggplot(mydata, aes(x=color)) +
geom_bar(fill="darkgreen") +
labs(title="Distribution of Colors",
x="Color",
y="Count")
Visualizations are a powerful way to explore and communicate insights from data. Experiment with different plot types and options in ggplot2 to find the most effective ways to visualize your data.
Data Manipulation
Raw data often needs to be cleaned, transformed, and reformatted before it‘s ready for analysis. Some common data manipulation tasks in R:
- Subset rows and columns from a data frame
- Merge/join multiple data frames
- Reshape data from wide to long format
- Create new variables
- Summarize data
The dplyr package provides a set of functions that make data manipulation a breeze. Some of the most useful are:
- filter(): subset rows based on conditions
- select(): subset columns by name
- mutate(): create new columns
- arrange(): sort rows
- summarize(): reduce rows to a single summary statistic
- group_by(): group rows by one or more variables
- join functions: merge two data frames
Here‘s an example using some of these:
library(dplyr)
mydata %>%
filter(age > 25) %>%
group_by(city) %>%
summarize(avg_income = mean(income))
This takes the data, filters it to only include people over age 25, groups the data by city, and calculates the average income for each city. The %>% operator is called a pipe and makes it easy to chain functions together in a logical way.
Spend some time practicing data manipulation with dplyr. It may take a bit to get used to the syntax, but you‘ll quickly appreciate how much easier it makes working with data compared to base R.
Machine Learning
Machine learning is a powerful set of techniques data scientists use to uncover patterns in data and make predictions. Building a machine learning model usually involves the following steps:
- Split data into a training set and test set
- Apply a machine learning algorithm to the training data to build a model
- Use the model to make predictions on the test set
- Evaluate the model‘s prediction accuracy
R has many different packages for machine learning, but a great place to start is with the caret package. It provides a consistent interface to train and test models across a wide range of algorithms.
Linear Regression
Linear regression models the relationship between a numeric response variable and one or more numeric predictor variables. Use the lm() function to build a linear model:
fit <- lm(Price ~ Mileage, data=mydata)
summary(fit)
This fits a linear regression model predicting price of a car from its mileage. The summary() function shows the regression coefficients, p-values, and model fit statistics.
Decision Trees and Random Forests
Decision trees are a machine learning method that splits the data into smaller and smaller subsets based on the predictor variables, forming a tree-like structure. They can be used for both regression and classification problems.
Random forests are an ensemble learning method that combines a large number of decision trees to make a prediction. Each tree is built on a random subset of the data and predictor variables. The predictions are then averaged (regression) or majority vote is taken (classification) to get the final prediction. Random forests are very powerful and usually outperform single decision trees.
Here‘s an example of building a random forest model using caret:
library(caret)
trainIndex <- createDataPartition(mydata$Purchased, p = .8, list = FALSE)
train <- mydata[ trainIndex,]
test <- mydata[-trainIndex,]
rf_model <- train(Purchased ~ .,
data = train,
method = "rf")
predictions <- predict(rf_model, newdata = test)
confusionMatrix(predictions, test$Purchased)
This code splits the data into 80% training and 20% test sets, trains a random forest model to predict if a customer made a purchase based on various predictor variables, makes predictions on the test set, and finally prints a confusion matrix comparing the predicted vs actual values to assess accuracy.
Caret makes it very easy to try different algorithms just by changing the "method" argument. I encourage you to experiment with decision trees, random forests, and other methods like naive Bayes, support vector machines, etc.
Conclusion
Whew, that was a lot to cover! Congrats on making it through this whirlwind tour of data science and R programming. Of course, this really only scratched the surface of all there is to learn. But you should now have a solid foundation to continue building your data science skills.
Some next steps to consider:
- Work through a more comprehensive interactive course like those on DataCamp
- Pick a dataset you‘re interested in and try to answer some questions through exploratory analysis
- Learn about some other key machine learning concepts like cross validation, feature selection, and hyperparameter tuning
- Check out some of the other great R packages like broom (for tidying model outputs), rmarkdown (for creating data science reports), and shiny (for building interactive web apps with R)
- Try out data science and machine learning in Python and see how it compares to R
Data science is a fascinating and highly rewarding field to get into. And there‘s always something new to learn, whether it‘s the latest packages, new types of data to work with, or advanced techniques to master. The most important thing is to not get overwhelmed and just keep practicing and building your skills bit by bit. With tools like R becoming more accessible and a wealth of online resources available, it‘s never been easier to learn. So what are you waiting for? Get out there and start doing some data science!