1. The file question1.RData contains a selection of R objects:
events a data frame containing information on the event name (event name), event date
(Date), speaker (speaker), number of attendees (attendance), the organiser (organiser)
and the general theme of the event (theme)
employers a data frame containing a list of employer names (name), the number of managers (managers), the number of employees (employees)
(a) Read the file question1.RData into R.
(1 marks)
(b) Focusing on the events data set, using any appropriate method:
(i) Calculate the average attendance across all of the events held
(1 marks)
(ii) Calculate the total number of attendees for each individual theme. Which theme was the
most popular?
(3 marks)
(iii) Calculate the proportion of all attendees that were obtained by each organiser. Produce
an appropriate plot to compare the success of the organisers.
(4 marks)
(c) Moving on to the employers data set, using any appropriate method:
(i) Add a column to the data set that contains the ratio of employees to managers
(1 marks)
(ii) Add a column to the data set that contains a label for the size of the organisation based
on the following rules:
small: fewer than 30 employees
medium: 30 to 100 employees
large: more than 100 employees
(3 marks)
(iii) Produce an appropriate plot to compare the ratio column by the size of the organisation.
What does this tell you?
(3 marks)
CONTINUED ON NEXT SHEET
MM916 Page 2 of 4
2. In this question you will be asked to create and manipulate a series of R objects.
(a) Predictions from a linear model can be calculated using a design matrix X and a vector of
coecients and doing the matrix multiplication X
(i) Create an object X that contains the following matrix:
2
6
6
6
6
6
6
6
6
6
6
6
6
6
6
4
1 2.2 10.1
1 2.3 9.2
1 2.4 8.3
1 2.5 7.4
1 2.6 6.5
1 2.7 5.6
1 2.8 4.7
1 2.9 3.8
3
7
7
7
7
7
7
7
7
7
7
7
7
7
7
5
(2 marks)
(ii) Create a vector beta that contains the values 0.85, -2.5, 1.2
(1 marks)
(iii) Calculate the predictions from the linear model.
(1 marks)
(b) (i) Create a list which the following elements:
A factor with elements slow, fast, slow, average, slow, average, fast, fast,
slow, average. The factor levels should be ordered from slow to fast.
A vector of 100 values drawn from the normal distribution with mean 3 and standard
deviation 0.75
A character vector that contains the following entries Orange, Orange, Orange,
Orange, Orange, Lemon, Lemon, Lemon, Lime, Lime, Lime, Lime
You should name the elements of your list speed, normal and fruits respectively.
(4 marks)
PLEASE TURN OVER
MM916 Page 3 of 4
2. (cont.)
(ii) Create a function called location that will take a vector and return an appropriate
measure of location. Specifically, the mode should be returned for categorical values, and
the median should be returned for numeric values.
You can assume that you have access to the function calculate mode() defined below:
calculate_mode <- function(x){
y <- table(x)
return(names(y)[which.max(y)])
}
(4 marks)
(iii) Construct a loop that will apply the function average to each element of the list created
in (b)(i) and store the result of each in an element of a list.
(3 marks)
(iv) Using an appropriate apply function, write code that will obtain the result in part (iii)
without using a loop.
(2 marks)
3. You have been given two csv files:
admissions.csv Admissions and attendances due to poisoning for hospitals across Wales
accesses.csv Accesses to a poisons information database for hospitals across Wales
We are interested in the relationship whether there may be some influence of accesses on admission
to hospital, using attendances to account for fluctuations in actual poisonings.
(a) Read in these two data sets, and tidy if required
(3 marks)
(b) What is the average proportion of poisoning attendances admitted across Wales?
(2 marks)
(c) Which hospital in Wales had the lowest proportion of poisoning attendances admitted?
(2 marks)
(d) What is the relationship between the proportion of poisoning attendances admitted and the
ratio of accesses to attendances?
(5 marks)
(e) Do the proportion of poisoning attendances admitted and the ratio of accesses to attendances
follow the same trend through time?
(5 marks)
PART I: MATHEMATICAL AND STATISTICAL COMPUTING
Address all the parts of the questions below using the appropriate R commands and functions. Your MS Word document should provide the answers to each part with any required
interpretation. There are marks for correct answers and marks for using R to compute the
answer. The answer and the R code
used to compute the answer should be clearly shown in
your document. Correct answers with no appropriate R code will only be awarded half marks.
1. The dataset Salary Data.csv contains data on the salary of 30 individuals against their
years of experience. Download this dataset into R from Myplace, read it into R, attach it,
and answer the questions below:
(2 marks)
(a) Create a plot of the Salary against Years of Experience. Label the axes appropriately
and give the plot a suitable title.
(4 marks)
(b) Fit a least squares regression line of Salary against Years of Experience and add the
line to the plot.
(3 marks)
(c) What is the predicted salary of someone who has six years of experience? Find a 90%
prediction interval for this value.
(3 marks)
PLEASE TURN OVER
MM916 Page 3 of 4
2. The following data show the weights of the five women in kg on a diet A
72.6 82.4 86.2 69.7 58.3
and the weights of eight women in kg on a diet B
62.5 57.3 82.7 74.6 83.2 76.2 75.8 71.4
(a) Use R to calculate the mean and standard deviation of the weights of women on diet
A.
(1 marks)
(b) If we wish to use the sample mean and sample standard deviation in (a) to calculate a
parametric 90% confidence interval for the population mean weight of women on diet
A what assumption must we make about the weights of the women on diet A?
(1 marks)
(c) Making this assumption use R to calculate a parametric 90% confidence interval for
the population mean weight of women on diet A.
(3 marks)
(d) Give the interpretation of this confidence interval.
(1 marks)
(e) Now suppose that we are told that the weights of the women do not follow a normal
distribution on either diet. What test should we use to decide whether the weights of
the women in kg on diet A are different from the weights of the women in kg on diet
B?
(1 marks)
(f) State the null and alternative hypotheses clearly.
(2 marks)
(g) Perform this test using a 5% significance level. State your conclusions clearly.
(3 marks)
3. If the random variable X follows a Binomial distribution with N =10 and p = 0.5 calculate:
P(X ? 7),
(1 marks)
CONTINUED ON NEXT SHEET
MM916 Page 4 of 4
PART II: REGRESSION MODELLING
4. A group of microbiologists are interested in examining the relationship between cooking
time (x) and the levels of a particular virus when cooking to get an idea of how long it
will take to reduce the level of virus down to a safe level. They choose to measure the
log reduction (log10(Final particle count) ? log10(Initial particle count)) in the virus (y).
They take measurements each minute of cooking over the course of 10 minutes and get the
following data:
Cooking Time 1 2 3 4 5 6 7 8 9 10
Log Reduction 0.25 0.59 1.00 1.28 1.59 1.31 1.53 2.44 2.60 2.60
(a) They want to fit a simple linear regression. Given the following summary statistics,
calculate the estimated intercept and slope coefficients.
P10
i=1 xi = 55,
P10
i=1 yi = 15.19 P10
i=1 x
2
i = 385,
P10
i=1 y
2
i = 29.108,
P10
i=1 xiyi = 104.99
(4 marks)
(b) The residual variance is ?
2 = 0.0793. Calculate a 95% confidence interval for the
coefficient of ?
1, the slope coefficient. Provide an interpretation of the effect of cooking
time on the log reduction.
(4 marks)
(c) The researchers decide that temperature is likely to influence the log reduction in
addition to cooking time. They conduct experiments at a total of 5 temperatures,
noting the log reduction every minute for 10 minutes as before.
(i) Complete the ANOVA table shown below:
Degrees of Freedom SS MS F
Regression ? 56.321 ? ?
Residual ? ? 0.0856
Total ? 60.346
(3 marks)
(ii) Conduct the F-test. Clearly state your conclusions.
(2 marks)
PLEASE TURN OVER
MM916 Page 5 of 4
4. (c) ctd.
(iii) They fit a multiple linear regression and obtain the output as follows:
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.0386 0.1624 -0.238 0.4066
Time 0.2695 0.0712 3.785 2.174e-04
Temperature 0.1582 0.0917 1.725 0.0455
Perform a hypothesis test to test whether the coefficient for temperature is significantly greater than 0.
(4 marks)
(iv) Calculate R2 and comment on what this tells you about the model fit.
(3 marks)
(d) Using the plots provided below, in addition to any other relevant information.
(i) Comment on whether the modelling assumptions have been met.
(4 marks)
(ii) Name another plot that would be of interest when checking the assumptions of this
model.
(1 marks)
?1.5 ?0.5 0.5 1.5
?2 ?1
0 1 2
3
Residuals vs Fitted
Fitted values
Residuals
?2 ?1 0 1 2
?2 ?1
0 1 2
3
Normal Q?Q
Theoretical Quantiles
Standardized Residuals
END OF PAPER
(Prof. D. Greenhalgh, Dr. K. Pyper)