Questions
On your PC go to your Y: drive (or any drive on your personal PC/Mac) and create a new folder called matlabexam1920. Then, go to the EC5200 Moodle page, download the data-set prisons.csv, and save this data-set in the folder that you have just cre- ated.
Now open matlab and create a script file named matlabexam firstname lastname.m, save this script file in the folder that you just created and make sure that matlab has this folder set as the current working folder.
Please answer ALL of the following questions.
(i) Open the data-set prisons.csv using the command importdata. The syntax is
the following:
myData = importdata(Filename, delimiter, Nb of header lines)
(You will lose marks if you dont use the above command). (2 Marks)
You should now have a structure named myData that contains three elements: the raw data, the textdata and the column headers. The data-set contains information on prisoners in a certain district over time. The following syntax:
varname = myData.data(rows,cols)
allows you to create a new variable based on the rows and cols of the raw data that you specify.
(ii) Using the data in the first column of the raw data create a variable called year.
(5 Marks)
(iii) Create a variable called ssize and fill this with the sample size. What is the sample size? (5 Marks)
(iv) Now create the variables inmates, time, time a and urate using the correct columns in the raw data matrix. (5 Marks)
Page 3 of 6 NEXT PAGE 2018-19
EC5200
EC5200
The variable year defines the particular year of the prison sentence. The variable time describes the year and month of the prison sentence. The variable time a is equal to time plus 12 months (we wont use this variable). The variable urate describes the unemployment rate at the year and month of the prison sentence.
(v) What is the mean number of inmates? Store this value in a variable named minmate. (5 Marks)
(vi) What is the mean unemployment rate? Store this value in a variable named murate. (5 Marks)
(vii) Create a new variable called loginmate that is equal to the log of the variable inmates. (5 Marks)
Next we would like to inspect the relationship between the number of inmates and the other variables in the dataset.
(viii) Create a scatter plot between year (x-axis) and inmates (y-axis). Label each axes and give the graph the title EC5200 Exam Figure 1. Save the figure as a .jpeg file. Comment on the relationship. (5 Marks)
(ix) Create a scatter plot between urate (x-axis) and inmates (y-axis). Label each axes and give the graph the title EC5200 Exam Figure 2. Save the figure as a .jpeg file. Comment on the relationship. (5 Marks)
Next we would like to estimate the impact of the unemployment rate on the number of prisoners (inmates). To do this we can run an OLS regression using the fitlm function with the following syntax:
lm = fitlm(X,y,linear)
(ix) Run the regression between inmates (Y) and urate (X). What is the slope coefficient 1? What is the intercept coefficient 0? Comment on the value of the slope, what does this number imply, why is it strange? (5 Marks)
(x) Plot the regression line [Hint: plot(lm)]. Label the axes and give the graph the title EC5200 Exam Figure 3. Save the figure as a .jpeg file. (5 Marks)
The last thing we would like to do is imagine that we live in a world where the function fitlm does not exist. Therefore if we want to run a regression we would need to write our own function that does this for us.
Page 4 of 6 NEXT PAGE 2018-19
EC5200
(xi) Open a new script and create a function called OLSestim. We want the function to take the matrix of data X and the vector of data y as input variables and we want the function to return the estimated OLS coefficients (beta hat) and their standard errors (sigma hat) as outputs. The syntax on the script should therefore be the following:
function [beta hat, sigma hat] = OLSestim(X,Y) .
end
Save the function script in your created folder as OLSestim.m. (xii) The formula for (beta hat) is the following:
(5 Marks)
= (XX)1Xy
In your function script write the above formula [Hint: use the inv() function for
matrix inversion. The syntax will be: beta hat = ]. (5 Marks)
For our OLSestim function to calculate the standard errors, , of the OLS coefficients we first need to calculate the residuals: u. To calculate the residuals we use the follow- ing formula:
u = y y = y X
(xiii) In the next line of your function script create a variable called residuals using the
above formula. Hint:
(xiv) The unbiased estimator of the error variance, 2, is given by: 2 = u u
nk
residuals = Y X*beta hat
(5 Marks)
Where u are the residuals and (n k) is the degrees of freedom. In the next line
of your function script create a variable called df which is equal to the degrees
of freedom. [Hint: degrees of freedom is the difference between the length of X and the length of beta hat.] (5 Marks)
Page 5 of 6 NEXT PAGE 2018-19
EC5200
(xv) Using the residuals u and degrees of freedom (n k), in the next line of your function script create a variable sigma sq that is equal to the error variance. Hint: The syntax will be
sigma sq = (residuals*residuals)/df
(xvi) The unbiased estimator of the standard errors is given by:
= 2
(5 Marks)
Therefore, in the final line of your function script define the output variable sigma hat using the above formula. Hint: the syntax uses the sqrt() function for squared root. (5 Marks)
Now you have created your OLSestim function, save it, and return to your original exam matlabexam firstname lastname.m script file. In order to check that our function works we need to convert our existing data into the X and y variables.
(xvii) The y variable is just our variable inmates so you can leave this as is or create a variable y = inmates. Our X variable is the variable urate, however the X matrix needs to be of the form:
1 urate(1,1)
1 urate(2,1) X = . . ..
1 urate(n,1)
Create the X matrix by combining the the variable urate with a vector of ones of
length samplesize. (10 Marks)
(xviii) Now use your function to obtain the estimated OLS coefficients (beta hat) and
their standard errors (sigma hat), the syntax will be the following: [beta hat, sigma hat] = OLSestim(X,y)
Compare the outcome vector beta hat with the estimated coefficients you ob- tained using the fitlm function (i.e. lm.coefficients) if your function worked correctly these values should be the same. (10 Marks)
END
Page 6 of 6
[Total 100 marks]
Reviews
There are no reviews yet.