Starting from:

$29.99

MAST90104 Lab 3 Solution

Week 3 Workshop/Lab
R exercises
Questions 1-4 are taken from Chapter 3 of spuRs (Introduction to Scientific Programming and Simulation Using R).
1. Consider the function y = f(x) defined by
x ≤ 0 ∈ (0,1] > 1 √
f(x) −x3 x2 x
Supposing that you are given x, write an R expression for y using if statements.
Add your expression for y to the following program, then run it to plot the function f.
# input
x.values <- seq(-2, 2, by = 0.1)
# for each x calculate y n <- length(x.values) y.values <- rep(0, n) for (i in 1:n) { x <- x.values[i]
# your expression for y goes here y.values[i] <- y
}
# output
plot(x.values, y.values, type = "l")
Your plot should look like Figure 1. Do you think f has a derivative at 1? What about at 0?
2. Let . Write an R program to calculate h(x,n) using a for loop.
3. The function h(x,n) from Exercise 2 is the finite sum of a geometric sequence. It has the following explicit formula, for x 6= 1,
.
Test your program from Exercise 2 against this formula using the following values

You should use the computer to calculate the formula rather than doing it yourself.
4. First write a program that achieves the same result as in Exercise 2 but using a while loop. Then write a program that does this using vector operations (and no loops).
If it doesn’t already, make sure your program works for the case x = 1.

x.values
Figure 1: The graph produced by Exercise 1.
5. We return to the house price example in Module 4.
Price (× $10k) Age (years) Area (×100m2)

50 1 1
40 5 1
52 5 2
47 10 2
65 20 3
Recall that we want to predict a house price based on its age and area. The linear model is of the form y .
Create a data frame in R with this data. Fit the linear model using function lm() in R.
Workshop questions
1. Let X be a 10 × 5 matrix of full rank and let H = X(XTX)−1XT.
Find tr(H) and r(H).
2. Let
.
Let z = yTAy. Write out z in full, then find directly and using the matrix formula.
3. Let y be a random vector with mean µ , and assume that
Varyi = 4 and Cov(yi,yj) = 0.
(a) Write down Vary.
(b) Let

and find VarAy and E[yTAy]
4. Let y be a normal random vector with mean and variance
µ and .
Let
.
(a) Find the distributions of yTAy and yTBy.
(b) Are yTAy and yTBy independent?
(c) What is the distribution of yTAy + yTBy?
5. Prove corollaries 3.6 and 3.7 in Module 3’s notes.

More products