Answered step by step
Verified Expert Solution
Question
1 Approved Answer
PLEASE PROVIDE CODE IN R (a) Given the linear model E(%)-30 + ir, and a vector of observation weights w-u, . . . , wny,
PLEASE PROVIDE CODE IN R
(a) Given the linear model E(%)-30 + ir, and a vector of observation weights w-u, . . . , wny, the weighted least squares estimate of the regression coefficient 3 (Bo B1) is where t1 X- 1 rn is the design matrix and 01 0 n is the diagonal matrix containing the weights. (a) Write a function weighted ls which takes the vectors (NumPy arrays) x, y, and w as arguments and which 2 marks] S) Suppose we want to predict the response for a new observation, for which the covariate takes the value ro- returns the weighted least squares estimate . The predicted value of the response is then where B predict.wls, which takes x, y, w and x0 as arguments, and returns the least squares prediction o (Bo, BY is the coefficient estimate obtained using the function weighted.1s. Write a function Your function should first call the function weighted-ls to compute 3 (from x, y and w) and then compute 1 mark] o using equation (1) (b) Locally linear regression is a method for fitting a smooth function f(.) to data points (ri'yi), where x=(xi, rn), is the covariate and y - (vi,...,yn)' is the response variable. In locally linear regression the weighted lin- ear model is used to construct a non-linear method for regression. This is done by computing a different set of weights for each value of.ro. Given.ro and a parameter (which controls how smooth the fitted function will be). the set of weights w (wi,..., wn) is computed using i.e. observations close to ro become larger weights. Write a function compute weights which takes the vector x, the new observation x0 (a single number), and the parameter rho as arguments, and which computes the weights w. 1 mark] Assuming that your data lives in a data frame mcycle and that your poredictions are called yhat you can then plot the data and the fitted regression function using the commands. import matplotlib.pyplot as plt plt plot (mcycle["times"], mcycle[ "accel", 'o') plt plot (x0, yhat) plt.show ()Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started