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(1)
rnorm(30,mean=c(0,2,20),sd=c(2,20,200))
##  [1]   -1.25290762    5.67286648 -147.12572248    3.19056160    8.59015544
##  [6] -144.09367682    0.97485810   16.76649410  135.15627033   -0.61077677
## [11]   32.23562337   97.96864728   -1.24248116  -42.29399774  244.98618363
## [16]   -0.08986722    1.67619474  208.76724214    1.64244239   13.87802642
## [21]  203.79547432    1.56427260    3.49129967 -377.87033917    1.23965150
## [26]    0.87742521  -11.15910134   -2.94150477   -7.56300110  103.58831204
  1. Simulate 2 continuous variables (normal distribution) (n=20) and plot the relationship between them
set.seed(2)
x<- rnorm(20,0,1)

set.seed(3)
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.
set.seed(4)
x1<- runif(100,2,20)
x2<- runif(100,4,40)
y<- rnorm(100,0,1)

z<- lm(y~x1+x2)
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.
a<- data.frame(group=rep(letters[1:3]),factor=rep(letters[4:5]), x=rnorm(6,0,1),y=rnorm(6,1,2))
a