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.
set.seed(23)
rnorm(30, mean=c(0, 5, 10), sd=c(1, 5, 10))
##  [1]  0.1932123  2.8265895 19.1326710  1.7933881  9.9830255 21.0749049
##  [7] -0.2780863 10.0960274 10.4543718  1.5757796  6.0914423 -0.4653534
## [13] -0.2886886  7.4077514 -2.1637643  0.3081369  2.3991084  5.5768620
## [19] -0.5993128 11.4728891 18.3539125 -0.5660151  8.9420968 -1.6592933
## [25] -0.5308200  4.9947063  4.8743763  1.2428675  1.6970857 11.6662421
  1. Simulate 2 continuous variables (normal distribution) (n=20) and plot the relationship between them
set.seed(3)
x <- rnorm(20, 0, 1)
set.seed(4)
y <- rnorm(20, 0, 1)
plot(x, y)

  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, 100, 200)
y <- rnorm(100, 0, 1)
summary(lm(y~x1+x2))
## 
## Call:
## lm(formula = y ~ x1 + x2)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.30644 -0.76587 -0.06063  0.79112  1.97996 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)
## (Intercept)  0.341921   0.613757   0.557    0.579
## x1          -0.042546   0.040271  -1.056    0.293
## x2          -0.001189   0.003705  -0.321    0.749
## 
## Residual standard error: 1.061 on 97 degrees of freedom
## Multiple R-squared:  0.01216,    Adjusted R-squared:  -0.008205 
## F-statistic: 0.5972 on 2 and 97 DF,  p-value: 0.5524
  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 list of 6 datasets (n = 30) each with 3 groups, 2 factors and two quantitative response variables. Use the replicate function.
simlist = replicate(n=6, expr=data.frame(group=rep(letters[1:3]), factor=rep(letters[4:5]), x=rnorm(30,0,1), y=rnorm(30,1,2)), simplify = FALSE)
str(simlist)
## List of 6
##  $ :'data.frame':    30 obs. of  4 variables:
##   ..$ group : Factor w/ 3 levels "a","b","c": 1 2 3 1 2 3 1 2 3 1 ...
##   ..$ factor: Factor w/ 2 levels "d","e": 1 2 1 2 1 2 1 2 1 2 ...
##   ..$ x     : num [1:30] -0.289 0.667 -0.137 0.224 1.117 ...
##   ..$ y     : num [1:30] 1.791 -0.843 -0.353 -2.888 1.24 ...
##  $ :'data.frame':    30 obs. of  4 variables:
##   ..$ group : Factor w/ 3 levels "a","b","c": 1 2 3 1 2 3 1 2 3 1 ...
##   ..$ factor: Factor w/ 2 levels "d","e": 1 2 1 2 1 2 1 2 1 2 ...
##   ..$ x     : num [1:30] -0.22518 -0.00894 -1.47836 -1.84775 -0.84603 ...
##   ..$ y     : num [1:30] 0.386 2.7797 1.7761 -3.5488 -0.0999 ...
##  $ :'data.frame':    30 obs. of  4 variables:
##   ..$ group : Factor w/ 3 levels "a","b","c": 1 2 3 1 2 3 1 2 3 1 ...
##   ..$ factor: Factor w/ 2 levels "d","e": 1 2 1 2 1 2 1 2 1 2 ...
##   ..$ x     : num [1:30] 0.5931 0.2451 -1.7458 0.0624 -1.086 ...
##   ..$ y     : num [1:30] 2.866 0.455 3.484 -1.121 4.776 ...
##  $ :'data.frame':    30 obs. of  4 variables:
##   ..$ group : Factor w/ 3 levels "a","b","c": 1 2 3 1 2 3 1 2 3 1 ...
##   ..$ factor: Factor w/ 2 levels "d","e": 1 2 1 2 1 2 1 2 1 2 ...
##   ..$ x     : num [1:30] -1.182 0.456 -0.238 0.812 -0.731 ...
##   ..$ y     : num [1:30] 3.65 7.35 -2.74 -1.7 5.61 ...
##  $ :'data.frame':    30 obs. of  4 variables:
##   ..$ group : Factor w/ 3 levels "a","b","c": 1 2 3 1 2 3 1 2 3 1 ...
##   ..$ factor: Factor w/ 2 levels "d","e": 1 2 1 2 1 2 1 2 1 2 ...
##   ..$ x     : num [1:30] -0.0363 0.7874 0.4292 0.6565 -0.7485 ...
##   ..$ y     : num [1:30] 0.785 1.95 -0.674 1.856 -0.292 ...
##  $ :'data.frame':    30 obs. of  4 variables:
##   ..$ group : Factor w/ 3 levels "a","b","c": 1 2 3 1 2 3 1 2 3 1 ...
##   ..$ factor: Factor w/ 2 levels "d","e": 1 2 1 2 1 2 1 2 1 2 ...
##   ..$ x     : num [1:30] 0.349 -1.258 0.476 0.873 -0.544 ...
##   ..$ y     : num [1:30] -0.774 2.274 3.998 -2.002 6.381 ...