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(1)
rnorm(30,mean=c(0,1,10),sd=c(1,10,100))
##  [1]   -0.62645381    2.83643324  -73.56286124    1.59528080    4.29507772
##  [6]  -72.04683841    0.48742905    8.38324705   67.57813517   -0.30538839
## [11]   16.11781168   48.98432364   -0.62124058  -21.14699887  122.49309181
## [16]   -0.04493361    0.83809737  104.38362107    0.82122120    6.93901321
## [21]  101.89773716    0.78213630    1.74564983 -188.93516959    0.61982575
## [26]    0.43871260   -5.57955067   -1.47075238   -3.78150055   51.79415602
  1. Simulate 2 continuous variables (normal distribution) (n=20) and plot the relationship between them
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.
x1 <- runif(30, min = 1, max = 5)
x2 <- runif(30, min = 10, max = 25)
y <- rnorm(30, mean = 0, sd = 1)
model1 <- lm(y ~ x1+x2)
plot(model1)

  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 dataframe (n = 27) with 3 groups, 2 factors and two quantitative response variables. Use the replicate function.
set.seed(16)
group <- rep(letters[1:3],length.out=27)
factor <- rep(letters[3:4], length.out=27)
response <- replicate(n=2, expr = rnorm(27, mean = 0, sd = 1))
df <- data.frame(group, factor, response)
df
##    group factor          X1          X2
## 1      a      c  0.47641339  1.03007101
## 2      b      d -0.12538000  0.84016086
## 3      c      c  1.09621620  0.21696470
## 4      a      d -1.44422904 -0.67252558
## 5      b      c  1.14782930  0.13259853
## 6      c      d -0.46841204 -0.07092735
## 7      a      c -1.00595059 -0.94269547
## 8      b      d  0.06356268 -1.02203100
## 9      c      c  1.02497260  0.28055512
## 10     a      d  0.57314202  0.54478337
## 11     b      c  1.84718210  0.13086975
## 12     c      d  0.11193337  0.28184439
## 13     a      c -0.74603732 -0.29273075
## 14     b      d  1.65821366 -1.32535312
## 15     c      c  0.72172057  2.06513565
## 16     a      d -1.66308050  0.24217304
## 17     b      c  0.57590953 -0.34909723
## 18     c      d  0.47276012 -0.63081237
## 19     a      c -0.54273166  0.28390672
## 20     b      d  1.12768707  0.12157699
## 21     c      c -1.64779762  0.56634411
## 22     a      d -0.31417395  0.56903290
## 23     b      c -0.18268157 -0.09058676
## 24     c      d  1.47047849  0.23049649
## 25     a      c -0.86589878  0.75285378
## 26     b      d  1.52746698  0.86338541
## 27     c      c  1.05417806  0.83819864