Questions
- Simulate data for 30 draws from a normal distribution where the means and standard deviations vary among three distributions.
rnorm(30,mean = c(10,15,20),sd=c(5,15,25))
## [1] 10.5320180 15.6655910 7.7516597 8.5855728 -7.3050996 11.5138094
## [7] 11.2347909 5.8691022 15.5593515 10.5240630 0.2358476 28.6286174
## [13] 4.9955433 10.1182608 67.1821322 8.8163883 29.0953625 98.8950476
## [19] 13.5551158 19.3198679 32.0042510 21.3046826 9.2540808 29.6594515
## [25] 12.5198638 -6.2648255 15.0995153 12.2667669 13.9394460 52.8049924
- Simulate 2 continuous variables (normal distribution) (n=20) and plot the relationship between them
x=rnorm(20,mean = 5,sd=5)
y=rnorm(20,mean=5,sd=5)
plot(x,y)

- 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.
x1=runif(20,min = 0,max = 20)
x2=runif(20,min = 20,max=50)
y=rnorm(20,mean=5,sd=5)
lm(y~x1+x2)
##
## Call:
## lm(formula = y ~ x1 + x2)
##
## Coefficients:
## (Intercept) x1 x2
## 6.378345 -0.003146 -0.088919
- 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"
- Create a dataframe (n = 27) with 3 groups, 2 factors and two quantitative response variables. Use the replicate function.
data.frame(group = rep(letters[20:22]),
factor = rep(LETTERS[22:23]),
x = rnorm(30, mean =5, sd=10),
y = rnorm(30, mean = 10, sd =15))
## group factor x y
## 1 t V 4.6428785 32.7125401
## 2 u W 22.8969820 3.9107277
## 3 v V 0.1187494 -11.1941519
## 4 t W -3.5076477 24.0393004
## 5 u V 7.8820449 16.1290632
## 6 v W 31.5805793 20.1826827
## 7 t V 17.0947034 -0.2006129
## 8 u W 1.1440457 25.6713101
## 9 v V -0.4797382 -0.1405531
## 10 t W -19.7515147 -6.2062851
## 11 u V -0.8758138 30.7724113
## 12 v W 6.1361054 -1.4448210
## 13 t V -4.2186615 14.2229058
## 14 u W -0.1214227 8.3546785
## 15 v V 14.8597965 36.1670467
## 16 t W 17.7405539 -2.4818807
## 17 u V 14.3709136 -19.0546497
## 18 v W 2.7306604 -8.7987447
## 19 t V -4.6410302 3.7393894
## 20 u W 1.3594178 -8.0568408
## 21 v V 4.4150682 0.2667532
## 22 t W 5.9611925 5.0510937
## 23 u V 11.1667545 16.7508082
## 24 v W -0.5151528 25.7537081
## 25 t V 1.8438355 20.6993031
## 26 u W -4.9329225 26.8617589
## 27 v V 19.9136332 2.5550751
## 28 t W -10.8524102 7.1237993
## 29 u V -11.1761965 -23.4070355
## 30 v W 16.6993012 -15.7021740