Directions

The objective of this assignment is to introduce you to R and R markdown and to complete some basic data simulation exercises.

Please include all code needed to perform the tasks. 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.

To submit this homework you will create the document in Rstudio, using the knitr package (button included in Rstudio) and then submit the document to your Rpubs account. Once uploaded you will submit the link to that document on Moodle. Please make sure that this link is hyperlinked and that I can see the visualization and the code required to create it.

Questions

  1. Simulate data for 30 draws from a normal distribution where the means and standard deviations vary among three distributions.
# place the code to simulate the data here
question1 = rnorm(48,c(34,4,65),sd=c(2,5,6))
question1
##  [1] 36.9981765075  2.7151489306 63.6892442642 31.6162905638 -2.4179234498
##  [6] 68.0135487370 35.4802341291  5.1141398993 70.9626758150 35.3477647926
## [11]  7.2693716302 69.6151148880 34.5219519037  4.8089980174 73.0801105055
## [16] 33.5054110508 -0.6005645909 71.3628164811 32.8703301508  7.2433538166
## [21] 73.3499106680 31.3982795268  3.4392766237 67.2570362291 36.2711251440
## [26]  0.6287392877 62.2881354708 36.0035318129  1.6476445047 63.8256585384
## [31] 28.9174095803  5.1459755144 69.1958783855 34.6374708545  2.1681688756
## [36] 69.4614149982 35.3806339081  4.0727268930 72.6247889700 33.5000883131
## [41]  9.7432574966 63.8828468610 34.6095070806  0.1659969176 70.0754255132
## [46] 33.1961379685 -0.0009891846 63.7858238138
  1. Simulate 2 continuous variables (normal distribution) (n=20) and plot the relationship between them
# place the code to simulate the data here
set.seed(100)
x=rnorm(20,5,10)
set.seed(101)
y=rnorm(20,5,10)
x
##  [1] -0.02192351  6.31531165  4.21082910 13.86784809  6.16971271
##  [6]  8.18630088 -0.81790685 12.14532711 -3.25259426  1.40137869
## [11]  5.89886144  5.96274460  2.98366048 12.39840500  6.23379501
## [16]  4.70683291  1.11145753 10.10856257 -4.13814185 28.10296823
y
##  [1]   1.739635  10.524619  -1.749438   7.143595   8.107692  16.739663
##  [7]  11.187899   3.872657  14.170283   2.767406  10.264481  -2.948444
## [13]  19.277555  -9.668197   2.633166   3.066620  -3.497547   5.584655
## [19]  -3.176704 -15.503078
plot(y~x)

  1. Simulate 3 variables (x1, x2 and y). x1 and x2 should be drawn from a uniform distribution and y should be drawn from a normal distribution. Fit a multiple linear regression.
# place the code to simulate the data here
set.seed(25)
x1 = runif(100,min = 5, max = 10)
set.seed(26)
x2 = runif(100, min = 5, max = 10)
y = rnorm(100, mean = 5, sd = 10)
x1
##   [1] 7.080592 8.473818 5.744003 9.486925 5.621960 9.925579 8.130481
##   [8] 6.687692 5.333952 6.410708 6.639836 6.817562 9.792725 7.944371
##  [15] 8.477140 5.737527 7.698435 8.641000 7.384588 8.646980 5.203427
##  [22] 7.254525 5.462967 5.770932 6.459343 6.501551 5.363216 7.964644
##  [29] 7.664749 6.190116 9.115935 6.746220 6.183791 5.508122 9.160378
##  [36] 7.019397 9.694669 6.869813 5.781521 5.618607 8.556883 6.260028
##  [43] 7.835079 8.720863 7.808457 6.429215 9.955260 8.539889 5.282050
##  [50] 8.584831 5.674489 8.624577 9.083403 7.464582 7.636169 5.601602
##  [57] 5.625908 7.732528 5.918605 5.664144 5.576851 6.181468 6.524842
##  [64] 8.389515 8.101457 9.199678 7.364709 9.475686 8.225645 6.233777
##  [71] 9.368950 5.688288 5.374943 8.891272 6.140063 7.320156 5.695624
##  [78] 9.239685 5.904671 5.919314 8.002176 6.725436 7.608743 5.292313
##  [85] 9.038425 8.820960 6.550050 6.982990 8.924697 7.792378 7.403737
##  [92] 6.098207 8.059820 8.115918 5.047579 6.969275 8.188167 9.517401
##  [99] 6.153248 5.861849
x2
##   [1] 5.082962 6.447391 9.372471 8.999609 6.561216 7.356768 8.978477
##   [8] 8.674943 6.704634 8.346393 7.795697 6.161002 7.754768 5.043177
##  [15] 9.100133 6.558044 7.420355 6.959443 8.314036 8.703124 9.731374
##  [22] 8.418799 7.459385 7.230404 6.402555 9.067815 9.957022 8.940323
##  [29] 7.821639 7.113302 9.215603 7.343735 7.553894 5.447557 5.674424
##  [36] 9.981141 5.208610 6.250981 8.925785 9.626174 6.390051 9.206691
##  [43] 8.325077 9.193394 9.974510 8.503617 8.201358 6.677760 8.046358
##  [50] 8.599474 5.906584 8.038014 5.426914 6.153540 5.351050 9.081619
##  [57] 5.091160 9.678115 7.706789 9.787303 6.203871 5.340688 9.243872
##  [64] 8.522715 7.350419 6.762131 5.885214 8.272378 5.760657 5.063171
##  [71] 9.140789 7.960415 7.447513 5.431186 7.739302 7.523034 9.506429
##  [78] 7.876727 9.598441 8.614525 5.748528 7.983275 7.332636 7.004984
##  [85] 9.311161 8.495494 9.371905 8.819146 9.752246 9.554212 6.938062
##  [92] 5.373753 6.532338 9.317564 5.134293 6.994243 9.647719 5.489423
##  [99] 7.235519 9.720921
y
##   [1]   8.09792641   3.98222692  17.82315238  -1.85881134  -8.96151178
##   [6]  -2.57831330  -7.04292439  -5.16390910   1.86165562  10.15028830
##  [11]   9.09829913  11.95300559  -5.20878449  -3.14874488  10.49813527
##  [16]   1.58299309  11.79773743  -4.70643774 -14.81964405   5.27555600
##  [21]   2.43268559  26.11691766  -0.21901322 -11.65111057   4.08994409
##  [26]  -0.84213206  10.89795567   3.78414550   6.90358009 -14.93397496
##  [31]  12.16432533  -3.69974279  11.53684321 -23.26252617  -1.47852623
##  [36]   7.65450643   7.66495058  10.93811989   6.73786760  20.54726241
##  [41]   2.85417429 -11.22395326   3.50397318   4.66432463  -5.68378491
##  [46]  -4.51521121   7.65973310  -8.46470385  -8.08686428  26.83268761
##  [51]   0.59524870  13.90666409   4.36180546  -0.02429939   0.45670163
##  [56]  -0.89855699  11.44763889   4.51088045  11.55339143  11.63362666
##  [61]  -4.07261035  11.64808175   5.97076724  22.78711589   2.34972279
##  [66]   3.60250672  -0.20211119  16.00663528   6.31925855  11.31514939
##  [71]  16.73842721  11.53011344   3.71108277   8.40215904   3.73118515
##  [76]  -0.79778282   7.90794054  -4.43621886  12.03861139  -2.95840991
##  [81]   4.85891350 -10.13681080   5.69470730  12.90397746   7.04664754
##  [86]  21.08360535  16.21398445 -12.50070307   1.21714693   7.39430696
##  [91] -17.54289877   9.05036597  12.92166117  -0.15674115   4.96775475
##  [96]   6.21250353   1.71788521  -7.12175513  -3.81139701  -5.35767712
model = lm(y~x1+x2)
model
## 
## Call:
## lm(formula = y ~ x1 + x2)
## 
## Coefficients:
## (Intercept)           x1           x2  
##      -7.045        0.539        0.884
plot(model)

  1. Simulate 3 letters repeating each letter twice, 2 times.
# place the code to simulate the data here
 rep(letters[1:3], each = 2, times = 2)
##  [1] "a" "a" "b" "b" "c" "c" "a" "a" "b" "b" "c" "c"
  1. Create a dataframe (n = 27) with 3 groups, 2 factors and two quantitative response variables. Use the replicate function.
# place the code to simulate the data here
a=data.frame(group=rep(letters[1:3]),factor=rep(letters[4:5]),
           x=rnorm(6,0,1),y=rnorm(6,1,2))
a
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
b = replicate(20, expr= a, simplify = FALSE)
b
## [[1]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
## 
## [[2]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
## 
## [[3]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
## 
## [[4]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
## 
## [[5]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
## 
## [[6]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
## 
## [[7]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
## 
## [[8]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
## 
## [[9]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
## 
## [[10]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
## 
## [[11]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
## 
## [[12]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
## 
## [[13]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
## 
## [[14]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
## 
## [[15]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
## 
## [[16]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
## 
## [[17]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
## 
## [[18]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
## 
## [[19]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129
## 
## [[20]]
##   group factor           x          y
## 1     a      d -0.70229970 -0.7846671
## 2     b      e  0.03879741 -0.4036961
## 3     c      d  0.42688481  1.3795660
## 4     a      e  0.55899025  2.4235531
## 5     b      d -0.96373828  1.1870716
## 6     c      e -0.15851833  0.2829129