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(17)
rnorm(30, mean = c(0,5,10), sd = c(1,2,4))
##  [1] -1.01500872  4.84072653  9.06805191 -0.81726793  6.54418168
##  [6]  9.33755226  0.97287443  8.43306796 11.02094802  0.36658112
## [11]  7.36157847 12.57276828  1.29532187  5.37583615 16.36482039
## [16] -0.05517906  6.67694224 10.63748050  0.62595440  6.26716945
## [21] 12.72411059 -0.68203337  3.55348653 16.69410386 -0.59575563
## [26]  7.31968769 10.46968965  0.25922139  5.76472421  7.15407306
  1. Simulate 2 continuous variables (normal distribution) (n=20) and plot the relationship between them
a = rnorm(20, mean = 0, sd=1)
b = rnorm(20, mean = 1, sd=2)
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.
set.seed(17)
x1 = runif(20, min = 2, max = 4)
x2 = runif(20, min = 3, max = 6)
y = rnorm(20, mean = 0, sd = 1)
lm(y ~ x1 + x2)
## 
## Call:
## lm(formula = y ~ x1 + x2)
## 
## Coefficients:
## (Intercept)           x1           x2  
##     -0.8640      -0.3891       0.3367
  1. Simulate 3 letters repeating each letter twice, 2 times.
rep(letters[1:3], each = 2, times =2)
##  [1] "a" "a" "b" "b" "c" "c" "a" "a" "b" "b" "c" "c"
  1. Create a list of 6 datasets (n = 30) each with 3 groups, 2 factors and two quantitative response variables. Use the replicate function.
replicate(3, expr = data.frame(group = rep(LETTERS[5:7]),factor = rep(LETTERS[1:2]),a = rnorm(30, mean =0, sd=1),b = rnorm(30, mean = 1, sd =2)),simplify = FALSE)
## [[1]]
##    group factor           a          b
## 1      E      A  0.25439675  2.1204697
## 2      F      B -0.33152046 -2.5848356
## 3      G      A -0.20631931 -2.1308337
## 4      E      B  1.21533709 -5.6406378
## 5      F      A  1.89205642  1.3094431
## 6      G      B  0.07419352  0.2707467
## 7      E      A  1.75169617 -3.8673677
## 8      F      B -0.23148744  1.6729286
## 9      G      A  0.54345248 -0.2809056
## 10     E      B -0.98900140  4.6422408
## 11     F      A  0.31553146 -2.6934469
## 12     G      B  2.44232746  2.2834337
## 13     E      A  0.54969286  3.3710497
## 14     F      B -0.02924337  0.6446239
## 15     G      A -0.83078338 -1.5722518
## 16     E      B  1.24643439  1.9269013
## 17     F      A -1.37575529  2.7613325
## 18     G      B -0.33110845  1.2788439
## 19     E      A  0.98091149  0.2498903
## 20     F      B  2.19077056  1.0850781
## 21     G      A  0.03054575  2.2677606
## 22     E      B -0.78551741 -2.4149769
## 23     F      A  0.32544056  1.0393731
## 24     G      B -0.88084355  0.4024073
## 25     E      A  0.20932594  0.1164743
## 26     F      B  0.15103295  2.1819506
## 27     G      A -0.34347879 -1.2686416
## 28     E      B  0.90587760  0.1225108
## 29     F      A  0.91895485  1.6898027
## 30     G      B -0.55598749  0.1011993
## 
## [[2]]
##    group factor           a           b
## 1      E      A  0.04382022 -2.42081714
## 2      F      B -0.56799159  1.45251839
## 3      G      A  1.82486031  2.90170052
## 4      E      B  0.43300257  5.21683109
## 5      F      A -1.35396628 -0.24245239
## 6      G      B  1.15446591  3.10929492
## 7      E      A  0.92073695 -2.27801891
## 8      F      B -0.44865746  2.68196639
## 9      G      A  2.07057372 -0.05095001
## 10     E      B -0.19955613  1.12771709
## 11     F      A -1.12912850  0.07269526
## 12     G      B -1.29555486  2.32208947
## 13     E      A  1.24041888  0.12002782
## 14     F      B  1.69505872  0.42368092
## 15     G      A  0.50230094 -1.63189663
## 16     E      B  0.33363165  4.42045451
## 17     F      A -0.50820263  0.34491992
## 18     G      B -0.37063235 -2.67691537
## 19     E      A  1.50864295  3.63882405
## 20     F      B -0.04962579  1.69776417
## 21     G      A  1.38175662  6.83214449
## 22     E      B  0.44893243  1.19983291
## 23     F      A -0.15230996  3.62632158
## 24     G      B  0.25929398  2.37884756
## 25     E      A -1.61140167  0.19654832
## 26     F      B -2.00895599  3.07261705
## 27     G      A -1.99937124 -1.22050411
## 28     E      B  0.12104068  0.68454522
## 29     F      A -0.22999480  4.15321627
## 30     G      B -2.19910208 -1.36167081
## 
## [[3]]
##    group factor            a          b
## 1      E      A -0.929982354 -1.4924405
## 2      F      B -0.690891665 -1.0711312
## 3      G      A -2.454772578 -1.8084179
## 4      E      B -1.169851838 -0.1101744
## 5      F      A -0.968519095  0.2968882
## 6      G      B  0.500602759  2.3336642
## 7      E      A  1.838355197  0.1677266
## 8      F      B  0.013434281 -0.1802875
## 9      G      A -0.064876155 -1.5549570
## 10     E      B -0.178998622  1.4988619
## 11     F      A -0.490471756  0.2399077
## 12     G      B -0.499026408  2.6282122
## 13     E      A  1.092963517  0.4665879
## 14     F      B -0.002666991  3.7338311
## 15     G      A -0.738211350  1.6949850
## 16     E      B  0.357214084  1.6807931
## 17     F      A  0.997496519  3.5550659
## 18     G      B  0.407236722  0.2794558
## 19     E      A -1.053979869  2.1693977
## 20     F      B -1.222487881  3.5132057
## 21     G      A -1.400066901  0.3109449
## 22     E      B -1.218526584 -1.5245291
## 23     F      A -1.200635962  2.3736825
## 24     G      B  2.393215612  0.9041862
## 25     E      A -1.888254891  0.2778562
## 26     F      B  0.544036699  3.2851169
## 27     G      A -0.784871590  3.2156128
## 28     E      B  1.398375475  1.3514631
## 29     F      A  0.836234909  1.3316600
## 30     G      B -1.186698051  0.9238459
megalist <- replicate(6, replicate(3, expr = data.frame(group = rep(LETTERS[5:7]),factor = rep(LETTERS[1:2]),a = rnorm(30, mean =0, sd=1),b = rnorm(30, mean = 1, sd =2)),simplify = FALSE))