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.
set.seed(1995)
rnorm(30, mean = c(0,5,10), sd = c(1,3,5))
##  [1]  1.060763265  3.993494795 10.726521054  0.394078983  9.994762183
##  [6]  8.265473031 -0.005393194  4.947461376  8.806591755  1.670791287
## [11]  4.928218823  9.334958797  0.188207631  7.993069619  3.396602758
## [16] -0.072543746 -1.913829197  3.058819470 -2.653018940  4.849832882
## [21]  8.369086270  0.293081629  0.706771914 11.375341927 -2.132264981
## [26]  3.080277536  4.848819299  0.206696762  6.138042981 10.861662905
  1. Simulate 2 continuous variables (normal distribution) (n=20) and plot the relationship between them
x = rnorm(20, mean = 10, sd=1)
y = rnorm(20, mean = 20, sd=5)
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.
x1 = runif(50, min = 10, max = 100)
x2 = runif(50, min = 0, max = 10)
y = rnorm(50)
lm(y ~ x1 + x2)
## 
## Call:
## lm(formula = y ~ x1 + x2)
## 
## Coefficients:
## (Intercept)           x1           x2  
##    0.250397     0.002401    -0.070273
  1. Simulate 3 letters repeating each letter twice, 2 times.
rep( c("X","Y","Z"), each = 2, times = 2)
##  [1] "X" "X" "Y" "Y" "Z" "Z" "X" "X" "Y" "Y" "Z" "Z"
  1. Create a dataframe with 3 groups, 2 factors and two quantitative response variables. Use the replicate function.
data.frame(group = rep(letters[1:3],time = 10),factor = rep(LETTERS[1:2], time = 15),x = rnorm(30, mean =0, sd=10), y = rnorm(30, mean = 0, sd =10))
##    group factor          x          y
## 1      a      A   5.802173 -23.522361
## 2      b      B  14.788854 -13.945076
## 3      c      A  16.158572 -12.357687
## 4      a      B  -3.103241  -4.765202
## 5      b      A  -5.293007  -4.686147
## 6      c      B  11.757887  -3.107518
## 7      a      A -13.143190   6.027219
## 8      b      B  -5.499469  -3.017784
## 9      c      A   2.635502  -2.253546
## 10     a      B   1.892875 -20.748872
## 11     b      A  17.598278  -7.794778
## 12     c      B   7.540676  -9.314308
## 13     a      A   9.443436  -6.025985
## 14     b      B   3.045825   2.898934
## 15     c      A  -6.154158   6.638792
## 16     a      B  -2.621706  -2.524916
## 17     b      A   7.358197   7.232890
## 18     c      B  -8.807589   7.958472
## 19     a      A  14.230338   9.349891
## 20     b      B -11.256325 -14.825067
## 21     c      A   8.130921 -16.326079
## 22     a      B  13.897050  24.502928
## 23     b      A -10.362836   8.475704
## 24     c      B   3.028917  13.033368
## 25     a      A -18.163087  -6.955070
## 26     b      B   7.188592   4.999233
## 27     c      A  11.040131   4.790662
## 28     a      B  12.960219  -3.048139
## 29     b      A  -6.706139  -4.154095
## 30     c      B  -1.564294  -7.656052