linear regresssion with lm

Author

kirit ved

linear regression with r

creating dataframe

set.seed(1234)
n=10
x=1:n
y=23.67*x+54.78+runif(n,-50,50)
d=data.frame(x,y)
d
    x         y
1   1  39.82034
2   2 114.34994
3   3 136.71747
4   4 161.79794
5   5 209.22154
6   6 210.83106
7   7 171.41958
8   8 217.39505
9   9 284.41838
10 10 292.90511

checking correlation

cor(x,y)
[1] 0.9335673

fitting values

mylm=lm(y~x,d);mylm;summary(mylm);str(mylm)

Call:
lm(formula = y ~ x, data = d)

Coefficients:
(Intercept)            x  
      53.82        23.65  

Call:
lm(formula = y ~ x, data = d)

Residuals:
   Min     1Q Median     3Q    Max 
-47.94 -18.56  12.59  14.69  37.16 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)    53.82      19.92   2.702    0.027 *  
x              23.65       3.21   7.367 7.86e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 29.16 on 8 degrees of freedom
Multiple R-squared:  0.8715,    Adjusted R-squared:  0.8555 
F-statistic: 54.28 on 1 and 8 DF,  p-value: 7.861e-05
List of 12
 $ coefficients : Named num [1:2] 53.8 23.6
  ..- attr(*, "names")= chr [1:2] "(Intercept)" "x"
 $ residuals    : Named num [1:10] -37.6 13.2 12 13.4 37.2 ...
  ..- attr(*, "names")= chr [1:10] "1" "2" "3" "4" ...
 $ effects      : Named num [1:10] -581.5 214.8 18.2 21.6 47.3 ...
  ..- attr(*, "names")= chr [1:10] "(Intercept)" "x" "" "" ...
 $ rank         : int 2
 $ fitted.values: Named num [1:10] 77.5 101.1 124.8 148.4 172.1 ...
  ..- attr(*, "names")= chr [1:10] "1" "2" "3" "4" ...
 $ assign       : int [1:2] 0 1
 $ qr           :List of 5
  ..$ qr   : num [1:10, 1:2] -3.162 0.316 0.316 0.316 0.316 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:10] "1" "2" "3" "4" ...
  .. .. ..$ : chr [1:2] "(Intercept)" "x"
  .. ..- attr(*, "assign")= int [1:2] 0 1
  ..$ qraux: num [1:2] 1.32 1.27
  ..$ pivot: int [1:2] 1 2
  ..$ tol  : num 1e-07
  ..$ rank : int 2
  ..- attr(*, "class")= chr "qr"
 $ df.residual  : int 8
 $ xlevels      : Named list()
 $ call         : language lm(formula = y ~ x, data = d)
 $ terms        :Classes 'terms', 'formula'  language y ~ x
  .. ..- attr(*, "variables")= language list(y, x)
  .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:2] "y" "x"
  .. .. .. ..$ : chr "x"
  .. ..- attr(*, "term.labels")= chr "x"
  .. ..- attr(*, "order")= int 1
  .. ..- attr(*, "intercept")= int 1
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. ..- attr(*, "predvars")= language list(y, x)
  .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
  .. .. ..- attr(*, "names")= chr [1:2] "y" "x"
 $ model        :'data.frame':  10 obs. of  2 variables:
  ..$ y: num [1:10] 39.8 114.3 136.7 161.8 209.2 ...
  ..$ x: int [1:10] 1 2 3 4 5 6 7 8 9 10
  ..- attr(*, "terms")=Classes 'terms', 'formula'  language y ~ x
  .. .. ..- attr(*, "variables")= language list(y, x)
  .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. ..$ : chr [1:2] "y" "x"
  .. .. .. .. ..$ : chr "x"
  .. .. ..- attr(*, "term.labels")= chr "x"
  .. .. ..- attr(*, "order")= int 1
  .. .. ..- attr(*, "intercept")= int 1
  .. .. ..- attr(*, "response")= int 1
  .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. .. ..- attr(*, "predvars")= language list(y, x)
  .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
  .. .. .. ..- attr(*, "names")= chr [1:2] "y" "x"
 - attr(*, "class")= chr "lm"
plot(x,y,col="lightgreen",lwd=5,pch=23,main="plot of y vs x",xlab="x",ylab="y",type="b")
lines(d$x,mylm$fitted.values)

x1=c(7.3,2.9,6.78)
dpred=data.frame(x=x1)
dpred
     x
1 7.30
2 2.90
3 6.78
ypred=predict(mylm,dpred,add=T)
ypred
       1        2        3 
226.4561 122.3999 214.1585 
dpred$ypred=ypred
dpred
     x    ypred
1 7.30 226.4561
2 2.90 122.3999
3 6.78 214.1585