1/36
Looks like no tags are added yet.
Run 2+7
Run sqrt(2)
Run a vector for 1-10 and store it in ‘x’ and calculate its mean
Run a matrix with data = 1to10 row = 2 col = 5
Summarise the matrix you made
expression <- matrix(
c(12, 15, 18,
20, 17, 22,
14, 19, 16,
25, 21, 23),
nrow = 4,
ncol = 3,
byrow = TRUE
)
Display the matrix expression.
Extract the expression level of Gene B in sample 3.
Extract the entire second row.
Extract the entire third column.
Replace the value in row 1, column 1 with 30.
Use dim() to find the dimensions of the matrix.
Use ls() to see which objects you have created.
Use summary() on the matrix.age <- c(22, 35, 48, 56, 29, 67, 41, 52, 18, 73)Write R commands to:
Identify patients older than 50.
Identify patients younger than or equal to 40.
Select patients aged between 30 and 60 using &.
Select patients younger than 30 OR older than 65 using |.
Select patients who are NOT older than 50 using !.
Select patients whose ages are either 22, 48 or 73 using %in%.
Count how many patients are older than 50.
patient_id <- c(101, 102, 103, 104, 105, 106)
treatment <- c("Chemo", "Radio", "Chemo", "Radio", "Chemo", "Radio")
age <- c(45, 62, 38, 55, 71, 49)
response <- c(1, 0, 1, 1, 0, 1)Create a data frame called patients containing all four variables.
Display the complete data frame.
Extract only the age column using $.
Extract the treatment and response columns using column indexing.
Extract rows 2, 4 and 6, showing only patient_id and age.
Find the dimensions of the data frame using dim().
Display the first six rows using head().
Display the structure of the data frame using str().
Generate a statistical summary using summary().
Extract all columns except patient_id.
A researcher is analysing an oncology dataset.
Create an object called patient_age containing the values 34, 45, 56, 62 and 71.
Display all objects currently in your workspace.
Save the complete workspace as oncology.RData.
Save only the object patient_age as age_data.RData.
Remove the object patient_age.
Load age_data.RData back into R.
Find your current working directory.
Change your working directory to a folder called Oncology_Project (use a path that exists on your computer).
You have a file called patients.csv containing patient information.
Write an R command to import this CSV file and store it as patients.
Import a tab-delimited file called patients.txt.
Import a general text file using read.table(), specifying that the first row contains column names.
tumour_size <- c(2.1, 3.5, 4.2, 5.1, 6.0)Calculate the mean tumour size.
Calculate the logarithm of 100 using base 10.
Calculate the square root of the mean tumour size.
Store the result of the mean in an object called mean_size.
Display the help documentation for mean().
age <- c(34, 45, NA, 62, NA, 71, 56)Identify which observations are missing.
Count the number of missing and non-missing observations.
Calculate the mean age.
Calculate the mean age after removing missing values.
Calculate the median age after removing missing values.
Create a table that includes missing values.
A clinical researcher records treatment allocation:
treatment <- c(
"Chemotherapy",
"Radiotherapy",
"Chemotherapy",
"Immunotherapy",
"Radiotherapy",
"Immunotherapy"
)Convert treatment into a factor.
Display its levels.
Display the internal numerical representation of the factor.
Create a factor with the levels ordered as follows:
Immunotherapy
Chemotherapy
Radiotherapy
Change the reference group to Radiotherapy using relevel().
Count the number of patients in each treatment group using table().
A researcher wants to investigate whether age and treatment type are associated with tumour response.
Write the appropriate R formulas for:
Tumour response explained by age.
Tumour response explained by age and treatment.
Tumour response explained by age, treatment and their interaction.
An interaction term between age and treatment only.
Tumour response explained by the logarithm of age.
A researcher fits a linear regression model:
model <- lm(tumour_size ~ age, data = patients)Write the R command to:
Display a short model output.
Display a detailed model summary.
Extract the regression coefficients.
Extract the residuals.
Obtain the confidence intervals.
Obtain the fitted values.
Predict tumour size for new patients with ages 40 and 60.
The dataset is currently in wide format:
patients <- data.frame(
patient_id = c(1, 2, 3, 4, 5, 6),
baseline = c(5.2, 4.8, 6.1, 5.5, 7.0, 4.9),
month_3 = c(4.8, 4.2, 5.5, 5.0, 6.2, 4.5),
month_6 = c(4.1, 3.9, 4.8, 4.4, 5.5, 4.0)
)Tasks:
Load the tidyr package.
Convert the dataset from wide to long format using pivot_longer().
The dataset is currently in long format:
response <- data.frame(
patient_id = c(1, 1, 1, 2, 2, 2, 3, 3, 3,
4, 4, 4, 5, 5, 5),
time = rep(c("baseline", "month_3", "month_6"), 5),
response = c(2, 4, 6,
3, 5, 7,
1, 3, 5,
4, 5, 6,
2, 3, 4)
)Tasks:
Convert the dataset from long to wide format using pivot_wider().
What is Descriptive statistics
what do the data look like, and can
they be trusted?
What is Confidence intervals
how uncertain is my estimate?
What is Diagnostic testing statistics
sensitivity, specificity, and the
number often misunderstood: the predictive value of a positive
test
3 types of data
•Binary data: HPV status (infected/not infected), myocardial- t-test, ANOVA, linear regression
infarction (yes/no), alive
•Continuous data: cholesterol, triglyceride concentration, quality of life- chi2, logistic regression
•Time-to-event data (survival): time to death, time to
recurrence after treatment, time to get employed- Kaplan-Meier, log-rank, Cox
Shapes of distribution

Measures of center
Mean
Median
Mode
If the distribution is right-skewed, then
Median < mean
What are the Measures of spread/dispersion (no need to know the formulas)
•Standard deviation
•Variance
•Range
•Interquartile range
Common practice in medical articles
•For symmetric distributions, report mean and SD
•For skewed distributions, report median and IQR
•For proportions, report n and % (GOOD TO KNOW)
Pearson correlation
r = +1 perfectly positive linear association
r = 0 no linear association
r = -1 perfectly negative linear association
Correlation tells you X and Y move together , but not why
What is the mean FEV1 in a population of children aged 7 – 10
year?
Sample: N = 636 children, mean is 1.59 L, SD = 0.30
The confidence interval is good because the mean falls between it.

Defind Confidence interval for mean
An interval, derived from sample data, that has a specified probability (such as 95%) of containing the true population parameter (here the mean) if the sampling process is repeated indefinitely
What proportion of children in a population of children aged 7 –
10 year have an FEV1 <1.5L?
> mean(fev1 < 1.5)= 0.3726415

Can a Covid-19 test correctly distinguish Covid-19 cases from healthy controls?
Sensitivity and specificity do not directly inform on the
predictive value of a positive or negative tests. So you can perform
Positive predictive value (PPV)
= positive cases / number of positives
Negative predictive value (NPV)
= negative controls / number of negatives
Descriptive statistics
A researcher investigates tumour volumes in 200 patients diagnosed with lymphoma. The following summary statistics are obtained:
Measure | Result |
|---|---|
Mean tumour volume | 710 ml |
Median tumour volume | 410 ml |
Standard deviation | 855 ml |
Minimum | 50 ml |
Maximum | 6,050 ml |
Interquartile range | 760 ml |
1. Based on the mean and median, what can you conclude about the shape of the distribution? Explain your answer. (2 points)
2. Which measure of central tendency would you prefer to report: mean or median? Explain why. (2 points)
3. Explain the difference between standard deviation and variance. (2 points)
4. What does the interquartile range tell you about this dataset? (2 points)
5. The researcher creates a boxplot and observes several points above the upper whisker. What do these points represent, and how are potential outliers defined using the IQR? (3 points)
Confidence intervals
A clinical researcher investigates the mean reduction in tumour volume after six months of chemotherapy.
The study includes 100 patients.
The results are:
Mean reduction: 12 mm
Standard deviation: 8 mm
Sample size: 100
1. Calculate the standard error of the mean. (2 points)
2. Calculate the 95% confidence interval for the population mean reduction using:
\[ 95\%\,CI=\bar{x}\pm1.96\times SE \]
(3 points)
3. Interpret the confidence interval in the context of this study. (2 points)
4. Suppose another study uses a sample of 400 patients, with the same standard deviation and mean. Would its confidence interval be wider or narrower? Explain why. (2 points)
5. Explain why a 95% confidence interval does not mean that there is a 95% probability that the true population mean lies within this particular calculated interval. (2 points)
Diagnostic testing
Researchers develop a new blood test to detect an early-stage cancer. Histopathological examination is used as the gold standard.
The test is evaluated in 1,000 individuals.
The results are:
Blood test | Cancer present | Cancer absent | Total |
|---|---|---|---|
Positive | 45 | 95 | 140 |
Negative | 5 | 855 | 860 |
Total | 50 | 950 | 1,000 |
1. Calculate the sensitivity of the blood test. (2 points)
2. Calculate the specificity of the blood test. (2 points)
3. Calculate the positive predictive value (PPV). (2 points)
4. Calculate the negative predictive value (NPV). (2 points)
5. A patient receives a positive test result. What is the probability that this patient actually has cancer, based on this study? Which diagnostic measure answers this question? (2 points)
6. Explain why a test can have high sensitivity and specificity but a relatively low PPV. (3 points)
Prevalence and predictive values
A screening test for a rare cancer has:
Sensitivity = 90%
Specificity = 95%
The test is used in two populations, each containing 1,000 individuals.
Population A: Cancer prevalence = 1%.
Population B: Cancer prevalence = 30%.
1. Calculate the number of individuals with cancer and without cancer in each population. (2 points)
2. For Population A, calculate:
True positives.
False positives.
PPV.
(3 points)
3. For Population B, calculate the same three values. (3 points)
4. Compare the PPV between the two populations. Why does it differ even though sensitivity and specificity remain unchanged? (3 points)
5. What happens to NPV when disease prevalence increases? Explain using these two populations. (2 points)
Confidence interval for a proportion
A study investigates the proportion of patients who respond to a new immunotherapy.
Out of 200 patients, 120 show a clinical response.
1. Calculate the observed response proportion. (1 point)
2. Calculate the standard error of the proportion using:
\[ SE_p=\sqrt{\frac{p(1-p)}{n}} \]
(2 points)
3. Calculate the 95% confidence interval for the response proportion. (2 points)
4. State the assumption underlying the confidence interval formula presented in the lecture. (2 points)
R questions
A researcher collects tumour volume measurements (ml) and patient ages (years) from 10 oncology patients.
volume <- c(120, 150, 180, 200, 250,
300, 350, 420, 600, 1500)
age <- c(35, 42, 50, 55, 61,
48, 67, 72, 58, 75)1. R-question. Calculate the mean, median and standard deviation of tumour volume. Show your code. (3 points)
2. R-question. Calculate the 25th and 75th percentiles of tumour volume. (2 points)
3. R-question. Create a histogram of tumour volume. (1 point)
4. Based on the histogram and summary statistics, would you describe the distribution as symmetric or skewed? Which measure of central tendency would you report? Explain. (2 points)
5. R-question. Create a boxplot of tumour volume and add horizontal lines indicating the first quartile, median and third quartile. (3 points)
6. R-question. Create a scatterplot showing the relationship between age and tumour volume. (2 points)
7. R-question. Calculate the Pearson correlation coefficient between age and tumour volume. (1 point)
8. Interpret the correlation coefficient. Does it establish that increasing age causes tumour volume to increase? Explain. (2 points)