Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
All questions with a bold R next to the question number can be solved in groups up to size three. For such groups a single writeup can be turned in, but make sure to indicate who the three are. All datasets used in the homework assignmentscanbefoundoncourseworkinthedirectoryDatasets.
Often a linear regression is inadequate, and we turn to more flexible forms of regression. One such form is locally weighted regression. Consider the case of a single predictor x, and suppose the model is y= f(x) + s, with s∼(0,σ2). Local linear regression approximates f(x) at x0 by f (x0) = a0 + b0x0, where a0 and b0 are chosen to minimize
1This is known as the tri-cube weight function, invented by John Tukey, and is a rounded form of the uniform weight function. ωis known as the window size or bandwidth. Since x0 is arbitrary, we have an approximation to f at any value of x. Local regression is thought of as a direct way of estimating the regression function f (x) = E(Y |X = x).
Explainin words why local regression makes sense, and why the result- ing function estimate is smooth as a function of x.
Explain the role of ω. What happens as ω approaches 0? As ω ap- proaches ∞ ?
2Show that fˆ(x0) = h(x0)Ty where h(x0) is a n vector, and y is the vector of responses, and give an expression for hi(x0).
If the data xi, yiall lie on a straight line, show that fˆ(x0) lies on the same line .
Showthat the bias of fˆ(x0) is f(x0) −h(x0)Tf, where fi= f(xi). By expanding each element of fiin a first order Taylor series about x= x0,
conclude that to first order
Use this to show that to first order, fˆ(x0) is unbiased.
R In practice we often use ω = ωs(x0), e. a bandwidth thatdepends on the target point x0 and some other tuning parameter s. For the rest of this homework, we will assume the following form for ωs, for s ∈ (0, 1], the so-called span. Let k = |s · n∫, and let x(k) be the kth nearest neighbor among the xito x0. Then ωs(x0) = |x0 −x(k)|.
Write an R function llr(x,y,x0,span,hat=FALSE) to compute the local linear regression fit at x0. If hat=TRUE, your function should return h.
R Apply your function to the prostate cancer data (dat), availableon the Canvas website. Use lpsa as the response, pgg45 as the predictor, and chose span=1/3. Compute the fit at a uniform sequence of values of x0, and superimpose your fit (using lines()) on a plot of the data. Try a few different values of span, and select a good value by eye(what this means is up to you).
RPick 3 values of x0 (one near end of the range of lpsa, and two at random). With span=1/4, produce for each value of x0 a plot showing the weights produced by the weighting function W . Superimpose on each plot the weight vector h, sometimes known as the equivalentker-nel. (For these plots use a three by one layout via par(mfrow=c(3,1)); ideally each plot should be wide and not too tall. )
What might be a more principled way of selecting span? (Just say how; you do not need to implement it.)
Howmight you measure the “effective degrees of freedom” of your local regression fitter .
R Write a wrapper function that uses llr to compute the local-linear regression fit at all the data points. It should return the df of the fit, and optionally the linear operator (hat) matrix that produces the fit. It should estimate the error variance σ2, and for each fitted value, the estimated standard error of the Augment your plot with upper and lower 2× standard-error bands.
R Superimpose on your plot the linear regression How might you conduct an informal F-test to compare the linear fit with the non- linear? Do it.
R Create an image plot of the hat matrix (making sure the rows are order by increasing value of pgg45. What do youobserve?
R The dataset SA csv is available on the Canvas site. These are data on the occurrence of heart attacks in a farming community in South Africa. Read it in using read.csv() and plot the binary response chd against systolic blood pressure sbp. Fit a local-linear regression to these data, and superimpose it on the fit. What is this curveestimating?