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.
- rnorm(30,mean=c(0,5,50),sd=c(1,5,50))
##  [1]  -0.05534132  -1.73213588 -58.79905208  -0.32841138  -9.56992128
##  [6]   6.24274388  -0.40272684   0.13474864 -90.37500864   0.47500898
## [11] -10.21880386 -24.32505531   0.44750144   1.77447043  34.42350785
## [16]  -0.63980604  -4.03829728   4.43645133  -0.23971759  -5.68031867
## [21] -56.17604212   1.48026801  -3.11839178 -39.73670249   0.23301151
## [26]  -8.85451242   8.29250361   1.83350939   0.20801260 -52.57939928
  1. Simulate 2 continuous variables (normal distribution) (n=20) and plot the relationship between them
 x = rnorm(20, mean = 0, sd = 1)
 y = rnorm(20, mean = 0, sd = 2)
 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(100,1,10)
x2=runif(100,10,50)
y=rnorm(100,0,1)
z=lm(y~x1+x2)
z
## 
## Call:
## lm(formula = y ~ x1 + x2)
## 
## Coefficients:
## (Intercept)           x1           x2  
##     0.24343      0.02187     -0.01174
plot(z)

  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.
data.frame(group = rep(letters[1:3], each = 9),
           factor = rep(LETTERS[3:4], length.out = 27),
           response1 = rnorm(n = 27, mean = 0, sd = 1),
           response2 = rnorm(n = 27, mean = 5, sd = 10))
##    group factor   response1  response2
## 1      a      C  0.09226074   6.443345
## 2      a      D -1.51739655   1.288241
## 3      a      C  1.53244407   3.326551
## 4      a      D  0.88334411   8.067706
## 5      a      C  0.93157642  -9.965306
## 6      a      D -1.18092937  21.523020
## 7      a      C -0.98260998  15.283673
## 8      a      D  0.34962727  10.137495
## 9      a      C  0.06869167  10.593271
## 10     b      D -0.72870261   3.912825
## 11     b      C -0.91255616  -4.057265
## 12     b      D  0.13758202  13.537658
## 13     b      C  1.07742584   3.563702
## 14     b      D  1.71665547 -10.872134
## 15     b      C -0.16653991  -1.578495
## 16     b      D  1.30055147  10.919823
## 17     b      C  0.43256327  15.331849
## 18     b      D  0.05458147   7.423865
## 19     c      C -1.73655788   5.334525
## 20     c      D -0.37156025  20.943954
## 21     c      C  0.41717382   7.541988
## 22     c      D  0.15441627   4.347190
## 23     c      C  0.87989926  -0.485862
## 24     c      D  1.47678302  -6.930454
## 25     c      C -0.95344479   4.616637
## 26     c      D -0.19437369   9.687533
## 27     c      C  2.31451961   4.092424