THE CHINESE UNIVERSITY OF HONG KONG
STAT2005 Programming Languages for Statistics
Take Home Test
Answer ALL questions.
Use R programming language to answer Question 1‐2 and save your answers in a script file
named “[Your_Student_ID].r”, where [Your_Student_ID] is your 10‐digit
student ID.
Question 1 (14 marks)
The use of det() function is prohibited in this question.
Let 𝐴 be a n‐by‐n matrix and |𝐴| be its determinant, |𝐴| could be computed using the
following algorithm.
where 𝐴 is a (n‐1)‐by‐(n1) matrix that results from deleting row 𝑖 and column 𝑗 of 𝐴. Write
a function named my_det() which takes a square matrix and returns the value of its
determinant. Your function should check if the input matrix is a square matrix and return the
error message “Input matrix is not a square matrix.” when necessary.
Question 2 (14 marks)
Secant method is an alternative root‐finding algorithm in numerical analysis to solve f(x) = 0.
Given two initial values x0 and x1, the successive terms are defined recursively as follows.
Implement the Secant method as a R function in the following form
secant <- function(f,x0,x1,n,err) {…}
where f is a function to evaluate f(x), x0 and x1 are the values of x0 and x1 respectively, and
err is the error tolerance in absolute value of f(x). Try to solve
0.5 1.0 1.5 2.0 3 3 3 103 98.39 yyy y eee e
using your function and compare the number of iterations of the Secant method with the
bisection method as discussed in the lecture notes.
Use SAS programming language to answer Question 3‐4 and save your answers in a script
file named “[Your_Student_ID].sas”, where [Your_Student_ID] is your 10‐
digit student ID.
Question 3 (36 marks)
We have a data set of four variables on the murder rates (per 100,000 inhabitants) for cities
from South, North, and West in USA in 1980 as follows:
city state coast rate
----------------------------------------
Denver CO West 9
Los Angeles CA West 23
San Diego CA West 10
Atlanta GA South 14
Dallas TX South 18
Washington DC South 11
Chicago IL North 14
Cleveland OH North 16
Detroit MI North 16
Madison WI North 2
Write SAS programs to read the murder rates data in (a) - (c) as in-stream data. Store the data
in a temporary file, called Q3.
You can copy and paste the data lines from Q3.txt. ‘#’ in the data lines are not part of the
data.
The order of the variables in the file must be CITY, STATE, COAST, and RATE.
(a) (12 marks)
Denver CO West 9 ####
Los Angeles CA West 23
San Diego CA West 10
Atlanta GA South 14
Dallas TX South 18
Washington DC South 11
Chicago IL North 14
Cleveland OH North 16
Detroit MI North 16
Madison WI North 2
3
(b) (12 marks)
Denver CO West 9 Los Angeles CA
West 23 San Diego CA West 10
Atlanta GA South 14 Dallas TX South
18 Washington DC South 11
Chicago IL
North 14 Cleveland OH North 16
Detroit MI North 16 Madison
WI North 2
(c) (12 marks)
Denver CO ## West 9 ####
Los Angeles CA # West 23
San Diego CA ### West 10
Atlanta GA # South 14
Dallas TX # South 18
Washington DC # South 11
Chicago IL ## North 14
Cleveland OH # North 16
Detroit MI # North 16
Madison WI ### North 2
Hint: For part (a), you can use the following template.
data Q3;
* Your input and/or length statements here;
cards;
Denver CO West 9 ####
Los Angeles CA West 23
San Diego CA West 10
Atlanta GA South 14
Dallas TX South 18
Washington DC South 11
Chicago IL North 14
Cleveland OH North 16
Detroit MI North 16
Madison WI North 2
run;
Part (b) and (c) are similar by replacing the in‐stream data between the cards; and run;
statements.
4
Question 4 (36 marks)
Write a SAS program to read the following information about Tom's Tuesday appointments
as in-stream data.
Time With Place Subject Length_Meeting Confirmed?
------------------------------------------------------------------------------
11:00 Li Lan Room 30 Personnel review 45 minutes Yes
13:00 Leung Mei Fai Leung's office Marketing 30 minutes No
15:00 Mak David Lab Test results 20 minutes Yes
The SAS data set should be a permanent one locating in D:\ with filename Q4. The data set
should contain seven variables: TIME, LAST_NAME, FULL_NAME, PLACE, SUBJECT,
LENGTH_MEETING, CONFIRM.
TIME and LENGTH_MEETING are numeric and all other variables are character.
The data are listed below in (a - (c).
You can copy and paste the data lines from Q4.txt. ‘#’ in the data lines are not part of the
data.
(a) (12 marks)
11:00 Li Lan Room 30 Personnel review 45 minutes Yes
13:00 Leung Mei Fai Leung's office Marketing 30 minutes No
15:00 Mak David Lab Test results 20 minutes Yes
(b) (12 marks)
11:00 Li Lan Room 30 Personnel review 45 Yes#
13:00 Leung Mei Fai Leung's office Marketing 30 No
15:00 Mak David Lab Test results 20 Yes
(c) (12 marks)
11:00 Li Lan Room 30 Personnel review 45 minsYes
13:00Leung Mei Fai Leung's office Marketing 30 mins No
15:00 Mak David Lab Test results 20 mins Yes
End of Questions
版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。