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(5)
rnorm(30,mean = c(1, 20, 400), sd = c(5, 25 , 125))
##  [1]  -3.2042774  54.6089836 243.0635172   1.3507138  62.7860218
##  [6] 324.6365023  -1.3608319   4.1157172 364.2782956   1.6905411
## [11]  50.6907586 299.7775682  -4.4019630  16.0616411 266.0299950
## [16]   0.3050693   5.0671726 127.0041550   2.2040863  13.5161148
## [21] 512.5639932   5.7093470  56.6990476 488.3451362   5.0950447
## [26]  12.6629538 577.3236341   8.4938691   3.5729476 293.4005700
  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(7)
x=rnorm(20,0,1)
set.seed(8)
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(5)
x1=runif(40,5,35)
x2=runif(40,20,140)
y=rnorm(40,0,1)
fitR=lm(y~x1+x2)
summary(fitR)
## 
## Call:
## lm(formula = y ~ x1 + x2)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.81346 -0.37544 -0.04157  0.38539  1.78756 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)  
## (Intercept)  0.988223   0.478037   2.067   0.0458 *
## x1          -0.021601   0.014756  -1.464   0.1517  
## x2          -0.006996   0.003319  -2.108   0.0418 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7672 on 37 degrees of freedom
## Multiple R-squared:  0.1251, Adjusted R-squared:  0.07782 
## F-statistic: 2.646 on 2 and 37 DF,  p-value: 0.08435
plot(fitR)

  1. Simulate 3 letters repeating each letter twice, 2 times.
# place the code to simulate the data here
rep(letters[5:7],each=2,times=2)
##  [1] "e" "e" "f" "f" "g" "g" "e" "e" "f" "f" "g" "g"
  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(5)
group = rep(letters[5:7],length.out=27)
factor= rep(letters[1:2], length.out=27)
response = replicate(n=2, expr = rnorm(27, mean = 0, sd = 1))
data.frame(group, factor, response)
##    group factor          X1          X2
## 1      e      a -0.84085548  1.49877383
## 2      f      b  1.38435934 -0.65708209
## 3      g      a -1.25549186 -0.85279544
## 4      e      b  0.07014277  0.31591504
## 5      f      a  1.71144087  1.10969417
## 6      g      b -0.60290798  2.21546057
## 7      e      a -0.47216639  1.21710364
## 8      f      b -0.63537131  1.47922179
## 9      g      a -0.28577363  0.95157383
## 10     e      b  0.13810822 -1.00953265
## 11     f      a  1.22763034 -2.00047274
## 12     g      b -0.80177945 -1.76218587
## 13     e      a -1.08039260 -0.14260813
## 14     f      b -0.15753436  1.55006037
## 15     g      a -1.07176004 -0.80242318
## 16     e      b -0.13898614 -0.07457892
## 17     f      a -0.59731309  1.89566795
## 18     g      b -2.18396676 -0.45656894
## 19     e      a  0.24081726  0.56222336
## 20     f      b -0.25935541 -0.88700851
## 21     g      a  0.90051195 -0.46024458
## 22     e      b  0.94186939 -0.72432849
## 23     f      a  1.46796190 -0.06921116
## 24     g      b  0.70676109  1.46324856
## 25     e      a  0.81900893  0.18772610
## 26     f      b -0.29348185  1.02202286
## 27     g      a  1.41858907 -0.59183483