How to implement linear regression in R and start make predictions

Linear regression is an approach to modeling the relationship between a dependent variable Y and one or more independent variables X.
When you have one independent variable it is called simple linear regression. For more than one independent variable, the process is called multiple linear regression. In all forms of regression analysis, linear regression focuses on the conditional probability distribution of Y given X.

Basically, linear regression can be seen as a prediction technique during which we try to predict Y given the value of X

This is done for example in trend estimation as you can read from Wikipedia

For implementing it in R we have to

1- read the dataset and load it in a dataframe.

2- print out the R graphics.

The dataset we are going to use contains data from diffrent countries. In those countries has been observed a relation between the GDP per capita and the rate of inhabitants with higher education degrees in that country.

The dataset we are going to use are the gdp dataset per country and the rate of higer education per country. is the following :

The gdp per capit dataset

Rank,Country,GDP
1,Liechtenstein,55100
2,Australia,40199
3,Luxembourg,38000
4,Qatar,37030
5,Brunei,31000
5,China,12000
6,Bermuda,9000
7,Bangladesh,4000

Rates of higher education in percentages of population


Country,Education
Australia,49
Bangladesh,11
Brunei,29
China,20
Liechtenstein,45
Qatar,39
Luxembourg,39
Bermuda,13

Let's load the datasets education and gdp and merge them in a unique data frame as of the third line.


education = read.csv("C:\Users\Alket\Documents\education.csv")
gdp = read.table("gdp.csv", sep=",", header=TRUE)
countries = merge(x = gdp, y = piracy)

Built the plot between GDP and


plot(countries$GDP, countries$Education)

Calculate the correlation between the two variable GDP and Education

cor.test(countries$GDP, countries$Education)

Make the line for the linear regression with the function lm() linear model.


line = lm(countries$Education ~ countries$GDP)

And visualize it


abline(line)

The R graphic will be a plot as the one in the picture: It can be clearly seen that the gdp per capita of a country is strongly related with per possibilities of study of its inhabitants.

inhabit.PNG

Furthermore, there can be noticed that there are two clusters of countries: the ones in the top right that are: the rich coutries and the ones in the bottom left the low-income countries

If you liked this R guide on linear regression then please share it:

Sort:  

wow nice post, thanks for your upvote also be happy if you comment and follow

You are welcome :-)

Hi, datatreemap! I just resteemed your post!
I can also re-steem and upvote some of your other posts


Curious? Check out @resteembot's' introduction post
PS: If your reputation is lower than 30 re-blogging with @resteembot only costs 0.001 SBD


This post was resteemed by @steemitrobot! and Got 10 Upvotes
Good Luck!

Resteem your post just send 0.100 SBD or Steem with your post ID on memo. We have over 2000 followers. Take our service to reach more People.

Pro Plan: just send 1 SBD or Steem with your post ID on memo we will resteem your post and send 10 upvotes from our Associate Accounts.

The @steemitrobot users are a small but growing community.
Check out the other resteemed posts in steemitrobot's feed.
Some of them are truly great. Please upvote this comment for helping me grow.

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.097
BTC 64507.51
ETH 1870.78
USDT 1.00
SBD 0.38