In: Statistics and Probability
C |
D |
3 |
2 |
6 |
7 |
8 |
5 |
9 |
4 |
1 |
0 |
3 |
4 |
Plot the data and line of best fit in R and paste it into this document
Code:
Plot:
Since we are not given which vaiable is dependent and which is independent, we will consider both the cases to solve this example. You can take any one of the as per the requirement.
Code :
C = c(3,6,8,9,1,3)
D = c(2,7,5,4,0,4)
### Part 1
# Let C be the dependent variable and D be the independent
variable.
>reg1 = lm( C ~ D )
>coef1 = reg1$coefficients
>coef1
(Intercept) D
1.7500000 0.8863636
>coef1 = unname(coef1) # returns only values
>plot(x = D, y = C, main = 'C vs D')
>abline(a = coef1[1], b = coef1[2]) # gives regression line, a
is intercept and b is slope
### Part 2
# Let D be the dependent variable and C be the independent
variable.
>reg2 = lm( D ~ C )
>coef2 = reg2$coefficients
>coef2
(Intercept) C
1.066667 0.520000
>coef2 = unname(coef2) # returns only values
>plot(x = C, y = D, main = 'D vs C')
>abline(a = coef2[1], b = coef2[2]) # gives regression line, a
is intercept and b is slope