correlation and Regression using R programming

How do I calculate correlation 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 xand y.

Example1

What is the correlation between X and Y if y = (100,94,150,160,180), and x = (23,21,32,40,45).

Solution

Codes>>

y=c(100,94,150,160,180)
x =c(23,21,32,40,45)
cor(x,y)

Result>>

> y=c(100,94,150,160,180)
> x =c(23,21,32,40,45)
> cor(x,y)
[1] 0.978522

Example2

What is the correlation between X and Y if y =(100,94,150,160,180,80), and x = (23,21,32,40,45, 10).

Solution

Codes>>

y=c(100,94,150,160,180,80)
x =c(23,21,32,40,45, 10)
cor(x,y)

Result>>

> y=c(100,94,150,160,180,80)
> x =c(23,21,32,40,45, 10)
> cor(x,y)
[1] 0.972529

How to calculate the regression

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.

How do I calculate the beta and alpha of regression using R programming.

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

Example1

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

Codes>>

y=c(100,94,150,160,180,80)
x =c(23,21,32,40,45, 10)
lm(y ~ x)

Result>>

> 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

Example2

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.

Solution

Codes>>
y=c(100,94,150,160,180)
x =c(23,21,32,40,45)
lm(y ~ x)
Result>>
>  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.

Example3

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.

Solution.

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.

codes>>
x=20
Y=3.075*x+39.693 
print(Y)

Result>>

> x=20
> Y=3.075*x+39.693 
> print(Y)
[1] 101.193

Example4

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

Solution.

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.

Codes>>

Y=200
x=(Y-39.693)/3.075
print(x)

Result>>>

> 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