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
rnorm(30,mean = c(0, 5, 20), sd = c(2, 5 , 8))
##  [1]  2.86524249 12.02750843 18.30542536 -0.80271769  7.61919260
##  [6] 20.11090259  0.29054330  8.70850496 29.35166480 -2.62530829
## [11] -5.63746695 17.32919378 -1.45703382  5.13512197 18.54838869
## [16] -0.06279852  2.82801536 24.17587302 -1.37274296  6.25315103
## [21] 17.21477193  0.47357472 -5.58277977 10.22947262 -2.00781254
## [26] 10.96556459 29.20083330  1.83908200  5.53389675 25.57967724
  1. Simulate 2 continuous variables (normal distribution) (n=20) and plot the relationship between them
# place the code to simulate the data here
z=rnorm(20,mean = 1,sd=2)
y=rnorm(20,mean = 2,sd=1)
plot(y~z)

  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
x1 = runif(30, min = 0, max = 5)
x2 = runif(30, min = 10, max = 30)
y = rnorm(30, mean = 0, sd = 1)
yfit = lm(y ~ x1+x2)
plot(yfit)

  1. Simulate 3 letters repeating each letter twice, 2 times.
# place the code to simulate the data here
rep(LETTERS[1:2], each = 2,times = 2)
## [1] "A" "A" "B" "B" "A" "A" "B" "B"
  1. Create a dataframe with 3 groups, 2 factors and two quantitative response variables. Use the replicate function (n = 25).
# place the code to simulate the data here
data.frame(group=rep(letters[1:3],length.out = 25),
           factor=rep(LETTERS[6:8], length.out = 25),
           response1=rnorm(25, mean = 0, sd = 1),
           response2=runif(25, min = 5, max = 10))
##    group factor   response1 response2
## 1      a      F -0.14950361  5.956279
## 2      b      G -0.83226714  5.647198
## 3      c      H  1.37941389  9.681643
## 4      a      F -2.46851613  7.531703
## 5      b      G  0.31693804  5.007539
## 6      c      H -0.31997401  9.345863
## 7      a      F  0.29330784  7.389729
## 8      b      G  0.21225407  9.074388
## 9      c      H -0.24953419  5.128219
## 10     a      F  1.07701121  8.107565
## 11     b      G -1.25798700  9.872151
## 12     c      H  1.00187937  9.491266
## 13     a      F -0.44082180  6.128091
## 14     b      G  0.53761457  5.151063
## 15     c      H  0.73686489  7.160390
## 16     a      F  0.15923943  5.843257
## 17     b      G -0.01763773  6.338234
## 18     c      H  0.39110587  5.507422
## 19     a      F -0.05581924  5.204717
## 20     b      G -0.77307500  7.129891
## 21     c      H  0.25206865  6.420727
## 22     a      F  0.94039517  9.096427
## 23     b      G  2.02735842  5.225068
## 24     c      H -0.42120555  5.077661
## 25     a      F -1.43366033  8.538559