Below are the solutions to these exercises on sampling and probability distributions.

#############

## Exercise 1a##

#############

runif(100, min = 1, max = 10)

#[1] 9.780551 4.915710 4.008769 8.372824 6.775713

#[6] 8.832459 6.957932 4.194109 6.925421 2.308527

#[11] 2.044076 2.291099 4.509208 7.335082 4.246440

#[16] 2.228025 6.610441 7.768081 2.994688 3.047809

#[21] 6.909353 3.990725 3.446598 3.222873 5.109256

#[26] 5.702262 3.280422 3.542001 8.696024 7.736156

#[31] 3.269599 3.149147 8.731397 1.709249 3.984788

#[36] 1.807158 5.651274 7.799826 7.332424 9.943115

#[41] 9.248685 6.592572 6.543715 8.931380 9.926542

#[46] 1.351960 4.103141 2.737698 4.135201 4.285058

#[51] 4.692175 5.457288 8.196830 8.955838 7.295562

#[56] 1.913668 8.874886 2.235781 1.683170 1.211319

#[61] 8.980673 1.421672 1.663627 8.327886 8.071481

#[66] 7.646086 4.215541 3.071268 3.143475 5.267710

#[71] 7.605349 8.098806 4.144262 4.797972 7.670589

#[76] 2.710996 6.106898 4.389118 1.487348 6.579999

#[81] 8.058196 4.577448 4.683571 8.551447 1.037305

#[86] 2.620450 7.805814 4.455469 6.445145 1.178804

#[91] 1.168007 1.149788 7.193945 8.209478 8.701719

#[96] 5.369072 7.512252 2.746432 8.820689 1.332865

#############

## Exercise 1b##

#############

x<-runif(100, min = 1, max = 10)

## You can try with any of these functions

trunc(x)

round(x)

floor(x)

as.integer(x)

ceiling(x)

## Output for as.integer(x)

#[1] 7 1 4 1 6 5 2 6 7 2 1 8 9 3 8 2 5 7 1 8 1 3 1 8 1

#[26] 7 2 5 6 4 9 7 6 5 9 5 9 8 8 9 2 3 7 3 4 7 5 8 4 2

#[51] 3 2 2 5 1 6 4 8 5 7 8 7 6 8 6 3 4 1 4 4 4 1 1 7 6

#[76] 6 7 6 4 5 8 9 5 3 3 3 2 4 4 9 3 7 2 1 1 1 6 8 9 5

#############

## Exercise 2a##

#############

rbinom(20, # Number of cases

1, # Success

0.5) # Probability of success

#[1] 1 1 0 0 0 0 0 1 1 0 0 0 1 1 0 1 1 0 1 0

#############

## Exercise 2b##

#############

sample (c(0,1), ## Sample Space. In this case, failures (0s) and successes (1s)

20, ## Number of cases in our sample

replace = TRUE) ## For each trial, R can choose between 0 and 1

#[1] 1 1 1 1 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1

#############

## Exercise 3###

#############

rpois(20, # Number of cases

100 # Lambda; i.e. rate

)

#[1] 102 97 91 101 102 110 81 100 89 103 110 101 113

#[14] 99 84 95 89 100 110 89

#############

## Exercise 4###

#############

rnorm(100, # Number of measurements

mean = 70, # Mean weight

sd = 15) # Standard deviation

#[1] 86.49731 80.63634 82.32721 46.76626 68.03364

#[6] 89.45663 73.36500 75.52357 95.97515 75.10273

#[11] 73.84059 66.05854 75.16734 95.47462 64.56994

#[16] 89.75081 76.09787 90.08907 62.14693 54.08657

#[21] 86.42257 58.76324 64.47074 94.76718 80.03427

#[26] 62.10984 86.90517 52.46937 71.17778 75.02063

#[31] 50.98269 97.36343 85.32673 72.21644 92.34989

#[36] 59.82479 66.35089 102.12031 74.95219 76.32532

#[41] 64.85528 79.06342 73.50814 61.51803 58.32016

#[46] 46.41298 67.66412 56.96889 68.10957 60.94251

#[51] 100.01151 83.26851 75.86673 97.42299 84.91384

#[56] 73.98103 98.91787 53.64478 72.08669 70.16648

#[61] 67.38428 63.53133 86.94389 106.73524 74.22453

#[66] 89.21939 60.35366 69.23425 76.92985 77.97779

#[71] 27.50811 52.20721 66.40375 84.95595 53.69006

#[76] 64.99024 61.52867 73.67423 96.90350 63.21939

#[81] 78.21546 78.85665 67.49837 42.25173 52.28590

#[86] 58.02115 62.76109 57.64997 65.80515 45.67784

#[91] 63.05681 43.54221 87.71432 68.41412 36.66735

#[96] 58.23280 73.16955 68.35519 58.33367 79.36997

#############

## Exercise 5a##

#############

sample(1:6, # The Sample Space

10, # Trials

replace = TRUE)

#[1] 4 3 1 2 2 2 3 3 2 5

#############

## Exercise 5b##

#############

sample(1:6, # The Sample Space

10, # Trials

replace = TRUE,

prob = c(0.16,0.16,0.16,0.16,0.16,0.34))

#[1] 4 1 2 1 6 4 4 1 2 2

#############

## Exercise 6a##

#############

x<- c("Allan","Nicholas","Danielle","Antony","Jessica","John","Alexandre","Gil","Lizeth","Erick","Kelly","Mario","Ruben","Victoria","Axel","Diane","Denisse","Shaira","Alice","Daniel")

Intervention_ind<- sample(seq_len(length(x)), 10)

Intervention<- x[Intervention_ind]

print(Intervention)

#[1] "Erick" "Antony" "Mario" "Ruben"

#[5] "Allan" "Alice" "Victoria" "Gil"

#[9] "Nicholas" "Jessica"

#############

## Exercise 6b##

#############

Intervention_ind<- sample(seq_len(length(x)), 10)

Intervention<- x[Intervention_ind]

Controls<- x[-Intervention_ind]

data.frame(Intervention,Controls)

# Intervention Controls

#1 Erick Danielle

#2 Antony John

#3 Mario Alexandre

#4 Ruben Lizeth

#5 Allan Kelly

#6 Alice Axel

#7 Victoria Diane

#8 Gil Denisse

#9 Nicholas Shaira

#10 Jessica Daniel

#############

## Exercise 7###

#############

Ind<- sample(seq_len(dim(dB)[1]),200)

dS<- dB[Ind,]

dim(dS) ## Just to show that we only sampled 200 cases

print(head(dS[,1:5])) ## Due to space constraints, it isn't feasible to show 200 cases

# HOSPNUM RDELAY RCONSC SEX AGE

#1920 27 44 D M 56

#10169 303 9 D F 67

#7116 191 8 F M 70

#9365 78 35 F F 70

#12397 390 40 F F 89

#7970 70 7 D M 85

#############

## Exercise 8###

#############

set.seed(221) # Here you can put any number you want, as long as it is the same, the random numbers will be replicated

Ind<- sample(seq_len(dim(dB)[1]),200)

dS<- dB[Ind,]

dim(dS)

head(dS[,1:5])

# HOSPNUM RDELAY RCONSC SEX AGE

#11768 501 43 F F 65

#93 5 36 F M 68

#11746 515 40 F M 84

#11665 37 25 F M 67

#5362 171 29 F M 73

#4377 74 32 F F 60

set.seed(221)

Ind<- sample(seq_len(dim(dB)[1]),200)

dS<- dB[Ind,]

dim(dS)

head(dS[,1:5])

# HOSPNUM RDELAY RCONSC SEX AGE

#11768 501 43 F F 65

#93 5 36 F M 68

#11746 515 40 F M 84

#11665 37 25 F M 67

#5362 171 29 F M 73

#4377 74 32 F F 60

## Additional demonstration of replicable random numbers. REMEMBER that the seed is ## always returned to "random" after each number generation occurs, so in order to ## ensure replicability

## the seed should be set back to the SAME Number before each trial

set.seed(221)

x<-ceiling(runif(10,min = 0,max = 100))

set.seed(221)

y<-ceiling(runif(10,min = 0,max = 100))

set.seed(221)

z<-ceiling(runif(10,min = 0,max = 100))

set.seed(221)

xx<-ceiling(runif(10,min = 0,max = 100))

set.seed(221)

xy<-ceiling(runif(10,min = 0,max = 100))

data.frame(x,y,z,xx,xy)

# x y z xx xy

#1 61 61 61 61 61

#2 1 1 1 1 1

#3 61 61 61 61 61

#4 61 61 61 61 61

#5 28 28 28 28 28

#6 23 23 23 23 23

#7 11 11 11 11 11

#8 39 39 39 39 39

#9 58 58 58 58 58

#10 23 23 23 23 23

Dave X says

5b is wrong, since the probabilities sum to more than one, they will be normalized, and sample() will reduce them all.