R coef Function


coef() function extracts model coefficients from objects returned by modeling functions.
It's an alias of coefficients().

>x <- c(2,1,3,2,5,3.3,1);
>y <- c(4,2,6,3,8,6,2.2);


Plot the data:

Calculate the coefficients of linear model:

>m < lm(y~x) #Linear Regression Model
>c <- coef(lm(y~x))
>c

(Intercept) x
0.5487805 1.5975610


Draw the regression line:

>abline(c, col="blue")




Calculate the Correlation Coefficient (r2):

>cr = cor(y,x,method="pearson")
>cr = round(cr,digits=3)
>cr

[1] 0.978