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
set.seed(50)
rnorm(30,mean=c(5,17,40),sd=c(3,10,18))
##  [1]  6.6490097  8.5839626 40.5939628  6.5724491 -0.2760411 34.9984385
##  [7]  6.0824853 11.0908756 57.5606300  0.6627502 19.9520677 49.9855401
## [13]  3.5040934 18.9573384 31.8002702  3.9114336 15.4318662 26.2254749
## [19]  1.5019479 13.7657432 33.7013077  3.2393086  1.1012235 70.4120720
## [25]  6.6907509 43.6763339 46.4176291  3.9136388 22.6874633 40.5161416
  1. Simulate 2 continuous variables (normal distribution) (n=20) and plot the relationship between them
# place the code to simulate the data here
var1 = rnorm(20, mean = 1, sd=2)
var2 = rnorm(20, mean = 2, sd=1)
plot(var2~var1)

  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(40)

x1 = runif(20, min = 5, max = 7)
x2 = runif(20, min = 6, max = 7)
y = rnorm(20, mean = 0, sd = 1)

nd=lm(y~x1+x2)
nd
## 
## Call:
## lm(formula = y ~ x1 + x2)
## 
## Coefficients:
## (Intercept)           x1           x2  
##      4.6181       0.4717      -1.1551
plot(nd)

  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

df1=data.frame(group=rep(letters[1:3]),factor=rep(letters[4:5]),
           x=rnorm(6,1,3),y=rnorm(6,2,3))
df1
##   group factor          x         y
## 1     a      d  5.0324652  6.708973
## 2     b      e  1.9466716  5.810699
## 3     c      d  7.1363346 -0.614330
## 4     a      e  0.4525884  3.082650
## 5     b      d -3.4518147  4.747663
## 6     c      e  2.1914969  3.723554
df2=replicate(10, expr = df1,simplify = FALSE)
df2
## [[1]]
##   group factor          x         y
## 1     a      d  5.0324652  6.708973
## 2     b      e  1.9466716  5.810699
## 3     c      d  7.1363346 -0.614330
## 4     a      e  0.4525884  3.082650
## 5     b      d -3.4518147  4.747663
## 6     c      e  2.1914969  3.723554
## 
## [[2]]
##   group factor          x         y
## 1     a      d  5.0324652  6.708973
## 2     b      e  1.9466716  5.810699
## 3     c      d  7.1363346 -0.614330
## 4     a      e  0.4525884  3.082650
## 5     b      d -3.4518147  4.747663
## 6     c      e  2.1914969  3.723554
## 
## [[3]]
##   group factor          x         y
## 1     a      d  5.0324652  6.708973
## 2     b      e  1.9466716  5.810699
## 3     c      d  7.1363346 -0.614330
## 4     a      e  0.4525884  3.082650
## 5     b      d -3.4518147  4.747663
## 6     c      e  2.1914969  3.723554
## 
## [[4]]
##   group factor          x         y
## 1     a      d  5.0324652  6.708973
## 2     b      e  1.9466716  5.810699
## 3     c      d  7.1363346 -0.614330
## 4     a      e  0.4525884  3.082650
## 5     b      d -3.4518147  4.747663
## 6     c      e  2.1914969  3.723554
## 
## [[5]]
##   group factor          x         y
## 1     a      d  5.0324652  6.708973
## 2     b      e  1.9466716  5.810699
## 3     c      d  7.1363346 -0.614330
## 4     a      e  0.4525884  3.082650
## 5     b      d -3.4518147  4.747663
## 6     c      e  2.1914969  3.723554
## 
## [[6]]
##   group factor          x         y
## 1     a      d  5.0324652  6.708973
## 2     b      e  1.9466716  5.810699
## 3     c      d  7.1363346 -0.614330
## 4     a      e  0.4525884  3.082650
## 5     b      d -3.4518147  4.747663
## 6     c      e  2.1914969  3.723554
## 
## [[7]]
##   group factor          x         y
## 1     a      d  5.0324652  6.708973
## 2     b      e  1.9466716  5.810699
## 3     c      d  7.1363346 -0.614330
## 4     a      e  0.4525884  3.082650
## 5     b      d -3.4518147  4.747663
## 6     c      e  2.1914969  3.723554
## 
## [[8]]
##   group factor          x         y
## 1     a      d  5.0324652  6.708973
## 2     b      e  1.9466716  5.810699
## 3     c      d  7.1363346 -0.614330
## 4     a      e  0.4525884  3.082650
## 5     b      d -3.4518147  4.747663
## 6     c      e  2.1914969  3.723554
## 
## [[9]]
##   group factor          x         y
## 1     a      d  5.0324652  6.708973
## 2     b      e  1.9466716  5.810699
## 3     c      d  7.1363346 -0.614330
## 4     a      e  0.4525884  3.082650
## 5     b      d -3.4518147  4.747663
## 6     c      e  2.1914969  3.723554
## 
## [[10]]
##   group factor          x         y
## 1     a      d  5.0324652  6.708973
## 2     b      e  1.9466716  5.810699
## 3     c      d  7.1363346 -0.614330
## 4     a      e  0.4525884  3.082650
## 5     b      d -3.4518147  4.747663
## 6     c      e  2.1914969  3.723554