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(1)
rnorm(30,mean=c(0,1,10),sd=c(1,10,100))
##  [1]   -0.62645381    2.83643324  -73.56286124    1.59528080    4.29507772
##  [6]  -72.04683841    0.48742905    8.38324705   67.57813517   -0.30538839
## [11]   16.11781168   48.98432364   -0.62124058  -21.14699887  122.49309181
## [16]   -0.04493361    0.83809737  104.38362107    0.82122120    6.93901321
## [21]  101.89773716    0.78213630    1.74564983 -188.93516959    0.61982575
## [26]    0.43871260   -5.57955067   -1.47075238   -3.78150055   51.79415602
  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(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.
# place the code to simulate the data here
set.seed(4)
x1=runif(100,1,10)
x2=runif(100,100,200)
y=rnorm(100,0,1)
z=lm(y~x1+x2)
summary(z)
## 
## Call:
## lm(formula = y ~ x1 + x2)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.26719 -0.64226  0.00468  0.81089  2.01481 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.597013   0.584765  -1.021    0.310
## x1           0.023268   0.038059   0.611    0.542
## x2           0.002585   0.003606   0.717    0.475
## 
## Residual standard error: 1.019 on 97 degrees of freedom
## Multiple R-squared:  0.009247,   Adjusted R-squared:  -0.01118 
## F-statistic: 0.4526 on 2 and 97 DF,  p-value: 0.6373
plot(z)

  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 (n = 27) with 3 groups, 2 factors and two quantitative response variables. Use the replicate function.
# place the code to simulate the data here
a=data.frame(group=rep(letters[1:3]),factor=rep(letters[4:5]),
           x=rnorm(6,0,1),y=rnorm(6,1,2))
a
##   group factor          x          y
## 1     a      d  1.2147301 -3.8441746
## 2     b      e -1.5478003  2.1126566
## 3     c      d -0.3022460  3.2110678
## 4     a      e  1.0392077  1.3328736
## 5     b      d -0.7678417  0.5490754
## 6     c      e  1.5246726  0.5431753