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(54)
rnorm(30, mean = c(6,9,12), sd = c(1.2,3,2.3))
##  [1]  8.260550 10.483753 11.161301  7.944748 12.498975  9.609317  5.982528
##  [8]  5.488571 16.082362  6.946315 13.992035 13.099439  5.733944 10.434021
## [15]  6.861762  4.436541  9.878774 11.270828  5.027146 12.351800  7.814384
## [22]  6.479338  8.591813 14.303841  6.793682 12.326495 11.388301  3.905868
## [29]  9.498199 10.055245
  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(94)

x <- rnorm(20, mean = 2, sd=0.8)
y <- rnorm(20, mean = 2, sd=3)

plot(x, y, main=" x and y Scatterplot ",
   xlab="variable x ", ylab="variable y ", pch=19)

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

y <- rnorm(40, mean = 0, sd = 1)

x1 <- runif(40, min = -2, max = 5)
x2 <- runif(40, min = 0, max = 1)

lm(y ~ x1 + x2)
## 
## Call:
## lm(formula = y ~ x1 + x2)
## 
## Coefficients:
## (Intercept)           x1           x2  
##   -0.105909     0.003429     0.176627
  1. Simulate 3 letters repeating each letter twice, 2 times.
# place the code to simulate the data here

rep(letters[4:6], each = 2, times =2)
##  [1] "d" "d" "e" "e" "f" "f" "d" "d" "e" "e" "f" "f"
  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

set.seed(17)

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))
data.frame(group, factor, response)
##    group factor          X1          X2
## 1      a      c -1.01500872  0.25922139
## 2      b      d -0.07963674  0.38236210
## 3      c      c -0.23298702 -0.71148174
## 4      a      d -0.81726793 -1.17756957
## 5      b      c  0.77209084 -0.96016651
## 6      c      d -0.16561194 -0.87895224
## 7      a      c  0.97287443 -3.55613648
## 8      b      d  1.71653398 -1.41674984
## 9      c      c  0.25523700 -0.44876927
## 10     a      d  0.36658112 -0.77596771
## 11     b      c  1.18078924 -0.83182805
## 12     c      d  0.64319207  0.05183012
## 13     a      c  1.29532187 -0.61655131
## 14     b      d  0.18791807  0.25439675
## 15     c      c  1.59120510 -0.33152046
## 16     a      d -0.05517906 -0.20631931
## 17     b      c  0.83847112  1.21533709
## 18     c      d  0.15937013  1.89205642
## 19     a      c  0.62595440  0.07419352
## 20     b      d  0.63358473  1.75169617
## 21     c      c  0.68102765 -0.23148744
## 22     a      d -0.68203337  0.54345248
## 23     b      c -0.72325674 -0.98900140
## 24     c      d  1.67352596  0.31553146
## 25     a      c -0.59575563  2.44232746
## 26     b      d  1.15984384  0.54969286
## 27     c      c  0.11742241 -0.02924337