#Exercise 1(a)
set.seed(123)
n= 100
x <- 1:n/(n+1)
head(x)
## [1] 0.00990099 0.01980198 0.02970297 0.03960396 0.04950495 0.05940594
tail(x)
## [1] 0.9405941 0.9504950 0.9603960 0.9702970 0.9801980 0.9900990
beta0 <- 10
beta1 <- 2
expy <- beta0 + beta1*x
mu <- 0.0
sigma <- 2.7
err <- rnorm(n, mu, sigma)
head(err)
## [1] -1.5132842 -0.6214792 4.2085124 0.1903727 0.3490769 4.6306755
tail(err)
## [1] 3.673762 -1.620701 5.905799 4.138049 -0.636391 -2.771336
yobs <- expy + err
require(ggplot2)
## Loading required package: ggplot2
p <- qplot(x, yobs)
p <- p + geom_abline(intercept = beta0, slope = beta1)
p

cor(x, yobs)
## [1] 0.2994951
my_data <- data.frame(x, yobs)
my_data
## x yobs
## 1 0.00990099 8.506518
## 2 0.01980198 9.418125
## 3 0.02970297 14.267918
## 4 0.03960396 10.269581
## 5 0.04950495 10.448087
## 6 0.05940594 14.749487
## 7 0.06930693 11.383088
## 8 0.07920792 6.742751
## 9 0.08910891 8.323715
## 10 0.09900990 8.994732
## 11 0.10891089 13.522843
## 12 0.11881188 11.209121
## 13 0.12871287 11.339509
## 14 0.13861386 10.576071
## 15 0.14851485 8.796259
## 16 0.15841584 15.141497
## 17 0.16831683 11.680830
## 18 0.17821782 5.046569
## 19 0.18811881 12.269899
## 20 0.19801980 9.119503
## 21 0.20792079 7.532718
## 22 0.21782178 9.847111
## 23 0.22772277 7.685234
## 24 0.23762376 8.507241
## 25 0.24752475 8.807443
## 26 0.25742574 5.960780
## 27 0.26732673 12.796678
## 28 0.27722772 10.968563
## 29 0.28712871 7.501288
## 30 0.29702970 13.979360
## 31 0.30693069 11.765315
## 32 0.31683168 9.836970
## 33 0.32673267 13.070305
## 34 0.33663366 13.044228
## 35 0.34653465 12.911338
## 36 0.35643564 12.572200
## 37 0.36633663 12.228251
## 38 0.37623762 10.585314
## 39 0.38613861 9.946178
## 40 0.39603960 9.764808
## 41 0.40594059 8.936172
## 42 0.41584158 10.270307
## 43 0.42574257 7.434915
## 44 0.43564356 16.727468
## 45 0.44554455 14.152587
## 46 0.45544554 7.878498
## 47 0.46534653 9.842904
## 48 0.47524752 9.690526
## 49 0.48514851 13.076203
## 50 0.49504950 10.765003
## 51 0.50495050 11.693861
## 52 0.51485149 10.952627
## 53 0.52475248 10.933755
## 54 0.53465347 14.764533
## 55 0.54455446 10.479527
## 56 0.55445545 15.203382
## 57 0.56435644 6.947080
## 58 0.57425743 12.726972
## 59 0.58415842 11.502723
## 60 0.59405941 11.771161
## 61 0.60396040 12.232947
## 62 0.61386139 9.871449
## 63 0.62376238 10.347865
## 64 0.63366337 8.517173
## 65 0.64356436 8.393292
## 66 0.65346535 12.126458
## 67 0.66336634 12.536899
## 68 0.67326733 11.489646
## 69 0.68316832 13.856459
## 70 0.69306931 16.921367
## 71 0.70297030 10.080156
## 72 0.71287129 5.190987
## 73 0.72277228 14.161039
## 74 0.73267327 9.550504
## 75 0.74257426 9.627525
## 76 0.75247525 14.273993
## 77 0.76237624 10.755865
## 78 0.77227723 8.248617
## 79 0.78217822 12.053876
## 80 0.79207921 11.209152
## 81 0.80198020 11.619524
## 82 0.81188119 12.664019
## 83 0.82178218 10.642782
## 84 0.83168317 13.403183
## 85 0.84158416 11.087855
## 86 0.85148515 12.598782
## 87 0.86138614 14.684238
## 88 0.87128713 12.917564
## 89 0.88118812 10.882361
## 90 0.89108911 14.883959
## 91 0.90099010 14.484441
## 92 0.91089109 13.302454
## 93 0.92079208 12.486160
## 94 0.93069307 10.166040
## 95 0.94059406 15.554950
## 96 0.95049505 10.280289
## 97 0.96039604 17.826591
## 98 0.97029703 16.078643
## 99 0.98019802 11.324005
## 100 0.99009901 9.208862
#Exercise 1(b)
sigma2 <- 0.7
err2 <- rnorm(n, mu, sigma2)
yobs2 <- expy + err2
p2 <- qplot(x, yobs2)
p2 <- p2 + geom_abline(intercept = beta0, slope = beta1)
p2

cor(x,yobs2)
## [1] 0.6884403
my_data <- data.frame(x, yobs2)
my_data
## x yobs2
## 1 0.00990099 9.522517
## 2 0.01980198 10.219423
## 3 0.02970297 9.886722
## 4 0.03960396 9.835928
## 5 0.04950495 9.432877
## 6 0.05940594 10.087292
## 7 0.06930693 9.589181
## 8 0.07920792 8.990856
## 9 0.08910891 9.912059
## 10 0.09900990 10.841317
## 11 0.10891089 9.815079
## 12 0.11881188 10.663199
## 13 0.12871287 9.124908
## 14 0.13861386 10.238334
## 15 0.14851485 10.660615
## 16 0.15841584 10.527639
## 17 0.16831683 10.410607
## 18 0.17821782 9.907941
## 19 0.18811881 9.781445
## 20 0.19801980 9.679149
## 21 0.20792079 10.498194
## 22 0.21782178 9.772411
## 23 0.22772277 10.112055
## 24 0.23762376 10.295983
## 25 0.24752475 11.785753
## 26 0.25742574 10.058487
## 27 0.26732673 10.699424
## 28 0.27722772 10.609028
## 29 0.28712871 9.900958
## 30 0.29702970 10.544144
## 31 0.30693069 11.625047
## 32 0.31683168 10.949716
## 33 0.32673267 10.682328
## 34 0.33663366 10.377520
## 35 0.34653465 9.255796
## 36 0.35643564 11.504807
## 37 0.36633663 9.710225
## 38 0.37623762 11.270439
## 39 0.38613861 12.108650
## 40 0.39603960 9.781354
## 41 0.40594059 11.303130
## 42 0.41584158 10.648145
## 43 0.42574257 9.750984
## 44 0.43564356 9.811020
## 45 0.44554455 9.770014
## 46 0.45544554 10.539257
## 47 0.46534653 9.907464
## 48 0.47524752 11.432037
## 49 0.48514851 12.440373
## 50 0.49504950 10.089178
## 51 0.50495050 11.561318
## 52 0.51485149 11.568033
## 53 0.52475248 11.282047
## 54 0.53465347 10.363443
## 55 0.54455446 11.005492
## 56 0.55445545 10.912634
## 57 0.56435644 11.522806
## 58 0.57425743 10.887808
## 59 0.58415842 11.852198
## 60 0.59405941 10.925912
## 61 0.60396040 11.944819
## 62 0.61386139 10.493299
## 63 0.62376238 10.365416
## 64 0.63366337 13.536055
## 65 0.64356436 10.995328
## 66 0.65346535 11.515690
## 67 0.66336634 11.772331
## 68 0.67326733 11.007888
## 69 0.68316832 11.728140
## 70 0.69306931 11.644414
## 71 0.70297030 11.255174
## 72 0.71287129 11.471448
## 73 0.72277228 11.421697
## 74 0.73267327 12.955263
## 75 0.74257426 10.966213
## 76 0.75247525 10.737753
## 77 0.76237624 11.551204
## 78 0.77227723 11.761891
## 79 0.78217822 11.869923
## 80 0.79207921 11.263303
## 81 0.80198020 10.859632
## 82 0.81188119 12.507992
## 83 0.82178218 11.398809
## 84 0.83168317 11.057507
## 85 0.84158416 11.517773
## 86 0.85148515 11.564947
## 87 0.86138614 12.499716
## 88 0.87128713 11.801890
## 89 0.88118812 12.290214
## 90 0.89108911 11.432674
## 91 0.90099010 11.952092
## 92 0.91089109 11.594502
## 93 0.92079208 11.907793
## 94 0.93069307 11.234632
## 95 0.94059406 10.963627
## 96 0.95049505 13.299039
## 97 0.96039604 12.341288
## 98 0.97029703 11.064704
## 99 0.98019802 11.532580
## 100 0.99009901 11.150362
#Exercise 1(c)
sigma3 <- 0.27
err3 <- rnorm(n, mu, sigma3)
yobs3 <- expy + err3
p3 <- qplot(x, yobs3)
p3 <- p3 + geom_abline(intercept = beta0, slope = beta1)
p3

cor(x,yobs3)
## [1] 0.9215917
my_data <- data.frame(x, yobs3)
my_data
## x yobs3
## 1 0.00990099 10.613481
## 2 0.01980198 10.393955
## 3 0.02970297 9.987817
## 4 0.03960396 10.225870
## 5 0.04950495 9.987138
## 6 0.05940594 9.990225
## 7 0.06930693 9.925691
## 8 0.07920792 9.997869
## 9 0.08910891 10.623963
## 10 0.09900990 10.183432
## 11 0.10891089 10.250018
## 12 0.11881188 10.303419
## 13 0.12871287 10.590194
## 14 0.13861386 10.137890
## 15 0.14851485 10.029053
## 16 0.15841584 10.769270
## 17 0.16831683 10.217520
## 18 0.17821782 10.161208
## 19 0.18811881 10.042444
## 20 0.19801980 10.049166
## 21 0.20792079 10.260869
## 22 0.21782178 10.602500
## 23 0.22772277 10.755105
## 24 0.23762376 10.666296
## 25 0.24752475 10.396862
## 26 0.25742574 10.530984
## 27 0.26732673 10.344412
## 28 0.27722772 10.360807
## 29 0.28712871 10.813113
## 30 0.29702970 10.319849
## 31 0.30693069 11.141791
## 32 0.31683168 10.609277
## 33 0.32673267 10.711391
## 34 0.33663366 10.473865
## 35 0.34653465 10.537984
## 36 0.35643564 10.357277
## 37 0.36633663 10.683283
## 38 0.37623762 10.865600
## 39 0.38613861 10.859839
## 40 0.39603960 10.581064
## 41 0.40594059 10.598953
## 42 0.41584158 10.696090
## 43 0.42574257 11.255422
## 44 0.43564356 10.564215
## 45 0.44554455 10.842745
## 46 0.45544554 11.424529
## 47 0.46534653 10.903430
## 48 0.47524752 10.583338
## 49 0.48514851 10.790809
## 50 0.49504950 11.121173
## 51 0.50495050 10.908488
## 52 0.51485149 10.877996
## 53 0.52475248 10.956647
## 54 0.53465347 11.093741
## 55 0.54455446 11.520706
## 56 0.55445545 11.084998
## 57 0.56435644 11.420529
## 58 0.57425743 11.318818
## 59 0.58415842 11.137634
## 60 0.59405941 10.774235
## 61 0.60396040 11.067219
## 62 0.61386139 11.095458
## 63 0.62376238 11.260256
## 64 0.63366337 11.618380
## 65 0.64356436 11.906260
## 66 0.65346535 11.724778
## 67 0.66336634 11.290782
## 68 0.67326733 10.872272
## 69 0.68316832 11.261366
## 70 0.69306931 11.410225
## 71 0.70297030 11.634094
## 72 0.71287129 11.685625
## 73 0.72277228 11.630308
## 74 0.73267327 11.088622
## 75 0.74257426 11.714552
## 76 0.75247525 11.384380
## 77 0.76237624 11.571949
## 78 0.77227723 11.564683
## 79 0.78217822 11.679961
## 80 0.79207921 11.590821
## 81 0.80198020 11.153742
## 82 0.81188119 11.822616
## 83 0.82178218 11.747792
## 84 0.83168317 11.591640
## 85 0.84158416 11.715067
## 86 0.85148515 11.739161
## 87 0.86138614 11.782448
## 88 0.87128713 12.185603
## 89 0.88118812 11.703233
## 90 0.89108911 11.827556
## 91 0.90099010 12.117444
## 92 0.91089109 12.106411
## 93 0.92079208 12.150805
## 94 0.93069307 11.705470
## 95 0.94059406 12.421858
## 96 0.95049505 11.918999
## 97 0.96039604 12.424842
## 98 0.97029703 11.575850
## 99 0.98019802 11.966062
## 100 0.99009901 12.317675
#Observation Exercise 1
# cor(x, yobs) = 0.2994951
# cor(x,yobs2) = 0.6884403
# cor(x,yobs3) = 0.9215917
# The 1st observation in exercise 1 is that as soon as we change the sigma the distriburion of the points also gets changed.
# Exercise 1(a)--> cor(x, yobs) = 0.2994951
# When sigma change is equal to 2.7 we see the points are scattered.
# Exercise 1(b)--> cor(x,yobs2) = 0.6884403
#output values are seen closer to the value line when sigma changes from 2.7 to 0.7.
# Exercise 1(c)--> cor(x,yobs3) = 0.9215917
#We see the points are more closer to the line when it changes further from 0.7 to 0.27.
## Thus as the sigma changes we see there is a change in correlation measure and Y gets gets more and more closer to the expected line as sigma changes from 2.7 to 0.7 to 0.27 .
## Further as correlation gets closer to 1,the points are seen closer to the expected line.
#Exercise 2(d)
n <- 10:80
x=n
head(x)
## [1] 10 11 12 13 14 15
tail(x)
## [1] 75 76 77 78 79 80
beta0 <- 10
beta1 <- 2
expy <- beta0 + beta1*x
mu <- 0.0
sigma <-2.7
err <- rnorm(x, mu, sigma)
head(err)
## [1] -1.9311539 -2.0322602 -2.5340545 -2.8417859 -1.1803307 0.8941838
tail(err)
## [1] -0.5664416 1.0210530 -2.5526038 2.3136921 -1.2448035 6.5252881
yobs4 <- expy + err
require(ggplot2)
p4 <- qplot(x, yobs4, xlab = 'x4')
p4 <- p4 + geom_abline(intercept = beta0, slope = beta1)
p4

cor(x,yobs4)
## [1] 0.9975831
my_data <- data.frame(x, yobs4)
my_data
## x yobs4
## 1 10 28.06885
## 2 11 29.96774
## 3 12 31.46595
## 4 13 33.15821
## 5 14 36.81967
## 6 15 40.89418
## 7 16 36.56163
## 8 17 44.57235
## 9 18 49.33902
## 10 19 53.50145
## 11 20 53.51318
## 12 21 54.04329
## 13 22 49.33783
## 14 23 54.37593
## 15 24 57.04947
## 16 25 61.89951
## 17 26 61.71469
## 18 27 60.60165
## 19 28 70.54798
## 20 29 70.46076
## 21 30 70.64106
## 22 31 75.28889
## 23 32 70.38531
## 24 33 77.78421
## 25 34 76.58814
## 26 35 81.84611
## 27 36 81.83578
## 28 37 85.70899
## 29 38 89.60590
## 30 39 88.01968
## 31 40 92.74741
## 32 41 88.79123
## 33 42 92.05167
## 34 43 100.10189
## 35 44 99.01895
## 36 45 94.45900
## 37 46 98.31710
## 38 47 103.45789
## 39 48 108.33760
## 40 49 107.72492
## 41 50 111.68531
## 42 51 114.58931
## 43 52 118.51185
## 44 53 116.15125
## 45 54 117.85965
## 46 55 115.26626
## 47 56 122.26818
## 48 57 122.45600
## 49 58 123.37017
## 50 59 127.51425
## 51 60 132.74035
## 52 61 126.61958
## 53 62 132.84635
## 54 63 136.31492
## 55 64 135.58834
## 56 65 140.90154
## 57 66 143.11086
## 58 67 143.91080
## 59 68 139.34207
## 60 69 154.94294
## 61 70 149.44569
## 62 71 153.75822
## 63 72 154.73917
## 64 73 158.76662
## 65 74 160.20768
## 66 75 159.43356
## 67 76 163.02105
## 68 77 161.44740
## 69 78 168.31369
## 70 79 166.75520
## 71 80 176.52529
#Observation Exercise 2
# cor(x, yobs) = 0.2994951
# cor(x,yobs2) = 0.6884403
# cor(x,yobs3) = 0.9215917
# cor(x,yobs4) = 0.9979845
# The 1st observation in exercise 1 is that as soon as we change the sigma the distriburion of the points also gets changed.
# Exercise 1(a)--> cor(x, yobs) = 0.2994951
# When sigma change is equal to 2.7 we see the points are scattered.
# Exercise 1(b)--> cor(x,yobs2) = 0.6884403
# output values are seen closer to the value line when sigma changes from 2.7 to 0.7.
# Exercise 1(c)--> cor(x,yobs3) = 0.9215917
# We see the points are more closer to the line when it changes further from 0.7 to 0.27.
# Exercise 1(c)--> cor(x,yobs4) = 0.9979845
# We see the points are more closer to the line when it changes further from 0.7 to 2.7 and as the input data range for x with minimum 10 and maximum 80.
## Thus as the sigma changes we see there is a change in correlation measure and Y gets gets more and more closer to the expected line.
## It gets even closer as we almost see points ploted on the expected line when the sigma changes to 2.7 again and there is a input data range for x with minimum 10 and maximum 80.