You should use R for this assignment. Deadline: 12 noon, Tuesday 23rd January 2018.
Submit it using Turnitin on the MAS6002/6024/468 MOLE page.
The R le Assignment_3_part_1_data.R on MOLE contains data relating to an exper-
iment with a single covariate, x, and a single response, y. Consider the 2-parameter
(intercept and gradient) linear model y = X + where
y is the vector of responses;
= ( 0; 1)T is the parameter vector where 0 is the parameter representing the
intercept and 1 is the parameter representing the gradient;
X is the corresponding design matrix of dimension n 2;
is the error vector of length n given by N(0;32In) where In is the n by n
identity matrix.
The log-likelihood of the parameter vector given the data is
l( ;y;X) = c 118(y X )T(y X )
where c is a constant.
1. Specify the value of c for the data in Assignment_3_part_1_data.R.
2. Write a function to calculate the log-likelihood for values of 0 and 1 over a
2 dimensional grid of 0 and 1 values. The function should allow the user to
specify the relevant features of the grid. If possible avoid using ‘for’ loops in your
code.
3. Using your answer to part (2.) determine ^ 0 and ^ 1, the maximum likelihood esti-
mates (MLEs), to 2 decimal places.
Do not use any of the built-in optimization functions to do this, nor the known result for
the MLE in a linear model (although it would be sensible to check using these). You are
expected to do exactly what is asked of you in the question.
Part 2
Consider an object moving around the four vertices of a square labelled v1;v2;v3;v4. At
time t = 1 it starts at a speci ed vertex and then ‘moves’ at time t = 2;3;:::;n. At times
t = 2;3;:::;n the object makes either a ‘connected move’ with probability p or a ‘diagonal
move’ with probability 1 p independently of both the time at which the object makes
the move and the current vertex that the object is visiting. These two types of move are
de ned here:
A ‘connected move’ in which the object can move to one of the two vertices it is
connected to by an edge (it cannot make diagonal moves across the square). When
the object moves, it moves to each connected vertex with probability 0.5. In a
‘connected move’ the object cannot stay at the same vertex.
A ‘diagonal move’ in which the object moves to the vertex diagonally across the
square with probability 1.
Let Xt;t2f1;:::;ngrepresent the vertex that the object is visiting at time t. For example
if the object starts at vertex v3 at time t = 1 and moves to vertex v4 at time t = 2 then
X1 = v3 and X2 = v4.
We are interested in the proportion of the n visits that occur within a certain subset of
the vertices (called the ‘target set’ of vertices H) for a given starting position and value of
p. So we are interested in
Pn
t=1 a49(Xt2H)n where a49(A) = 1 if A is true and is zero otherwise.
1. Write a function that simulates the movement of the object around the square,
records the vertices visited at times t2f1;:::;ng and returns
Pn
t=1 a49(Xt2H)n . Your
function should allow the user to specify the value of n, p, the vertices in the target
set H and the starting vertex.
2. Use your function to assess how
Pn
t=1 a49(Xt2H)n is a ected by n and p when H is the
same as the starting vertex. Comment on your results.
Hint: There are many valid approaches to writing this function but you may nd it easier
to consider the vertices of the unit square (i.e. fv1;v2;v3;v4g=f(0;0);(1;0);(1;1);(0;1)g.
There are functions in R that convert binary strings to integers and vice versa. You may
nd it easier to answer this question if your main function in (1.) calls several other func-
tions that you have written.
A Simple illustrative example: consider an object that starts at vertex (0;0) so
that X1 = (0;0) and makes 4 moves. Then we might observe the following sequence of
vertices visited:
X1 = (0;0)
X2 = (1;0)
X3 = (1;1)
X4 = (0;1)
X5 = (1;1)
If H =f(0;0);(1;0)g then
P5
t=1 a49(Xt2f(0;0);(1;0)g)n = 2=5:
Administrative Information
Marking is anonymous, so do not write your name anywhere in the report. Your registra-
tion number should be shown on the rst page of the report. You should submit a single pdf
le for this assignment. The report should be named studentnumber-modulenumber-A3.pdf.
Report Structure and Content
There is no page limit for the report but as a guide 7 pages or fewer should be su cient.
Think about how to communicate your answers concisely and clearly whilst providing
su cient detail. Unnecessarily long reports will be penalized. Just answer the questions
directly and justify your answers. Include all the R code used for each part of the question
in the main report at the point it is used. We strongly recommend you use knitr to do
this. There is a knitr assignment template on MOLE. Annotate the R code where you
think another MSc student might not easily understand what the R code does.
Assessment and Feedback
The Assignments should be your own work and should be undertaken in accordance
with the University’s rules for non-invigilated assessment. Marks will be given on the
scale described in Chapter 1 of the notes. You will receive individual feedback and some
general comments will be posted on MOLE.