A1 — Describe an algorithm
Specify input and desired output → identify missing answers → select valid answers → compute sum and sample size → divide sum by valid count → document the exclusion rule.
Original worked examples, R script, expected results and interpretation guidance for the programming foundations lesson.
All exercises and numbers below are original, fictional teaching examples. They are not solutions to UCL's original assessments. Publishing the key is intentional; students should attempt the starter file before checking it.
# ferdo R Lab — Lesson 0 · instructor worked solution (synthetic data)
scores <- c(12, 10, 14, NA, 9)
length(scores) # 5
mean(scores) # NA
mean(scores, na.rm = TRUE) # 11.25
sum(!is.na(scores)) # 4
id <- c("A", "B", "C", "D", "E")
before <- c(12, 10, 14, NA, 9)
after <- c(14, 13, 13, 15, 12)
delta <- after - before
print(data.frame(id, before, after, delta))
valid_pairs <- sum(!is.na(delta))
increased <- sum(delta > 0, na.rm = TRUE)
decreased <- sum(delta < 0, na.rm = TRUE)
paired_mean_change <- mean(delta, na.rm = TRUE)
pre_mean <- mean(before, na.rm = TRUE)
post_mean <- mean(after)
stopifnot(valid_pairs == 4, increased == 3, decreased == 1,
paired_mean_change == 1.75,
pre_mean == 11.25, post_mean == 13.4)
Download instructor .R solution
Uses only base R; no additional packages or private data.
| Calculation | Answer | Reason |
|---|---|---|
| Number of values | 5 | `length()` counts the missing element too. |
| Mean without removing NA | NA | R propagates missingness under the default rule. |
| Mean of observed pretest values | 11.25 | (12+10+14+9)/4 |
| Valid matched participants | 4 | D has no pretest; D's change is unknown. |
| Individual change values | 2, 3, −1, NA, 3 | Computed as posttest minus pretest for each person. |
| Positive / negative changes | 3 / 1 | Only matched records can be classified. |
| Mean of matched changes | 1.75 | (2+3−1+3)/4 |
| Mean of all posttest values | 13.40 | (14+13+13+15+12)/5 |
| Posttest mean − observed pretest mean | 2.15 | Different denominators; not the same as mean matched change. |
Specify input and desired output → identify missing answers → select valid answers → compute sum and sample size → divide sum by valid count → document the exclusion rule.
`mean(c(2,4,6))` = 4.
`sum(!is.na(before))` = 4.
`sum(delta>0,na.rm=TRUE)` = 3; `sum(delta<0,na.rm=TRUE)` = 1.
`mean(before)` returns NA; `mean(before, na.rm=TRUE)` returns 11.25. This excludes missingness from the calculation but does not solve attrition bias.
A difference alone cannot establish a treatment effect: consider missing observations, regression to the mean, selection and control-group/counterfactual evidence.
The mean of paired individual changes uses four people because D has no pretest; the mean of all posttest scores includes five people. Subtracting the mean of four observed pretests from the mean of five posttests compares different sets of records. Even correct arithmetic can be a poor match for the research question.
Neither figure, by itself, establishes causation.