==— title: “ANLY 505 - Data Simulation in R” subtitle: “Week 1” author: “Abhilasha Vyas” date: “2020-01-21” output: html_document —

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(24)
rnorm(30, mean = c(2,4,24), sd = c(5,10,100))
##  [1]  -0.7294038   9.3658530  65.9623149  -0.9181360  12.4746002  50.6021979
##  [7]   4.2229264  -0.6649512 -60.8370044   2.0115597  -9.1690812  83.8269113
## [13]  -1.8110719 -10.2909030  57.2244449  -0.3453034   0.6501321 177.6252156
## [19]   5.0499727   9.1633570  16.5691439  -1.0257847 -13.0964518  -2.8693105
## [25]  -1.2429575   3.0588987  15.4459049   2.5976554   2.8370361 -70.3827236
  1. Simulate 2 continuous variables (normal distribution) (n=20) and plot the relationship between them
set.seed(24)
x = rnorm(20, mean = 0, sd = 1)

set.seed(48)
y = rnorm(20, mean = 0, sd = 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(6)
x1 <- runif(240, min = 4, max = 40)
x2 <- runif(240, min = 40, max = 400)
y  <- rnorm(240,2,6)

reg_model <- lm(y~x1+x2)

reg_model
## 
## Call:
## lm(formula = y ~ x1 + x2)
## 
## Coefficients:
## (Intercept)           x1           x2  
##   -0.295902     0.013084     0.007702
plot(reg_model, which = c(1,1))

  1. Simulate 3 letters repeating each letter twice, 2 times.
rep(letters[20:22],each=2,times=2)
##  [1] "t" "t" "u" "u" "v" "v" "t" "t" "u" "u" "v" "v"
  1. Create a dataframe (n = 27) with 3 groups, 2 factors and two quantitative response variables. Use the replicate function.
data_frame =data.frame(group = rep(letters[17:19],each=9),factor=rep(letters[4:5], length.out = 27),
           x=rnorm(27,0,1),y=rnorm(27,1,2))

data_frame
##    group factor          x             y
## 1      q      d  0.4632402  0.4512904062
## 2      q      e -1.8238217  0.9911453391
## 3      q      d -0.1098550  0.6825682315
## 4      q      e  1.9445952 -0.6096713632
## 5      q      d -0.5072251  3.3595908226
## 6      q      e -1.6688437 -0.0002675354
## 7      q      d  0.6399316  2.4407661394
## 8      q      e  2.5534569  0.4419748169
## 9      q      d -0.5199372  3.7572906959
## 10     r      e -1.3712730 -1.5332664302
## 11     r      d -0.6760043  1.3758984656
## 12     r      e  0.3733537 -1.6710486160
## 13     r      d -0.6691904  0.1314524201
## 14     r      e -0.7721971  0.8356280046
## 15     r      d  1.5343056  0.6900020943
## 16     r      e -1.6339393 -0.5796955806
## 17     r      d  1.1893242 -1.1380878547
## 18     r      e  0.6784130  0.0429970921
## 19     s      d -1.2677317  3.1603701460
## 20     s      e -0.3778494  0.8062473786
## 21     s      d  0.4466205  0.1824575915
## 22     s      e  1.5638974 -0.0979142554
## 23     s      d  0.5285161  0.5053365089
## 24     s      e  1.1134828 -0.5105598328
## 25     s      d -0.5829032  1.2516075158
## 26     s      e -0.3649710 -1.0540390062
## 27     s      d -0.4466576  0.2630736877
df_replicate = replicate(2, expr = data_frame ,simplify = FALSE)

df_replicate
## [[1]]
##    group factor          x             y
## 1      q      d  0.4632402  0.4512904062
## 2      q      e -1.8238217  0.9911453391
## 3      q      d -0.1098550  0.6825682315
## 4      q      e  1.9445952 -0.6096713632
## 5      q      d -0.5072251  3.3595908226
## 6      q      e -1.6688437 -0.0002675354
## 7      q      d  0.6399316  2.4407661394
## 8      q      e  2.5534569  0.4419748169
## 9      q      d -0.5199372  3.7572906959
## 10     r      e -1.3712730 -1.5332664302
## 11     r      d -0.6760043  1.3758984656
## 12     r      e  0.3733537 -1.6710486160
## 13     r      d -0.6691904  0.1314524201
## 14     r      e -0.7721971  0.8356280046
## 15     r      d  1.5343056  0.6900020943
## 16     r      e -1.6339393 -0.5796955806
## 17     r      d  1.1893242 -1.1380878547
## 18     r      e  0.6784130  0.0429970921
## 19     s      d -1.2677317  3.1603701460
## 20     s      e -0.3778494  0.8062473786
## 21     s      d  0.4466205  0.1824575915
## 22     s      e  1.5638974 -0.0979142554
## 23     s      d  0.5285161  0.5053365089
## 24     s      e  1.1134828 -0.5105598328
## 25     s      d -0.5829032  1.2516075158
## 26     s      e -0.3649710 -1.0540390062
## 27     s      d -0.4466576  0.2630736877
## 
## [[2]]
##    group factor          x             y
## 1      q      d  0.4632402  0.4512904062
## 2      q      e -1.8238217  0.9911453391
## 3      q      d -0.1098550  0.6825682315
## 4      q      e  1.9445952 -0.6096713632
## 5      q      d -0.5072251  3.3595908226
## 6      q      e -1.6688437 -0.0002675354
## 7      q      d  0.6399316  2.4407661394
## 8      q      e  2.5534569  0.4419748169
## 9      q      d -0.5199372  3.7572906959
## 10     r      e -1.3712730 -1.5332664302
## 11     r      d -0.6760043  1.3758984656
## 12     r      e  0.3733537 -1.6710486160
## 13     r      d -0.6691904  0.1314524201
## 14     r      e -0.7721971  0.8356280046
## 15     r      d  1.5343056  0.6900020943
## 16     r      e -1.6339393 -0.5796955806
## 17     r      d  1.1893242 -1.1380878547
## 18     r      e  0.6784130  0.0429970921
## 19     s      d -1.2677317  3.1603701460
## 20     s      e -0.3778494  0.8062473786
## 21     s      d  0.4466205  0.1824575915
## 22     s      e  1.5638974 -0.0979142554
## 23     s      d  0.5285161  0.5053365089
## 24     s      e  1.1134828 -0.5105598328
## 25     s      d -0.5829032  1.2516075158
## 26     s      e -0.3649710 -1.0540390062
## 27     s      d -0.4466576  0.2630736877