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(100)
rnorm(30,mean=c(0,20,40),sd=c(10,20,30))
##  [1] -5.0219235 22.6306233 37.6324873  8.8678481 22.3394254 49.5589026
##  [7] -5.8179068 34.2906542 15.2422172 -3.5986213 21.7977229 42.8882338
## [13] -2.0163395 34.7968100 43.7013850 -0.2931671 12.2229151 55.3256877
## [19] -9.1381419 66.2059365 26.8573006  7.6406062 25.2392258 63.2021379
## [25] -8.1437912 11.2309886 18.3933535  2.3094453 -3.1545892 47.4122798
  1. Simulate 2 continuous variables (normal distribution) (n=20) and plot the relationship between them
x=rnorm(20,mean=10,sd=1)
y=rnorm(20,mean=20,sd=2)
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.
x1=runif(50,1,15)
x2=runif(50,50,100)
y=rnorm(50,0,1)
a=lm(y~x1+x2)
a
## 
## Call:
## lm(formula = y ~ x1 + x2)
## 
## Coefficients:
## (Intercept)           x1           x2  
##    0.123977     0.020337    -0.004282
  1. Simulate 3 letters repeating each letter twice, 2 times.
rep(letters[2:4],each=2,times=2)
##  [1] "b" "b" "c" "c" "d" "d" "b" "b" "c" "c" "d" "d"
  1. Create a dataframe (n = 30) with 3 groups, 2 factors and two quantitative response variables. Use the replicate function.
replicate(3,expr=data.frame(group=rep(LETTERS[3:5]), factor=rep(LETTERS[12:13]), response1=rnorm(30, mean=1, sd=2), response2=rnorm(30, mean=2, sd=3)),simplify=FALSE)
## [[1]]
##    group factor   response1  response2
## 1      C      L  2.26414812  2.0845153
## 2      D      M  1.40282705  0.9298898
## 3      E      L  0.81785871  4.5578791
## 4      C      M  1.57896825  3.5400957
## 5      D      L  0.89063012  5.0546090
## 6      E      M -3.08369971 -1.0644373
## 7      C      L  1.71673848  0.3149952
## 8      D      M  0.25479830 -1.0376682
## 9      E      L  3.53661768 -7.0624429
## 10     C      M  5.33720063  2.9970508
## 11     D      L -1.47944568  5.7215347
## 12     E      M  2.17974778  4.0140488
## 13     C      L  1.24803858 -1.9901023
## 14     D      M -0.04741557 -0.5517409
## 15     E      L  2.24045601 -3.3664922
## 16     C      M  2.41644317  5.6945657
## 17     D      L  0.81360330  0.9968010
## 18     E      M  0.40960660 -0.9019663
## 19     C      L -1.17163046  4.6381110
## 20     D      M -0.24963011  1.2392639
## 21     E      L  0.53398691 -2.5536274
## 22     C      M  0.49836627  1.9283502
## 23     D      L  2.90779067  2.0799784
## 24     E      M  0.46805499  2.4910436
## 25     C      L  4.79055189  3.2300238
## 26     D      M  0.14001835  0.1179122
## 27     E      L  4.15109399  3.9107540
## 28     C      M  1.32388240 -1.6058043
## 29     D      L -1.17090581  6.0396272
## 30     E      M  2.15387460  0.2036981
## 
## [[2]]
##    group factor   response1  response2
## 1      C      L  1.88440888  2.6461739
## 2      D      M  2.22705282  3.2663260
## 3      E      L  0.40180725 -1.6634448
## 4      C      M -2.20117619  2.9366835
## 5      D      L  0.32106609  4.6593864
## 6      E      M -0.16261549  3.4244321
## 7      C      L  0.21927598  4.6095241
## 8      D      M  2.69102871 -0.6595714
## 9      E      L  2.36945441  1.6140334
## 10     C      M  1.47542617  5.2895315
## 11     D      L -0.42124377  3.5540886
## 12     E      M  6.22663792  3.1940995
## 13     C      L -2.25329470  5.1999782
## 14     D      M -2.21461259  0.6513798
## 15     E      L  1.68063489  3.8783060
## 16     C      M  6.45577541 -0.4565079
## 17     D      L  0.34561992  1.2094514
## 18     E      M -1.51379350  2.4637259
## 19     C      L  0.13720379  3.7053366
## 20     D      M  6.09770650 -5.9804790
## 21     E      L -0.54281600  5.4095976
## 22     C      M -0.01468471  3.2653183
## 23     D      L  0.45995483  6.0502478
## 24     E      M  2.49623313  5.3112707
## 25     C      L  2.33718762  3.9411383
## 26     D      M  0.08940268  2.5269074
## 27     E      L -2.85569045  3.9606734
## 28     C      M  4.76395413  1.8035291
## 29     D      L -1.49942226  3.9365665
## 30     E      M  0.02131006  3.0928821
## 
## [[3]]
##    group factor   response1   response2
## 1      C      L  2.68688314 -1.66685283
## 2      D      M -0.35295773  4.67358211
## 3      E      L  0.21713419  2.76176853
## 4      C      M  1.80988129  1.80255072
## 5      D      L -1.21371739  2.60439810
## 6      E      M  0.23339918  9.43310154
## 7      C      L  1.58804050  3.41525838
## 8      D      M  4.58612134  5.97859425
## 9      E      L  2.62806434  4.00579546
## 10     C      M -1.64004879  1.58781676
## 11     D      L -2.71089045 -2.23351509
## 12     E      M -1.03712943  5.60108931
## 13     C      L -1.09022217  2.89727032
## 14     D      M -0.30257531  3.36868065
## 15     E      L  1.76536531  3.31423533
## 16     C      M  3.06379942  1.35493659
## 17     D      L  1.26214995  1.06363257
## 18     E      M  1.75438347 -0.07789126
## 19     C      L -2.77539016 -5.18868905
## 20     D      M  2.04043881 -2.75282480
## 21     E      L  2.71830632  7.27624470
## 22     C      M -0.60497731 -5.08866037
## 23     D      L  0.01607897  3.17951620
## 24     E      M  0.57809136  3.69781359
## 25     C      L  0.07264678 -1.13197111
## 26     D      M  1.59895913  5.06933988
## 27     E      L -0.21094765  0.46200796
## 28     C      M  0.12197573  2.14321002
## 29     D      L -0.44150726  6.17863099
## 30     E      M  2.56161006  0.70250262