IAI CS2 study materials
IAI CS2 study resources

IAI CS2: Statistical modelling with R and data analysis

IAI CS2 · Work through your exam, one topic at a time.

All guide sections

Statistical modelling with R and data analysis

CS2 tests practical R skills: building model matrices, fitting survival and GLM models, extracting results and checking assumptions.

Core R

Formulas use y ~ x1 + x2, with * for main effects and interactions and : for interaction only. Factors are coded with treatment contrasts by default; use relevel() to change the baseline. survival::Surv(time, status) treats status=1 as the event. survfit() gives Kaplan–Meier curves; coxph() fits Cox models; glm() fits GLMs with family= and offset(). summary() reports coefficients, standard errors, z-values and deviance.

Worked example

coxph(Surv(time, event) ~ age + sex, data=d) returns a coefficient for age of 0.03. The hazard ratio per year is exp(0.03)=1.030; for a 10-year age difference it is exp(0.30)=1.35. A fitted survival curve for a new subject uses survfit(fit, newdata=data.frame(age=60, sex='F')). Do not read the baseline hazard directly from the coefficient table.

Checks

Inspect residuals, influence, martingale residuals for functional form, and Schoenfeld residuals for proportional hazards. For GLMs check deviance, Pearson residuals and overdispersion. Use set.seed() for reproducible simulation. Handle missing data explicitly with na.omit() or multiple imputation.

Traps

Coding status as 1 for censored reverses every result. Treating a factor as numeric fits a trend, not group effects. Omitting newdata in predict() can give the wrong covariate profile. Singular fits, empty factor levels and perfect separation produce unreliable coefficients. Do not report p-values without checking the model.