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
rnorm(30, mean=c(1, 50, 200), sd=c(1,5,20))
##  [1]   0.73049413  52.81353180 167.04964632   0.06638393  47.79057959
##  [6] 167.41130601   1.87439242  61.50336150 220.78192344  -0.86212245
## [11]  47.54487497 195.48769204   1.87055825  48.14036413 218.14614681
## [16]   0.56127932  47.40023481 186.04796424   0.61512987  53.79718003
## [21] 210.38589571   1.79445181  36.77730990 220.27214275   0.19797819
## [26]  56.28191261 218.37943658   1.90814664  50.79988056 193.53165615
  1. Simulate 2 continuous variables (normal distribution) (n=20) and plot the relationship between them
# place the code to simulate the data here
x=rnorm(20, mean=0,sd=1)
y=rnorm(20, mean=0,sd=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
x1=runif(250,min=20, max=100)
x2=runif(250,min=15, max=150)
y=rnorm(250, mean=40, sd=5)
lm(y~x1+x2)
## 
## Call:
## lm(formula = y ~ x1 + x2)
## 
## Coefficients:
## (Intercept)           x1           x2  
##  41.3473020   -0.0005936   -0.0152548
  1. Simulate 3 letters repeating each letter twice, 2 times.
# place the code to simulate the data here
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 with 3 groups, 2 factors and two quantitative response variables. Use the replicate function (n = 25).
# place the code to simulate the data here
simlist=replicate(n=25, expr=data.frame(group=rep(letters[1:3],length.out=6), factor=rep(LETTERS[4:5],length.out=6),response=rnorm(n=6, mean=1, sd=1), response2=runif(6, min=30,max=45)),simplify = FALSE)