This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

#########Part2-Question 2.1
######### using this function knitr is an engine for dynamic report generation with R. It is a #########package in the statistical programming language R that enables integration of R code into #########LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents.

library("knitr")

binomialFunction<-function (x) {
  dbinom(x,20,0.5)

}

#Let's setup an array x values
###### building array 

x <- seq(0,20,by=1)

px <- sapply(x,binomialFunction)

######let's obtain f(x)
fx <- sample(x,1000,replace=TRUE,prob=px)

par(mfrow=c(1,2)) 

hist(fx, breaks=x)

#######Part2-Question 2.2
#######2.2. Principal Components Analysis. For the auto data set attached with the nal
#######exam, please perform a Principal Components Analysis by performing an SVD on the 4
#######independent variables (with mpg as the dependent variable) and select the top 2 directions.
#######Please scatter plot the data set after it has been projected to these two dimensions. Your
#######code should print out the two orthogonal vectors and also perform the scatter plot of the
#######data after it has been projected to these two dimensions.

auto_mpg_data = read.fwf(file='C:\\temp\\auto-mpg.data', widths = c(5,11,11,10,8))

auto_mpg_data
##       V1          V2          V3   V4       V5
## 1   18.0   8   307.0       130.0 3504 .      1
## 2   15.0   8   350.0       165.0 3693 .      1
## 3   18.0   8   318.0       150.0 3436 .      1
## 4   16.0   8   304.0       150.0 3433 .      1
## 5   17.0   8   302.0       140.0 3449 .      1
## 6   15.0   8   429.0       198.0 4341 .      1
## 7   14.0   8   454.0       220.0 4354 .       
## 8   14.0   8   440.0       215.0 4312 .       
## 9   14.0   8   455.0       225.0 4425 .      1
## 10  15.0   8   390.0       190.0 3850 .       
## 11  15.0   8   383.0       170.0 3563 .      1
## 12  14.0   8   340.0       160.0 3609 .       
## 13  15.0   8   400.0       150.0 3761 .       
## 14  14.0   8   455.0       225.0 3086 .      1
## 15  24.0   4   113.0       95.00 2372 .      1
## 16  22.0   6   198.0       95.00 2833 .      1
## 17  18.0   6   199.0       97.00 2774 .      1
## 18  21.0   6   200.0       85.00 2587 .      1
## 19  27.0   4   97.00       88.00 2130 .      1
## 20  26.0   4   97.00       46.00 1835 .      2
## 21  25.0   4   110.0       87.00 2672 .      1
## 22  24.0   4   107.0       90.00 2430 .      1
## 23  25.0   4   104.0       95.00 2375 .      1
## 24  26.0   4   121.0       113.0 2234 .      1
## 25  21.0   6   199.0       90.00 2648 .      1
## 26  10.0   8   360.0       215.0 4615 .      1
## 27  10.0   8   307.0       200.0 4376 .      1
## 28  11.0   8   318.0       210.0 4382 .      1
## 29   9.0   8   304.0       193.0 4732 .      1
## 30  27.0   4   97.00       88.00 2130 .      1
## 31  28.0   4   140.0       90.00 2264 .      1
## 32  25.0   4   113.0       95.00 2228 .      1
## 33  25.0   4   98.00       ?     2046 .      1
## 34  19.0   6   232.0       100.0 2634 .      1
## 35  16.0   6   225.0       105.0 3439 .      1
## 36  17.0   6   250.0       100.0 3329 .      1
## 37  19.0   6   250.0       88.00 3302 .      1
## 38  18.0   6   232.0       100.0 3288 .      1
## 39  14.0   8   350.0       165.0 4209 .      1
## 40  14.0   8   400.0       175.0 4464 .      1
## 41  14.0   8   351.0       153.0 4154 .      1
## 42  14.0   8   318.0       150.0 4096 .      1
## 43  12.0   8   383.0       180.0 4955 .      1
## 44  13.0   8   400.0       170.0 4746 .      1
## 45  13.0   8   400.0       175.0 5140 .      1
## 46  18.0   6   258.0       110.0 2962 .      1
## 47  22.0   4   140.0       72.00 2408 .      1
## 48  19.0   6   250.0       100.0 3282 .      1
## 49  18.0   6   250.0       88.00 3139 .      1
## 50  23.0   4   122.0       86.00 2220 .      1
## 51  28.0   4   116.0       90.00 2123 .      1
## 52  30.0   4   79.00       70.00 2074 .      1
## 53  30.0   4   88.00       76.00 2065 .      1
## 54  31.0   4   71.00       65.00 1773 .      1
## 55  35.0   4   72.00       69.00 1613 .      1
## 56  27.0   4   97.00       60.00 1834 .      1
## 57  26.0   4   91.00       70.00 1955 .      2
## 58  24.0   4   113.0       95.00 2278 .      1
## 59  25.0   4   97.50       80.00 2126 .      1
## 60  23.0   4   97.00       54.00 2254 .      2
## 61  20.0   4   140.0       90.00 2408 .      1
## 62  21.0   4   122.0       86.00 2226 .      1
## 63  13.0   8   350.0       165.0 4274 .      1
## 64  14.0   8   400.0       175.0 4385 .      1
## 65  15.0   8   318.0       150.0 4135 .      1
## 66  14.0   8   351.0       153.0 4129 .      1
## 67  17.0   8   304.0       150.0 3672 .      1
## 68  11.0   8   429.0       208.0 4633 .      1
## 69  13.0   8   350.0       155.0 4502 .      1
## 70  12.0   8   350.0       160.0 4456 .      1
## 71  13.0   8   400.0       190.0 4422 .      1
## 72  19.0   3   70.00       97.00 2330 .      1
## 73  15.0   8   304.0       150.0 3892 .      1
## 74  13.0   8   307.0       130.0 4098 .      1
## 75  13.0   8   302.0       140.0 4294 .      1
## 76  14.0   8   318.0       150.0 4077 .      1
## 77  18.0   4   121.0       112.0 2933 .      1
## 78  22.0   4   121.0       76.00 2511 .      1
## 79  21.0   4   120.0       87.00 2979 .      1
## 80  26.0   4   96.00       69.00 2189 .      1
## 81  22.0   4   122.0       86.00 2395 .      1
## 82  28.0   4   97.00       92.00 2288 .      1
## 83  23.0   4   120.0       97.00 2506 .      1
## 84  28.0   4   98.00       80.00 2164 .      1
## 85  27.0   4   97.00       88.00 2100 .      1
## 86  13.0   8   350.0       175.0 4100 .      1
## 87  14.0   8   304.0       150.0 3672 .      1
## 88  13.0   8   350.0       145.0 3988 .      1
## 89  14.0   8   302.0       137.0 4042 .      1
## 90  15.0   8   318.0       150.0 3777 .      1
## 91  12.0   8   429.0       198.0 4952 .      1
## 92  13.0   8   400.0       150.0 4464 .      1
## 93  13.0   8   351.0       158.0 4363 .      1
## 94  14.0   8   318.0       150.0 4237 .      1
## 95  13.0   8   440.0       215.0 4735 .      1
## 96  12.0   8   455.0       225.0 4951 .      1
## 97  13.0   8   360.0       175.0 3821 .      1
## 98  18.0   6   225.0       105.0 3121 .      1
## 99  16.0   6   250.0       100.0 3278 .      1
## 100 18.0   6   232.0       100.0 2945 .      1
## 101 18.0   6   250.0       88.00 3021 .      1
## 102 23.0   6   198.0       95.00 2904 .      1
## 103 26.0   4   97.00       46.00 1950 .      2
## 104 11.0   8   400.0       150.0 4997 .      1
## 105 12.0   8   400.0       167.0 4906 .      1
## 106 13.0   8   360.0       170.0 4654 .      1
## 107 12.0   8   350.0       180.0 4499 .      1
## 108 18.0   6   232.0       100.0 2789 .      1
## 109 20.0   4   97.00       88.00 2279 .      1
## 110 21.0   4   140.0       72.00 2401 .      1
## 111 22.0   4   108.0       94.00 2379 .      1
## 112 18.0   3   70.00       90.00 2124 .      1
## 113 19.0   4   122.0       85.00 2310 .      1
## 114 21.0   6   155.0       107.0 2472 .      1
## 115 26.0   4   98.00       90.00 2265 .      1
## 116 15.0   8   350.0       145.0 4082 .      1
## 117 16.0   8   400.0       230.0 4278 .      9
## 118 29.0   4   68.00       49.00 1867 .      1
## 119 24.0   4   116.0       75.00 2158 .      1
## 120 20.0   4   114.0       91.00 2582 .      1
## 121 19.0   4   121.0       112.0 2868 .      1
## 122 15.0   8   318.0       150.0 3399 .      1
## 123 24.0   4   121.0       110.0 2660 .      1
## 124 20.0   6   156.0       122.0 2807 .      1
## 125 11.0   8   350.0       180.0 3664 .      1
## 126 20.0   6   198.0       95.00 3102 .      1
## 127 21.0   6   200.0       ?     2875 .      1
## 128 19.0   6   232.0       100.0 2901 .      1
## 129 15.0   6   250.0       100.0 3336 .      1
## 130 31.0   4   79.00       67.00 1950 .      1
## 131 26.0   4   122.0       80.00 2451 .      1
## 132 32.0   4   71.00       65.00 1836 .      2
## 133 25.0   4   140.0       75.00 2542 .      1
## 134 16.0   6   250.0       100.0 3781 .      1
## 135 16.0   6   258.0       110.0 3632 .      1
## 136 18.0   6   225.0       105.0 3613 .      1
## 137 16.0   8   302.0       140.0 4141 .      1
## 138 13.0   8   350.0       150.0 4699 .      1
## 139 14.0   8   318.0       150.0 4457 .      1
## 140 14.0   8   302.0       140.0 4638 .      1
## 141 14.0   8   304.0       150.0 4257 .      1
## 142 29.0   4   98.00       83.00 2219 .      1
## 143 26.0   4   79.00       67.00 1963 .      1
## 144 26.0   4   97.00       78.00 2300 .      1
## 145 31.0   4   76.00       52.00 1649 .      1
## 146 32.0   4   83.00       61.00 2003 .      1
## 147 28.0   4   90.00       75.00 2125 .      1
## 148 24.0   4   90.00       75.00 2108 .      1
## 149 26.0   4   116.0       75.00 2246 .      1
## 150 24.0   4   120.0       97.00 2489 .      1
## 151 26.0   4   108.0       93.00 2391 .      1
## 152 31.0   4   79.00       67.00 2000 .      1
## 153 19.0   6   225.0       95.00 3264 .      1
## 154 18.0   6   250.0       105.0 3459 .      1
## 155 15.0   6   250.0       72.00 3432 .      2
## 156 15.0   6   250.0       72.00 3158 .      1
## 157 16.0   8   400.0       170.0 4668 .      1
## 158 15.0   8   350.0       145.0 4440 .      1
## 159 16.0   8   318.0       150.0 4498 .      1
## 160 14.0   8   351.0       148.0 4657 .      1
## 161 17.0   6   231.0       110.0 3907 .      2
## 162 16.0   6   250.0       105.0 3897 .      1
## 163 15.0   6   258.0       110.0 3730 .      1
## 164 18.0   6   225.0       95.00 3785 .      1
## 165 21.0   6   231.0       110.0 3039 .      1
## 166 20.0   8   262.0       110.0 3221 .      1
## 167 13.0   8   302.0       129.0 3169 .      1
## 168 29.0   4   97.00       75.00 2171 .      1
## 169 23.0   4   140.0       83.00 2639 .      1
## 170 20.0   6   232.0       100.0 2914 .      1
## 171 23.0   4   140.0       78.00 2592 .      1
## 172 24.0   4   134.0       96.00 2702 .      1
## 173 25.0   4   90.00       71.00 2223 .      1
## 174 24.0   4   119.0       97.00 2545 .      1
## 175 18.0   6   171.0       97.00 2984 .      1
## 176 29.0   4   90.00       70.00 1937 .      1
## 177 19.0   6   232.0       90.00 3211 .      1
## 178 23.0   4   115.0       95.00 2694 .      1
## 179 23.0   4   120.0       88.00 2957 .      1
## 180 22.0   4   121.0       98.00 2945 .      1
## 181 25.0   4   121.0       115.0 2671 .      1
## 182 33.0   4   91.00       53.00 1795 .      1
## 183 28.0   4   107.0       86.00 2464 .      1
## 184 25.0   4   116.0       81.00 2220 .      1
## 185 25.0   4   140.0       92.00 2572 .      1
## 186 26.0   4   98.00       79.00 2255 .      1
## 187 27.0   4   101.0       83.00 2202 .      1
## 188 17.5   8   305.0       140.0 4215 .      1
## 189 16.0   8   318.0       150.0 4190 .      1
## 190 15.5   8   304.0       120.0 3962 .      1
## 191 14.5   8   351.0       152.0 4215 .      1
## 192 22.0   6   225.0       100.0 3233 .      1
## 193 22.0   6   250.0       105.0 3353 .      1
## 194 24.0   6   200.0       81.00 3012 .      1
## 195 22.5   6   232.0       90.00 3085 .      1
## 196 29.0   4   85.00       52.00 2035 .      2
## 197 24.5   4   98.00       60.00 2164 .      2
## 198 29.0   4   90.00       70.00 1937 .      1
## 199 33.0   4   91.00       53.00 1795 .      1
## 200 20.0   6   225.0       100.0 3651 .      1
## 201 18.0   6   250.0       78.00 3574 .      2
## 202 18.5   6   250.0       110.0 3645 .      1
## 203 17.5   6   258.0       95.00 3193 .      1
## 204 29.5   4   97.00       71.00 1825 .      1
## 205 32.0   4   85.00       70.00 1990 .      1
## 206 28.0   4   97.00       75.00 2155 .      1
## 207 26.5   4   140.0       72.00 2565 .      1
## 208 20.0   4   130.0       102.0 3150 .      1
## 209 13.0   8   318.0       150.0 3940 .      1
## 210 19.0   4   120.0       88.00 3270 .      2
## 211 19.0   6   156.0       108.0 2930 .      1
## 212 16.5   6   168.0       120.0 3820 .      1
## 213 16.5   8   350.0       180.0 4380 .      1
## 214 13.0   8   350.0       145.0 4055 .      1
## 215 13.0   8   302.0       130.0 3870 .      1
## 216 13.0   8   318.0       150.0 3755 .      1
## 217 31.5   4   98.00       68.00 2045 .      1
## 218 30.0   4   111.0       80.00 2155 .      1
## 219 36.0   4   79.00       58.00 1825 .      1
## 220 25.5   4   122.0       96.00 2300 .      1
## 221 33.5   4   85.00       70.00 1945 .      1
## 222 17.5   8   305.0       145.0 3880 .      1
## 223 17.0   8   260.0       110.0 4060 .      1
## 224 15.5   8   318.0       145.0 4140 .      1
## 225 15.0   8   302.0       130.0 4295 .      1
## 226 17.5   6   250.0       110.0 3520 .      1
## 227 20.5   6   231.0       105.0 3425 .      1
## 228 19.0   6   225.0       100.0 3630 .      1
## 229 18.5   6   250.0       98.00 3525 .      1
## 230 16.0   8   400.0       180.0 4220 .      1
## 231 15.5   8   350.0       170.0 4165 .      1
## 232 15.5   8   400.0       190.0 4325 .      1
## 233 16.0   8   351.0       149.0 4335 .      1
## 234 29.0   4   97.00       78.00 1940 .      1
## 235 24.5   4   151.0       88.00 2740 .      1
## 236 26.0   4   97.00       75.00 2265 .      1
## 237 25.5   4   140.0       89.00 2755 .      1
## 238 30.5   4   98.00       63.00 2051 .      1
## 239 33.5   4   98.00       83.00 2075 .      1
## 240 30.0   4   97.00       67.00 1985 .      1
## 241 30.5   4   97.00       78.00 2190 .      1
## 242 22.0   6   146.0       97.00 2815 .      1
## 243 21.5   4   121.0       110.0 2600 .      1
## 244 21.5   3   80.00       110.0 2720 .      1
## 245 43.1   4   90.00       48.00 1985 .      2
## 246 36.1   4   98.00       66.00 1800 .      1
## 247 32.8   4   78.00       52.00 1985 .      1
## 248 39.4   4   85.00       70.00 2070 .      1
## 249 36.1   4   91.00       60.00 1800 .      1
## 250 19.9   8   260.0       110.0 3365 .      1
## 251 19.4   8   318.0       140.0 3735 .      1
## 252 20.2   8   302.0       139.0 3570 .      1
## 253 19.2   6   231.0       105.0 3535 .      1
## 254 20.5   6   200.0       95.00 3155 .      1
## 255 20.2   6   200.0       85.00 2965 .      1
## 256 25.1   4   140.0       88.00 2720 .      1
## 257 20.5   6   225.0       100.0 3430 .      1
## 258 19.4   6   232.0       90.00 3210 .      1
## 259 20.6   6   231.0       105.0 3380 .      1
## 260 20.8   6   200.0       85.00 3070 .      1
## 261 18.6   6   225.0       110.0 3620 .      1
## 262 18.1   6   258.0       120.0 3410 .      1
## 263 19.2   8   305.0       145.0 3425 .      1
## 264 17.7   6   231.0       165.0 3445 .      1
## 265 18.1   8   302.0       139.0 3205 .      1
## 266 17.5   8   318.0       140.0 4080 .      1
## 267 30.0   4   98.00       68.00 2155 .      1
## 268 27.5   4   134.0       95.00 2560 .      1
## 269 27.2   4   119.0       97.00 2300 .      1
## 270 30.9   4   105.0       75.00 2230 .      1
## 271 21.1   4   134.0       95.00 2515 .      1
## 272 23.2   4   156.0       105.0 2745 .      1
## 273 23.8   4   151.0       85.00 2855 .      1
## 274 23.9   4   119.0       97.00 2405 .      1
## 275 20.3   5   131.0       103.0 2830 .      1
## 276 17.0   6   163.0       125.0 3140 .      1
## 277 21.6   4   121.0       115.0 2795 .      1
## 278 16.2   6   163.0       133.0 3410 .      1
## 279 31.5   4   89.00       71.00 1990 .      1
## 280 29.5   4   98.00       68.00 2135 .      1
## 281 21.5   6   231.0       115.0 3245 .      1
## 282 19.8   6   200.0       85.00 2990 .      1
## 283 22.3   4   140.0       88.00 2890 .      1
## 284 20.2   6   232.0       90.00 3265 .      1
## 285 20.6   6   225.0       110.0 3360 .      1
## 286 17.0   8   305.0       130.0 3840 .      1
## 287 17.6   8   302.0       129.0 3725 .      1
## 288 16.5   8   351.0       138.0 3955 .      1
## 289 18.2   8   318.0       135.0 3830 .      1
## 290 16.9   8   350.0       155.0 4360 .      1
## 291 15.5   8   351.0       142.0 4054 .      1
## 292 19.2   8   267.0       125.0 3605 .      1
## 293 18.5   8   360.0       150.0 3940 .      1
## 294 31.9   4   89.00       71.00 1925 .      1
## 295 34.1   4   86.00       65.00 1975 .      1
## 296 35.7   4   98.00       80.00 1915 .      1
## 297 27.4   4   121.0       80.00 2670 .      1
## 298 25.4   5   183.0       77.00 3530 .      2
## 299 23.0   8   350.0       125.0 3900 .      1
## 300 27.2   4   141.0       71.00 3190 .      2
## 301 23.9   8   260.0       90.00 3420 .      2
## 302 34.2   4   105.0       70.00 2200 .      1
## 303 34.5   4   105.0       70.00 2150 .      1
## 304 31.8   4   85.00       65.00 2020 .      1
## 305 37.3   4   91.00       69.00 2130 .      1
## 306 28.4   4   151.0       90.00 2670 .      1
## 307 28.8   6   173.0       115.0 2595 .      1
## 308 26.8   6   173.0       115.0 2700 .      1
## 309 33.5   4   151.0       90.00 2556 .      1
## 310 41.5   4   98.00       76.00 2144 .      1
## 311 38.1   4   89.00       60.00 1968 .      1
## 312 32.1   4   98.00       70.00 2120 .      1
## 313 37.2   4   86.00       65.00 2019 .      1
## 314 28.0   4   151.0       90.00 2678 .      1
## 315 26.4   4   140.0       88.00 2870 .      1
## 316 24.3   4   151.0       90.00 3003 .      2
## 317 19.1   6   225.0       90.00 3381 .      1
## 318 34.3   4   97.00       78.00 2188 .      1
## 319 29.8   4   134.0       90.00 2711 .      1
## 320 31.3   4   120.0       75.00 2542 .      1
## 321 37.0   4   119.0       92.00 2434 .      1
## 322 32.2   4   108.0       75.00 2265 .      1
## 323 46.6   4   86.00       65.00 2110 .      1
## 324 27.9   4   156.0       105.0 2800 .      1
## 325 40.8   4   85.00       65.00 2110 .      1
## 326 44.3   4   90.00       48.00 2085 .      2
## 327 43.4   4   90.00       48.00 2335 .      2
## 328 36.4   5   121.0       67.00 2950 .      1
## 329 30.0   4   146.0       67.00 3250 .      2
## 330 44.6   4   91.00       67.00 1850 .      1
## 331 40.9   4   85.00       ?     1835 .      1
## 332 33.8   4   97.00       67.00 2145 .      1
## 333 29.8   4   89.00       62.00 1845 .      1
## 334 32.7   6   168.0       132.0 2910 .      1
## 335 23.7   3   70.00       100.0 2420 .      1
## 336 35.0   4   122.0       88.00 2500 .      1
## 337 23.6   4   140.0       ?     2905 .      1
## 338 32.4   4   107.0       72.00 2290 .      1
## 339 27.2   4   135.0       84.00 2490 .      1
## 340 26.6   4   151.0       84.00 2635 .      1
## 341 25.8   4   156.0       92.00 2620 .      1
## 342 23.5   6   173.0       110.0 2725 .      1
## 343 30.0   4   135.0       84.00 2385 .      1
## 344 39.1   4   79.00       58.00 1755 .      1
## 345 39.0   4   86.00       64.00 1875 .      1
## 346 35.1   4   81.00       60.00 1760 .      1
## 347 32.3   4   97.00       67.00 2065 .      1
## 348 37.0   4   85.00       65.00 1975 .      1
## 349 37.7   4   89.00       62.00 2050 .      1
## 350 34.1   4   91.00       68.00 1985 .      1
## 351 34.7   4   105.0       63.00 2215 .      1
## 352 34.4   4   98.00       65.00 2045 .      1
## 353 29.9   4   98.00       65.00 2380 .      2
## 354 33.0   4   105.0       74.00 2190 .      1
## 355 34.5   4   100.0       ?     2320 .      1
## 356 33.7   4   107.0       75.00 2210 .      1
## 357 32.4   4   108.0       75.00 2350 .      1
## 358 32.9   4   119.0       100.0 2615 .      1
## 359 31.6   4   120.0       74.00 2635 .      1
## 360 28.1   4   141.0       80.00 3230 .      2
## 361 30.7   6   145.0       76.00 3160 .      1
## 362 25.4   6   168.0       116.0 2900 .      1
## 363 24.2   6   146.0       120.0 2930 .      1
## 364 22.4   6   231.0       110.0 3415 .      1
## 365 26.6   8   350.0       105.0 3725 .      1
## 366 20.2   6   200.0       88.00 3060 .      1
## 367 17.6   6   225.0       85.00 3465 .      1
## 368 28.0   4   112.0       88.00 2605 .      1
## 369 27.0   4   112.0       88.00 2640 .      1
## 370 34.0   4   112.0       88.00 2395 .      1
## 371 31.0   4   112.0       85.00 2575 .      1
## 372 29.0   4   135.0       84.00 2525 .      1
## 373 27.0   4   151.0       90.00 2735 .      1
## 374 24.0   4   140.0       92.00 2865 .      1
## 375 23.0   4   151.0       ?     3035 .      2
## 376 36.0   4   105.0       74.00 1980 .      1
## 377 37.0   4   91.00       68.00 2025 .      1
## 378 31.0   4   91.00       68.00 1970 .      1
## 379 38.0   4   105.0       63.00 2125 .      1
## 380 36.0   4   98.00       70.00 2125 .      1
## 381 36.0   4   120.0       88.00 2160 .      1
## 382 36.0   4   107.0       75.00 2205 .      1
## 383 34.0   4   108.0       70.00 2245        1
## 384 38.0   4   91.00       67.00 1965 .      1
## 385 32.0   4   91.00       67.00 1965 .      1
## 386 38.0   4   91.00       67.00 1995 .      1
## 387 25.0   6   181.0       110.0 2945 .      1
## 388 38.0   6   262.0       85.00 3015 .      1
## 389 26.0   4   156.0       92.00 2585 .      1
## 390 22.0   6   232.0       112.0 2835        1
## 391 32.0   4   144.0       96.00 2665 .      1
## 392 36.0   4   135.0       84.00 2370 .      1
## 393 27.0   4   151.0       90.00 2950 .      1
## 394 27.0   4   140.0       86.00 2790 .      1
## 395 44.0   4   97.00       52.00 2130 .      2
## 396 32.0   4   135.0       84.00 2295 .      1
## 397 28.0   4   120.0       79.00 2625 .      1
## 398 31.0   4   119.0       82.00 2720 .      1
colnames(auto_mpg_data) <- c("displacement", "horsepower", "weight", "acceleration","mpg")

A <- scale(data.matrix(auto_mpg_data[1:4]))
mpg <- data.matrix(auto_mpg_data[5])
colnames(mpg)<- "mpg"


#######calculate the covariance matrix
cov <- cov(A)
print(cov)
##              displacement horsepower     weight acceleration
## displacement    1.0000000 -0.6522363  0.4215846   -0.8317409
## horsepower     -0.6522363  1.0000000 -0.5256948    0.7590080
## weight          0.4215846 -0.5256948  1.0000000   -0.4807430
## acceleration   -0.8317409  0.7590080 -0.4807430    1.0000000
####### Now, let's do SVD on the covariance Matrix
LSA <- svd(cov)

print(LSA)
## $d
## [1] 2.8621693 0.6571824 0.3341016 0.1465468
## 
## $u
##            [,1]        [,2]        [,3]        [,4]
## [1,] -0.5176258 -0.37976775 -0.52752464  0.55637911
## [2,]  0.5185625  0.01775182 -0.80995911 -0.27339360
## [3,] -0.4048118  0.88250074 -0.23941248 -0.00124319
## [4,]  0.5470685  0.27686418  0.09146395  0.78466342
## 
## $v
##            [,1]        [,2]        [,3]        [,4]
## [1,] -0.5176258 -0.37976775 -0.52752464  0.55637911
## [2,]  0.5185625  0.01775182 -0.80995911 -0.27339360
## [3,] -0.4048118  0.88250074 -0.23941248 -0.00124319
## [4,]  0.5470685  0.27686418  0.09146395  0.78466342
####### and the variance eigenvectors are
LSA$d
## [1] 2.8621693 0.6571824 0.3341016 0.1465468
LSA$u
##            [,1]        [,2]        [,3]        [,4]
## [1,] -0.5176258 -0.37976775 -0.52752464  0.55637911
## [2,]  0.5185625  0.01775182 -0.80995911 -0.27339360
## [3,] -0.4048118  0.88250074 -0.23941248 -0.00124319
## [4,]  0.5470685  0.27686418  0.09146395  0.78466342
LSA$v
##            [,1]        [,2]        [,3]        [,4]
## [1,] -0.5176258 -0.37976775 -0.52752464  0.55637911
## [2,]  0.5185625  0.01775182 -0.80995911 -0.27339360
## [3,] -0.4048118  0.88250074 -0.23941248 -0.00124319
## [4,]  0.5470685  0.27686418  0.09146395  0.78466342
#########Plot as 
plot(LSA$d,type='b',pch=10,xlab='Singular value',ylab='magnitude')

#### Compare the result

my.ir.pca <- prcomp(A,center=FALSE,scale.=FALSE)
my.ir.pca
## Standard deviations:
## [1] 1.6917947 0.8106679 0.5780152 0.3828143
## 
## Rotation:
##                     PC1         PC2         PC3         PC4
## displacement  0.5176258 -0.37976775  0.52752464  0.55637911
## horsepower   -0.5185625  0.01775182  0.80995911 -0.27339360
## weight        0.4048118  0.88250074  0.23941248 -0.00124319
## acceleration -0.5470685  0.27686418 -0.09146395  0.78466342
my.ir.pca$sdev^2
## [1] 2.8621693 0.6571824 0.3341016 0.1465468
#################

LSA <- svd(A)

# Let's now project the data onto these two dimensions
depth <-2
us <- as.matrix(LSA$u[, 1:depth])
vs <- as.matrix(LSA$v[, 1:depth])
ds <- as.matrix(diag(LSA$d)[1:depth, 1:depth])

A_dn <- us %*% ds %*% t(vs)
colnames(A_dn) <- c("displacement", "horsepower", "weight", "acceleration")
pairs(~.,data=cbind(A_dn,mpg), main=paste("Scatterplot Matrix of auto-mpg Data onto the First",depth,"Dimensions."))

#colnames(A) <- c("displacement", "horsepower", "weight", "acceleration")
pairs(~.,data=cbind(A,mpg), main="Scatterplot Matrix of auto-mpg Data (Original)")

#######Part2-Question 2.3

infinity<-5000
n <- c(1:infinity)
q <-(1-1/n)^n

plot(n,q)

###identify maximum value of Q

lim <- max(q)
lim
## [1] 0.3678427
#####pofInfinity is equal to 1-lim
1-lim
## [1] 0.6321573
# In this program lines, we will do the experiment and  boostrap n elements from a sequence of values from 1:n

bsExperiment <- function(n){

  # Define the sequence of values to sample from
  values <- seq(1:n)
  
  # let's sample with replacement n-times
  samples <- replicate(n,{sample(values,1,replace=TRUE)})

  # return the percentage of values samples in this iteration
  
  return(length(unique(samples))/n)
}

# The output of a single experiment with n=1000 is:
n <- 10000
bsExperiment(n)
## [1] 0.6326
### we can repeat for several values

m<-500
bsExperiment(m)
## [1] 0.614

You can also embed plots, for example: Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.