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

set.seed(10)
rnorm(30, c(0,1,2), c(1,2,3))
##  [1]  0.01874617  0.63149492 -2.11399165 -0.59916772  1.58909025
##  [6]  3.16938290 -1.20807618  0.27264797 -2.88001805 -0.25647839
## [11]  3.20355901  4.26734452 -0.23823356  2.97488941  4.22417039
## [16]  0.08934727 -0.90988771  1.41454885  0.92552126  1.96595705
## [21]  0.21106809 -2.18528684 -0.34973188 -4.35718358 -1.26519802
## [26]  0.25267689 -0.06266629 -0.87215883  0.79647799  1.23865841
  1. Simulate 2 continuous variables (normal distribution) (n=20) and plot the relationship between them
# place the code to simulate the data here
set.seed(1)
x = rnorm(20, 0, 1)
y = rnorm(20, 2, 4)
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
set.seed(3)

x1 = runif(100, min = 50, max = 100)
x2 = runif(100, min = 70, max = 110)

y = rnorm(100, 0, 1)

model = lm(y ~ x1 + x2)
plot(model)

summary(model)
## 
## Call:
## lm(formula = y ~ x1 + x2)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.37635 -0.80796  0.09194  0.69199  2.64910 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.131098   1.154744  -0.114    0.910
## x1           0.005420   0.007808   0.694    0.489
## x2          -0.002822   0.010466  -0.270    0.788
## 
## Residual standard error: 1.106 on 97 degrees of freedom
## Multiple R-squared:  0.00614,    Adjusted R-squared:  -0.01435 
## F-statistic: 0.2996 on 2 and 97 DF,  p-value: 0.7418
  1. Simulate 3 letters repeating each letter twice, 2 times.
# place the code to simulate the data here


rep(letters[3:6], each = 3, times = 2)
##  [1] "c" "c" "c" "d" "d" "d" "e" "e" "e" "f" "f" "f" "c" "c" "c" "d" "d"
## [18] "d" "e" "e" "e" "f" "f" "f"
  1. Create a dataframe (n = 27) with 3 groups, 2 factors and two quantitative response variables. Use the replicate function.
# place the code to simulate the data here

set.seed(1)
group = rep(letters[1:3], length.out = 27)
factor = rep(LETTERS[1:2], length.out = 27)
data.frame(group , factor, x = rnorm(27,0,1), y = runif(27,-1,1))
##    group factor           x           y
## 1      a      A -0.62645381 -0.85864191
## 2      b      B  0.18364332 -0.80106768
## 3      c      A -0.83562861 -0.36745659
## 4      a      B  1.59528080  0.03726853
## 5      b      A  0.32950777  0.32401015
## 6      c      B -0.82046838 -0.18633963
## 7      a      A  0.48742905  0.82575185
## 8      b      B  0.73832471 -0.41279325
## 9      c      A  0.57578135 -0.08186855
## 10     a      B -0.30538839 -0.33521065
## 11     b      A  1.51178117  0.30174093
## 12     c      B  0.38984324 -0.48396644
## 13     a      A -0.62124058 -0.04290950
## 14     b      B -2.21469989  0.53262134
## 15     c      A  1.12493092 -0.83150617
## 16     a      B -0.04493361  0.75064266
## 17     b      A -0.01619026 -0.32185412
## 18     c      B  0.94383621  0.67888070
## 19     a      A  0.82122120 -0.30663302
## 20     b      B  0.59390132 -0.33245014
## 21     c      A  0.91897737 -0.04729751
## 22     a      B  0.78213630  0.78439667
## 23     b      A  0.07456498  0.72867894
## 24     c      B -1.98935170 -0.22002091
## 25     a      A  0.61982575  0.55464140
## 26     b      B -0.05612874  0.92123599
## 27     c      A -0.15579551 -0.13068103