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.
# place the code to simulate the data here
set.seed(1)
rnorm(30,mean=c(1,3,5),sd=c(1,10,20))
##  [1]   0.3735462   4.8364332 -11.7125722   2.5952808   6.2950777
##  [6] -11.4093677   1.4874291  10.3832471  16.5156270   0.6946116
## [11]  18.1178117  12.7968647   0.3787594 -19.1469989  27.4986184
## [16]   0.9550664   2.8380974  23.8767242   1.8212212   8.9390132
## [21]  23.3795474   1.7821363   3.7456498 -34.7870339   1.6198257
## [26]   2.4387126   1.8840899  -0.4707524  -1.7815006  13.3588312
  1. Simulate 2 continuous variables (normal distribution) (n=20) and plot the relationship between them
# place the code to simulate the data here
set.seed(2)
x <- rnorm(20,0,10)
set.seed(4)
y <- rnorm(20,0,1)
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.
# place the code to simulate the data here
set.seed(3)
x1 <- runif(50,1,100)
x2 <- runif(50,100,200)
y <- rnorm(50,0,10)
mod1 <- lm(y~x1+x2)
mod1
## 
## Call:
## lm(formula = y ~ x1 + x2)
## 
## Coefficients:
## (Intercept)           x1           x2  
##     7.74775     -0.01825     -0.03982
plot(mod1)

  1. Simulate 3 letters repeating each letter twice, 2 times.
# place the code to simulate the data here
rep(letters[24:26],each=2,times=2)
##  [1] "x" "x" "y" "y" "z" "z" "x" "x" "y" "y" "z" "z"
  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
mod2<- replicate(6,
       expr = data.frame(group = rep(LETTERS[1:3]),
                         factor = rep(LETTERS[4:5]),
                         a = rnorm(30, mean =1, sd=2),
                         b = rnorm(30, mean =3, sd=4)),
                         simplify = FALSE)

str(mod2)
## List of 6
##  $ :'data.frame':    30 obs. of  4 variables:
##   ..$ group : Factor w/ 3 levels "A","B","C": 1 2 3 1 2 3 1 2 3 1 ...
##   ..$ factor: Factor w/ 2 levels "D","E": 1 2 1 2 1 2 1 2 1 2 ...
##   ..$ a     : num [1:30] 2.23 0.19 3.11 2.2 3.03 ...
##   ..$ b     : num [1:30] 5.77 3.57 8.97 -3.53 3.51 ...
##  $ :'data.frame':    30 obs. of  4 variables:
##   ..$ group : Factor w/ 3 levels "A","B","C": 1 2 3 1 2 3 1 2 3 1 ...
##   ..$ factor: Factor w/ 2 levels "D","E": 1 2 1 2 1 2 1 2 1 2 ...
##   ..$ a     : num [1:30] -0.331 2.826 2.929 4.216 4.671 ...
##   ..$ b     : num [1:30] -2.429 0.315 5.6 6.085 13.707 ...
##  $ :'data.frame':    30 obs. of  4 variables:
##   ..$ group : Factor w/ 3 levels "A","B","C": 1 2 3 1 2 3 1 2 3 1 ...
##   ..$ factor: Factor w/ 2 levels "D","E": 1 2 1 2 1 2 1 2 1 2 ...
##   ..$ a     : num [1:30] 1.404 4.369 -0.93 2.324 -0.107 ...
##   ..$ b     : num [1:30] 0.86 8.472 3.567 -0.131 10.526 ...
##  $ :'data.frame':    30 obs. of  4 variables:
##   ..$ group : Factor w/ 3 levels "A","B","C": 1 2 3 1 2 3 1 2 3 1 ...
##   ..$ factor: Factor w/ 2 levels "D","E": 1 2 1 2 1 2 1 2 1 2 ...
##   ..$ a     : num [1:30] 0.994 0.61 -3.469 1.957 4.245 ...
##   ..$ b     : num [1:30] 4.042 5.631 6.063 -5.941 -0.611 ...
##  $ :'data.frame':    30 obs. of  4 variables:
##   ..$ group : Factor w/ 3 levels "A","B","C": 1 2 3 1 2 3 1 2 3 1 ...
##   ..$ factor: Factor w/ 2 levels "D","E": 1 2 1 2 1 2 1 2 1 2 ...
##   ..$ a     : num [1:30] 0.684 5.977 3.143 1.684 1.443 ...
##   ..$ b     : num [1:30] 8.481 0.939 0.127 2.291 1.394 ...
##  $ :'data.frame':    30 obs. of  4 variables:
##   ..$ group : Factor w/ 3 levels "A","B","C": 1 2 3 1 2 3 1 2 3 1 ...
##   ..$ factor: Factor w/ 2 levels "D","E": 1 2 1 2 1 2 1 2 1 2 ...
##   ..$ a     : num [1:30] 5.328 2.761 2.01 1.221 0.414 ...
##   ..$ b     : num [1:30] 2.545 0.425 6.763 10.675 -1.024 ...