Friday, 22 May 2015

How to fit a multiple linear regression equation in R.


> y=scan()    [enter y values]
1: 10
2: 20
3: 50
4: 70
5: 90
6: 100
7: 130
8: 150
9: 155
10: 160
11: 155
12
Read 11 items
> x1=scan()    [enter x1values]
1: 50
2: 50
3: 50
4: 51
5: 52
6: 53
7: 54
8: 55
9: 55
10: 56
11: 58
12:
Read 11 items
> x2=scan()       [enter x2 values]
1: 1
2: 1.2
3: 1.5
4: 2
5: 2.5
6: 3
7: 3.3
8: 4
9: 6
10: 6.5
11: 7.5
12:
Read 11 items
> df=data.frame(y,x1,x2)    [represent data in tabular form]
> df
     y x1  x2
1   10 50 1.0
2   20 50 1.2
3   50 50 1.5
4   70 51 2.0
5   90 52 2.5
6  100 53 3.0
7  130 54 3.3
8  150 55 4.0
9  155 55 6.0
10 160 56 6.5
11 155 58 7.5
> cor(df)                                                     [get rank correlation coefficient of y on x1 and x2]
           y        x1        x2
y  1.0000000 0.9350985 0.8942423
x1 0.9350985 1.0000000 0.9617563
x2 0.8942423 0.9617563 1.0000000
> model=lm(y~x1+x2)                            [to fit regression equation of y on x1 and x2]
> model

Call:                                                         [output result]
lm(formula = y ~ x1 + x2)

Coefficients:
(Intercept)           x1           x2
   -976.197       20.365       -1.682



No comments:

Post a Comment