R vs Python: Linear Regression
By: Christopher Zita
Tags: Python, Data Science, R, Linear Regression, Programming

R vs Python: Linear Regression

Photo from Unsplash from Christopher Gower

There are dozens of articles out there that compare R vs. Python from a subjective, opinion-based perspective. Both Python and R are great options for data analysis, or any work in the data science field.

But if your goal is to figure out which language is right for you, reading the opinion of someone else may not be helpful. One person’s “easy” is another person’s “hard,” and vice versa.

This article is the first of a series I will be writing that takes an objective look at how both languages handle everyday data science tasks so that you can look at them side-by-side, and see which one looks better for you.

The first topic I will be showing is how to do Linear Regression in R and Python.

Linear Regression: Background

Linear regression (also referred to as Simple Linear Regression) is the simplest type of regression model.

Regression models describe the relationship between variables by fitting a line to the observed data. Linear regression models use a straight line, and they estimate the relationship between two continuous variables.

Plot of what a Linear Regression graph looks like

The dataset I will be fitting the models on includes two variables. The first is the X variable (independent variable) which is the years of experience someone has at a job. The second is the Y variable (dependent variable) which is the salary that someone has. We can use linear regression here to see how strong the relationship is between years of experience and salary.

Example of what the dataset looks like

R

I’m going to start off by showing you how to perform linear regression in R. The first thing we have to do is import the dataset by using the read.csv() function. Inside the brackets you would input the file path of the dataset being used.

#Importing the dataset

dataset = read.csv(Salary_Data.csv)

Next we need to call a function that does linear regression for us. The good thing about R is that it already has a function programmed in it that does linear regression. It is the lm() function. Inside this function we will input our linear regression formula, and specify which dataset we will be using.

The linear regression formula includes a dependent variable and an independent variable. In our dataset we are trying to model the Salary based on Years of Experience. Therefore Salary is our dependent variable and Years of Experience is our independent variable. To show which is the dependent and independent variable in R we use a ~ sign (Dependent Variable ~ Independent Variable).

So the formula becomes: Salary ~ YearsExperience

# Fitting Linear Regression to the Dataset

model = lm(formula = Salary ~ YearsExperience,

data = dataset)

Now that a linear regression model has been fitted to our dataset, we can visualize the results using a library in R called ggplot2. ggplot2 is a library most famously used for data visualization in R.

#Visualizing the Linear Regression results

library(ggplot2)

ggplot() +

geom_point(aes(x = dataset$YearsExperience, y = dataset$Salary),

colour = 'red') +

geom_line(aes(x = dataset$YearsExperience, y = predict(model, newdata = dataset)),

colour = 'blue') +

ggtitle('Salary vs Experience') +

xlab('Years of experience') +

ylab('Salary')

From the plot above we can visualize our linear regression line (blue line) and see that it fits the data pretty good as most points are near that blue line. But just to be sure, there are ways we can further evaluate the linear regression model to make sure it fits the data well.

Evaluating Linear Regression in R

R has a special function called summary() that will output a bunch of statistics and analysis about the model fitted. These statistics and analysis go into further detail about the model and how it fits the data.

#Evaluating the Linear Regression model

summary(model)

Output of summary(model)

As you can see there a bunch of number and stats above. Most of these numbers come from the math behind linear regression and the others tell you you how well the model fits the data.

The main statistic we want to look at is Adjusted R-squared. In basic terminology Adjusted R-squared tells you how well the model fits the data on a scale from 0 to 1. 1 being the model fits the data perfectly, and 0 being the model does not fit the data at all. As you can see the Adjusted R-squared for our linear regression model is 0.963 which means this model fits the data extremely well which is what we want.

Final R code:

Python

Just like with R the first thing we must do is import our dataset. To do this though we must first import a library called pandas, then we can use the read_csv() function from the pandas library.

#Importing the dataset

import pandas as pd

dataset = pd.read_csv(Salary_Data.csv)

Next we need to call a function that does linear regression for us. Unlike in R, Python does not have a function programmed that does this already. We must then call a library that has a function that can perform linear regression. This library is the Sklearn library and we will get the linear regression function by typing: from sklearn.linear_model import LinearRegression

From this we need to call the linear regression function which is LinearRegression(), then we use the fit() function with the independent and dependent variables inside of it to fit the linear regression model to our dataset.

# Fitting Linear Regression to the Dataset

from sklearn.linear_model import LinearRegression

model = LinearRegression()

model.fit(dataset[['YearsExperience']], dataset[['Salary']])

Just like with R, Python has it’s own specialized library for data visualization called matplotlib, where we can visualize the results of the linear regression model.

#Visualizing the Linear Regression results

import matplotlib.pyplot as plt

plt.scatter(dataset[['YearsExperience']], dataset[['Salary']], color = 'red')

plt.plot(dataset[['YearsExperience']], model.predict(dataset[['YearsExperience']]), color = 'blue')

plt.title('Salary vs Experience')

plt.xlabel('Years of Experience')

plt.ylabel('Salary')

plt.show()

From the plot above we can visualize our linear regression line (blue line) and see that it looks nearly identical to the one in R which means it fits the data pretty good as again most points are near that blue line. But again we must evaluate the model even further to really tell if it fits the data well.

Evaluating Linear Regression in Python

Unfortunately Python does not have a summary like function in R that gives a bunch of statistics and analysis on the model fitted. Instead it has functions that will show similar data, but they need to be run line by line.

Just like in R we are interested in the Adjusted R-squared statistic to determine if the model fits the data well. To do this in Python we use the score() function on our model with the independent and dependent variables inside of it.

#Adjusted R-squared

model.score(dataset[['YearsExperience']], dataset[['Salary']])

The output is 0.957 which is a little lower than R’s but it is still very close to 1, which means the linear regression model in Python still fits the data very well.

Final Python Code:

Conclusion

As you can see there are some differences in the syntax between R and Python for fitting a linear regression model. These differences though have nearly no affect on the output as they were pretty much the same for our dataset. The option of the two just comes down to which one you think looks better for you.

Thank you for reading this article! and again this is the first of a series I will be doing comparing R and Python for various data science topics.

So if you’re interested and want to read more, just follow me so you get notified when I upload a new article.