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(16)
rnorm(30,mean = c(0,15,20),sd=c(2,12,22))
##  [1]   0.9528268  13.4954400  44.1167564  -2.8884581  28.7739515
##  [6]   9.6949351  -2.0119012  15.7627521  42.5493972   1.1462840
## [11]  37.1661852  22.4625341  -1.4920746  34.8985639  35.8778525
## [16]  -3.3261610  21.9109144  30.4007226  -1.0854633  28.5322449
## [21] -16.2515476  -0.6283479  12.8078212  52.3505268  -1.7317976
## [26]  33.3296038  43.1919173   2.0601420  25.0819303  24.7732235
  1. Simulate 2 continuous variables (normal distribution) (n=20) and plot the relationship between them
# place the code to simulate the data here
X=rnorm(20,mean=10,sd=10)
Y=rnorm(20,mean=10,sd=10)
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.
# place the code to simulate the data here
set.seed(15)
x1=runif(30,min=0,max=30)
x2=runif(30,min=30,max=100)
y=rnorm(30,mean = 10,sd=2)
lm(y~x1+x2)
## 
## Call:
## lm(formula = y ~ x1 + x2)
## 
## Coefficients:
## (Intercept)           x1           x2  
##     8.40045      0.02279      0.02868
  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 with 3 groups, 2 factors and two quantitative response variables. Use the replicate function (n = 25).
data.frame(group = rep(letters[4:6]),
           factor = rep(LETTERS[6:7]),
           x = rnorm(30, mean =0, sd=1),
           y = rnorm(30, mean = 10, sd =15))
##    group factor            x          y
## 1      d      F  0.298601577  -1.640431
## 2      e      G -0.427671565  -9.772237
## 3      f      F -0.589199432  41.560901
## 4      d      G -1.296619746  15.682816
## 5      e      F -1.487791903   6.638860
## 6      f      G -1.160348729  33.278334
## 7      d      F -0.332793231   7.448808
## 8      e      G  0.542287542  29.269665
## 9      f      F -0.740264300  -5.030834
## 10     d      G  1.241494105 -26.562308
## 11     e      F -1.525213383   9.399614
## 12     f      G -1.876918033   9.555150
## 13     d      F -1.326949625  -8.121199
## 14     e      G -0.496877106  31.980078
## 15     f      F  0.040101820  26.186630
## 16     d      G -0.544629514   4.978399
## 17     e      F  0.360012921  13.079430
## 18     f      G  1.003106596  23.820814
## 19     d      F -0.982049982 -28.811118
## 20     e      G  2.040274306  16.241093
## 21     f      F  0.003183053  -3.100353
## 22     d      G  2.335035528  19.353651
## 23     e      F  0.154263359  12.082491
## 24     f      G -0.465579380  16.090965
## 25     d      F -0.469543894  15.005901
## 26     e      G -0.496542207  19.177405
## 27     f      F -0.322340429 -12.625296
## 28     d      G  0.614664971   3.104069
## 29     e      F  0.127627263  25.821625
## 30     f      G -0.151659147   7.040857