Question: Part 3 : Writing your own regression function ( 3 0 pts total ) For this question, you will write and test your own substitute
Part : Writing your own regression function pts total
For this question, you will write and test your own substitute for the function. Your
function will take in a dataframe with columns labeled and and fit the regression of the
form:
Your function should return a list object with named components:
data, which is a dataframe that contains the original data used to estimate the regression.
params, which is a dataframe with the estimates widehatwidehat and their corresponding standard
errors hathat which can be computed as follows: hat
hathat
hathat
hathathat
where is the number of observations.
pred, which is a vector of the predicted values, hat where
hathathat
resid, which is a vector of residuals:
hat
mse which is the estimate of the error variance:
hat
rsq which is the or coefficient of determination, which is the proportion of variance
explained by the regression:
plot which is a ggplot object that contains a scatterplot of the data with a line of
best fit superimposed. To add the line, use geomabline and your estimates of hathat
Make sure the line is a fun color!
I suggest beginning by writing individual functions for each of these tasks and testing these
functions repeatedly against the actual output of to be sure you're getting similar results.
A good way to simulate univariate linear regression data to compare to is:
N
dreg data.framex rnormN
dregyegx rnormN
m lmy x dreg
summarym Call:
lmformula y ~ x data dreg
Residuals:
Min rrrrr
Coefficients:
Estimate Std Error t value PrtSignif codes:
Residual standard error: on degrees of freedom
Multiple Rsquared: Adjusted Rsquared:
Fstatistic: on and DF pvalue: e
Note that you can vary the coefficients, and the meansvariancesdistributions used to generate
and the noise in
Your function should:
Take only a dataframe with columns labeled and
Output a list object with named entities
Reproduce the corresponding output of the function exactly
Write your function, unpack the contents of the list object you produce, and show that
they produce a reasonable plot and reproduce the results from for some simulated data
it need not be the data I produced above
Hint: If you can't find something you need in the summary read the documentation for
# TODO: write your function, fit it to some simulated data, demonstrate that
# it reproduces the output from by unpacking the contents of the model
# object you build and comparing to the results from lm
MyLM functiond
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
