|
| 1 | +# Exercises: Basic operations in R --------------------------------------------- |
| 2 | + |
| 3 | + |
| 4 | +## Assign objects for weight and height values ---- |
| 5 | +weight_a <- 80 |
| 6 | +weight_b <- 120 |
| 7 | + |
| 8 | +height_a <- 1.6 |
| 9 | +height_b <- 210 / 100 # Convert from centimetres to metres |
| 10 | + |
| 11 | + |
| 12 | +## Exercises for arithmetic operators ---- |
| 13 | + |
| 14 | +### Calculate BMI for both persons ---- |
| 15 | + |
| 16 | +bmi_a <- weight_a / height_a ^ 2 |
| 17 | +bmi_b <- weight_b / height_b ^ 2 |
| 18 | + |
| 19 | + |
| 20 | +## Exercises for relational operators ---- |
| 21 | + |
| 22 | +### Is Person A heavier in weight compared to Person B? ---- |
| 23 | + |
| 24 | +weight_b > weight_b |
| 25 | +weight_b <= height_a # If FALSE, then person A heavier than person B |
| 26 | + |
| 27 | +### Is Person A taller in height compared to Person B? ---- |
| 28 | + |
| 29 | +height_a > height_b |
| 30 | +height_a <= height_a # if FALSE, then person A taller than person B |
| 31 | + |
| 32 | + |
| 33 | +### Whose BMI is greater, Person A or Person B? ---- |
| 34 | + |
| 35 | +bmi_a > bmi_b # if TRUE, then person A has greater BMI than person B |
| 36 | +bmi_a < bmi_b # if TRUE, then person B has greater BMI than person A |
| 37 | + |
| 38 | + |
| 39 | +## Logical operators ---- |
| 40 | + |
| 41 | +### Is the weight of Person A AND the weight of Person B both equal ---- |
| 42 | +### to 80 kilograms? |
| 43 | + |
| 44 | +weight_a == 80 && weight_b == 80 |
| 45 | + |
| 46 | +### Is the weight of Person A OR the weight of Person B less ---- |
| 47 | +### than 100 kilograms? |
| 48 | + |
| 49 | +height_a < 100 || height_b < 100 |
| 50 | + |
| 51 | +### Is the weight of Person A greater than the weight of Person B AND ---- |
| 52 | +### the height of Person A greater than the height of Person B? |
| 53 | + |
| 54 | +weight_a > weight_b && height_a > height_b |
| 55 | + |
| 56 | +### Is the weight of Person A greater than the weight of Person B OR ---- |
| 57 | +### the height of Person A greater than the height of Person B? |
| 58 | + |
| 59 | +weight_a > weight_b || height_a > height_b |
| 60 | + |
| 61 | + |
| 62 | +## Bonus challenge ---- |
| 63 | + |
| 64 | +### Assign objects for values of contingency table ---- |
| 65 | + |
| 66 | +A <- 15 # Exposed and with outcome |
| 67 | +B <- 100 # Exposed and with no outcome |
| 68 | +C <- 135 # Not exposed and with outcome |
| 69 | +D <- 150 # Not exposed and with no outcome |
| 70 | + |
| 71 | +### Calculate odd's ratio ----- |
| 72 | + |
| 73 | +or <- (A * D) / (B * C) |
| 74 | + |
| 75 | +### Calculate 95% confidence interval ---- |
| 76 | + |
| 77 | +#### Standard error of odds ratio ---- |
| 78 | +se <- sqrt(1 / A + 1 / B + 1 / C + 1 / D) |
| 79 | + |
| 80 | +#### Z-score for a 95% confidence interval ---- |
| 81 | +z_score <- 1.96 |
| 82 | + |
| 83 | +#### Natural logarithm of the odd's ratio ---- |
| 84 | +log_or <- log(or) |
| 85 | + |
| 86 | +#### Upper and lower confidence limits ---- |
| 87 | +uci <- exp(log_or + z_score * se) |
| 88 | +lci <- exp(log_or - z_score * se) |
| 89 | + |
| 90 | + |
| 91 | +## Test if calculations for odd's ratio is correct ---- |
| 92 | + |
| 93 | +tab <- matrix(c(A, B, C, D), nrow = 2, ncol = 2, byrow = TRUE) |
| 94 | + |
| 95 | +fisher.test(x = tab) |
0 commit comments