Tutorial 01 Soln

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

ST1131

Tutorial 1 Solution

1. Consider the following data on diastolic blood pressure (measured in mm Hg) for 9 patients. Answer
questions below and leave your final answers to 2 decimal places. Can use R.

Patient 1 2 3 4 5 6 7 8 9
Blood pressure 96 119 119 108 126 128 110 105 94

(a) Input the data into R and sort it. Manually find the median of the dataset, then use R to get the
median. Are the results the same?.
(b) Determine the mean, variance and standard deviation of the dataset.
(c) Suppose we are now told that the measurement machine was not zero-ed, and that everyone’s
reading should be reduced by 10 mm Hg. What is the new mean and variance of the dataset?
Does the mean change? Does the variance change?
(d) Create a histogram of the new data (using probability) and label its x-axis.
Solution:

(a) Create a variable, named “bp” which has 9 values as given:


> bp = c(96, 119, 119, 108, 126, 128, 110, 105, 94)
> sort(bp)
[1] 94 96 105 108 110 119 119 126 128
The 5th (middle) value after sorting is 110. Hence, the median is 110.
Median of bp derived from R is the same.
> median(bp)
[1] 110
(b) Mean, variance and standard deviation of bp:
> mean(bp)
[1] 111.6667
> var(bp)
[1] 149.75
> sd(bp)
[1] 12.23724
(c) Create the new variable “new-bp” which is less than bp by 10.
> new_bp = bp - 10
> mean(new_bp)
[1] 101.6667
> var(new_bp) # equal to variance of bp
[1] 149.75
(d) Histogram is in Figure1.
> hist(new_bp, col = 5, freq = FALSE, main = "Histogram of new BP", xlab = "New-BP")
> # this histogram has 8 bins (8 intervals) by default

1
ST1131

Figure 1: Histogram of New BP, Q1e

2. For each of the following experiments, identify the variable(s), the data type of each variable, and the
sample size.

(a) For each of 10 beetles, a biologist counted the number of times the beetle fed on a disease-resistant
plant during a 4-hour period.
(b) In a nutritional study, 40 healthy males were measured for height and weight as well as the weight
of their food intake over a 24-hour period.
(c) The birth weight, number of siblings, mother’s race, and age were recorded for each of 85 babies
born at NUH.
Solution:

(a) The only one quantitative discrete variable recorded is the count. The sample size is 10.
(b) There are 3 variables recorded: height (quantitative continuous), weight (quantitative continuous)
and weight of food intake (quantitative continuous). The sample size is 40.
(c) There are 4 variables recorded: birth weight (quantitative continuous), number of siblings (quan-
titative discrete), race (categorical nominal) and age (quantitative continuous). The sample size
is 85.

3. (Question for Group Work) The marks of a final exam of a module are recorded in the file final_marks.

(a) Import the data into R. Re-name the second column of the data as “mark”.
(b) How may student took the exam? Derive some summaries of the final mark (mean, median,
variance, IQR and range).

2
ST1131

(c) Create a histogram of the final marks (by probability) and label its x-axis. By default, how many
bins that the created histogram has? Do you know how to change the number of bins? Hint:
argument breaks should be used if you want to change the number of bins of a histogram.
Solution:

> #(a)
> data = read.csv("C:/Data/final_marks")
> names(data)[1] = 'ID' # first column
> names(data)[2] = 'mark' # second column
> attach(data)
> #(b)
> length(mark) # 98 students

[1] 98

> summaries = c( mean(mark), median(mark), var(mark), range(mark), IQR(mark))


> summaries

[1] 31.33163 33.00000 98.73682 6.00000 50.00000 11.75000

> #(c) # might add the tittle and labels


>
> hist(mark, col = 5, freq = FALSE, main = "Histogram of Final Mark", xlab = "Final Mark")
> # 9 bins by default
>
> hist(mark, col = 5, freq = FALSE, main = "Histogram of Final Mark",
+ xlab = "Final Mark", breaks = 20)
> # try with breaks = 15 and see how!

3
ST1131

Figure 2: Boxplot of Final Marks

Figure 3: Histograms of Final Marks with different number of bins

You might also like