Uploading the Data

In this example we are going to use the score and hour of study data that we created for our class. We have to upload it first and it was saved as a csv file.

Data= read.csv("C:/STAT 111/Example Data/Data.csv")
Data
##    Student  Sc Hr Prediction       e
## 1        1 100  6     8.0487 -2.0487
## 2        2  20  1     0.0007  0.9993
## 3        3  90  5     7.0427 -2.0427
## 4        4  40  2     2.0127 -0.0127
## 5        5 100  8     8.0487 -0.0487
## 6        6 100 10     8.0487  1.9513
## 7        7  60  3     4.0247 -1.0247
## 8        8  90  6     7.0427 -1.0427
## 9        9  30  3     1.0067  1.9933
## 10      10  50  2     3.0187 -1.0187
## 11      11  65  4     4.5277 -0.5277
## 12      12  77  3     5.7349 -2.7349
## 13      13 100  8     8.0487 -0.0487
## 14      14 100  8     8.0487 -0.0487
## 15      15  90  6     7.0427 -1.0427
## 16      16  88  6     6.8415 -0.8415
## 17      17  78  5     5.8355 -0.8355
## 18      18  90  8     7.0427  0.9573
## 19      19  56  2     3.6223 -1.6223
## 20      20 100  8     8.0487 -0.0487
## 21      21 100  8     8.0487 -0.0487
## 22      22  80  5     6.0367 -1.0367
## 23      23  66  2     4.6283 -2.6283
## 24      24  88  8     6.8415  1.1585
## 25      25  99  8     7.9481  0.0519
## 26      26  67  3     4.7289 -1.7289
## 27      27 100  8     8.0487 -0.0487
## 28      28  77  7     5.7349  1.2651
## 29      29  88  7     6.8415  0.1585
## 30      30  85  8     6.5397  1.4603
## 31      31  88  8     6.8415  1.1585
## 32      32  90  9     7.0427  1.9573
## 33      33  92  6     7.2439 -1.2439
## 34      34 100 10     8.0487  1.9513
## 35      35 100  8     8.0487 -0.0487
## 36      36  55  2     3.5217 -1.5217
## 37      37  20  1     0.0007  0.9993
## 38      38   0  0    -2.0113  2.0113
## 39      39  88  8     6.8415  1.1585
## 40      40  45  3     2.5157  0.4843
## 41      41  94 10     7.4451  2.5549
## 42      42  95  8     7.5457  0.4543
## 43      43  93  8     7.3445  0.6555
## 44      44 100  9     8.0487  0.9513
## 45      45  45  1     2.5157 -1.5157
## 46      46 100  9     8.0487  0.9513
## 47      47  87  6     6.7409 -0.7409
## 48      48  56  3     3.6223 -0.6223
## 49      49  90  8     7.0427  0.9573
## 50      50  99  8     7.9481  0.0519

In the data s=the coloumn Sc means the score of the student and Hr means the hour of study they have done in a week.

Fitting Least Squares Regression

Here in first excercise our response variable is Hr or hour of study and score is our explanatory varible.

plot (x= Data$Sc, y= Data$Hr, xlab= "Score", ylab= "Hours of Study")

m1= lm(Hr~Sc, data= Data)
m1$coefficients
## (Intercept)          Sc 
##  -2.0112678   0.1006299

Let’s do the Opposite

Now lets consider score as our response variable and hours of study as our explanatory variable.

Scatter Plot

plot (x= Data$Hr, y= Data$Sc, xlab= "Hours of Study", ylab= "Score")

Linear Regression

m2= lm(Sc~Hr, data= Data)
m2$coefficients
## (Intercept)          Hr 
##   32.163022    7.859553