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 Rates of higher education in percentages of population Let's load the datasets education and gdp and merge them in a unique data frame as of the third line. Built the plot between GDP and Calculate the correlation between the two variable GDP and Education Make the line for the linear regression with the function lm() linear model. And visualize it 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. 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:
1,Liechtenstein,55100
2,Australia,40199
3,Luxembourg,38000
4,Qatar,37030
5,Brunei,31000
5,China,12000
6,Bermuda,9000
7,Bangladesh,4000
Country,Education
Australia,49
Bangladesh,11
Brunei,29
China,20
Liechtenstein,45
Qatar,39
Luxembourg,39
Bermuda,13
education = read.csv("C:\Users\Alket\Documents\education.csv")
gdp = read.table("gdp.csv", sep=",", header=TRUE)
countries = merge(x = gdp, y = piracy)
plot(countries$GDP, countries$Education)
cor.test(countries$GDP, countries$Education)
line = lm(countries$Education ~ countries$GDP)
abline(line)
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
Ok, thanks.
This post was resteemed by @steemitrobot! and Got 10 Upvotes
Good Luck!
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.