24
correlation and Regression using R programming
To calculate correlation between two data sets is quite easy all you need is to make use of cor() function and insert the parameters x
and y
.
What is the correlation between X and Y if y = (100,94,150,160,180), and x = (23,21,32,40,45).
y=c(100,94,150,160,180)
x =c(23,21,32,40,45)
cor(x,y)
> y=c(100,94,150,160,180)
> x =c(23,21,32,40,45)
> cor(x,y)
[1] 0.978522
What is the correlation between X and Y if y =(100,94,150,160,180,80), and x = (23,21,32,40,45, 10).
y=c(100,94,150,160,180,80)
x =c(23,21,32,40,45, 10)
cor(x,y)
> y=c(100,94,150,160,180,80)
> x =c(23,21,32,40,45, 10)
> cor(x,y)
[1] 0.972529
To deal with regression there are two values very important they are : the beta and the alpha. they are used to form the equation that connect the two items.
i.e y=bx + a. where a and b represent the alpha and beta respectively.
note: alpha is the intercept why b is the slope.
To calculate your alpha and beta using code we make use of lm()
function.
where the parameters are place inside the lm() functions. let's take a look at some examples
If x = [23,21,32,40,45, 10] and y = [100,94,150,160,180,80]. Obtain the linear model of the relationship between y and x.
Solution
y=c(100,94,150,160,180,80)
x =c(23,21,32,40,45, 10)
lm(y ~ x)
> y=c(100,94,150,160,180,80)
> x =c(23,21,32,40,45, 10)
> lm(y ~ x)
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
39.693 3.075
Conclusion: from the result above alpha =39.693 while Beta=3.075.
and the equation is Y=3.075x+39.693
If x = [23,21,32,40,45] and y = [100,94,150,160,180]. Obtain the linear model of the relationship between y and x.
y=c(100,94,150,160,180)
x =c(23,21,32,40,45)
lm(y ~ x)
> y=c(100,94,150,160,180)
> x =c(23,21,32,40,45)
> lm(y ~ x)
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
22.071 3.563
Conclusion: from the result above alpha =22.071 while Betaa=3.563.
and the equation is Y=3.563x+22.071.
If x = [23,21,32,40,45, 10] and y = [100,94,150,160,180,80]. What would be the value of y if x = 20.
Note the we already write codes for this item in Example1. and our equation is Y=3.075x+39.693. so let's find the value of y when x=20.
x=20
Y=3.075*x+39.693
print(Y)
> x=20
> Y=3.075*x+39.693
> print(Y)
[1] 101.193
If x = [23,21,32,40,45, 10] and y = [100,94,150,160,180,80]. What would be the value of x if y = 200
Note the we already write codes for this item in Example1. and our equation is Y=3.075x+39.693. since we need to find x when y=200 therefore we have to make x the subject of the formula.
Working that out we get x=(Y-39.693)/3.075. Now let's write the codes.
Y=200
x=(Y-39.693)/3.075
print(x)
> Y=200
> x=(Y-39.693)/3.075
> print(x)
[1] 52.13236
Yeah. that is how easy it is to use codes to solve most of the problem relating with correletion and regressions. I hope you find this article helpful? Alright you can chat me up if you have any doubt or observation on 07045225718
or 09153036869
it is still your guy Maxwizard. Enjoy coding!
24