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 Canvas. 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.
set.seed(92)
rnorm(30, mean = c(5,8,32), sd = c(2,9,6))
##  [1]  2.04243316  3.52249618 37.58198820  2.62354911 -7.70773686
##  [6] 26.11055349  7.83668497  7.17924910 25.51736755  4.47253255
## [11]  3.26503702 30.77853457  9.85317372  0.08251203 37.97546918
## [16]  2.75053064  8.87082505 22.53777523  5.81816367  2.95923280
## [21] 38.24721773  5.37752574  3.21818050 40.24745960  7.12614936
## [26]  8.77429901 31.45812001  7.91355406 -8.04717573 24.84388671
  1. Simulate 2 continuous variables (normal distribution) (n=20) and plot the relationship between them
# place the code to simulate the data here
a = rnorm(20, mean = 1, sd=2)
b = rnorm(20, mean = 2, sd=1)
plot(b~a)

  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
y = rnorm(20, mean = 0, sd = 1)
x1 = runif(20, min = 3, max = 5)
x2 = runif(20, min = 4, max = 7)
lm(y ~ x1 + x2)
## 
## Call:
## lm(formula = y ~ x1 + x2)
## 
## Coefficients:
## (Intercept)           x1           x2  
##    -1.53543      0.36336      0.05275
  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
replicate(5, expr = data.frame(group = rep(LETTERS[6:8]),factor = rep(LETTERS[2:3]),a = rnorm(30, mean =1, sd=2),b = rnorm(30, mean = 3, sd =4)),simplify = FALSE)
## [[1]]
##    group factor           a          b
## 1      F      B -1.57512545 -0.3030495
## 2      G      C  2.03597373 -3.1191762
## 3      H      B  0.15497733 -2.7167606
## 4      F      C  2.41888039  3.3345419
## 5      G      B -0.47400047  3.9081751
## 6      H      C  4.24064874 -1.5687512
## 7      F      B  2.69952348 -2.1017672
## 8      G      C  0.68852386 -4.1149199
## 9      H      B  1.88593273  4.0209143
## 10     F      C -0.39796122  1.6859652
## 11     G      B  1.78885908  7.1391285
## 12     H      C  1.53785320  3.4697032
## 13     F      B -2.54006412  1.1387327
## 14     G      C -1.61006456 -0.8982569
## 15     H      B -0.45400499  4.9034461
## 16     F      C -1.83659215  7.2670391
## 17     G      B -3.08187433 -1.0160545
## 18     H      C -0.08877212 -5.2187661
## 19     F      B  2.54625273 -4.5028371
## 20     G      C  0.13526417 -2.7587764
## 21     H      B  0.07884547  9.4582244
## 22     F      C -2.92202190  2.4569935
## 23     G      B  3.77069333 -1.0396921
## 24     H      C  0.57294189  6.0706727
## 25     F      B -1.14270144  5.3566790
## 26     G      C  1.98394871  1.9684642
## 27     H      B  1.43991940  5.5645643
## 28     F      C  0.54147559 -1.4164765
## 29     G      B  2.05645253 -0.7530837
## 30     H      C  3.90163681 -1.5079730
## 
## [[2]]
##    group factor           a          b
## 1      F      B  3.57529343 -0.9704151
## 2      G      C  2.69180218 -0.4560307
## 3      H      B -5.50102796 10.4192922
## 4      F      C -0.76002868  2.3247956
## 5      G      B -0.28190147  5.3744433
## 6      H      C  3.44154154  2.6069606
## 7      F      B  0.46120841  2.1364472
## 8      G      C  1.17385666  2.3533276
## 9      H      B  3.30613314  6.5893420
## 10     F      C -0.40406899  1.9061459
## 11     G      B  4.35938392  1.8742626
## 12     H      C  2.15757187  8.8612386
## 13     F      B -0.02232940  3.7818706
## 14     G      C  0.35513116 -0.5382753
## 15     H      B -2.12791784  4.1528492
## 16     F      C  0.45867218  5.4213692
## 17     G      B -1.22778742  8.8095501
## 18     H      C  4.17877787  5.9323817
## 19     F      B  1.36063221  3.6061529
## 20     G      C -2.29088364  4.4992033
## 21     H      B -1.59201247  2.3464496
## 22     F      C  1.07870143  3.9565359
## 23     G      B -1.50818912  0.4262662
## 24     H      C -3.91859716  2.6741555
## 25     F      B  1.63008728  2.5819035
## 26     G      C  1.95829894 -2.1446637
## 27     H      B -0.08686027  6.4043717
## 28     F      C -2.16247840 -2.8135513
## 29     G      B  0.76154249 -3.7883843
## 30     H      C  0.56632941  1.9049432
## 
## [[3]]
##    group factor          a           b
## 1      F      B  3.1136018  2.71873957
## 2      G      C -0.2190304 -0.04469285
## 3      H      B  1.3287066  6.91997936
## 4      F      C  1.2940663  4.27155162
## 5      G      B -0.4647042  1.67055191
## 6      H      C  3.7247404  2.73932339
## 7      F      B -2.2179968  3.20663500
## 8      G      C  6.1048547  3.24058118
## 9      H      B  1.8557774  2.63532064
## 10     F      C -0.2167566  1.09980048
## 11     G      B  3.0617117  8.11357861
## 12     H      C  1.7670739 -5.83102770
## 13     F      B  2.9406067  1.03494244
## 14     G      C -1.1106930  6.44084693
## 15     H      B -0.5621658  2.68569613
## 16     F      C  1.0027570  1.17344618
## 17     G      B -0.8701546 -1.33963367
## 18     H      C  3.4774524  3.41234430
## 19     F      B  2.9409477  5.87917148
## 20     G      C  2.8070679  1.96283289
## 21     H      B  0.4108603  0.82242892
## 22     F      C  4.0724329  3.07938178
## 23     G      B  0.9902071  5.84421322
## 24     H      C  0.9048423  3.35588888
## 25     F      B  1.2562835 -1.33031976
## 26     G      C  3.9369340  0.55083370
## 27     H      B -2.4417303 11.33772053
## 28     F      C  0.8515822 -1.07482816
## 29     G      B  2.6635363  1.19552312
## 30     H      C  0.8606850 -1.06608243
## 
## [[4]]
##    group factor           a           b
## 1      F      B -2.16988585  6.29196318
## 2      G      C  2.28366251 -1.54094515
## 3      H      B  2.52610178  0.18225149
## 4      F      C  1.06230188  6.53973307
## 5      G      B -0.83516216  1.70831236
## 6      H      C  1.46420790  7.12051043
## 7      F      B  1.79445098  1.90210238
## 8      G      C  2.64561247  4.41114716
## 9      H      B  2.22617997  4.59078151
## 10     F      C -0.37433624  3.33142789
## 11     G      B  0.02535445 -0.61950743
## 12     H      C -1.43605967  8.80096247
## 13     F      B  1.11263960 -1.20788187
## 14     G      C -0.89259693  6.18974827
## 15     H      B  3.13154019  8.56958822
## 16     F      C  3.18072973  2.68191661
## 17     G      B  2.61780274 -2.81575122
## 18     H      C  4.13666225  7.90665287
## 19     F      B -2.16562006  8.13537760
## 20     G      C  0.10668899  7.31497427
## 21     H      B  2.55828521  5.39971670
## 22     F      C  0.92912365 15.32496114
## 23     G      B  0.99172903  3.83353230
## 24     H      C -1.95923925 -1.42083057
## 25     F      B -1.58621027  4.13109775
## 26     G      C  0.74912115 -2.57689380
## 27     H      B  2.51671396  4.99914426
## 28     F      C -0.93482903  6.36503229
## 29     G      B -1.72552320 10.05786477
## 30     H      C  0.85426152 -0.03813883
## 
## [[5]]
##    group factor          a          b
## 1      F      B  3.6449340  0.8505510
## 2      G      C  0.9285450  2.2507314
## 3      H      B  1.9986136  3.5654759
## 4      F      C -0.2376656  6.3255546
## 5      G      B -1.1388444  1.9710263
## 6      H      C  3.2647016 11.7227508
## 7      F      B  0.7246367  7.1215020
## 8      G      C -2.5291630  0.2350242
## 9      H      B -0.1096591 -6.6179065
## 10     F      C  4.0077601  0.4889404
## 11     G      B  0.6950299 -0.4254841
## 12     H      C -1.6203024 -3.0300415
## 13     F      B  0.7401295  1.4677261
## 14     G      C -0.4967594  8.3698571
## 15     H      B  2.6468786  8.9940976
## 16     F      C  3.2080177  5.1922551
## 17     G      B -2.0613639  7.4954605
## 18     H      C -2.0674994  7.2797354
## 19     F      B -2.8026120  4.0482893
## 20     G      C -2.9560480  7.6359086
## 21     H      B  2.1419890 -2.4591690
## 22     F      C  0.5513818  5.7742737
## 23     G      B -0.8645194  2.6188264
## 24     H      C  3.6917392  3.1455144
## 25     F      B  3.2404748  1.6642770
## 26     G      C -1.7730930  4.8125262
## 27     H      B  1.8219519  6.2455010
## 28     F      C -2.1915182  1.7175286
## 29     G      B -1.2655283  6.7289661
## 30     H      C -1.1083110  0.9757617